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

Commit 21b76e3b authored by Annie Chin's avatar Annie Chin
Browse files

Create/Manage TimerId through SharedPreferences.

Bug: 18285551
Change-Id: I15b1af308494e397b38b0b98b92c37b6be10e0f9
parent a8ae565c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -187,7 +187,7 @@ public class HandleApiCalls extends Activity {
        boolean skipUi = intent.getBooleanExtra(EXTRA_SKIP_UI, false);
        if (timer == null) {
            // Use a new timer
            timer = new TimerObj(length, label);
            timer = new TimerObj(length, label, this /* context */);
            // Timers set without presenting UI to the user will be deleted after use
            timer.mDeleteAfterUse = skipUi;
        }
+2 −1
Original line number Diff line number Diff line
@@ -446,7 +446,8 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
                @Override
                public void onAnimationStart(Animator animation) {
                    final int timerLength = mSetupView.getTime();
                    final TimerObj timerObj = new TimerObj(timerLength * DateUtils.SECOND_IN_MILLIS);
                    final TimerObj timerObj = new TimerObj(timerLength * DateUtils.SECOND_IN_MILLIS,
                        getActivity());
                    timerObj.mState = TimerObj.STATE_RUNNING;
                    updateTimerState(timerObj, Timers.START_TIMER);

+1 −1
Original line number Diff line number Diff line
@@ -955,7 +955,7 @@ public class TimerFullScreenFragment extends DeskClockFragment
            if (timerLength == 0) {
                return;
            }
            TimerObj t = new TimerObj(timerLength * DateUtils.SECOND_IN_MILLIS);
            TimerObj t = new TimerObj(timerLength * DateUtils.SECOND_IN_MILLIS, getActivity());
            t.mState = TimerObj.STATE_RUNNING;
            mAdapter.addTimer(t);
            updateTimersState(t, Timers.START_TIMER);
+28 −10
Original line number Diff line number Diff line
@@ -20,8 +20,8 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;

import com.android.deskclock.R;
import com.android.deskclock.Utils;
@@ -35,6 +35,8 @@ import java.util.Set;

public class TimerObj implements Parcelable {

    public static final String KEY_NEXT_TIMER_ID = "next_timer_id";

    private static final String TAG = "TimerObj";
    // Max timer length is 9 hours + 99 minutes + 9 seconds
    public static final long MAX_TIMER_LENGTH = (9 * 3600 + 99 * 60  + 99) * 1000;
@@ -137,11 +139,13 @@ public class TimerObj implements Parcelable {
        editor.remove(key);
        key = PREF_DELETE_AFTER_USE + id;
        editor.remove(key);
        if (timersList.isEmpty()) {
            editor.remove(KEY_NEXT_TIMER_ID);
        }
        editor.commit();
        //dumpTimersFromSharedPrefs(prefs);
    }


    @Override
    public int describeContents() {
        return 0;
@@ -168,29 +172,43 @@ public class TimerObj implements Parcelable {
        mLabel = p.readString();
    }

    public TimerObj() {
        this(0);
    private TimerObj() {
        this(0 /* timerLength */, 0 /* timerId */);
    }

    public TimerObj(long timerLength, int timerId) {
      init(timerLength, timerId);
    }

    public TimerObj(long timerLength) {
      init(timerLength);
    public TimerObj(long timerLength, Context context) {
        init(timerLength, getNextTimerId(context));
    }

    public TimerObj(long length, String label) {
        this(length);
    public TimerObj(long length, String label, Context context) {
        this(length, context);
        mLabel = label != null ? label : "";
    }

    private void init (long length) {
    private void init (long length, int timerId) {
        /* TODO: mTimerId must avoid StopwatchService.NOTIFICATION_ID,
         * TimerReceiver.IN_USE_NOTIFICATION_ID, and alarm ID's (which seem to be 1, 2, ..)
         */
        mTimerId = (int) Utils.getTimeNow();
        mTimerId = timerId;
        mStartTime = Utils.getTimeNow();
        mTimeLeft = mOriginalLength = mSetupLength = length;
        mLabel = "";
    }

    private int getNextTimerId(Context context) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        final int nextTimerId;
        synchronized (TimerObj.class) {
            nextTimerId = prefs.getInt(KEY_NEXT_TIMER_ID, 0);
            prefs.edit().putInt(KEY_NEXT_TIMER_ID, nextTimerId + 1).apply();
        }
        return nextTimerId;
    }

    public long updateTimeLeft(boolean forceUpdate) {
        if (isTicking() || forceUpdate) {
            long millis = Utils.getTimeNow();