diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index ad14ceb300..77365576bf 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -876,14 +876,6 @@ android:permission="android.permission.ACCESS_FINE_LOCATION"> - - - - - - Config.setLocationRequested()); @@ -157,7 +156,7 @@ public class SplashActivity extends AppCompatActivity return; } - if (Counters.isFirstLaunch(this) && LocationUtils.checkLocationPermission(this)) + if (Config.isFirstStartDialogSeen(this) && LocationUtils.checkLocationPermission(this)) { final LocationHelper locationHelper = app.getLocationHelper(); locationHelper.onEnteredIntoFirstRun(); @@ -194,7 +193,7 @@ public class SplashActivity extends AppCompatActivity return; } } - Counters.setFirstStartDialogSeen(this); + Config.setFirstStartDialogSeen(this); startActivity(result); finish(); } diff --git a/android/app/src/main/java/app/organicmaps/background/UpgradeReceiver.java b/android/app/src/main/java/app/organicmaps/background/UpgradeReceiver.java deleted file mode 100644 index 8969601541..0000000000 --- a/android/app/src/main/java/app/organicmaps/background/UpgradeReceiver.java +++ /dev/null @@ -1,18 +0,0 @@ -package app.organicmaps.background; - -import android.content.Context; -import android.content.Intent; - -import androidx.annotation.NonNull; - -import app.organicmaps.MwmApplication; -import app.organicmaps.MwmBroadcastReceiver; - -public class UpgradeReceiver extends MwmBroadcastReceiver -{ - @Override - protected void onReceiveInitialized(@NonNull Context context, @NonNull Intent intent) - { - MwmApplication.onUpgrade(context); - } -} diff --git a/android/app/src/main/java/app/organicmaps/util/Config.java b/android/app/src/main/java/app/organicmaps/util/Config.java index e46d608acf..99b55a8cd9 100644 --- a/android/app/src/main/java/app/organicmaps/util/Config.java +++ b/android/app/src/main/java/app/organicmaps/util/Config.java @@ -1,18 +1,11 @@ package app.organicmaps.util; -import static app.organicmaps.util.Counters.KEY_APP_FIRST_INSTALL_FLAVOR; -import static app.organicmaps.util.Counters.KEY_APP_FIRST_INSTALL_VERSION; -import static app.organicmaps.util.Counters.KEY_APP_LAST_SESSION_TIMESTAMP; -import static app.organicmaps.util.Counters.KEY_APP_LAUNCH_NUMBER; -import static app.organicmaps.util.Counters.KEY_APP_SESSION_NUMBER; -import static app.organicmaps.util.Counters.KEY_LIKES_LAST_RATED_SESSION; -import static app.organicmaps.util.Counters.KEY_MISC_FIRST_START_DIALOG_SEEN; -import static app.organicmaps.util.Counters.KEY_MISC_NEWS_LAST_VERSION; - import android.content.Context; +import android.content.SharedPreferences; import android.os.Build; import androidx.annotation.NonNull; +import androidx.preference.PreferenceManager; import app.organicmaps.BuildConfig; import app.organicmaps.MwmApplication; @@ -38,29 +31,39 @@ public final class Config private static final String KEY_MISC_USE_MOBILE_DATA = "UseMobileData"; private static final String KEY_MISC_USE_MOBILE_DATA_TIMESTAMP = "UseMobileDataTimestamp"; private static final String KEY_MISC_USE_MOBILE_DATA_ROAMING = "UseMobileDataRoaming"; - private static final String KEY_MISC_AD_FORBIDDEN = "AdForbidden"; private static final String KEY_MISC_ENABLE_SCREEN_SLEEP = "EnableScreenSleep"; private static final String KEY_MISC_SHOW_ON_LOCK_SCREEN = "ShowOnLockScreen"; private static final String KEY_MISC_AGPS_TIMESTAMP = "AGPSTimestamp"; private static final String KEY_DONATE_URL = "DonateUrl"; - private Config() {} + /** + * The total number of app launches. + */ + private static final String KEY_APP_LAUNCH_NUMBER = "LaunchNumber"; + /** + * The timestamp for the most recent app launch. + */ + private static final String KEY_APP_LAST_SESSION_TIMESTAMP = "LastSessionTimestamp"; + /** + * The version code of the first installed version of the app. + */ + private static final String KEY_APP_FIRST_INSTALL_VERSION_CODE = "FirstInstallVersion"; + /** + * The version code of the last launched version of the app. + */ + private static final String KEY_APP_LAST_INSTALL_VERSION_CODE = "LastInstallVersion"; + /** + * True if the first start animation has been seen. + */ + private static final String KEY_MISC_FIRST_START_DIALOG_SEEN = "FirstStartDialogSeen"; - private static int getInt(String key) - { - return getInt(key, 0); - } + private Config() {} private static int getInt(String key, int def) { return nativeGetInt(key, def); } - private static long getLong(String key) - { - return getLong(key, 0L); - } - private static long getLong(String key, long def) { return nativeGetLong(key, def); @@ -113,25 +116,6 @@ public final class Config nativeSetBoolean(key, value); } - public static void migrateCountersToSharedPrefs(@NonNull Context context) - { - int version = getInt(KEY_APP_FIRST_INSTALL_VERSION, BuildConfig.VERSION_CODE); - MwmApplication.prefs(context) - .edit() - .putInt(KEY_APP_LAUNCH_NUMBER, getInt(KEY_APP_LAUNCH_NUMBER)) - .putInt(KEY_APP_FIRST_INSTALL_VERSION, version) - .putString(KEY_APP_FIRST_INSTALL_FLAVOR, getString(KEY_APP_FIRST_INSTALL_FLAVOR)) - .putLong(KEY_APP_LAST_SESSION_TIMESTAMP, getLong(KEY_APP_LAST_SESSION_TIMESTAMP)) - .putInt(KEY_APP_SESSION_NUMBER, getInt(KEY_APP_SESSION_NUMBER)) - .putBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, - getBool(KEY_MISC_FIRST_START_DIALOG_SEEN)) - .putInt(KEY_MISC_NEWS_LAST_VERSION, getInt(KEY_MISC_NEWS_LAST_VERSION)) - .putInt(KEY_LIKES_LAST_RATED_SESSION, getInt(KEY_LIKES_LAST_RATED_SESSION)) - .putBoolean(KEY_MISC_ENABLE_SCREEN_SLEEP, - getBool(KEY_MISC_ENABLE_SCREEN_SLEEP)) - .apply(); - } - public static String getStoragePath() { return getString(KEY_APP_STORAGE); @@ -350,6 +334,43 @@ public final class Config return getString(KEY_DONATE_URL); } + public static void updateCounters(@NonNull Context context) + { + PreferenceManager.setDefaultValues(context, R.xml.prefs_main, false); + + final SharedPreferences prefs = MwmApplication.prefs(context); + final SharedPreferences.Editor editor = prefs.edit(); + + final int launchNumber = prefs.getInt(KEY_APP_LAUNCH_NUMBER, 0); + editor.putInt(KEY_APP_LAUNCH_NUMBER, launchNumber + 1); + editor.putLong(KEY_APP_LAST_SESSION_TIMESTAMP, System.currentTimeMillis()); + editor.putInt(KEY_APP_LAST_INSTALL_VERSION_CODE, BuildConfig.VERSION_CODE); + if (launchNumber == 0 || prefs.getInt(KEY_APP_FIRST_INSTALL_VERSION_CODE, 0) == 0) + editor.putInt(KEY_APP_FIRST_INSTALL_VERSION_CODE, BuildConfig.VERSION_CODE); + + // Clean up legacy counters. + editor.remove("FirstInstallFlavor"); + editor.remove("SessionNumber"); + editor.remove("WhatsNewShownVersion"); + editor.remove("LastRatedSession"); + editor.remove("RatedDialog"); + + editor.apply(); + } + + public static boolean isFirstStartDialogSeen(@NonNull Context context) + { + return MwmApplication.prefs(context).getBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, false); + } + + public static void setFirstStartDialogSeen(@NonNull Context context) + { + MwmApplication.prefs(context) + .edit() + .putBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, true) + .apply(); + } + private static native boolean nativeGetBoolean(String name, boolean defaultValue); private static native void nativeSetBoolean(String name, boolean value); private static native int nativeGetInt(String name, int defaultValue); diff --git a/android/app/src/main/java/app/organicmaps/util/Counters.java b/android/app/src/main/java/app/organicmaps/util/Counters.java deleted file mode 100644 index c17dd555b5..0000000000 --- a/android/app/src/main/java/app/organicmaps/util/Counters.java +++ /dev/null @@ -1,162 +0,0 @@ -package app.organicmaps.util; - -import android.content.Context; -import android.text.TextUtils; -import android.text.format.DateUtils; - -import androidx.annotation.NonNull; -import androidx.fragment.app.DialogFragment; -import androidx.preference.PreferenceManager; - -import app.organicmaps.BuildConfig; -import app.organicmaps.MwmApplication; -import app.organicmaps.R; - -public final class Counters -{ - static final String KEY_APP_LAUNCH_NUMBER = "LaunchNumber"; - static final String KEY_APP_FIRST_INSTALL_VERSION = "FirstInstallVersion"; - static final String KEY_APP_FIRST_INSTALL_FLAVOR = "FirstInstallFlavor"; - static final String KEY_APP_LAST_SESSION_TIMESTAMP = "LastSessionTimestamp"; - static final String KEY_APP_SESSION_NUMBER = "SessionNumber"; - static final String KEY_MISC_FIRST_START_DIALOG_SEEN = "FirstStartDialogSeen"; - static final String KEY_MISC_NEWS_LAST_VERSION = "WhatsNewShownVersion"; - static final String KEY_LIKES_LAST_RATED_SESSION = "LastRatedSession"; - - private static final String KEY_LIKES_RATED_DIALOG = "RatedDialog"; - - private Counters() {} - - public static void initCounters(@NonNull Context context) - { - PreferenceManager.setDefaultValues(context, R.xml.prefs_main, false); - updateLaunchCounter(context); - } - - public static int getFirstInstallVersion(@NonNull Context context) - { - return MwmApplication.prefs(context).getInt(KEY_APP_FIRST_INSTALL_VERSION, 0); - } - - public static boolean isFirstLaunch(@NonNull Context context) - { - return !MwmApplication.prefs(context).getBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, false); - } - - public static void setFirstStartDialogSeen(@NonNull Context context) - { - MwmApplication.prefs(context) - .edit() - .putBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, true) - .apply(); - } - - public static void resetAppSessionCounters(@NonNull Context context) - { - MwmApplication.prefs(context).edit() - .putInt(KEY_APP_LAUNCH_NUMBER, 0) - .putInt(KEY_APP_SESSION_NUMBER, 0) - .putLong(KEY_APP_LAST_SESSION_TIMESTAMP, 0L) - .putInt(KEY_LIKES_LAST_RATED_SESSION, 0) - .apply(); - incrementSessionNumber(context); - } - - public static boolean isSessionRated(@NonNull Context context, int session) - { - return (MwmApplication.prefs(context).getInt(KEY_LIKES_LAST_RATED_SESSION, - 0) >= session); - } - - public static void setRatedSession(@NonNull Context context, int session) - { - MwmApplication.prefs(context).edit() - .putInt(KEY_LIKES_LAST_RATED_SESSION, session) - .apply(); - } - - /** - * Session = single day, when app was started any number of times. - */ - public static int getSessionCount(@NonNull Context context) - { - return MwmApplication.prefs(context).getInt(KEY_APP_SESSION_NUMBER, 0); - } - - public static boolean isRatingApplied(@NonNull Context context, - Class dialogFragmentClass) - { - return MwmApplication.prefs(context) - .getBoolean(KEY_LIKES_RATED_DIALOG + dialogFragmentClass.getSimpleName(), - false); - } - - public static void setRatingApplied(@NonNull Context context, - Class dialogFragmentClass) - { - MwmApplication.prefs(context).edit() - .putBoolean(KEY_LIKES_RATED_DIALOG + dialogFragmentClass.getSimpleName(), true) - .apply(); - } - - public static String getInstallFlavor(@NonNull Context context) - { - return MwmApplication.prefs(context).getString(KEY_APP_FIRST_INSTALL_FLAVOR, ""); - } - - private static void updateLaunchCounter(@NonNull Context context) - { - if (incrementLaunchNumber(context) == 0) - { - if (getFirstInstallVersion(context) == 0) - { - MwmApplication.prefs(context) - .edit() - .putInt(KEY_APP_FIRST_INSTALL_VERSION, BuildConfig.VERSION_CODE) - .apply(); - } - - updateInstallFlavor(context); - } - - incrementSessionNumber(context); - } - - private static int incrementLaunchNumber(@NonNull Context context) - { - return increment(context, KEY_APP_LAUNCH_NUMBER); - } - - private static void updateInstallFlavor(@NonNull Context context) - { - String installedFlavor = getInstallFlavor(context); - if (TextUtils.isEmpty(installedFlavor)) - { - MwmApplication.prefs(context).edit() - .putString(KEY_APP_FIRST_INSTALL_FLAVOR, BuildConfig.FLAVOR) - .apply(); - } - } - - private static void incrementSessionNumber(@NonNull Context context) - { - long lastSessionTimestamp = MwmApplication.prefs(context) - .getLong(KEY_APP_LAST_SESSION_TIMESTAMP, 0); - if (DateUtils.isToday(lastSessionTimestamp)) - return; - - MwmApplication.prefs(context).edit() - .putLong(KEY_APP_LAST_SESSION_TIMESTAMP, System.currentTimeMillis()) - .apply(); - increment(context, KEY_APP_SESSION_NUMBER); - } - - private static int increment(@NonNull Context context, @NonNull String key) - { - int value = MwmApplication.prefs(context).getInt(key, 0); - MwmApplication.prefs(context).edit() - .putInt(key, ++value) - .apply(); - return value; - } -}