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

Commit 91ffb2a0 authored by Isaac Katzenelson's avatar Isaac Katzenelson
Browse files

Add support for SET_TIMER api

Bug: 7497400, 8131590
Fixes alarm not created when using the API with SKIP_UI is false

Change-Id: If401c88d2d4945a1d5b85b421f7c52a329b63c49
parent 121b8a59
Loading
Loading
Loading
Loading
+77 −10
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package com.android.deskclock;

import static android.provider.AlarmClock.ACTION_SET_ALARM;
import static android.provider.AlarmClock.ACTION_SET_TIMER;
import static android.provider.AlarmClock.EXTRA_DELETE_AFTER_USE;
import static android.provider.AlarmClock.EXTRA_HOUR;
import static android.provider.AlarmClock.EXTRA_LENGTH;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
import static android.provider.AlarmClock.EXTRA_MINUTES;
import static android.provider.AlarmClock.EXTRA_SKIP_UI;
@@ -27,16 +29,28 @@ import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.android.deskclock.provider.Alarm;
import com.android.deskclock.provider.DaysOfWeek;
import com.android.deskclock.timer.TimerFragment;
import com.android.deskclock.timer.TimerObj;
import com.android.deskclock.timer.Timers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class HandleApiCalls extends Activity {

    public static final long TIMER_MIN_LENGTH = 1000;
    public static final long TIMER_MAX_LENGTH = 24 * 60 * 60 * 1000;

    @Override
    protected void onCreate(Bundle icicle) {
        try {
@@ -107,24 +121,77 @@ public class HandleApiCalls extends Activity {
    }

    private void handleSetTimer(Intent intent) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        // If no length is supplied , show the timer setup view
        if (!intent.hasExtra(EXTRA_LENGTH)) {
            startActivity(new Intent(this, DeskClock.class)
                  .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX)
                  .putExtra(TimerFragment.GOTO_SETUP_VIEW, true));
            return;
        }

        final long length = 1000l * intent.getIntExtra(EXTRA_LENGTH, 0);
        if (length < TIMER_MIN_LENGTH || length > TIMER_MAX_LENGTH) {
            Log.i("Invalid timer length requested: " + length);
            return;
        }
        String label = intent.getStringExtra(EXTRA_MESSAGE);
        if (label == null) {
            label = "";
        }
        final boolean deleteAfterUse = intent.getBooleanExtra(EXTRA_DELETE_AFTER_USE, false);

        TimerObj timer = null;
        // Do not delete existing timers by reusing them and deleting them after use
        if (!deleteAfterUse) {
            // Find an existing matching timer
            final ArrayList<TimerObj> timers = new ArrayList<TimerObj>();
            TimerObj.getTimersFromSharedPrefs(prefs, timers);
            for (TimerObj t : timers) {
                if (t.mSetupLength == length && (TextUtils.equals(label, t.mLabel))
                        && t.mState == TimerObj.STATE_RESTART) {
                    timer = t;
                    break;
                }
            }
        }

        if (timer == null) {
            // Use a new timer
            timer = new TimerObj(length, label);
        }

        timer.mState = TimerObj.STATE_RUNNING;
        timer.mStartTime = Utils.getTimeNow();
        timer.mDeleteAfterUse = deleteAfterUse;
        timer.writeToSharedPref(prefs);

        // Tell TimerReceiver that the timer was started
        sendBroadcast(new Intent().setAction(Timers.START_TIMER)
                .putExtra(Timers.TIMER_INTENT_EXTRA, timer.mTimerId));

        if (intent.getBooleanExtra(EXTRA_SKIP_UI, false)) {
            Utils.showInUseNotifications(this);
        } else {
            startActivity(new Intent(this, DeskClock.class)
                    .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX));
        }
    }

    private void enableAlarm(Alarm alarm, boolean enable, boolean skipUi) {
        if (enable) {
            Alarms.enableAlarm(this, alarm.id, true);
            Alarms.enableAlarm(this, (int)alarm.id, true);
            alarm.enabled = true;
        }
        AlarmUtils.popAlarmSetToast(this, alarm.calculateAlarmTime());
        if (skipUi) {
        Alarms.setAlarm(this, alarm);
        } else {
            Intent createAlarm = new Intent(this, DeskClock.class);
            createAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            createAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
            createAlarm.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
            createAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
            startActivity(createAlarm);
        AlarmUtils.popAlarmSetToast(this, alarm.calculateAlarmTime());
        if (!skipUi) {
            Intent createdAlarm = new Intent(this, DeskClock.class);
            createdAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            createdAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
            createdAlarm.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
            createdAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
            startActivity(createdAlarm);
        }
    }
}
+63 −33
Original line number Diff line number Diff line
@@ -67,6 +67,8 @@ public class TimerFragment extends DeskClockFragment
    private static final String TAG = "TimerFragment";
    private static final String KEY_SETUP_SELECTED = "_setup_selected";
    private static final String KEY_ENTRY_STATE = "entry_state";
    public static final String GOTO_SETUP_VIEW = "deskclock.timers.gotosetup";

    private Bundle mViewState = null;
    private ListView mTimersList;
    private View mNewTimerPage;
@@ -513,8 +515,12 @@ public class TimerFragment extends DeskClockFragment

    @Override
    public void onResume() {
        Intent newIntent = null;

        if (getActivity() instanceof DeskClock) {
            ((DeskClock)getActivity()).registerPageChangedListener(this);
            DeskClock activity = (DeskClock) getActivity();
            activity.registerPageChangedListener(this);
            newIntent = activity.getIntent();
        }
        super.onResume();
        mPrefs.registerOnSharedPreferenceChangeListener(this);
@@ -557,6 +563,10 @@ public class TimerFragment extends DeskClockFragment
        if (v != null) {
            getView().setVisibility(View.VISIBLE);
        }

        if (newIntent != null) {
            processIntent(newIntent);
        }
    }

    @Override
@@ -724,35 +734,7 @@ public class TimerFragment extends DeskClockFragment
                if (t.mState == TimerObj.STATE_TIMESUP) {
                    cancelTimerNotification(t.mTimerId);
                }
                // Animate deletion, first alpha, then height
                ObjectAnimator a = ObjectAnimator.ofFloat(t.mView, View.ALPHA, 1f, 0f);
                a.setInterpolator(new AccelerateInterpolator());
                a.setDuration(100);
                a.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ObjectAnimator b = ObjectAnimator.ofInt(
                                t.mView, "animatedHeight", t.mView.getHeight(), 0);
                        b.setInterpolator(new AccelerateInterpolator());
                        b.setDuration(200);
                        b.addListener(new AnimatorListenerAdapter() {
                                @Override
                            public void onAnimationEnd(Animator animation) {
                                mAdapter.deleteTimer(t.mTimerId);
                                if (mAdapter.getCount() == 0) {
                                    if (mOnEmptyListListener == null) {
                                        mTimerSetup.reset();
                                        gotoSetupView();
                                    } else {
                                        mOnEmptyListListener.onEmptyList();
                                    }
                                }
                            }
                        });
                        b.start();
                    }
                });
                a.start();
                animateTimerDeletion(t);
                // Tell receiver the timer was deleted.
                // It will stop all activity related to the
                // timer
@@ -808,9 +790,6 @@ public class TimerFragment extends DeskClockFragment
        }
    }




    private void onStopButtonPressed(TimerObj t) {
        switch(t.mState) {
            case TimerObj.STATE_RUNNING:
@@ -836,6 +815,13 @@ public class TimerFragment extends DeskClockFragment
                updateTimersState(t, Timers.TIMER_DONE);
                cancelTimerNotification(t.mTimerId);
                updateTimesUpMode(t);
                if (t.mDeleteAfterUse) {
                    animateTimerDeletion(t);
                    // Tell receiver the timer was deleted.
                    // It will stop all activity related to the
                    // timer
                    updateTimersState(t, Timers.DELETE_TIMER);
                }
                break;
            case TimerObj.STATE_DONE:
                break;
@@ -850,6 +836,38 @@ public class TimerFragment extends DeskClockFragment
        }
    }

    private void animateTimerDeletion(final TimerObj t) {
        // Animate deletion, first alpha, then height
        ObjectAnimator a = ObjectAnimator.ofFloat(t.mView, View.ALPHA, 1f, 0f);
        a.setInterpolator(new AccelerateInterpolator());
        a.setDuration(100);
        a.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ObjectAnimator b = ObjectAnimator.ofInt(
                        t.mView, "animatedHeight", t.mView.getHeight(), 0);
                b.setInterpolator(new AccelerateInterpolator());
                b.setDuration(200);
                b.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mAdapter.deleteTimer(t.mTimerId);
                        if (mAdapter.getCount() == 0) {
                            if (mOnEmptyListListener == null) {
                                mTimerSetup.reset();
                                gotoSetupView();
                            } else {
                                mOnEmptyListListener.onEmptyList();
                            }
                        }
                    }
                });
                b.start();
            }
        });
        a.start();
    }

    private void onLabelPressed(TimerObj t) {
        final FragmentTransaction ft = getFragmentManager().beginTransaction();
        final Fragment prev = getFragmentManager().findFragmentByTag("label_dialog");
@@ -935,10 +953,13 @@ public class TimerFragment extends DeskClockFragment
        }
    }

    // Starts the ticks that animate the timers.
    private void startClockTicks() {
        mTimersList.postDelayed(mClockTick, 20);
        mTicking = true;
    }

    // Stops the ticks that animate the timers.
    private void stopClockTicks() {
        if (mTicking) {
            mTimersList.removeCallbacks(mClockTick);
@@ -976,6 +997,15 @@ public class TimerFragment extends DeskClockFragment
        mAdapter.onRestoreInstanceState(null);
    }

    // Process extras that were sent to the app and were intended for the timer
    // fragment
    public void processIntent(Intent intent) {
        // switch to timer setup view
        if (intent.getBooleanExtra(GOTO_SETUP_VIEW, false)) {
            gotoSetupView();
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (prefs.equals(mPrefs)) {
+16 −1
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ public class TimerObj implements Parcelable {
    public View mView;
    public int mState;
    public String mLabel;
    public boolean mDeleteAfterUse;

    public static final int STATE_RUNNING = 1;
    public static final int STATE_STOPPED = 2;
@@ -59,6 +60,7 @@ public class TimerObj implements Parcelable {
    private static final String PREF_SETUP_TIME = "timer_setup_timet_";
    private static final String PREF_STATE = "timer_state_";
    private static final String PREF_LABEL = "timer_label_";
    private static final String PREF_DELETE_AFTER_USE = "delete_after_use_";

    private static final String PREF_TIMERS_LIST = "timers_list";

@@ -94,6 +96,8 @@ public class TimerObj implements Parcelable {
        editor.putStringSet(PREF_TIMERS_LIST, timersList);
        key = PREF_LABEL + id;
        editor.putString(key, mLabel);
        key = PREF_DELETE_AFTER_USE + id;
        editor.putBoolean(key, mDeleteAfterUse);
        editor.apply();
    }

@@ -112,6 +116,8 @@ public class TimerObj implements Parcelable {
        mState = prefs.getInt(key, 0);
        key = PREF_LABEL + id;
        mLabel = prefs.getString(key, "");
        key = PREF_DELETE_AFTER_USE + id;
        mDeleteAfterUse = prefs.getBoolean(key, false);
    }

    public void deleteFromSharedPref(SharedPreferences prefs) {
@@ -125,6 +131,8 @@ public class TimerObj implements Parcelable {
        editor.remove (key);
        key = PREF_ORIGINAL_TIME + id;
        editor.remove (key);
        key = PREF_SETUP_TIME + id;
        editor.remove (key);
        key = PREF_STATE + id;
        editor.remove (key);
        Set <String> timersList = prefs.getStringSet(PREF_TIMERS_LIST, new HashSet<String>());
@@ -132,6 +140,8 @@ public class TimerObj implements Parcelable {
        editor.putStringSet(PREF_TIMERS_LIST, timersList);
        key = PREF_LABEL + id;
        editor.remove(key);
        key = PREF_DELETE_AFTER_USE + id;
        editor.remove(key);
        editor.commit();
        //dumpTimersFromSharedPrefs(prefs);
    }
@@ -164,13 +174,18 @@ public class TimerObj implements Parcelable {
    }

    public TimerObj() {
        init(0);
        this(0);
    }

    public TimerObj(long timerLength) {
      init(timerLength);
    }

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

    private void init (long length) {
        mTimerId = (int) Utils.getTimeNow();
        mStartTime = Utils.getTimeNow();