From b422c8276d4ba1539b6944405cac797726376f8b Mon Sep 17 00:00:00 2001 From: cepprice Date: Wed, 24 Feb 2021 18:18:36 +0500 Subject: [PATCH 1/4] Extract logic to new helper --- OsmAnd/build.gradle | 1 + .../net/osmand/plus/OsmandApplication.java | 19 ++- .../osmand/plus/activities/MapActivity.java | 36 +++--- ...islikeOsmAndBottomSheetDialogFragment.java | 37 +++--- .../RateUsBottomSheetDialogFragment.java | 82 ++---------- .../net/osmand/plus/dialogs/ReviewHelper.java | 44 +++++++ .../net/osmand/plus/helpers/RateUsHelper.java | 117 ++++++++++++++++++ .../plus/settings/backend/OsmandSettings.java | 10 +- 8 files changed, 222 insertions(+), 124 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java create mode 100644 OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java diff --git a/OsmAnd/build.gradle b/OsmAnd/build.gradle index 11c9388fa0..ee54aebea7 100644 --- a/OsmAnd/build.gradle +++ b/OsmAnd/build.gradle @@ -198,6 +198,7 @@ dependencies { } implementation 'com.jaredrummler:colorpicker:1.1.0' implementation "org.bouncycastle:bcpkix-jdk15on:1.56" + implementation 'com.google.android.play:core:1.9.1' huaweiImplementation 'com.huawei.hms:iap:5.0.2.300' diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 165a9a9c07..8f89660c00 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -24,13 +24,6 @@ import android.view.View; import android.view.accessibility.AccessibilityManager; import android.widget.Toast; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatDelegate; -import androidx.multidex.MultiDex; -import androidx.multidex.MultiDexApplication; - import net.osmand.AndroidUtils; import net.osmand.FileUtils; import net.osmand.IndexConstants; @@ -53,7 +46,6 @@ import net.osmand.plus.api.SQLiteAPI; import net.osmand.plus.api.SQLiteAPIImpl; import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.dialogs.CrashBottomSheetDialogFragment; -import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment; import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadService; import net.osmand.plus.download.IndexItem; @@ -62,6 +54,7 @@ import net.osmand.plus.helpers.DayNightHelper; import net.osmand.plus.helpers.LocaleHelper; import net.osmand.plus.helpers.LocationServiceHelper; import net.osmand.plus.helpers.LockHelper; +import net.osmand.plus.helpers.RateUsHelper; import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.helpers.enums.DrivingRegion; import net.osmand.plus.helpers.enums.MetricsConstants; @@ -106,6 +99,12 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatDelegate; +import androidx.multidex.MultiDex; +import androidx.multidex.MultiDexApplication; import btools.routingapp.BRouterServiceConnection; import btools.routingapp.IBRouterService; @@ -288,8 +287,8 @@ public class OsmandApplication extends MultiDexApplication { if (routingHelper != null) { routingHelper.getVoiceRouter().onApplicationTerminate(); } - if(RateUsBottomSheetDialogFragment.shouldShow(this)) { - osmandSettings.RATE_US_STATE.set(RateUsBottomSheetDialogFragment.RateUsState.IGNORED); + if(RateUsHelper.shouldShowRateDialog(this)) { + osmandSettings.RATE_US_STATE.set(RateUsHelper.RateUsState.IGNORED); } getNotificationHelper().removeNotifications(false); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 7adfd1a811..2273c91742 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -30,21 +30,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import androidx.core.app.ActivityCompat; -import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback; -import androidx.core.content.ContextCompat; -import androidx.drawerlayout.widget.DrawerLayout; -import androidx.fragment.app.DialogFragment; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentManager.BackStackEntry; -import androidx.preference.Preference; -import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceFragmentCompat.OnPreferenceStartFragmentCallback; - import net.osmand.AndroidUtils; import net.osmand.GPXUtilities.GPXFile; import net.osmand.Location; @@ -87,7 +72,6 @@ import net.osmand.plus.dashboard.DashBaseFragment; import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dialogs.CrashBottomSheetDialogFragment; import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment; -import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment; import net.osmand.plus.dialogs.SendAnalyticsBottomSheetDialogFragment; import net.osmand.plus.dialogs.WhatsNewDialogFragment; import net.osmand.plus.dialogs.XMasDialogFragment; @@ -102,6 +86,7 @@ import net.osmand.plus.helpers.DiscountHelper; import net.osmand.plus.helpers.IntentHelper; import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.helpers.LockHelper.LockUIAdapter; +import net.osmand.plus.helpers.RateUsHelper; import net.osmand.plus.helpers.ScrollHelper; import net.osmand.plus.helpers.ScrollHelper.OnScrollEventListener; import net.osmand.plus.importfiles.ImportHelper; @@ -170,6 +155,21 @@ import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.core.app.ActivityCompat; +import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback; +import androidx.core.content.ContextCompat; +import androidx.drawerlayout.widget.DrawerLayout; +import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentManager.BackStackEntry; +import androidx.preference.Preference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceFragmentCompat.OnPreferenceStartFragmentCallback; + import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID; public class MapActivity extends OsmandActionBarActivity implements DownloadEvents, @@ -731,9 +731,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (CrashBottomSheetDialogFragment.shouldShow(settings, this)) { SecondSplashScreenFragment.SHOW = false; CrashBottomSheetDialogFragment.showInstance(getSupportFragmentManager()); - } else if (RateUsBottomSheetDialogFragment.shouldShow(app)) { + } else if (RateUsHelper.shouldShowRateDialog(app)) { SecondSplashScreenFragment.SHOW = false; - RateUsBottomSheetDialogFragment.showInstance(getSupportFragmentManager()); + RateUsHelper.showRateDialog(this); } } } else { diff --git a/OsmAnd/src/net/osmand/plus/dialogs/DislikeOsmAndBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/dialogs/DislikeOsmAndBottomSheetDialogFragment.java index 4cd7275db1..eaa1f067d1 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/DislikeOsmAndBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/DislikeOsmAndBottomSheetDialogFragment.java @@ -7,26 +7,26 @@ import android.net.Uri; import android.os.Bundle; import android.view.View; +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.helpers.RateUsHelper; +import net.osmand.plus.helpers.RateUsHelper.RateUsState; + +import org.apache.commons.logging.Log; + import androidx.annotation.NonNull; import androidx.appcompat.view.ContextThemeWrapper; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; -import net.osmand.PlatformUtil; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.settings.backend.OsmandSettings; -import net.osmand.plus.R; -import net.osmand.plus.base.MenuBottomSheetDialogFragment; -import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; -import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment.RateUsState; - -import org.apache.commons.logging.Log; - public class DislikeOsmAndBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { public static final String TAG = "DislikeOsmAndBottomSheetDialogFragment"; private static final Log LOG = PlatformUtil.getLog(DislikeOsmAndBottomSheetDialogFragment.class); - private RateUsState newRateUsState = RateUsState.IGNORED; + private RateUsHelper rateUsHelper; @Override public void createMenuItems(Bundle savedInstanceState) { @@ -35,6 +35,8 @@ public class DislikeOsmAndBottomSheetDialogFragment extends MenuBottomSheetDialo return; } + rateUsHelper = new RateUsHelper(); + final View titleView = View.inflate(new ContextThemeWrapper(context, themeRes), R.layout.dislike_title, null); final SimpleBottomSheetItem titleItem = (SimpleBottomSheetItem) new SimpleBottomSheetItem.Builder() .setCustomView(titleView) @@ -51,7 +53,7 @@ public class DislikeOsmAndBottomSheetDialogFragment extends MenuBottomSheetDialo protected void onDismissButtonClickAction() { OsmandApplication app = getMyApplication(); if (app != null) { - newRateUsState = RateUsState.DISLIKED_WITHOUT_MESSAGE; + rateUsHelper.updateState(RateUsState.DISLIKED_WITHOUT_MESSAGE); } } @@ -64,7 +66,7 @@ public class DislikeOsmAndBottomSheetDialogFragment extends MenuBottomSheetDialo protected void onRightBottomButtonClick() { OsmandApplication app = getMyApplication(); if (app != null) { - newRateUsState = RateUsState.DISLIKED_WITH_MESSAGE; + rateUsHelper.updateState(RateUsState.DISLIKED_WITH_MESSAGE); String email = getString(R.string.support_email); Intent sendEmail = new Intent(Intent.ACTION_SENDTO); sendEmail.setData(Uri.parse("mailto:" + email)); @@ -78,14 +80,7 @@ public class DislikeOsmAndBottomSheetDialogFragment extends MenuBottomSheetDialo public void onDismiss(@NonNull DialogInterface dialog) { super.onDismiss(dialog); FragmentActivity activity = getActivity(); - if (newRateUsState != null && activity != null && !activity.isChangingConfigurations()) { - OsmandApplication app = (OsmandApplication) activity.getApplication(); - OsmandSettings settings = app.getSettings(); - RateUsState newState = RateUsState.getNewState(app, newRateUsState); - settings.RATE_US_STATE.set(newState); - settings.NUMBER_OF_APP_STARTS_ON_DISLIKE_MOMENT.set(app.getAppInitializer().getNumberOfStarts()); - settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); - } + rateUsHelper.storeRateResult(activity); } public static void showInstance(@NonNull FragmentManager fm) { diff --git a/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java index 188031ea1d..c414004c74 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java @@ -8,26 +8,27 @@ import android.os.Bundle; import android.view.ContextThemeWrapper; import android.view.View; -import androidx.annotation.NonNull; -import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.FragmentManager; - import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.helpers.RateUsHelper; +import net.osmand.plus.helpers.RateUsHelper.RateUsState; import org.apache.commons.logging.Log; +import androidx.annotation.NonNull; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; + public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { public static final String TAG = "RateUsBottomSheetDialogFragment"; private static final Log LOG = PlatformUtil.getLog(SendAnalyticsBottomSheetDialogFragment.class); private static final long SIXTY_DAYS = 60 * 24 * 60 * 60 * 1000L; - private RateUsState newRateUsState = RateUsState.IGNORED; + private RateUsHelper rateUsHelper; @Override public void createMenuItems(Bundle savedInstanceState) { @@ -36,6 +37,8 @@ public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragme return; } + rateUsHelper = new RateUsHelper(); + final View titleView = View.inflate(new ContextThemeWrapper(context, themeRes), R.layout.rate_us_title, null); final SimpleBottomSheetItem titleItem = (SimpleBottomSheetItem) new SimpleBottomSheetItem.Builder() .setCustomView(titleView) @@ -52,7 +55,7 @@ public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragme protected void onDismissButtonClickAction() { FragmentManager fm = getFragmentManager(); if (fm != null) { - newRateUsState = null; + rateUsHelper.updateState(null); DislikeOsmAndBottomSheetDialogFragment.showInstance(fm); } } @@ -66,7 +69,7 @@ public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragme protected void onRightBottomButtonClick() { OsmandApplication app = getMyApplication(); if (app != null) { - newRateUsState = RateUsState.LIKED; + rateUsHelper.updateState(RateUsState.LIKED); Uri uri = Uri.parse(Version.getUrlWithUtmRef(app, app.getPackageName())); try { Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); @@ -82,16 +85,7 @@ public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragme public void onDismiss(@NonNull DialogInterface dialog) { super.onDismiss(dialog); FragmentActivity activity = getActivity(); - if (newRateUsState != null && activity != null && !activity.isChangingConfigurations()) { - OsmandApplication app = (OsmandApplication) activity.getApplication(); - OsmandSettings settings = app.getSettings(); - RateUsState newState = RateUsState.getNewState(app, newRateUsState); - settings.RATE_US_STATE.set(newState); - if (newState != RateUsState.LIKED) { - settings.NUMBER_OF_APP_STARTS_ON_DISLIKE_MOMENT.set(app.getAppInitializer().getNumberOfStarts()); - } - settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); - } + rateUsHelper.storeRateResult(activity); } public static void showInstance(@NonNull FragmentManager fm) { @@ -105,56 +99,4 @@ public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragme } } - public static boolean shouldShow(OsmandApplication app) { - long firstInstalledDays = app.getAppInitializer().getFirstInstalledDays(); - //Do not show dialog if not google play version or more than 350 days left from the first start - if (!Version.isGooglePlayEnabled() || firstInstalledDays > 350) { - return false; - } - OsmandSettings settings = app.getSettings(); - int numberOfStarts = app.getAppInitializer().getNumberOfStarts(); - RateUsState state = settings.RATE_US_STATE.get(); - switch (state) { - //Do not show anymore if liked - case LIKED: - case DISLIKED_OR_IGNORED_AGAIN: - return false; - //First dialog after 15 days from the first start or 100 starts - case INITIAL_STATE: - return firstInstalledDays > 15 || numberOfStarts > 100; - //Second dialog after 60 days or 50 starts from the first appearance (if ignored or disliked) - case IGNORED: - case DISLIKED_WITH_MESSAGE: - case DISLIKED_WITHOUT_MESSAGE: - int startsOnDislikeMoment = settings.NUMBER_OF_APP_STARTS_ON_DISLIKE_MOMENT.get(); - long lastDisplayTimeInMillis = settings.LAST_DISPLAY_TIME.get(); - long currentTime = System.currentTimeMillis(); - return currentTime - lastDisplayTimeInMillis > SIXTY_DAYS || numberOfStarts - startsOnDislikeMoment > 50; - } - return false; - } - - public enum RateUsState { - INITIAL_STATE, - IGNORED, - LIKED, - DISLIKED_WITH_MESSAGE, - DISLIKED_WITHOUT_MESSAGE, - DISLIKED_OR_IGNORED_AGAIN; - - public static RateUsState getNewState(OsmandApplication app, RateUsState requiredState) { - RateUsState currentState = app.getSettings().RATE_US_STATE.get(); - switch (requiredState) { - case INITIAL_STATE: - case LIKED: - case DISLIKED_OR_IGNORED_AGAIN: - return requiredState; - case IGNORED: - case DISLIKED_WITH_MESSAGE: - case DISLIKED_WITHOUT_MESSAGE: - return currentState == INITIAL_STATE ? requiredState : RateUsState.DISLIKED_OR_IGNORED_AGAIN; - } - return requiredState; - } - } } diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java b/OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java new file mode 100644 index 0000000000..70a01cab2a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java @@ -0,0 +1,44 @@ +package net.osmand.plus.dialogs; + +import android.os.Debug; +import android.util.Log; + +import com.google.android.play.core.review.ReviewInfo; +import com.google.android.play.core.review.ReviewManager; +import com.google.android.play.core.review.ReviewManagerFactory; +import com.google.android.play.core.tasks.OnCompleteListener; +import com.google.android.play.core.tasks.Task; + +import net.osmand.plus.activities.MapActivity; + +import androidx.annotation.NonNull; + +public class ReviewHelper { + + public static void review(final MapActivity mapActivity) { + final ReviewManager manager = ReviewManagerFactory.create(mapActivity); + Task request = manager.requestReviewFlow(); + request.addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + showInAppReview(manager, mapActivity, task.getResult()); + } + } + }); + } + + private static void showInAppReview(ReviewManager manager, MapActivity mapActivity, ReviewInfo task) { + Task flow = manager.launchReviewFlow(mapActivity, task); + flow.addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + // TODO: Update + Log.v("M_ReviewHelper", "Shown"); + } + } + }); + } + +} diff --git a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java new file mode 100644 index 0000000000..7e5258739f --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java @@ -0,0 +1,117 @@ +package net.osmand.plus.helpers; + +import android.os.Build; + +import com.google.android.play.core.review.ReviewInfo; +import com.google.android.play.core.review.ReviewManager; +import com.google.android.play.core.review.ReviewManagerFactory; +import com.google.android.play.core.tasks.OnCompleteListener; +import com.google.android.play.core.tasks.Task; + +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.Version; +import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment; +import net.osmand.plus.settings.backend.OsmandSettings; + +import org.apache.commons.logging.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentActivity; + +public class RateUsHelper { + + private static final Log log = PlatformUtil.getLog(RateUsHelper.class); + private static final long SIXTY_DAYS = 60 * 24 * 60 * 60 * 1000L; + + private RateUsState rateUsState; + + public RateUsHelper() { + this.rateUsState = RateUsState.IGNORED; + } + + public void storeRateResult(FragmentActivity activity) { + storeRateResult(activity, rateUsState); + } + + private static void storeRateResult(FragmentActivity activity, RateUsState state) { + if (state != null && activity != null && !activity.isChangingConfigurations()) { + OsmandApplication app = (OsmandApplication) activity.getApplication(); + OsmandSettings settings = app.getSettings(); + RateUsState newState = RateUsState.getNewState(app, state); + settings.RATE_US_STATE.set(newState); + if (newState != RateUsState.LIKED) { + settings.NUMBER_OF_APP_STARTS_ON_DISLIKE_MOMENT.set(app.getAppInitializer().getNumberOfStarts()); + } + settings.LAST_DISPLAY_TIME.set(System.currentTimeMillis()); + } + } + + public void updateState(@Nullable RateUsState state) { + this.rateUsState = state; + } + + public static boolean shouldShowRateDialog(OsmandApplication app) { + long firstInstalledDays = app.getAppInitializer().getFirstInstalledDays(); + //Do not show dialog if not google play version or more than 350 days left from the first start + if (!Version.isGooglePlayEnabled() || firstInstalledDays > 350) { + return false; + } + OsmandSettings settings = app.getSettings(); + int numberOfStarts = app.getAppInitializer().getNumberOfStarts(); + RateUsState state = settings.RATE_US_STATE.get(); + switch (state) { + //Do not show anymore if liked + case LIKED: + case DISLIKED_OR_IGNORED_AGAIN: + return false; + //First dialog after 15 days from the first start or 100 starts + case INITIAL_STATE: + return firstInstalledDays > 15 || numberOfStarts > 100; + //Second dialog after 60 days or 50 starts from the first appearance (if ignored or disliked) + case IGNORED: + case DISLIKED_WITH_MESSAGE: + case DISLIKED_WITHOUT_MESSAGE: + int startsOnDislikeMoment = settings.NUMBER_OF_APP_STARTS_ON_DISLIKE_MOMENT.get(); + long lastDisplayTimeInMillis = settings.LAST_DISPLAY_TIME.get(); + long currentTime = System.currentTimeMillis(); + return currentTime - lastDisplayTimeInMillis > SIXTY_DAYS || numberOfStarts - startsOnDislikeMoment > 50; + } + return false; + } + + public static void showRateDialog(FragmentActivity activity) { + boolean inAppReviewSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; + if (inAppReviewSupported) { + // TODO + } else { + RateUsBottomSheetDialogFragment.showInstance(activity.getSupportFragmentManager()); + } + } + + public enum RateUsState { + INITIAL_STATE, + IGNORED, + LIKED, + DISLIKED_WITH_MESSAGE, + DISLIKED_WITHOUT_MESSAGE, + DISLIKED_OR_IGNORED_AGAIN; + + public static RateUsState getNewState(OsmandApplication app, RateUsState requiredState) { + RateUsState currentState = app.getSettings().RATE_US_STATE.get(); + switch (requiredState) { + case INITIAL_STATE: + case LIKED: + case DISLIKED_OR_IGNORED_AGAIN: + return requiredState; + case IGNORED: + case DISLIKED_WITH_MESSAGE: + case DISLIKED_WITHOUT_MESSAGE: + return currentState == INITIAL_STATE ? requiredState : RateUsState.DISLIKED_OR_IGNORED_AGAIN; + } + return requiredState; + } + } + +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index 9652fc3bad..db49cb4273 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -11,10 +11,6 @@ import android.net.NetworkInfo; import android.os.Build; import android.os.Environment; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.util.Pair; - import net.osmand.FileUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; @@ -35,8 +31,8 @@ import net.osmand.plus.api.SettingsAPI; import net.osmand.plus.api.SettingsAPI.SettingsEditor; import net.osmand.plus.api.SettingsAPIImpl; import net.osmand.plus.audionotes.NotesSortByMode; -import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment.RateUsState; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; +import net.osmand.plus.helpers.RateUsHelper.RateUsState; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.enums.AngularConstants; import net.osmand.plus.helpers.enums.AutoZoomMap; @@ -80,6 +76,10 @@ import java.util.Map; import java.util.Set; import java.util.StringTokenizer; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.util.Pair; + import static net.osmand.aidlapi.OsmAndCustomizationConstants.CONFIGURE_MAP_ITEM_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_ACTIONS; From 16593e1a34e5b59d7151c03cd07310d6996d7183 Mon Sep 17 00:00:00 2001 From: cepprice Date: Wed, 24 Feb 2021 21:59:31 +0500 Subject: [PATCH 2/4] Add in app review --- .../net/osmand/plus/helpers/RateUsHelper.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java index 7e5258739f..f993d885ae 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java @@ -84,12 +84,41 @@ public class RateUsHelper { public static void showRateDialog(FragmentActivity activity) { boolean inAppReviewSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; if (inAppReviewSupported) { - // TODO + showInAppRateDialog(activity); } else { RateUsBottomSheetDialogFragment.showInstance(activity.getSupportFragmentManager()); } } + private static void showInAppRateDialog(final FragmentActivity activity) { + final ReviewManager reviewManager = ReviewManagerFactory.create(activity); + Task requestReview = reviewManager.requestReviewFlow(); + requestReview.addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + showInAppRateDialogInternal(reviewManager, activity, task.getResult()); + } else { + log.error(task.getException()); + } + } + }); + } + + private static void showInAppRateDialogInternal(ReviewManager reviewManager, final FragmentActivity activity, ReviewInfo reviewInfo) { + Task reviewFlow = reviewManager.launchReviewFlow(activity, reviewInfo); + reviewFlow.addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + storeRateResult(activity, RateUsState.IGNORED); + } else { + log.error(task.getException()); + } + } + }); + } + public enum RateUsState { INITIAL_STATE, IGNORED, From e084416ce13040bcd68dca0062db448c05b64250 Mon Sep 17 00:00:00 2001 From: cepprice Date: Thu, 25 Feb 2021 13:59:35 +0500 Subject: [PATCH 3/4] Show old rate dialog if not google play installed --- OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java index f993d885ae..e61edbe5b5 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java @@ -11,6 +11,7 @@ import com.google.android.play.core.tasks.Task; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.Version; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment; import net.osmand.plus.settings.backend.OsmandSettings; @@ -81,12 +82,12 @@ public class RateUsHelper { return false; } - public static void showRateDialog(FragmentActivity activity) { + public static void showRateDialog(MapActivity mapActivity) { boolean inAppReviewSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; - if (inAppReviewSupported) { - showInAppRateDialog(activity); + if (inAppReviewSupported && Version.isGooglePlayInstalled(mapActivity.getMyApplication())) { + showInAppRateDialog(mapActivity); } else { - RateUsBottomSheetDialogFragment.showInstance(activity.getSupportFragmentManager()); + RateUsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager()); } } From 588d9b2916965f24a8f48825006d73eeb4119d34 Mon Sep 17 00:00:00 2001 From: cepprice Date: Thu, 25 Feb 2021 15:55:55 +0500 Subject: [PATCH 4/4] Small edit --- .../RateUsBottomSheetDialogFragment.java | 1 - .../net/osmand/plus/dialogs/ReviewHelper.java | 44 ------------------- .../net/osmand/plus/helpers/RateUsHelper.java | 18 ++++++-- 3 files changed, 14 insertions(+), 49 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java diff --git a/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java index c414004c74..67204cbbad 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/RateUsBottomSheetDialogFragment.java @@ -26,7 +26,6 @@ import androidx.fragment.app.FragmentManager; public class RateUsBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { public static final String TAG = "RateUsBottomSheetDialogFragment"; private static final Log LOG = PlatformUtil.getLog(SendAnalyticsBottomSheetDialogFragment.class); - private static final long SIXTY_DAYS = 60 * 24 * 60 * 60 * 1000L; private RateUsHelper rateUsHelper; diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java b/OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java deleted file mode 100644 index 70a01cab2a..0000000000 --- a/OsmAnd/src/net/osmand/plus/dialogs/ReviewHelper.java +++ /dev/null @@ -1,44 +0,0 @@ -package net.osmand.plus.dialogs; - -import android.os.Debug; -import android.util.Log; - -import com.google.android.play.core.review.ReviewInfo; -import com.google.android.play.core.review.ReviewManager; -import com.google.android.play.core.review.ReviewManagerFactory; -import com.google.android.play.core.tasks.OnCompleteListener; -import com.google.android.play.core.tasks.Task; - -import net.osmand.plus.activities.MapActivity; - -import androidx.annotation.NonNull; - -public class ReviewHelper { - - public static void review(final MapActivity mapActivity) { - final ReviewManager manager = ReviewManagerFactory.create(mapActivity); - Task request = manager.requestReviewFlow(); - request.addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - showInAppReview(manager, mapActivity, task.getResult()); - } - } - }); - } - - private static void showInAppReview(ReviewManager manager, MapActivity mapActivity, ReviewInfo task) { - Task flow = manager.launchReviewFlow(mapActivity, task); - flow.addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - // TODO: Update - Log.v("M_ReviewHelper", "Shown"); - } - } - }); - } - -} diff --git a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java index e61edbe5b5..29068ab46d 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/RateUsHelper.java @@ -17,6 +17,8 @@ import net.osmand.plus.settings.backend.OsmandSettings; import org.apache.commons.logging.Log; +import java.lang.ref.WeakReference; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; @@ -91,14 +93,18 @@ public class RateUsHelper { } } - private static void showInAppRateDialog(final FragmentActivity activity) { + private static void showInAppRateDialog(FragmentActivity activity) { final ReviewManager reviewManager = ReviewManagerFactory.create(activity); + final WeakReference activityRef = new WeakReference<>(activity); Task requestReview = reviewManager.requestReviewFlow(); requestReview.addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { - showInAppRateDialogInternal(reviewManager, activity, task.getResult()); + FragmentActivity activity = activityRef.get(); + if (activity != null) { + showInAppRateDialogInternal(reviewManager, activity, task.getResult()); + } } else { log.error(task.getException()); } @@ -106,13 +112,17 @@ public class RateUsHelper { }); } - private static void showInAppRateDialogInternal(ReviewManager reviewManager, final FragmentActivity activity, ReviewInfo reviewInfo) { + private static void showInAppRateDialogInternal(ReviewManager reviewManager, FragmentActivity activity, ReviewInfo reviewInfo) { Task reviewFlow = reviewManager.launchReviewFlow(activity, reviewInfo); + final WeakReference activityRef = new WeakReference<>(activity); reviewFlow.addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { - storeRateResult(activity, RateUsState.IGNORED); + FragmentActivity activity = activityRef.get(); + if (activity != null) { + storeRateResult(activity, RateUsState.IGNORED); + } } else { log.error(task.getException()); }