forked from organicmaps/organicmaps
[android]Remove usage of MwmApplication.prefs from Counters.
This commit is contained in:
parent
01d6dabc42
commit
25fabdeff6
10 changed files with 90 additions and 80 deletions
|
@ -1480,10 +1480,12 @@ public class MwmActivity extends BaseMwmFragmentActivity
|
|||
super.onResumeFragments();
|
||||
RoutingController.get().restore();
|
||||
|
||||
if (!LikesManager.INSTANCE.isNewUser() && Counters.isShowReviewForOldUser())
|
||||
Context context = getApplicationContext();
|
||||
|
||||
if (!LikesManager.INSTANCE.isNewUser(context) && Counters.isShowReviewForOldUser(context))
|
||||
{
|
||||
LikesManager.INSTANCE.showRateDialogForOldUser(this);
|
||||
Counters.setShowReviewForOldUser(false);
|
||||
Counters.setShowReviewForOldUser(context, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -357,9 +357,9 @@ public class MwmApplication extends Application implements AppBackgroundTracker.
|
|||
return mPurchaseOperationObservable;
|
||||
}
|
||||
|
||||
public static void onUpgrade()
|
||||
public static void onUpgrade(@NonNull Context context)
|
||||
{
|
||||
Counters.resetAppSessionCounters();
|
||||
Counters.resetAppSessionCounters(context);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
|
|
@ -261,10 +261,11 @@ public class SplashActivity extends AppCompatActivity
|
|||
mBaseDelegate.onResume();
|
||||
mCanceled = false;
|
||||
|
||||
if (Counters.isMigrationNeeded())
|
||||
Context context = getApplicationContext();
|
||||
if (Counters.isMigrationNeeded(context))
|
||||
{
|
||||
Config.migrateCountersToSharedPrefs(getApplicationContext());
|
||||
Counters.setMigrationExecuted();
|
||||
Config.migrateCountersToSharedPrefs(context);
|
||||
Counters.setMigrationExecuted(context);
|
||||
}
|
||||
|
||||
final boolean isFirstLaunch = WelcomeDialogFragment.isFirstLaunch(this);
|
||||
|
|
|
@ -4,6 +4,8 @@ import androidx.annotation.NonNull;
|
|||
import androidx.fragment.app.DialogFragment;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.mapswithme.maps.BuildConfig;
|
||||
|
@ -28,7 +30,6 @@ public enum LikesManager
|
|||
|
||||
private static final int DIALOG_DELAY_DEFAULT = 30000;
|
||||
private static final int DIALOG_DELAY_SHORT = 5000;
|
||||
private static final int SESSION_NUM = Counters.getSessionCount();
|
||||
|
||||
/*
|
||||
Maps type of like dialog to the dialog, performing like.
|
||||
|
@ -72,13 +73,12 @@ public enum LikesManager
|
|||
sFragments.add(DownloaderFragment.class);
|
||||
}
|
||||
|
||||
private final boolean mIsNewUser = (Counters.getFirstInstallVersion() == BuildConfig.VERSION_CODE);
|
||||
private Runnable mLikeRunnable;
|
||||
private WeakReference<FragmentActivity> mActivityRef;
|
||||
|
||||
public boolean isNewUser()
|
||||
public boolean isNewUser(@NonNull Context context)
|
||||
{
|
||||
return mIsNewUser;
|
||||
return (Counters.getFirstInstallVersion(context) == BuildConfig.VERSION_CODE);
|
||||
}
|
||||
|
||||
public void showDialogs(FragmentActivity activity)
|
||||
|
@ -88,18 +88,22 @@ public enum LikesManager
|
|||
if (!ConnectionState.isConnected())
|
||||
return;
|
||||
|
||||
final LikeType type = mIsNewUser ? sNewUsersMapping.get(SESSION_NUM) : sOldUsersMapping.get(SESSION_NUM);
|
||||
Context context = activity.getApplicationContext();
|
||||
int sessionCount = Counters.getSessionCount(context);
|
||||
final LikeType type = isNewUser(context) ?
|
||||
sNewUsersMapping.get(sessionCount) : sOldUsersMapping.get(sessionCount);
|
||||
if (type != null)
|
||||
displayLikeDialog(type.clazz, type.delay);
|
||||
displayLikeDialog(context, type.clazz, type.delay);
|
||||
}
|
||||
|
||||
public void showRateDialogForOldUser(FragmentActivity activity)
|
||||
{
|
||||
if (mIsNewUser)
|
||||
Context context = activity.getApplicationContext();
|
||||
if (isNewUser(context))
|
||||
return;
|
||||
|
||||
mActivityRef = new WeakReference<>(activity);
|
||||
displayLikeDialog(LikeType.GPLAY_OLD_USERS.clazz, LikeType.GPLAY_OLD_USERS.delay);
|
||||
displayLikeDialog(context, LikeType.GPLAY_OLD_USERS.clazz, LikeType.GPLAY_OLD_USERS.delay);
|
||||
}
|
||||
|
||||
public void cancelDialogs()
|
||||
|
@ -118,12 +122,16 @@ public enum LikesManager
|
|||
return false;
|
||||
}
|
||||
|
||||
private void displayLikeDialog(final Class<? extends DialogFragment> dialogFragmentClass, final int delayMillis)
|
||||
private void displayLikeDialog(@NonNull Context context,
|
||||
final Class<? extends DialogFragment> dialogFragmentClass,
|
||||
final int delayMillis)
|
||||
{
|
||||
if (Counters.isSessionRated(SESSION_NUM) || Counters.isRatingApplied(dialogFragmentClass))
|
||||
int sessionCount = Counters.getSessionCount(context);
|
||||
if (Counters.isSessionRated(context, sessionCount) ||
|
||||
Counters.isRatingApplied(context, dialogFragmentClass))
|
||||
return;
|
||||
|
||||
Counters.setRatedSession(SESSION_NUM);
|
||||
Counters.setRatedSession(context, sessionCount);
|
||||
|
||||
UiThread.cancelDelayedTasks(mLikeRunnable);
|
||||
mLikeRunnable = new Runnable()
|
||||
|
|
|
@ -64,7 +64,7 @@ public class RateStoreDialogFragment extends BaseMwmDialogFragment implements Vi
|
|||
mRating = rating;
|
||||
if (rating >= BuildConfig.RATING_THRESHOLD)
|
||||
{
|
||||
Counters.setRatingApplied(RateStoreDialogFragment.class);
|
||||
Counters.setRatingApplied(requireContext(), RateStoreDialogFragment.class);
|
||||
dismiss();
|
||||
Utils.openAppInMarket(getActivity(), BuildConfig.REVIEW_URL);
|
||||
}
|
||||
|
|
|
@ -23,6 +23,6 @@ public class UpgradeReceiver extends BroadcastReceiver
|
|||
+ !backgroundTracker().isForeground();
|
||||
LOGGER.i(TAG, msg);
|
||||
CrashlyticsUtils.log(Log.INFO, TAG, msg);
|
||||
MwmApplication.onUpgrade();
|
||||
MwmApplication.onUpgrade(context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import android.view.View;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
@ -121,7 +122,9 @@ public class NewsFragment extends BaseNewsFragment implements AlertDialogCallbac
|
|||
public static boolean showOn(@NonNull FragmentActivity activity,
|
||||
final @Nullable NewsDialogListener listener)
|
||||
{
|
||||
if (Counters.getFirstInstallVersion() >= BuildConfig.VERSION_CODE)
|
||||
Context context = activity.getApplicationContext();
|
||||
|
||||
if (Counters.getFirstInstallVersion(context) >= BuildConfig.VERSION_CODE)
|
||||
return false;
|
||||
|
||||
FragmentManager fm = activity.getSupportFragmentManager();
|
||||
|
@ -140,16 +143,16 @@ public class NewsFragment extends BaseNewsFragment implements AlertDialogCallbac
|
|||
return true;
|
||||
}
|
||||
|
||||
String currentTitle = getCurrentTitleConcatenation(activity.getApplicationContext());
|
||||
String currentTitle = getCurrentTitleConcatenation(context);
|
||||
String oldTitle = SharedPropertiesUtils.getWhatsNewTitleConcatenation();
|
||||
if (currentTitle.equals(oldTitle) && !recreate(activity, NewsFragment.class))
|
||||
return false;
|
||||
|
||||
create(activity, NewsFragment.class, listener);
|
||||
|
||||
Counters.setWhatsNewShown();
|
||||
Counters.setWhatsNewShown(context);
|
||||
SharedPropertiesUtils.setWhatsNewTitleConcatenation(currentTitle);
|
||||
Counters.setShowReviewForOldUser(true);
|
||||
Counters.setShowReviewForOldUser(context, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ public class WelcomeDialogFragment extends BaseMwmDialogFragment implements View
|
|||
|
||||
public static boolean isFirstLaunch(@NonNull FragmentActivity activity)
|
||||
{
|
||||
if (Counters.getFirstInstallVersion() < BuildConfig.VERSION_CODE)
|
||||
if (Counters.getFirstInstallVersion(activity.getApplicationContext()) < BuildConfig.VERSION_CODE)
|
||||
return false;
|
||||
|
||||
FragmentManager fm = activity.getSupportFragmentManager();
|
||||
|
|
|
@ -31,12 +31,12 @@ public final class Counters
|
|||
public static void initCounters(@NonNull Context context)
|
||||
{
|
||||
PreferenceManager.setDefaultValues(context, R.xml.prefs_main, false);
|
||||
updateLaunchCounter();
|
||||
updateLaunchCounter(context);
|
||||
}
|
||||
|
||||
public static int getFirstInstallVersion()
|
||||
public static int getFirstInstallVersion(@NonNull Context context)
|
||||
{
|
||||
return MwmApplication.prefs().getInt(KEY_APP_FIRST_INSTALL_VERSION, 0);
|
||||
return MwmApplication.prefs(context).getInt(KEY_APP_FIRST_INSTALL_VERSION, 0);
|
||||
}
|
||||
|
||||
public static boolean isFirstStartDialogSeen(@NonNull Context context)
|
||||
|
@ -53,35 +53,34 @@ public final class Counters
|
|||
}
|
||||
|
||||
|
||||
public static void setWhatsNewShown()
|
||||
public static void setWhatsNewShown(@NonNull Context context)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
MwmApplication.prefs(context)
|
||||
.edit()
|
||||
.putInt(KEY_MISC_NEWS_LAST_VERSION, BuildConfig.VERSION_CODE)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static void resetAppSessionCounters()
|
||||
public static void resetAppSessionCounters(@NonNull Context context)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
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();
|
||||
incrementSessionNumber(context);
|
||||
}
|
||||
|
||||
public static boolean isSessionRated(int session)
|
||||
public static boolean isSessionRated(@NonNull Context context, int session)
|
||||
{
|
||||
return (MwmApplication.prefs().getInt(KEY_LIKES_LAST_RATED_SESSION, 0) >= session);
|
||||
return (MwmApplication.prefs(context).getInt(KEY_LIKES_LAST_RATED_SESSION,
|
||||
0) >= session);
|
||||
}
|
||||
|
||||
public static void setRatedSession(int session)
|
||||
public static void setRatedSession(@NonNull Context context, int session)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putInt(KEY_LIKES_LAST_RATED_SESSION, session)
|
||||
.apply();
|
||||
}
|
||||
|
@ -89,111 +88,108 @@ public final class Counters
|
|||
/**
|
||||
* Session = single day, when app was started any number of times.
|
||||
*/
|
||||
public static int getSessionCount()
|
||||
public static int getSessionCount(@NonNull Context context)
|
||||
{
|
||||
return MwmApplication.prefs().getInt(KEY_APP_SESSION_NUMBER, 0);
|
||||
return MwmApplication.prefs(context).getInt(KEY_APP_SESSION_NUMBER, 0);
|
||||
}
|
||||
|
||||
public static boolean isRatingApplied(Class<? extends DialogFragment> dialogFragmentClass)
|
||||
public static boolean isRatingApplied(@NonNull Context context,
|
||||
Class<? extends DialogFragment> dialogFragmentClass)
|
||||
{
|
||||
return MwmApplication.prefs()
|
||||
return MwmApplication.prefs(context)
|
||||
.getBoolean(KEY_LIKES_RATED_DIALOG + dialogFragmentClass.getSimpleName(),
|
||||
false);
|
||||
}
|
||||
|
||||
public static void setRatingApplied(Class<? extends DialogFragment> dialogFragmentClass)
|
||||
public static void setRatingApplied(@NonNull Context context,
|
||||
Class<? extends DialogFragment> dialogFragmentClass)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putBoolean(KEY_LIKES_RATED_DIALOG + dialogFragmentClass.getSimpleName(), true)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static String getInstallFlavor()
|
||||
public static String getInstallFlavor(@NonNull Context context)
|
||||
{
|
||||
return MwmApplication.prefs().getString(KEY_APP_FIRST_INSTALL_FLAVOR, "");
|
||||
return MwmApplication.prefs(context).getString(KEY_APP_FIRST_INSTALL_FLAVOR, "");
|
||||
}
|
||||
|
||||
private static void updateLaunchCounter()
|
||||
private static void updateLaunchCounter(@NonNull Context context)
|
||||
{
|
||||
if (incrementLaunchNumber() == 0)
|
||||
if (incrementLaunchNumber(context) == 0)
|
||||
{
|
||||
if (getFirstInstallVersion() == 0)
|
||||
if (getFirstInstallVersion(context) == 0)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
MwmApplication.prefs(context)
|
||||
.edit()
|
||||
.putInt(KEY_APP_FIRST_INSTALL_VERSION, BuildConfig.VERSION_CODE)
|
||||
.apply();
|
||||
}
|
||||
|
||||
updateInstallFlavor();
|
||||
updateInstallFlavor(context);
|
||||
}
|
||||
|
||||
incrementSessionNumber();
|
||||
incrementSessionNumber(context);
|
||||
}
|
||||
|
||||
private static int incrementLaunchNumber()
|
||||
private static int incrementLaunchNumber(@NonNull Context context)
|
||||
{
|
||||
return increment(KEY_APP_LAUNCH_NUMBER);
|
||||
return increment(context, KEY_APP_LAUNCH_NUMBER);
|
||||
}
|
||||
|
||||
private static void updateInstallFlavor()
|
||||
private static void updateInstallFlavor(@NonNull Context context)
|
||||
{
|
||||
String installedFlavor = getInstallFlavor();
|
||||
String installedFlavor = getInstallFlavor(context);
|
||||
if (TextUtils.isEmpty(installedFlavor))
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putString(KEY_APP_FIRST_INSTALL_FLAVOR, BuildConfig.FLAVOR)
|
||||
.apply();
|
||||
}
|
||||
}
|
||||
|
||||
private static void incrementSessionNumber()
|
||||
private static void incrementSessionNumber(@NonNull Context context)
|
||||
{
|
||||
long lastSessionTimestamp = MwmApplication.prefs().getLong(KEY_APP_LAST_SESSION_TIMESTAMP, 0);
|
||||
long lastSessionTimestamp = MwmApplication.prefs(context)
|
||||
.getLong(KEY_APP_LAST_SESSION_TIMESTAMP, 0);
|
||||
if (DateUtils.isToday(lastSessionTimestamp))
|
||||
return;
|
||||
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putLong(KEY_APP_LAST_SESSION_TIMESTAMP, System.currentTimeMillis())
|
||||
.apply();
|
||||
increment(KEY_APP_SESSION_NUMBER);
|
||||
increment(context, KEY_APP_SESSION_NUMBER);
|
||||
}
|
||||
|
||||
private static int increment(@NonNull String key)
|
||||
private static int increment(@NonNull Context context, @NonNull String key)
|
||||
{
|
||||
int value = MwmApplication.prefs().getInt(key, 0);
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
int value = MwmApplication.prefs(context).getInt(key, 0);
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putInt(key, ++value)
|
||||
.apply();
|
||||
return value;
|
||||
}
|
||||
|
||||
public static void setShowReviewForOldUser(boolean value)
|
||||
public static void setShowReviewForOldUser(@NonNull Context context, boolean value)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putBoolean(KEY_SHOW_REVIEW_FOR_OLD_USER, value)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static boolean isShowReviewForOldUser()
|
||||
public static boolean isShowReviewForOldUser(@NonNull Context context)
|
||||
{
|
||||
return MwmApplication.prefs().getBoolean(KEY_SHOW_REVIEW_FOR_OLD_USER, false);
|
||||
return MwmApplication.prefs(context).getBoolean(KEY_SHOW_REVIEW_FOR_OLD_USER, false);
|
||||
}
|
||||
|
||||
public static boolean isMigrationNeeded()
|
||||
public static boolean isMigrationNeeded(@NonNull Context context)
|
||||
{
|
||||
return !MwmApplication.prefs().getBoolean(KEY_MIGRATION_EXECUTED, false);
|
||||
return !MwmApplication.prefs(context).getBoolean(KEY_MIGRATION_EXECUTED, false);
|
||||
}
|
||||
|
||||
public static void setMigrationExecuted()
|
||||
public static void setMigrationExecuted(@NonNull Context context)
|
||||
{
|
||||
MwmApplication.prefs()
|
||||
.edit()
|
||||
MwmApplication.prefs(context).edit()
|
||||
.putBoolean(KEY_MIGRATION_EXECUTED, true)
|
||||
.apply();
|
||||
}
|
||||
|
|
|
@ -1008,7 +1008,7 @@ public enum Statistics
|
|||
Config.setStatisticsEnabled(isEnabled);
|
||||
|
||||
// We track if user turned on/off statistics to understand data better.
|
||||
trackEvent(EventName.STATISTICS_STATUS_CHANGED + " " + Counters.getInstallFlavor(),
|
||||
trackEvent(EventName.STATISTICS_STATUS_CHANGED + " " + Counters.getInstallFlavor(context),
|
||||
params().add(EventParam.ENABLED, String.valueOf(isEnabled)));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue