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

Commit c29c3e8a authored by Koushik Dutta's avatar Koushik Dutta Committed by Michael Bestas
Browse files

CMStats changes.

Remove the notification on boot. Users are thinking they need to opt out to get rid of the notification.
The setting to opt-out still exists in the Settings app.
If the user does not opt out within 'tFrame' time (1 day), the checkin service will now run; prior
to this, the checkin service would not run until the next reboot. That next reboot could occur within a
few minutes (startup crash, which then automatically checks in before the user has a chance to opt out)
or even the possibility of never. This is unpredictable and buggy.
Change the checkin frequency from 7 days to 1 day.

Change-Id: I66a26a6c200710146c0de3832253417fae557e52
parent 5ea0ba1e
Loading
Loading
Loading
Loading
+0 −9
Original line number Diff line number Diff line
@@ -40,8 +40,6 @@ public class AnonymousStats extends SettingsPreferenceFragment

    protected static final String ANONYMOUS_OPT_IN = "pref_anonymous_opt_in";

    protected static final String ANONYMOUS_FIRST_BOOT = "pref_anonymous_first_boot";

    protected static final String ANONYMOUS_LAST_CHECKED = "pref_anonymous_checked_in";

    protected static final String ANONYMOUS_ALARM_SET = "pref_anonymous_alarm_set";
@@ -65,13 +63,6 @@ public class AnonymousStats extends SettingsPreferenceFragment
            mPrefs = getActivity().getSharedPreferences("CMStats", 0);
            mEnableReporting = (CheckBoxPreference) prefSet.findPreference(ANONYMOUS_OPT_IN);
            mViewStats = (Preference) prefSet.findPreference(VIEW_STATS);
            boolean firstBoot = mPrefs.getBoolean(ANONYMOUS_FIRST_BOOT, true);
            if (mEnableReporting.isChecked() && firstBoot) {
                mPrefs.edit().putBoolean(ANONYMOUS_FIRST_BOOT, false).apply();
                ReportingServiceManager.launchService(getActivity());
            }
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.cancel(1);
        }
    }

+8 −29
Original line number Diff line number Diff line
@@ -54,10 +54,6 @@ public class ReportingService extends Service {

    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {
        if (intent.getBooleanExtra("firstBoot", false)) {
            promptUser();
            Log.d(TAG, "Prompting user for opt-in.");
        } else {
        Log.d(TAG, "User has opted in -- reporting.");
        Thread thread = new Thread() {
            @Override
@@ -66,7 +62,6 @@ public class ReportingService extends Service {
            }
        };
        thread.start();
        }
        return Service.START_REDELIVER_INTENT;
    }

@@ -125,20 +120,4 @@ public class ReportingService extends Service {
        ReportingServiceManager.setAlarm(this);
        stopSelf();
    }

    private void promptUser() {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent nI = new Intent();
        nI.setComponent(new ComponentName(getPackageName(),Settings.AnonymousStatsActivity.class.getName()));
        PendingIntent pI = PendingIntent.getActivity(this, 0, nI, 0);
        Notification.Builder builder = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_cm_stats_notif)
        .setAutoCancel(true)
        .setTicker(getString(R.string.anonymous_statistics_title))
        .setContentIntent(pI)
        .setWhen(0)
        .setContentTitle(getString(R.string.anonymous_statistics_title))
        .setContentText(getString(R.string.anonymous_notification_desc));
        nm.notify(1, builder.getNotification());
    }
}
+8 −8
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ import android.util.Log;

public class ReportingServiceManager extends BroadcastReceiver {

    public static final long dMill = 24 * 60 * 60 * 1000;
    public static final long tFrame = 7 * dMill;
    public static final long dMill = 24L * 60L * 60L * 1000L;
    public static final long tFrame = 1L * dMill;

    @Override
    public void onReceive(Context ctx, Intent intent) {
@@ -45,13 +45,15 @@ public class ReportingServiceManager extends BroadcastReceiver {
        SharedPreferences prefs = ctx.getSharedPreferences("CMStats", 0);
        prefs.edit().putBoolean(AnonymousStats.ANONYMOUS_ALARM_SET, false).apply();
        boolean optedIn = prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true);
        boolean firstBoot = prefs.getBoolean(AnonymousStats.ANONYMOUS_FIRST_BOOT, true);
        if (!optedIn || firstBoot) {
        if (!optedIn) {
            return;
        }
        long lastSynced = prefs.getLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, 0);
        if (lastSynced == 0) {
            return;
            // never synced, so let's fake out that the last sync was just now.
            // this will allow the user tFrame time to opt out before it will start
            // sending up anonymous stats.
            lastSynced = System.currentTimeMillis();
        }
        long timeLeft = (lastSynced + tFrame) - System.currentTimeMillis();
        Intent sIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
@@ -68,7 +70,6 @@ public class ReportingServiceManager extends BroadcastReceiver {
        if (networkInfo != null && networkInfo.isConnected()) {
            SharedPreferences prefs = ctx.getSharedPreferences("CMStats", 0);
            long lastSynced = prefs.getLong(AnonymousStats.ANONYMOUS_LAST_CHECKED, 0);
            boolean firstBoot = prefs.getBoolean(AnonymousStats.ANONYMOUS_FIRST_BOOT, true);
            boolean optedIn = prefs.getBoolean(AnonymousStats.ANONYMOUS_OPT_IN, true);
            boolean alarmSet = prefs.getBoolean(AnonymousStats.ANONYMOUS_ALARM_SET, false);
            if (alarmSet) {
@@ -80,10 +81,9 @@ public class ReportingServiceManager extends BroadcastReceiver {
            } else if (System.currentTimeMillis() - lastSynced >= tFrame) {
                shouldSync = true;
            }
            if ((shouldSync && optedIn) || firstBoot) {
            if (shouldSync && optedIn) {
                Intent sIntent = new Intent();
                sIntent.setComponent(new ComponentName(ctx.getPackageName(), ReportingService.class.getName()));
                sIntent.putExtra("firstBoot", firstBoot);
                ctx.startService(sIntent);
            } else if (optedIn) {
                setAlarm(ctx);