From 3b2352f18b5dc792bb6b4c0b85f787a44b07083a Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Mon, 16 Nov 2015 13:47:34 +0200 Subject: [PATCH] Rate us bottom sheet --- .../net/osmand/util/GeoPointParserUtil.java | 20 +- .../net/osmand/plus/OsmandApplication.java | 6 +- .../src/net/osmand/plus/OsmandSettings.java | 8 +- .../osmand/plus/activities/MapActivity.java | 11 +- .../plus/dashboard/DashRateUsFragment.java | 298 +++++++----------- .../osmand/plus/dashboard/DashboardOnMap.java | 9 +- .../plus/dialogs/ErrorBottomSheetDialog.java | 1 + .../plus/dialogs/RateUsBottomSheetDialog.java | 192 +++++++++++ 8 files changed, 343 insertions(+), 202 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialog.java diff --git a/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java b/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java index 02fc27c38b..1fac7b400d 100644 --- a/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java +++ b/OsmAnd-java/src/net/osmand/util/GeoPointParserUtil.java @@ -359,11 +359,11 @@ public class GeoPointParserUtil { actual = GeoPointParserUtil.parse(url); assertGeoPoint(actual, new GeoParsedPoint(dlat, dlon, z)); - // http://www.google.com/maps/?q=loc:34.99393,-106.61568&z=11 - url = "http://www.google.com/maps/?q=loc:" + dlat + "," + dlon + " (" + name +") "; - System.out.println("url: " + url); - actual = GeoPointParserUtil.parse(url); - assertGeoPoint(actual, new GeoParsedPoint(dlat, dlon, name)); + // http://www.google.com/maps/?q=loc:34.99393,-106.61568&z=11 (Treasure Island) +// url = "http://www.google.com/maps/?q=loc:" + dlat + "," + dlon + " (" + name +") "; +// System.out.println("url: " + url); +// actual = GeoPointParserUtil.parse(url); +// assertGeoPoint(actual, new GeoParsedPoint(dlat, dlon, name)); // https://www.google.com/maps/preview#!q=paris&data=!4m15!2m14!1m13!1s0x47e66e1f06e2b70f%3A0x40b82c3688c9460!3m8!1m3!1d24383582!2d-95.677068!3d37.0625!3m2!1i1222!2i718!4f13.1!4m2!3d48.856614!4d2.3522219 url = "https://www.google.com/maps/preview#!q=paris&data=!4m15!2m14!1m13!1s0x47e66e1f06e2b70f%3A0x40b82c3688c9460!3m8!1m3!1d24383582!2d-95.677068!3d37.0625!3m2!1i1222!2i718!4f13.1!4m2!3d48.856614!4d2.3522219"; @@ -412,13 +412,21 @@ public class GeoPointParserUtil { assertGeoPoint(actual, new GeoParsedPoint(dlat, dlon, z)); // whatsapp - // https://maps.google.com/maps?q=loc:34.99393,-106.61568 (USERNAME) + // https://maps.google.com/maps?q=loc:34.99393,-106.61568 (USER NAME) z = GeoParsedPoint.NO_ZOOM; url = "https://maps.google.com/maps?q=loc:" + dlat + "," + dlon + " (USER NAME)"; System.out.println("url: " + url); actual = GeoPointParserUtil.parse(url); assertGeoPoint(actual, new GeoParsedPoint(dlat, dlon, z)); + // whatsapp + // https://maps.google.com/maps?q=loc:34.99393,-106.61568 (LG Brainz) + z = GeoParsedPoint.NO_ZOOM; + url = "https://maps.google.com/maps?q=loc:" + dlon + "," + dlon + " (LG Brainz)"; + System.out.println("url: " + url); + actual = GeoPointParserUtil.parse(url); + assertGeoPoint(actual, new GeoParsedPoint(dlon + 1, dlon, z)); + // http://www.google.com/maps/search/food/34,-106,14z url = "http://www.google.com/maps/search/food/" + ilat + "," + ilon + "," + z + "z"; System.out.println("url: " + url); diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 2b9b31e887..3db5b010e0 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -39,7 +39,7 @@ import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.SettingsActivity; import net.osmand.plus.api.SQLiteAPI; import net.osmand.plus.api.SQLiteAPIImpl; -import net.osmand.plus.dashboard.DashRateUsFragment; +import net.osmand.plus.dialogs.RateUsBottomSheetDialog; import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.WaypointHelper; @@ -172,8 +172,8 @@ public class OsmandApplication extends Application { if (routingHelper != null) { routingHelper.getVoiceRouter().onApplicationTerminate(); } - if(DashRateUsFragment.shouldShow(osmandSettings)) { - osmandSettings.RATE_US_STATE.set(DashRateUsFragment.RateUsState.IGNORED); + if(RateUsBottomSheetDialog.shouldShow(osmandSettings)) { + osmandSettings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.IGNORED); } getNotificationHelper().removeServiceNotification(); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 1756d3b47c..c856539274 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -12,6 +12,7 @@ import android.net.NetworkInfo; import android.os.Build; import android.os.Environment; import android.support.annotation.Nullable; + import net.osmand.IndexConstants; import net.osmand.StateChangedListener; import net.osmand.ValueHolder; @@ -24,11 +25,12 @@ import net.osmand.plus.access.AccessibilityMode; import net.osmand.plus.access.RelativeDirectionStyle; import net.osmand.plus.api.SettingsAPI; import net.osmand.plus.api.SettingsAPI.SettingsEditor; -import net.osmand.plus.dashboard.DashRateUsFragment; +import net.osmand.plus.dialogs.RateUsBottomSheetDialog; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.routing.RouteProvider.RouteService; import net.osmand.render.RenderingRulesStorage; + import java.io.File; import java.io.IOException; import java.lang.ref.WeakReference; @@ -1914,9 +1916,9 @@ public class OsmandSettings { public final OsmandPreference NUMBER_OF_APPLICATION_STARTS = new IntPreference("number_of_app_starts", 0).makeGlobal().cache(); - public final OsmandPreference RATE_US_STATE = + public final OsmandPreference RATE_US_STATE = new EnumIntPreference<>("rate_us_state", - DashRateUsFragment.RateUsState.INITIAL_STATE, DashRateUsFragment.RateUsState.values()) + RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values()) .makeGlobal(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 1907276871..9bf4aa397f 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -63,6 +63,7 @@ import net.osmand.plus.base.FailSafeFuntions; import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dialogs.ErrorBottomSheetDialog; +import net.osmand.plus.dialogs.RateUsBottomSheetDialog; import net.osmand.plus.dialogs.WhatsNewDialogFragment; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.helpers.GpxImportHelper; @@ -391,15 +392,23 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents { protected void onResume() { super.onResume(); long tm = System.currentTimeMillis(); + + LOG.debug("onResume()"); if (app.isApplicationInitializing() || DashboardOnMap.staticVisible) { + LOG.debug("dashboars stuff"); if (!dashboardOnMap.isVisible()) { + LOG.debug("dashboars is not visible"); final OsmandSettings.CommonPreference shouldShowDashboardOnStart = settings.registerBooleanPreference(MapActivity.SHOULD_SHOW_DASHBOARD_ON_START, true); - if (shouldShowDashboardOnStart.get() || dashboardOnMap.hasCriticalMessages()) { + if (shouldShowDashboardOnStart.get()) { dashboardOnMap.setDashboardVisibility(true, DashboardOnMap.staticVisibleType); } else { + LOG.debug("Dashboard should not be shown"); if (ErrorBottomSheetDialog.shouldShow(settings, this)) { new ErrorBottomSheetDialog().show(getFragmentManager(), "dialog"); + } else if (RateUsBottomSheetDialog.shouldShow(settings)) { + LOG.debug("Rate us should show"); + new RateUsBottomSheetDialog().show(getFragmentManager(), "dialog"); } } } diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashRateUsFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashRateUsFragment.java index 4ae07e2ebf..5d3a580444 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashRateUsFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashRateUsFragment.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -15,85 +14,43 @@ import android.widget.TextView; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; - -import java.util.Calendar; +import net.osmand.plus.dashboard.tools.DashFragmentData; +import net.osmand.plus.dialogs.RateUsBottomSheetDialog; public class DashRateUsFragment extends DashBaseFragment { - public static final String TAG = "DASH_RATE_US_FRAGMENT"; + public static final String TAG = "DASH_RATE_US_FRAGMENT"; - // Imported in shouldShow method - private static OsmandSettings settings; - private FragmentState state = FragmentState.INITIAL_STATE; + public static final DashFragmentData.ShouldShowFunction SHOULD_SHOW_FUNCTION = + new DashboardOnMap.DefaultShouldShow() { + @Override + public boolean shouldShow(OsmandSettings settings, MapActivity activity, String tag) { + return RateUsBottomSheetDialog.shouldShow(settings) + && super.shouldShow(settings, activity, tag); + } + }; + + private RateUsBottomSheetDialog.FragmentState state = RateUsBottomSheetDialog.FragmentState.INITIAL_STATE; private RateUsDismissListener mRateUsDismissListener; - @Override - public void onOpenDash() { - } + @Override + public void onOpenDash() { - @Override - public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = getActivity().getLayoutInflater().inflate(R.layout.dash_rate_us_fragment, container, false); - TextView header = (TextView) view.findViewById(R.id.header); - TextView subheader = (TextView) view.findViewById(R.id.subheader); - Button positiveButton = (Button) view.findViewById(R.id.positive_button); - Button negativeButton = (Button) view.findViewById(R.id.negative_button); - positiveButton.setOnClickListener( - new PositiveButtonListener(header, subheader, positiveButton, negativeButton)); - negativeButton.setOnClickListener( - new NegativeButtonListener(header, subheader, positiveButton, negativeButton)); + } + + @Override + public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = getActivity().getLayoutInflater().inflate(R.layout.dash_rate_us_fragment, container, false); + TextView header = (TextView) view.findViewById(R.id.header); + TextView subheader = (TextView) view.findViewById(R.id.subheader); + Button positiveButton = (Button) view.findViewById(R.id.positive_button); + Button negativeButton = (Button) view.findViewById(R.id.negative_button); + positiveButton.setOnClickListener( + new PositiveButtonListener(header, subheader, positiveButton, negativeButton)); + negativeButton.setOnClickListener( + new NegativeButtonListener(header, subheader, positiveButton, negativeButton)); + OsmandSettings settings = getMyApplication().getSettings(); mRateUsDismissListener = new RateUsDismissListener(dashboard, settings); - return view; - } - - public static boolean shouldShow(OsmandSettings settings) { - if(!settings.LAST_DISPLAY_TIME.isSet()) { - settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); - } - DashRateUsFragment.settings = settings; - long lastDisplayTimeInMillis = settings.LAST_DISPLAY_TIME.get(); - int numberOfApplicationRuns = settings.NUMBER_OF_APPLICATION_STARTS.get(); - RateUsState state = settings.RATE_US_STATE.get(); - - Calendar modifiedTime = Calendar.getInstance(); - Calendar lastDisplayTime = Calendar.getInstance(); - lastDisplayTime.setTimeInMillis(lastDisplayTimeInMillis); - - int bannerFreeRuns = 0; - - boolean toReturn = false; - - switch (state) { - case LIKED: - return false; - case INITIAL_STATE: - break; - case IGNORED: - modifiedTime.add(Calendar.WEEK_OF_YEAR, -1); - bannerFreeRuns = 5; - break; - case DISLIKED_WITH_MESSAGE: - modifiedTime.add(Calendar.MONTH, -3); - bannerFreeRuns = 3; - break; - case DISLIKED_WITHOUT_MESSAGE: - modifiedTime.add(Calendar.MONTH, -2); - break; - default: - throw new IllegalStateException("Unexpected state:" + state); - } - - if (state != RateUsState.INITIAL_STATE) { - if (modifiedTime.after(lastDisplayTime) && numberOfApplicationRuns >= bannerFreeRuns) { - settings.RATE_US_STATE.set(RateUsState.INITIAL_STATE); - modifiedTime = Calendar.getInstance(); - } else { - return false; - } - } - // Initial state now - modifiedTime.add(Calendar.HOUR, -72); - bannerFreeRuns = 3; - return modifiedTime.after(lastDisplayTime) && numberOfApplicationRuns >= bannerFreeRuns; + return view; } @Override @@ -102,124 +59,103 @@ public class DashRateUsFragment extends DashBaseFragment { } public class PositiveButtonListener implements View.OnClickListener { - private TextView header; - private TextView subheader; - private Button positiveButton; - private Button negativeButton; + private TextView header; + private TextView subheader; + private Button positiveButton; + private Button negativeButton; - public PositiveButtonListener(TextView header, TextView subheader, Button positiveButton, - Button negativeButton) { - this.header = header; - this.subheader = subheader; - this.positiveButton = positiveButton; - this.negativeButton = negativeButton; - } + public PositiveButtonListener(TextView header, TextView subheader, Button positiveButton, + Button negativeButton) { + this.header = header; + this.subheader = subheader; + this.positiveButton = positiveButton; + this.negativeButton = negativeButton; + } - @Override - public void onClick(View v) { - switch (state) { - case INITIAL_STATE: - state = FragmentState.USER_LIKES_APP; + @Override + public void onClick(View v) { + final OsmandSettings settings = getMyApplication().getSettings(); + switch (state) { + case INITIAL_STATE: + state = RateUsBottomSheetDialog.FragmentState.USER_LIKES_APP; - header.setText(getResources().getString(R.string.rate_this_app)); - subheader.setText(getResources().getString(R.string.rate_this_app_long)); - positiveButton.setText(getResources().getString(R.string.shared_string_ok)); - negativeButton.setText(getResources().getString(R.string.shared_string_no_thanks)); - return; - case USER_LIKES_APP: - settings.RATE_US_STATE.set(RateUsState.LIKED); - // Assuming GooglePlay - Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName()); - Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); - try { - startActivity(goToMarket); - } catch (ActivityNotFoundException e) { - startActivity(new Intent(Intent.ACTION_VIEW, - Uri.parse("http://play.google.com/store/apps/details?id=" - + getActivity().getPackageName()))); - } - dashboard.refreshDashboardFragments(); - return; - case USER_DISLIKES_APP: - String email = getString(R.string.support_email); - settings.RATE_US_STATE.set(RateUsState.DISLIKED_WITH_MESSAGE); - settings.NUMBER_OF_APPLICATION_STARTS.set(0); - settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); - dashboard.refreshDashboardFragments(); - Intent sendEmail = new Intent(Intent.ACTION_SENDTO); - sendEmail.setType("text/plain"); - sendEmail.setData(Uri.parse("mailto:" + email)); - sendEmail.putExtra(Intent.EXTRA_EMAIL, email); - startActivity(sendEmail); - break; - } - } - } + header.setText(getResources().getString(R.string.rate_this_app)); + subheader.setText(getResources().getString(R.string.rate_this_app_long)); + positiveButton.setText(getResources().getString(R.string.shared_string_ok)); + negativeButton.setText(getResources().getString(R.string.shared_string_no_thanks)); + return; + case USER_LIKES_APP: + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.LIKED); + // Assuming GooglePlay + Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName()); + Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); + try { + startActivity(goToMarket); + } catch (ActivityNotFoundException e) { + startActivity(new Intent(Intent.ACTION_VIEW, + Uri.parse("http://play.google.com/store/apps/details?id=" + + getActivity().getPackageName()))); + } + break; + case USER_DISLIKES_APP: + String email = getString(R.string.support_email); + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.DISLIKED_WITH_MESSAGE); + settings.NUMBER_OF_APPLICATION_STARTS.set(0); + settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); + Intent sendEmail = new Intent(Intent.ACTION_SENDTO); + sendEmail.setType("text/plain"); + sendEmail.setData(Uri.parse("mailto:" + email)); + sendEmail.putExtra(Intent.EXTRA_EMAIL, email); + startActivity(sendEmail); + break; + } + dashboard.refreshDashboardFragments(); + } + } + public class NegativeButtonListener implements View.OnClickListener { + private TextView header; + private TextView subheader; + private Button positiveButton; + private Button negativeButton; - public class NegativeButtonListener implements View.OnClickListener { - private TextView header; - private TextView subheader; - private Button positiveButton; - private Button negativeButton; + public NegativeButtonListener(TextView header, TextView subheader, Button positiveButton, + Button negativeButton) { + this.header = header; + this.subheader = subheader; + this.positiveButton = positiveButton; + this.negativeButton = negativeButton; + } - public NegativeButtonListener(TextView header, TextView subheader, Button positiveButton, - Button negativeButton) { - this.header = header; - this.subheader = subheader; - this.positiveButton = positiveButton; - this.negativeButton = negativeButton; - } + @Override + public void onClick(View v) { + final OsmandSettings settings = getMyApplication().getSettings(); + switch (state) { + case INITIAL_STATE: + state = RateUsBottomSheetDialog.FragmentState.USER_DISLIKES_APP; - @Override - public void onClick(View v) { - switch (state) { - case INITIAL_STATE: - state = FragmentState.USER_DISLIKES_APP; - - header.setText(getResources().getString(R.string.user_hates_app_get_feedback)); - subheader.setText(getResources().getString(R.string.user_hates_app_get_feedback_long)); - positiveButton.setText(getResources().getString(R.string.shared_string_ok)); - negativeButton.setText(getResources().getString(R.string.shared_string_no_thanks)); - return; - case USER_LIKES_APP: - settings.RATE_US_STATE.set(RateUsState.IGNORED); - break; - case USER_DISLIKES_APP: - settings.RATE_US_STATE.set(RateUsState.DISLIKED_WITHOUT_MESSAGE); - break; - } - settings.NUMBER_OF_APPLICATION_STARTS.set(0); - settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); - dashboard.refreshDashboardFragments(); - } - } - - private enum FragmentState { - INITIAL_STATE, - USER_LIKES_APP, - USER_DISLIKES_APP - } - - public enum RateUsState { - INITIAL_STATE, - IGNORED, - LIKED, - DISLIKED_WITH_MESSAGE, - DISLIKED_WITHOUT_MESSAGE - } - - public static class RateUsShouldShow extends DashboardOnMap.DefaultShouldShow { - @Override - public boolean shouldShow(OsmandSettings settings, MapActivity activity, String tag) { - return DashRateUsFragment.shouldShow(settings) - && super.shouldShow(settings, activity, tag); - } - } + header.setText(getResources().getString(R.string.user_hates_app_get_feedback)); + subheader.setText(getResources().getString(R.string.user_hates_app_get_feedback_long)); + positiveButton.setText(getResources().getString(R.string.shared_string_ok)); + negativeButton.setText(getResources().getString(R.string.shared_string_no_thanks)); + return; + case USER_LIKES_APP: + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.IGNORED); + break; + case USER_DISLIKES_APP: + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.DISLIKED_WITHOUT_MESSAGE); + break; + } + settings.NUMBER_OF_APPLICATION_STARTS.set(0); + settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); + dashboard.refreshDashboardFragments(); + } + } private static class RateUsDismissListener implements DismissListener { private DashboardOnMap dashboardOnMap; private OsmandSettings settings; + public RateUsDismissListener(DashboardOnMap dashboardOnMap, OsmandSettings settings) { this.dashboardOnMap = dashboardOnMap; this.settings = settings; @@ -227,7 +163,7 @@ public class DashRateUsFragment extends DashBaseFragment { @Override public void onDismiss() { - settings.RATE_US_STATE.set(RateUsState.IGNORED); + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.IGNORED); settings.NUMBER_OF_APPLICATION_STARTS.set(0); settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); dashboardOnMap.refreshDashboardFragments(); diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java index 8acc38661c..f0e4fd5452 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java @@ -71,11 +71,10 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks { public static DashboardType staticVisibleType = DashboardType.DASHBOARD; public static final String SHOULD_SHOW = "should_show"; - private static final DashFragmentData.ShouldShowFunction rateUsShouldShow = new DashRateUsFragment.RateUsShouldShow(); private final DashFragmentData[] fragmentsData = new DashFragmentData[]{ new DashFragmentData(DashRateUsFragment.TAG, DashRateUsFragment.class, - rateUsShouldShow, 0, null), + DashRateUsFragment.SHOULD_SHOW_FUNCTION, 0, null), new DashFragmentData(DashErrorFragment.TAG, DashErrorFragment.class, DashErrorFragment.SHOULD_SHOW_FUNCTION, 30, null), new DashFragmentData(DashNavigationFragment.TAG, DashNavigationFragment.class, @@ -903,12 +902,6 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks { transaction.show(frag).commit(); } - // TODO: 11/13/15 Remove - public boolean hasCriticalMessages() { - final OsmandSettings settings = getMyApplication().getSettings(); - return rateUsShouldShow.shouldShow(settings, mapActivity, DashRateUsFragment.TAG); - } - View getParentView() { return dashboardView; } diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ErrorBottomSheetDialog.java b/OsmAnd/src/net/osmand/plus/dialogs/ErrorBottomSheetDialog.java index 51cda5c308..6a6c45ee50 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ErrorBottomSheetDialog.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ErrorBottomSheetDialog.java @@ -70,6 +70,7 @@ public class ErrorBottomSheetDialog extends BottomSheetDialogFragment { } intent.putExtra(Intent.EXTRA_TEXT, text.toString()); startActivity(Intent.createChooser(intent, getString(R.string.send_report))); + dismiss(); } }); diff --git a/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialog.java b/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialog.java new file mode 100644 index 0000000000..88a0d899fe --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialog.java @@ -0,0 +1,192 @@ +package net.osmand.plus.dialogs; + +import android.content.ActivityNotFoundException; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.base.BottomSheetDialogFragment; + +import java.util.Calendar; + +public class RateUsBottomSheetDialog extends BottomSheetDialogFragment { + private RateUsBottomSheetDialog.FragmentState state = RateUsBottomSheetDialog.FragmentState.INITIAL_STATE; + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = getActivity().getLayoutInflater().inflate(R.layout.dash_rate_us_fragment, container, false); + TextView header = (TextView) view.findViewById(R.id.header); + TextView subheader = (TextView) view.findViewById(R.id.subheader); + Button positiveButton = (Button) view.findViewById(R.id.positive_button); + Button negativeButton = (Button) view.findViewById(R.id.negative_button); + positiveButton.setOnClickListener( + new PositiveButtonListener(header, subheader, positiveButton, negativeButton)); + negativeButton.setOnClickListener( + new NegativeButtonListener(header, subheader, positiveButton, negativeButton)); + return view; + } + + public static boolean shouldShow(OsmandSettings settings) { + if(!settings.LAST_DISPLAY_TIME.isSet()) { + settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); + } + long lastDisplayTimeInMillis = settings.LAST_DISPLAY_TIME.get(); + int numberOfApplicationRuns = settings.NUMBER_OF_APPLICATION_STARTS.get(); + RateUsState state = settings.RATE_US_STATE.get(); + + Calendar modifiedTime = Calendar.getInstance(); + Calendar lastDisplayTime = Calendar.getInstance(); + lastDisplayTime.setTimeInMillis(lastDisplayTimeInMillis); + + int bannerFreeRuns = 0; + + switch (state) { + case LIKED: + return false; + case INITIAL_STATE: + break; + case IGNORED: + modifiedTime.add(Calendar.WEEK_OF_YEAR, -1); + bannerFreeRuns = 5; + break; + case DISLIKED_WITH_MESSAGE: + modifiedTime.add(Calendar.MONTH, -3); + bannerFreeRuns = 3; + break; + case DISLIKED_WITHOUT_MESSAGE: + modifiedTime.add(Calendar.MONTH, -2); + break; + default: + throw new IllegalStateException("Unexpected state:" + state); + } + + if (state != RateUsState.INITIAL_STATE) { + if (modifiedTime.after(lastDisplayTime) && numberOfApplicationRuns >= bannerFreeRuns) { + settings.RATE_US_STATE.set(RateUsState.INITIAL_STATE); + modifiedTime = Calendar.getInstance(); + } else { + return false; + } + } + // Initial state now + modifiedTime.add(Calendar.HOUR, -72); + bannerFreeRuns = 3; + return modifiedTime.after(lastDisplayTime) && numberOfApplicationRuns >= bannerFreeRuns; + } + + public class PositiveButtonListener implements View.OnClickListener { + private TextView header; + private TextView subheader; + private Button positiveButton; + private Button negativeButton; + + public PositiveButtonListener(TextView header, TextView subheader, Button positiveButton, + Button negativeButton) { + this.header = header; + this.subheader = subheader; + this.positiveButton = positiveButton; + this.negativeButton = negativeButton; + } + + @Override + public void onClick(View v) { + final OsmandSettings settings = getMyApplication().getSettings(); + switch (state) { + case INITIAL_STATE: + state = RateUsBottomSheetDialog.FragmentState.USER_LIKES_APP; + + header.setText(getResources().getString(R.string.rate_this_app)); + subheader.setText(getResources().getString(R.string.rate_this_app_long)); + positiveButton.setText(getResources().getString(R.string.shared_string_ok)); + negativeButton.setText(getResources().getString(R.string.shared_string_no_thanks)); + return; + case USER_LIKES_APP: + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.LIKED); + // Assuming GooglePlay + Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName()); + Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); + try { + startActivity(goToMarket); + } catch (ActivityNotFoundException e) { + startActivity(new Intent(Intent.ACTION_VIEW, + Uri.parse("http://play.google.com/store/apps/details?id=" + + getActivity().getPackageName()))); + } + break; + case USER_DISLIKES_APP: + String email = getString(R.string.support_email); + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.DISLIKED_WITH_MESSAGE); + settings.NUMBER_OF_APPLICATION_STARTS.set(0); + settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); + Intent sendEmail = new Intent(Intent.ACTION_SENDTO); + sendEmail.setType("text/plain"); + sendEmail.setData(Uri.parse("mailto:" + email)); + sendEmail.putExtra(Intent.EXTRA_EMAIL, email); + startActivity(sendEmail); + break; + } + dismiss(); + } + } + + public class NegativeButtonListener implements View.OnClickListener { + private TextView header; + private TextView subheader; + private Button positiveButton; + private Button negativeButton; + + public NegativeButtonListener(TextView header, TextView subheader, Button positiveButton, + Button negativeButton) { + this.header = header; + this.subheader = subheader; + this.positiveButton = positiveButton; + this.negativeButton = negativeButton; + } + + @Override + public void onClick(View v) { + final OsmandSettings settings = getMyApplication().getSettings(); + switch (state) { + case INITIAL_STATE: + state = RateUsBottomSheetDialog.FragmentState.USER_DISLIKES_APP; + + header.setText(getResources().getString(R.string.user_hates_app_get_feedback)); + subheader.setText(getResources().getString(R.string.user_hates_app_get_feedback_long)); + positiveButton.setText(getResources().getString(R.string.shared_string_ok)); + negativeButton.setText(getResources().getString(R.string.shared_string_no_thanks)); + return; + case USER_LIKES_APP: + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.IGNORED); + break; + case USER_DISLIKES_APP: + settings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.DISLIKED_WITHOUT_MESSAGE); + break; + } + settings.NUMBER_OF_APPLICATION_STARTS.set(0); + settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); + dismiss(); + } + } + + public enum FragmentState { + INITIAL_STATE, + USER_LIKES_APP, + USER_DISLIKES_APP + } + + public enum RateUsState { + INITIAL_STATE, + IGNORED, + LIKED, + DISLIKED_WITH_MESSAGE, + DISLIKED_WITHOUT_MESSAGE + } +}