Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Counters.java « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4869c06109115ae19aed4ce5d69378ced81c6a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.mapswithme.util;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.preference.PreferenceManager;
import android.text.TextUtils;
import android.text.format.DateUtils;

import com.mapswithme.maps.BuildConfig;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.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 static final String KEY_SHOW_REVIEW_FOR_OLD_USER = "ShowReviewForOldUser";
  private static final String KEY_MIGRATION_EXECUTED = "MigrationExecuted";

  private Counters() {}

  public static void initCounters(@NonNull Context context)
  {
    PreferenceManager.setDefaultValues(context, R.xml.prefs_main, false);
    updateLaunchCounter();
  }

  public static int getFirstInstallVersion()
  {
    return MwmApplication.prefs().getInt(KEY_APP_FIRST_INSTALL_VERSION, 0);
  }

  public static boolean isFirstStartDialogSeen()
  {
    return MwmApplication.prefs().getBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, false);
  }

  public static void setFirstStartDialogSeen()
  {
    MwmApplication.prefs()
                  .edit()
                  .putBoolean(KEY_MISC_FIRST_START_DIALOG_SEEN, true)
                  .apply();
  }


  public static void setWhatsNewShown()
  {
    MwmApplication.prefs()
                  .edit()
                  .putInt(KEY_MISC_NEWS_LAST_VERSION, BuildConfig.VERSION_CODE)
                  .apply();
  }

  public static void resetAppSessionCounters()
  {
    MwmApplication.prefs()
                  .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();
  }

  public static boolean isSessionRated(int session)
  {
    return (MwmApplication.prefs().getInt(KEY_LIKES_LAST_RATED_SESSION, 0) >= session);
  }

  public static void setRatedSession(int session)
  {
    MwmApplication.prefs()
                  .edit()
                  .putInt(KEY_LIKES_LAST_RATED_SESSION, session)
                  .apply();
  }

  /**
   * Session = single day, when app was started any number of times.
   */
  public static int getSessionCount()
  {
    return MwmApplication.prefs().getInt(KEY_APP_SESSION_NUMBER, 0);
  }

  public static boolean isRatingApplied(Class<? extends DialogFragment> dialogFragmentClass)
  {
    return MwmApplication.prefs()
                         .getBoolean(KEY_LIKES_RATED_DIALOG + dialogFragmentClass.getSimpleName(),
                                     false);
  }

  public static void setRatingApplied(Class<? extends DialogFragment> dialogFragmentClass)
  {
    MwmApplication.prefs()
                  .edit()
                  .putBoolean(KEY_LIKES_RATED_DIALOG + dialogFragmentClass.getSimpleName(), true)
                  .apply();
  }

  public static String getInstallFlavor()
  {
    return MwmApplication.prefs().getString(KEY_APP_FIRST_INSTALL_FLAVOR, "");
  }

  private static void updateLaunchCounter()
  {
    if (incrementLaunchNumber() == 0)
    {
      if (getFirstInstallVersion() == 0)
      {
        MwmApplication.prefs()
                      .edit()
                      .putInt(KEY_APP_FIRST_INSTALL_VERSION, BuildConfig.VERSION_CODE)
                      .apply();
      }

      updateInstallFlavor();
    }

    incrementSessionNumber();
  }

  private static int incrementLaunchNumber()
  {
    return increment(KEY_APP_LAUNCH_NUMBER);
  }

  private static void updateInstallFlavor()
  {
    String installedFlavor = getInstallFlavor();
    if (TextUtils.isEmpty(installedFlavor))
    {
      MwmApplication.prefs()
                    .edit()
                    .putString(KEY_APP_FIRST_INSTALL_FLAVOR, BuildConfig.FLAVOR)
                    .apply();
    }
  }

  private static void incrementSessionNumber()
  {
    long lastSessionTimestamp = MwmApplication.prefs().getLong(KEY_APP_LAST_SESSION_TIMESTAMP, 0);
    if (DateUtils.isToday(lastSessionTimestamp))
      return;

    MwmApplication.prefs()
                  .edit()
                  .putLong(KEY_APP_LAST_SESSION_TIMESTAMP, System.currentTimeMillis())
                  .apply();
    increment(KEY_APP_SESSION_NUMBER);
  }

  private static int increment(@NonNull String key)
  {
    int value = MwmApplication.prefs().getInt(key, 0);
    MwmApplication.prefs()
                  .edit()
                  .putInt(key, ++value)
                  .apply();
    return value;
  }

  public static void setShowReviewForOldUser(boolean value)
  {
    MwmApplication.prefs()
                  .edit()
                  .putBoolean(KEY_SHOW_REVIEW_FOR_OLD_USER, value)
                  .apply();
  }

  public static boolean isShowReviewForOldUser()
  {
    return MwmApplication.prefs().getBoolean(KEY_SHOW_REVIEW_FOR_OLD_USER, false);
  }

  public static boolean isMigrationNeeded()
  {
    return !MwmApplication.prefs().getBoolean(KEY_MIGRATION_EXECUTED, false);
  }

  public static void setMigrationExecuted()
  {
    MwmApplication.prefs()
                  .edit()
                  .putBoolean(KEY_MIGRATION_EXECUTED, true)
                  .apply();
  }
}