Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9b1aa93 authored by Adnan's avatar Adnan Committed by Brint E. Kriebel
Browse files

Settings: Migrate stats opt out to global settings.

Control newly introduced global setting

Change-Id: Iea22206cd8ac8bf7d45e4b7dabfb9f0d1e602e0e
(cherry picked from commit bf8e9f15)
parent 767d7318
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;

import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;

@@ -85,7 +86,7 @@ public class AnonymousStats extends SettingsPreferenceFragment implements
                mOkDialog.setOnDismissListener(this);
            } else {
                // Disable reporting
                mPrefs.edit().putBoolean(ANONYMOUS_OPT_IN, false).apply();
                Utilities.setStatsCollectionEnabled(getActivity(), false);
            }
        } else if (preference == mViewStats) {
            // Display the stats page
@@ -114,7 +115,7 @@ public class AnonymousStats extends SettingsPreferenceFragment implements
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            mOkClicked = true;
            mPrefs.edit().putBoolean(ANONYMOUS_OPT_IN, true).apply();
            Utilities.setStatsCollectionEnabled(getActivity(), true);
            ReportingServiceManager.launchService(getActivity());
        } else if (which == DialogInterface.BUTTON_NEGATIVE) {
            mEnableReporting.setChecked(false);
+15 −5
Original line number Diff line number Diff line
@@ -19,12 +19,12 @@ package com.android.settings.cmstats;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.util.Log;

public class ReportingServiceManager extends BroadcastReceiver {
@@ -43,8 +43,10 @@ public class ReportingServiceManager extends BroadcastReceiver {

    public static void setAlarm(Context context, long millisFromNow) {
        SharedPreferences prefs = AnonymousStats.getPreferences(context);
        boolean optedIn = prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true);
        if (!optedIn) {
        if (prefs.contains(AnonymousStats.ANONYMOUS_OPT_IN)) {
            migrate(context, prefs);
        }
        if (!Utilities.isStatsCollectionEnabled(context)) {
            return;
        }

@@ -80,10 +82,11 @@ public class ReportingServiceManager extends BroadcastReceiver {
        }

        SharedPreferences prefs = AnonymousStats.getPreferences(context);
        boolean optedIn = prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true);
        if (!optedIn) {

        if (!Utilities.isStatsCollectionEnabled(context)) {
            return;
        }

        long lastSynced = prefs.getLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, 0);
        if (lastSynced == 0) {
            setAlarm(context, 0);
@@ -100,4 +103,11 @@ public class ReportingServiceManager extends BroadcastReceiver {
        intent.setClass(context, ReportingService.class);
        context.startService(intent);
    }

    private static void migrate(Context context, SharedPreferences prefs) {
        Utilities.setStatsCollectionEnabled(context,
                prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true));
        prefs.edit().remove(AnonymousStats.ANONYMOUS_OPT_IN).commit();
    }

}
+21 −0
Original line number Diff line number Diff line
@@ -75,4 +75,25 @@ public class Utilities {
            return null;
        }
    }

    /**
     * Check to see if global stats are enabled.
     * @param context
     * @return Whether or not stats collection is enabled.
     */
    public static boolean isStatsCollectionEnabled(Context context) {
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.STATS_COLLECTION, 1) != 0;
    }

    /**
     * Enabled or disable stats collection
     * @param context
     * @param enabled Boolean that sets collection being enabled.
     */
    public static void setStatsCollectionEnabled(Context context, boolean enabled) {
        int enable = (enabled) ? 1 : 0;
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.STATS_COLLECTION, enable);
    }
}