From 1026a9314ab8526a78333394f0ccb6922e351e4d Mon Sep 17 00:00:00 2001 From: velichkomarija Date: Fri, 27 Nov 2020 18:52:27 +0300 Subject: [PATCH] [android] Refactoring MwmApplication.prefs. Refactoring MwmApplication, Statistic, SharedPropertiesUtils. --- android/src/com/mapswithme/maps/MwmApplication.java | 2 +- android/src/com/mapswithme/maps/SplashActivity.java | 2 +- .../mapswithme/maps/settings/SettingsPrefsFragment.java | 6 +++--- android/src/com/mapswithme/util/Config.java | 6 ++++-- .../src/com/mapswithme/util/SharedPropertiesUtils.java | 8 ++++---- .../src/com/mapswithme/util/statistics/Statistics.java | 6 +++--- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 0e144c1124..3113af3ced 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -253,7 +253,7 @@ public class MwmApplication extends Application implements AppBackgroundTracker. filesPath, tempPath, StorageUtils.getObbGooglePath(), BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE, UiUtils.isTablet()); - Config.setStatisticsEnabled(SharedPropertiesUtils.isStatisticsEnabled()); + Config.setStatisticsEnabled(SharedPropertiesUtils.isStatisticsEnabled(this)); @SuppressWarnings("unused") Statistics s = Statistics.INSTANCE; diff --git a/android/src/com/mapswithme/maps/SplashActivity.java b/android/src/com/mapswithme/maps/SplashActivity.java index e4b01f6ef3..9d7aabb7c9 100644 --- a/android/src/com/mapswithme/maps/SplashActivity.java +++ b/android/src/com/mapswithme/maps/SplashActivity.java @@ -263,7 +263,7 @@ public class SplashActivity extends AppCompatActivity if (Counters.isMigrationNeeded()) { - Config.migrateCountersToSharedPrefs(); + Config.migrateCountersToSharedPrefs(getApplicationContext()); Counters.setMigrationExecuted(); } diff --git a/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java b/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java index 5becd131ce..eab9f27661 100644 --- a/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java +++ b/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java @@ -677,13 +677,13 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment if (pref == null) return; - ((TwoStatePreference)pref).setChecked(SharedPropertiesUtils.isStatisticsEnabled()); + ((TwoStatePreference)pref).setChecked(SharedPropertiesUtils.isStatisticsEnabled(requireContext())); pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - Statistics.INSTANCE.setStatEnabled((Boolean) newValue); + Statistics.INSTANCE.setStatEnabled(requireContext(), (Boolean) newValue); return true; } }); @@ -710,7 +710,7 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment { boolean enabled = (Boolean) newValue; TrackRecorder.setEnabled(enabled); - Statistics.INSTANCE.setStatEnabled(enabled); + Statistics.INSTANCE.setStatEnabled(requireContext(), enabled); trackPref.setEnabled(enabled); if (root != null) root.setSummary(enabled ? R.string.on : R.string.off); diff --git a/android/src/com/mapswithme/util/Config.java b/android/src/com/mapswithme/util/Config.java index 7e0f652f11..4eb46e7788 100644 --- a/android/src/com/mapswithme/util/Config.java +++ b/android/src/com/mapswithme/util/Config.java @@ -1,5 +1,7 @@ package com.mapswithme.util; +import android.content.Context; + import androidx.annotation.NonNull; import com.mapswithme.maps.BuildConfig; @@ -102,10 +104,10 @@ public final class Config nativeSetBoolean(key, value); } - public static void migrateCountersToSharedPrefs() + public static void migrateCountersToSharedPrefs(@NonNull Context context) { int version = getInt(KEY_APP_FIRST_INSTALL_VERSION, BuildConfig.VERSION_CODE); - MwmApplication.prefs() + MwmApplication.prefs(context) .edit() .putInt(KEY_APP_LAUNCH_NUMBER, getInt(KEY_APP_LAUNCH_NUMBER)) .putInt(KEY_APP_FIRST_INSTALL_VERSION, version) diff --git a/android/src/com/mapswithme/util/SharedPropertiesUtils.java b/android/src/com/mapswithme/util/SharedPropertiesUtils.java index b252b309b7..de7b6d5989 100644 --- a/android/src/com/mapswithme/util/SharedPropertiesUtils.java +++ b/android/src/com/mapswithme/util/SharedPropertiesUtils.java @@ -37,14 +37,14 @@ public final class SharedPropertiesUtils throw new IllegalStateException("Try instantiate utility class SharedPropertiesUtils"); } - public static boolean isStatisticsEnabled() + public static boolean isStatisticsEnabled(@NonNull Context context) { - return MwmApplication.prefs().getBoolean(KEY_PREF_STATISTICS, true); + return MwmApplication.prefs(context).getBoolean(KEY_PREF_STATISTICS, true); } - public static void setStatisticsEnabled(boolean enabled) + public static void setStatisticsEnabled(@NonNull Context context, boolean enabled) { - MwmApplication.prefs().edit().putBoolean(KEY_PREF_STATISTICS, enabled).apply(); + MwmApplication.prefs(context).edit().putBoolean(KEY_PREF_STATISTICS, enabled).apply(); } public static void setShouldShowEmulateBadStorageSetting(boolean show) diff --git a/android/src/com/mapswithme/util/statistics/Statistics.java b/android/src/com/mapswithme/util/statistics/Statistics.java index 237d787df3..1f194e4d86 100644 --- a/android/src/com/mapswithme/util/statistics/Statistics.java +++ b/android/src/com/mapswithme/util/statistics/Statistics.java @@ -892,8 +892,8 @@ public enum Statistics Statistics() { - mEnabled = SharedPropertiesUtils.isStatisticsEnabled(); final Context context = MwmApplication.get(); + mEnabled = SharedPropertiesUtils.isStatisticsEnabled(context); // At the moment we need special handling for Alohalytics to enable/disable logging of events in core C++ code. if (mEnabled) org.alohalytics.Statistics.enable(context); @@ -1002,9 +1002,9 @@ public enum Statistics mMediator.getEventLogger().stopActivity(activity); } - public void setStatEnabled(boolean isEnabled) + public void setStatEnabled(@NonNull Context context, boolean isEnabled) { - SharedPropertiesUtils.setStatisticsEnabled(isEnabled); + SharedPropertiesUtils.setStatisticsEnabled(context, isEnabled); Config.setStatisticsEnabled(isEnabled); // We track if user turned on/off statistics to understand data better.