Merge pull request #11000 from osmandapp/in_app_review

Add Google Play In-app review
This commit is contained in:
Vitaliy 2021-02-25 13:33:03 +02:00 committed by GitHub
commit 8ec89a28e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 218 additions and 125 deletions

View file

@ -199,6 +199,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'

View file

@ -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);
}

View file

@ -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 {

View file

@ -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) {

View file

@ -8,26 +8,26 @@ 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 +36,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 +54,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 +68,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 +84,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 +98,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;
}
}
}

View file

@ -0,0 +1,157 @@
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.activities.MapActivity;
import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment;
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;
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(MapActivity mapActivity) {
boolean inAppReviewSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
if (inAppReviewSupported && Version.isGooglePlayInstalled(mapActivity.getMyApplication())) {
showInAppRateDialog(mapActivity);
} else {
RateUsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager());
}
}
private static void showInAppRateDialog(FragmentActivity activity) {
final ReviewManager reviewManager = ReviewManagerFactory.create(activity);
final WeakReference<FragmentActivity> activityRef = new WeakReference<>(activity);
Task<ReviewInfo> requestReview = reviewManager.requestReviewFlow();
requestReview.addOnCompleteListener(new OnCompleteListener<ReviewInfo>() {
@Override
public void onComplete(@NonNull Task<ReviewInfo> task) {
if (task.isSuccessful()) {
FragmentActivity activity = activityRef.get();
if (activity != null) {
showInAppRateDialogInternal(reviewManager, activity, task.getResult());
}
} else {
log.error(task.getException());
}
}
});
}
private static void showInAppRateDialogInternal(ReviewManager reviewManager, FragmentActivity activity, ReviewInfo reviewInfo) {
Task<Void> reviewFlow = reviewManager.launchReviewFlow(activity, reviewInfo);
final WeakReference<FragmentActivity> activityRef = new WeakReference<>(activity);
reviewFlow.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
FragmentActivity activity = activityRef.get();
if (activity != null) {
storeRateResult(activity, RateUsState.IGNORED);
}
} else {
log.error(task.getException());
}
}
});
}
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;
}
}
}

View file

@ -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;