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

Commit f67abfd4 authored by Isaac Katzenelson's avatar Isaac Katzenelson
Browse files

Clear data on boot

Bug: 7329086
Change-Id: I8c60351ff23ea70c34b50b1fb5522bd431190100
parent ef9888ea
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -16,10 +16,14 @@

package com.android.deskclock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.content.SharedPreferences;
import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager;

import com.android.deskclock.timer.TimerObj;

public class AlarmInitReceiver extends BroadcastReceiver {

@@ -41,8 +45,14 @@ public class AlarmInitReceiver extends BroadcastReceiver {
                if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
                    Alarms.saveSnoozeAlert(context, Alarms.INVALID_ALARM_ID, -1);
                    Alarms.disableExpiredAlarms(context);
                }

                    // Clear stopwatch and timers data
                    SharedPreferences prefs =
                            PreferenceManager.getDefaultSharedPreferences(context);
                    Log.v("AlarmInitReceiver - Cleaning old timer and stopwatch data");
                    TimerObj.cleanTimersFromSharedPrefs(prefs);
                    Utils.clearSwSharedPref(prefs);
                }
                Alarms.setNextAlert(context);
                result.finish();
                Log.v("AlarmInitReceiver finished");
+20 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.deskclock;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
@@ -25,6 +26,8 @@ import android.os.SystemClock;
import android.text.TextUtils;
import android.view.MenuItem;

import com.android.deskclock.stopwatch.Stopwatches;

import java.util.Locale;


@@ -129,4 +132,21 @@ public class Utils {
    public static int getGrayColorId() {
        return R.color.clock_gray;
    }

    /**
     * Clears the persistent data of stopwatch (start time, state, laps, etc...).
     */
    public static void clearSwSharedPref(SharedPreferences prefs) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.remove (Stopwatches.PREF_START_TIME);
        editor.remove (Stopwatches.PREF_ACCUM_TIME);
        editor.remove (Stopwatches.PREF_STATE);
        int lapNum = prefs.getInt(Stopwatches.PREF_LAP_NUM, Stopwatches.STOPWATCH_RESET);
        for (int i = 0; i < lapNum; i++) {
            String key = Stopwatches.PREF_LAP_TIME + Integer.toString(i);
            editor.remove(key);
        }
        editor.remove(Stopwatches.PREF_LAP_NUM);
        editor.apply();
    }
}
+2 −16
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref

        ArrayList<Lap> mLaps = new ArrayList<Lap>();
        private final LayoutInflater mInflater;
        private int mBackgroundColor;
        private final int mBackgroundColor;

        public LapsListAdapter(Context context) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@@ -361,7 +361,7 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref
    private void doReset() {
        SharedPreferences prefs =
                PreferenceManager.getDefaultSharedPreferences(getActivity());
        clearSharedPref(prefs);
        Utils.clearSwSharedPref(prefs);
        mTime.clearSharedPref(prefs, "sw");
        mAccumulatedTime = 0;
        mLapsAdapter.clearLaps();
@@ -679,20 +679,6 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref
        }
    }

    private void clearSharedPref(SharedPreferences prefs) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.remove (Stopwatches.PREF_START_TIME);
        editor.remove (Stopwatches.PREF_ACCUM_TIME);
        editor.remove (Stopwatches.PREF_STATE);
        int lapNum = prefs.getInt(Stopwatches.PREF_LAP_NUM, Stopwatches.STOPWATCH_RESET);
        for (int i = 0; i < lapNum; i++) {
            String key = Stopwatches.PREF_LAP_TIME + Integer.toString(i);
            editor.remove(key);
        }
        editor.remove(Stopwatches.PREF_LAP_NUM);
        editor.apply();
    }

    public class ImageLabelAdapter extends ArrayAdapter<CharSequence> {
        private final ArrayList<CharSequence> mStrings;
        private final ArrayList<Drawable> mDrawables;
+9 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class TimerObj implements Parcelable {
@@ -264,5 +265,13 @@ public class TimerObj implements Parcelable {
        }
    }

    public static void cleanTimersFromSharedPrefs(SharedPreferences prefs) {
        ArrayList<TimerObj> timers = new  ArrayList<TimerObj>();
        getTimersFromSharedPrefs(prefs, timers);
        Iterator<TimerObj> i = timers.iterator();
        while(i.hasNext()) {
            i.next().deleteFromSharedPref(prefs);
        }
    }

}