From 1993fed19063b5e6f0bb98326b6251b29f03e580 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Tue, 21 Mar 2017 12:35:05 +0300 Subject: [PATCH] Added subscribe email feature --- OsmAnd/res/layout/subscribe_email_header.xml | 75 +++++++++ OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/OsmandSettings.java | 1 + .../plus/download/DownloadActivity.java | 6 +- .../ui/DownloadResourceGroupFragment.java | 159 +++++++++++++++++- 5 files changed, 238 insertions(+), 5 deletions(-) create mode 100644 OsmAnd/res/layout/subscribe_email_header.xml diff --git a/OsmAnd/res/layout/subscribe_email_header.xml b/OsmAnd/res/layout/subscribe_email_header.xml new file mode 100644 index 0000000000..9452caf4ac --- /dev/null +++ b/OsmAnd/res/layout/subscribe_email_header.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index c9cbb7f252..1b4a3e70e0 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,8 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Error + Subscribe to our email list about app discounts and get 3 more map downloads! Set of maps allows you to see sea depth contour lines. Thank you for purchasing sea depth contours! Depth contours diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 432820c3d9..5e708e71f5 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -920,6 +920,7 @@ public class OsmandSettings { public final OsmandPreference LIVE_UPDATES_PURCHASED = new BooleanPreference("billing_live_updates_purchased", false).makeGlobal(); public final OsmandPreference FULL_VERSION_PURCHASED = new BooleanPreference("billing_full_version_purchased", false).makeGlobal(); public final OsmandPreference DEPTH_CONTOURS_PURCHASED = new BooleanPreference("billing_sea_depth_purchased", false).makeGlobal(); + public final OsmandPreference EMAIL_SUBSCRIBED = new BooleanPreference("email_subscribed", false).makeGlobal(); public final OsmandPreference DISCOUNT_ID = new IntPreference("discount_id", 0).makeGlobal(); public final OsmandPreference DISCOUNT_SHOW_NUMBER_OF_STARTS = new IntPreference("number_of_starts_on_discount_show", 0).makeGlobal(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 6852cb3c32..7c0ee7e85e 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -413,6 +413,10 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo downloadHasFinished(); } + public void updateBanner() { + visibleBanner.updateBannerInProgress(); + } + private int getCurrentTab() { return viewPager.getCurrentItem(); } @@ -427,7 +431,7 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo return Math.max(downloadsLeft, 0) > 0; } - private static boolean shouldShowFreeVersionBanner(OsmandApplication application) { + public static boolean shouldShowFreeVersionBanner(OsmandApplication application) { return (Version.isFreeVersion(application) && !application.getSettings().LIVE_UPDATES_PURCHASED.get() && !application.getSettings().FULL_VERSION_PURCHASED.get()) || application.getSettings().SHOULD_SHOW_FREE_VERSION_BANNER.get(); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 854422c5c1..c4e88a3749 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -1,10 +1,15 @@ package net.osmand.plus.download.ui; +import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.res.Resources; import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.os.Bundle; +import android.provider.Settings; import android.support.v4.app.DialogFragment; import android.support.v4.view.MenuItemCompat; +import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; import android.util.TypedValue; import android.view.LayoutInflater; @@ -14,11 +19,14 @@ import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; +import android.widget.EditText; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.TextView; +import net.osmand.AndroidNetworkUtils; +import net.osmand.AndroidUtils; import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -31,13 +39,20 @@ import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadResourceGroup; import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; import net.osmand.plus.inapp.InAppHelper; import net.osmand.plus.inapp.InAppHelper.InAppListener; import net.osmand.util.Algorithms; +import org.json.JSONException; +import org.json.JSONObject; + +import java.text.MessageFormat; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, InAppListener, OnChildClickListener { @@ -55,6 +70,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow private Toolbar toolbar; private View searchView; private View restorePurchasesView; + private View subscribeEmailView; @Override public void onCreate(Bundle savedInstanceState) { @@ -105,8 +121,8 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow banner = null; view.findViewById(R.id.freeVersionBanner).setVisibility(View.GONE); } - listView = (ExpandableListView) view.findViewById(android.R.id.list); + addSubscribeEmailRow(); addSearchRow(); addRestorePurchasesRow(); listView.setOnChildClickListener(this); @@ -116,6 +132,25 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return view; } + private void addSubscribeEmailRow() { + if (DownloadActivity.shouldShowFreeVersionBanner(activity.getMyApplication()) + && !getMyApplication().getSettings().EMAIL_SUBSCRIBED.get()) { + subscribeEmailView = activity.getLayoutInflater().inflate(R.layout.subscribe_email_header, null, false); + subscribeEmailView.findViewById(R.id.subscribe_btn).setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + subscribe(); + } + }); + listView.addHeaderView(subscribeEmailView); + IndexItem worldBaseMapItem = activity.getDownloadThread().getIndexes().getWorldBaseMapItem(); + if (worldBaseMapItem == null || !worldBaseMapItem.isDownloaded() + || DownloadActivity.isDownlodingPermitted(activity.getMyApplication().getSettings())) { + subscribeEmailView.findViewById(R.id.container).setVisibility(View.GONE); + } + } + } + private void addRestorePurchasesRow() { if (!openAsDialog() && !InAppHelper.isInAppIntentoryRead()) { restorePurchasesView = activity.getLayoutInflater().inflate(R.layout.restore_purchases_list_footer, null); @@ -177,6 +212,116 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow } } + private void updateSubscribeEmailView() { + if (subscribeEmailView != null && subscribeEmailView.findViewById(R.id.container).getVisibility() == View.GONE + && !DownloadActivity.isDownlodingPermitted(getMyApplication().getSettings()) + && !getMyApplication().getSettings().EMAIL_SUBSCRIBED.get()) { + IndexItem worldBaseMapItem = activity.getDownloadThread().getIndexes().getWorldBaseMapItem(); + if (worldBaseMapItem != null && worldBaseMapItem.isDownloaded()) { + subscribeEmailView.findViewById(R.id.container).setVisibility(View.VISIBLE); + } + } + } + + private void hideSubscribeEmailView() { + if (subscribeEmailView != null && subscribeEmailView.findViewById(R.id.container).getVisibility() == View.VISIBLE) { + subscribeEmailView.findViewById(R.id.container).setVisibility(View.GONE); + } + } + + private void subscribe() { + AlertDialog.Builder b = new AlertDialog.Builder(activity); + b.setTitle(R.string.shared_string_email_address); + final EditText editText = new EditText(activity); + int leftPadding = AndroidUtils.dpToPx(activity, 24f); + int topPadding = AndroidUtils.dpToPx(activity, 4f); + b.setView(editText, leftPadding, topPadding, leftPadding, topPadding); + b.setPositiveButton(R.string.shared_string_ok, null); + b.setNegativeButton(R.string.shared_string_cancel, null); + final AlertDialog alertDialog = b.create(); + alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + String email = editText.getText().toString(); + if (Algorithms.isEmpty(email) || !AndroidUtils.isValidEmail(email)) { + getMyApplication().showToastMessage(getString(R.string.osm_live_enter_email)); + return; + } + doSubscribe(email); + alertDialog.dismiss(); + } + }); + } + }); + alertDialog.show(); + } + + private void doSubscribe(final String email) { + new AsyncTask() { + + ProgressDialog dlg; + + @Override + protected void onPreExecute() { + dlg = new ProgressDialog(getActivity()); + dlg.setTitle(""); + dlg.setMessage(getString(R.string.wait_current_task_finished)); + dlg.setCancelable(false); + dlg.show(); + } + + @Override + protected String doInBackground(Void... params) { + try { + Map parameters = new HashMap<>(); + parameters.put("aid", Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID)); + parameters.put("email", email); + + return AndroidNetworkUtils.sendRequest(getMyApplication(), + "http://download.osmand.net/subscription/register_email.php", + parameters, "Subscribing email...", true, true); + + } catch (Exception e) { + return null; + } + } + + @Override + protected void onPostExecute(String response) { + if (dlg != null) { + dlg.dismiss(); + dlg = null; + } + OsmandApplication app = getMyApplication(); + if (response == null) { + app.showShortToastMessage(activity.getString(R.string.shared_string_unexpected_error)); + } else { + try { + JSONObject obj = new JSONObject(response); + String responseEmail = obj.getString("email"); + if (!email.equalsIgnoreCase(responseEmail)) { + app.showShortToastMessage(activity.getString(R.string.shared_string_unexpected_error)); + } else { + app.getSettings().NUMBER_OF_FREE_DOWNLOADS.set(DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - 3); + app.getSettings().EMAIL_SUBSCRIBED.set(true); + hideSubscribeEmailView(); + activity.updateBanner(); + } + } catch (JSONException e) { + String message = "JSON parsing error: " + + (e.getMessage() == null ? "unknown" : e.getMessage()); + app.showShortToastMessage(MessageFormat.format( + activity.getString(R.string.error_message_pattern), message)); + } + } + } + }.execute((Void) null); + } + @Override public void onError(String error) { } @@ -233,6 +378,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow if (!openAsDialog()) { updateSearchView(); } + updateSubscribeEmailView(); DownloadResources indexes = activity.getDownloadThread().getIndexes(); group = indexes.getGroupById(groupId); if (group != null) { @@ -258,7 +404,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow @Override public void newDownloadIndexes() { - if(banner != null) { + if (banner != null) { banner.updateBannerInProgress(); } reloadData(); @@ -266,15 +412,20 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow @Override public void downloadHasFinished() { - if(banner != null) { + if (banner != null) { banner.updateBannerInProgress(); } + if (subscribeEmailView != null + && !DownloadActivity.isDownlodingPermitted(activity.getMyApplication().getSettings()) + && !getMyApplication().getSettings().EMAIL_SUBSCRIBED.get()) { + subscribeEmailView.findViewById(R.id.container).setVisibility(View.VISIBLE); + } listAdapter.notifyDataSetChanged(); } @Override public void downloadInProgress() { - if(banner != null) { + if (banner != null) { banner.updateBannerInProgress(); } listAdapter.notifyDataSetChanged();