diff options
author | Steve Kondik <shade@chemlab.org> | 2015-09-28 14:38:16 -0700 |
---|---|---|
committer | Adnan Begovic <adnan@cyngn.com> | 2015-11-23 14:26:13 -0800 |
commit | 84c44757ac58d285aaaaf019c13c3afe9f664e80 (patch) | |
tree | 82e26aa0989a017337214b221df29023aef4f404 /src/com/android/settings/cmstats/AnonymousStats.java | |
parent | 53fefc5a6515d3903b7ffdfa4f013253ce68009b (diff) | |
download | packages_apps_Settings-84c44757ac58d285aaaaf019c13c3afe9f664e80.zip packages_apps_Settings-84c44757ac58d285aaaaf019c13c3afe9f664e80.tar.gz packages_apps_Settings-84c44757ac58d285aaaaf019c13c3afe9f664e80.tar.bz2 |
Settings: add opt out stats event
- Get rid of keeping track of queued jobs, job scheduler does this and
all we really want is a new job id to not interfere with old ones
(unless we hit the max jobs allowed, then start the count over).
- Persist whether we have reported metrics for the owner user (once per
device)
Ref: CYNGNOS-1131
Change-Id: Ib5bac2944b5ca4259ea82a357b24708377c1dc4c
Diffstat (limited to 'src/com/android/settings/cmstats/AnonymousStats.java')
-rw-r--r-- | src/com/android/settings/cmstats/AnonymousStats.java | 80 |
1 files changed, 38 insertions, 42 deletions
diff --git a/src/com/android/settings/cmstats/AnonymousStats.java b/src/com/android/settings/cmstats/AnonymousStats.java index 732a579..d85e24d 100644 --- a/src/com/android/settings/cmstats/AnonymousStats.java +++ b/src/com/android/settings/cmstats/AnonymousStats.java @@ -20,20 +20,27 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; -import android.util.ArraySet; +import android.os.UserHandle; +import android.preference.Preference; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; - -import java.util.Set; +import cyanogenmod.providers.CMSettings; public class AnonymousStats extends SettingsPreferenceFragment { + private static final String PREF_FILE_NAME = "CMStats"; /* package */ static final String ANONYMOUS_OPT_IN = "pref_anonymous_opt_in"; /* package */ static final String ANONYMOUS_LAST_CHECKED = "pref_anonymous_checked_in"; - /* package */ static final String KEY_JOB_QUEUE = "pref_job_queue"; + /* package */ static final String KEY_LAST_JOB_ID = "last_job_id"; /* package */ static final int QUEUE_MAX_THRESHOLD = 1000; + public static final String KEY_STATS = "stats_collection"; + + SwitchPreference mStatsSwitch; + public static SharedPreferences getPreferences(Context context) { return context.getSharedPreferences(PREF_FILE_NAME, 0); } @@ -42,62 +49,51 @@ public class AnonymousStats extends SettingsPreferenceFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.anonymous_stats); + mStatsSwitch = (SwitchPreference) findPreference(KEY_STATS); } - public static Set<String> getJobQueue(Context context) { - return getPreferences(context).getStringSet(KEY_JOB_QUEUE, new ArraySet<String>()); + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { + if (preference == mStatsSwitch) { + boolean checked = mStatsSwitch.isChecked(); + if (checked) { + // clear opt out flags + CMSettings.Secure.putIntForUser(getContentResolver(), + CMSettings.Secure.STATS_COLLECTION_REPORTED, 0, UserHandle.USER_OWNER); + } + // will initiate opt out sequence if necessary + ReportingServiceManager.setAlarm(getActivity()); + return true; + } + return super.onPreferenceTreeClick(preferenceScreen, preference); } - public static void clearJobQueue(Context context) { + public static void updateLastSynced(Context context) { getPreferences(context) .edit() - .remove(KEY_JOB_QUEUE) + .putLong(ANONYMOUS_LAST_CHECKED,System.currentTimeMillis()) .commit(); } - public static void addJob(Context context, int jobId) { - Set<String> jobQueue = getJobQueue(context); - jobQueue.add(String.valueOf(jobId)); - - getPreferences(context) - .edit() - .putStringSet(KEY_JOB_QUEUE, jobQueue) - .commit(); + private static int getLastJobId(Context context) { + return getPreferences(context).getInt(KEY_LAST_JOB_ID, 0); } - public static void removeJob(Context context, int jobId) { - Set<String> jobQueue = getJobQueue(context); - jobQueue.remove(String.valueOf(jobId)); + private static void setLastJobId(Context context, int id) { getPreferences(context) .edit() - .remove(KEY_JOB_QUEUE) - .commit(); - - getPreferences(context) - .edit() - .putStringSet(KEY_JOB_QUEUE, jobQueue) + .putInt(KEY_LAST_JOB_ID, id) .commit(); } - /** - * @param context context to use to get prefs - * @return Returns the next unused int in the job queue, up until {@link #QUEUE_MAX_THRESHOLD} - * is reached, then it will return -1 - */ public static int getNextJobId(Context context) { - Set<String> currentQueue = getJobQueue(context); - - if (currentQueue == null) { - return 1; - } else if (currentQueue.size() >= QUEUE_MAX_THRESHOLD) { - return -1; + int lastId = getLastJobId(context); + if (lastId >= QUEUE_MAX_THRESHOLD) { + lastId = 1; } else { - int i = 1; - while (currentQueue.contains(String.valueOf(i))) { - i++; - } - return i; - + lastId += 1; } + setLastJobId(context, lastId); + return lastId; } } |