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

Commit 8da3608e authored by James Lemieux's avatar James Lemieux Committed by Android (Google) Code Review
Browse files

Merge "Simplify HandleDeskClockApiCalls, TimerService and StopwatchService"...

Merge "Simplify HandleDeskClockApiCalls, TimerService and StopwatchService" into ub-deskclock-dazzle
parents feec1985 437da3b0
Loading
Loading
Loading
Loading
+24 −39
Original line number Diff line number Diff line
@@ -23,11 +23,8 @@ import android.os.Bundle;

import com.android.deskclock.data.City;
import com.android.deskclock.data.DataModel;
import com.android.deskclock.data.Stopwatch;
import com.android.deskclock.data.Timer;
import com.android.deskclock.events.Events;
import com.android.deskclock.stopwatch.StopwatchService;
import com.android.deskclock.timer.TimerService;
import com.android.deskclock.worldclock.CitySelectionActivity;

import java.util.List;
@@ -76,9 +73,8 @@ public class HandleDeskClockApiCalls extends Activity {
    public static final String EXTRA_TIMER_ID =
            "com.android.deskclock.extra.TIMER_ID";

    // extra for actions originating from the notifications
    public static final String EXTRA_FROM_NOTIFICATION =
            "com.android.deskclock.extra.FROM_NOTIFICATION";
    // Describes the entity responsible for the action being performed.
    public static final String EXTRA_EVENT_LABEL = "com.android.deskclock.extra.EVENT_LABEL";

    @Override
    protected void onCreate(Bundle icicle) {
@@ -123,39 +119,40 @@ public class HandleDeskClockApiCalls extends Activity {
        final String action = intent.getAction();

        // Determine where this intent originated.
        final boolean fromNotification =
                intent.getBooleanExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, false);
        final int label = fromNotification ? R.string.label_notification : R.string.label_intent;
        final int eventLabel = intent.getIntExtra(EXTRA_EVENT_LABEL, R.string.label_intent);

        if (ACTION_SHOW_STOPWATCH.equals(action)) {
            Events.sendStopwatchEvent(R.string.action_show, label);
            Events.sendStopwatchEvent(R.string.action_show, eventLabel);
        } else {
            final Stopwatch stopwatch = DataModel.getDataModel().getStopwatch();

            final String reason;
            boolean fail = false;
            switch (action) {
                case ACTION_START_STOPWATCH: {
                    Events.sendStopwatchEvent(R.string.action_start, label);
                    DataModel.getDataModel().startStopwatch();
                    Events.sendStopwatchEvent(R.string.action_start, eventLabel);
                    reason = getString(R.string.stopwatch_started);
                    break;
                }
                case ACTION_PAUSE_STOPWATCH: {
                    Events.sendStopwatchEvent(R.string.action_pause, label);
                    DataModel.getDataModel().pauseStopwatch();
                    Events.sendStopwatchEvent(R.string.action_pause, eventLabel);
                    reason = getString(R.string.stopwatch_paused);
                    break;
                }
                case ACTION_RESET_STOPWATCH: {
                    Events.sendStopwatchEvent(R.string.action_reset, label);
                    DataModel.getDataModel().clearLaps();
                    DataModel.getDataModel().resetStopwatch();
                    Events.sendStopwatchEvent(R.string.action_reset, eventLabel);
                    reason = getString(R.string.stopwatch_reset);
                    break;
                }
                case ACTION_LAP_STOPWATCH: {
                    if (!stopwatch.isRunning()) {
                    if (!DataModel.getDataModel().getStopwatch().isRunning()) {
                        fail = true;
                        reason = getString(R.string.stopwatch_isnt_running);
                    } else {
                        Events.sendStopwatchEvent(R.string.action_lap, label);
                        DataModel.getDataModel().addLap();
                        Events.sendStopwatchEvent(R.string.action_lap, eventLabel);
                        reason = getString(R.string.stopwatch_lapped);
                    }
                    break;
@@ -167,10 +164,6 @@ public class HandleDeskClockApiCalls extends Activity {
            if (fail) {
                Voice.notifyFailure(this, reason);
            } else {
                // Perform the action on the stopwatch.
                final Intent performActionIntent = new Intent(mAppContext, StopwatchService.class)
                        .setAction(action);
                startService(performActionIntent);
                Voice.notifySuccess(this, reason);
            }
            LogUtils.i(reason);
@@ -178,7 +171,6 @@ public class HandleDeskClockApiCalls extends Activity {

        // Open the UI to the stopwatch.
        final Intent stopwatchIntent = new Intent(mAppContext, DeskClock.class)
                .setAction(action)
                .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.STOPWATCH_TAB_INDEX);
        startActivity(stopwatchIntent);
    }
@@ -187,14 +179,12 @@ public class HandleDeskClockApiCalls extends Activity {
        final String action = intent.getAction();

        // Determine where this intent originated.
        final boolean fromNotification =
                intent.getBooleanExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, false);
        final int label = fromNotification ? R.string.label_notification : R.string.label_intent;
        int timerId = intent.getIntExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, -1);
        final int eventLabel = intent.getIntExtra(EXTRA_EVENT_LABEL, R.string.label_intent);
        int timerId = intent.getIntExtra(EXTRA_TIMER_ID, -1);
        Timer timer = null;

        if (ACTION_SHOW_TIMERS.equals(action)) {
            Events.sendTimerEvent(R.string.action_show, label);
            Events.sendTimerEvent(R.string.action_show, eventLabel);
        } else {
            String reason = null;
            if (timerId == -1) {
@@ -223,22 +213,23 @@ public class HandleDeskClockApiCalls extends Activity {
                // Otherwise the control command can be honored.
                switch (action) {
                    case ACTION_RESET_TIMER: {
                        if (timer.getDeleteAfterUse()) {
                            Events.sendTimerEvent(R.string.action_delete, label);
                        DataModel.getDataModel().resetOrDeleteTimer(timer, eventLabel);
                        if (timer.isExpired() && timer.getDeleteAfterUse()) {
                            reason = getString(R.string.timer_deleted);
                        } else {
                            Events.sendTimerEvent(R.string.action_reset, label);
                            reason = getString(R.string.timer_was_reset);
                        }
                        break;
                    }
                    case ACTION_START_TIMER: {
                        Events.sendTimerEvent(R.string.action_start, label);
                        DataModel.getDataModel().startTimer(timer);
                        Events.sendTimerEvent(R.string.action_start, eventLabel);
                        reason = getString(R.string.timer_started);
                        break;
                    }
                    case ACTION_PAUSE_TIMER: {
                        Events.sendTimerEvent(R.string.action_pause, label);
                        DataModel.getDataModel().pauseTimer(timer);
                        Events.sendTimerEvent(R.string.action_pause, eventLabel);
                        reason = getString(R.string.timer_paused);
                        break;
                    }
@@ -247,11 +238,6 @@ public class HandleDeskClockApiCalls extends Activity {
                }

                timerId = timer.getId();
                // Perform the action on the timer.
                final Intent performActionIntent = new Intent(mAppContext, TimerService.class)
                        .setAction(action)
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timerId);
                startService(performActionIntent);
                Voice.notifySuccess(this, reason);
            }

@@ -260,9 +246,8 @@ public class HandleDeskClockApiCalls extends Activity {

        // Open the UI to the timers.
        final Intent timerIntent = new Intent(mAppContext, DeskClock.class)
                .setAction(action)
                .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX)
                .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timerId);
                .putExtra(EXTRA_TIMER_ID, timerId);
        startActivity(timerIntent);
    }

+3 −3
Original line number Diff line number Diff line
@@ -285,12 +285,12 @@ public final class DataModel {
    /**
     * @param length the length of the timer in milliseconds
     * @param label describes the purpose of the timer
     * @param deleteAfterUser {@code true} indicates the timer should be deleted when it is reset
     * @param deleteAfterUse {@code true} indicates the timer should be deleted when it is reset
     * @return the newly added timer
     */
    public Timer addTimer(long length, String label, boolean deleteAfterUser) {
    public Timer addTimer(long length, String label, boolean deleteAfterUse) {
        enforceMainLooper();
        return mTimerModel.addTimer(length, label, deleteAfterUser);
        return mTimerModel.addTimer(length, label, deleteAfterUse);
    }

    /**
+12 −10
Original line number Diff line number Diff line
@@ -24,11 +24,12 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.SystemClock;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.widget.RemoteViews;

import com.android.deskclock.DeskClock;
import com.android.deskclock.HandleDeskClockApiCalls;
import com.android.deskclock.R;
import com.android.deskclock.stopwatch.StopwatchService;
@@ -203,12 +204,13 @@ final class StopwatchModel {
            return;
        }

        @StringRes final int eventLabel = R.string.label_notification;

        // Intent to load the app when the notification is tapped.
        final Intent showApp = new Intent(mContext, HandleDeskClockApiCalls.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .setAction(HandleDeskClockApiCalls.ACTION_SHOW_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true)
                .putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.STOPWATCH_TAB_INDEX);
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final PendingIntent pendingShowApp = PendingIntent.getActivity(mContext, 0, showApp,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
@@ -229,15 +231,15 @@ final class StopwatchModel {
        expanded.setOnClickPendingIntent(R.id.swn_expanded_hitspace, pendingShowApp);
        expanded.setImageViewResource(R.id.notification_icon, R.drawable.stat_notify_stopwatch);

        final int leftButtonId = R.id.swn_left_button;
        final int rightButtonId = R.id.swn_right_button;
        @IdRes final int leftButtonId = R.id.swn_left_button;
        @IdRes final int rightButtonId = R.id.swn_right_button;
        if (running) {
            // Left button: Pause
            expanded.setTextViewText(leftButtonId, res.getText(R.string.sw_pause_button));
            setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_pause_24dp);
            final Intent pause = new Intent(mContext, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
            expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(pause));

            // Right button: Add Lap
@@ -247,7 +249,7 @@ final class StopwatchModel {

                final Intent lap = new Intent(mContext, StopwatchService.class)
                        .setAction(HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH)
                        .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                        .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
                expanded.setOnClickPendingIntent(rightButtonId, pendingServiceIntent(lap));
                expanded.setViewVisibility(rightButtonId, VISIBLE);
            } else {
@@ -273,7 +275,7 @@ final class StopwatchModel {
            setTextViewDrawable(expanded, leftButtonId, R.drawable.ic_start_24dp);
            final Intent start = new Intent(mContext, StopwatchService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_START_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
            expanded.setOnClickPendingIntent(leftButtonId, pendingServiceIntent(start));

            // Right button: Reset (HandleDeskClockApiCalls will also bring forward the app)
@@ -282,7 +284,7 @@ final class StopwatchModel {
            setTextViewDrawable(expanded, rightButtonId, R.drawable.ic_reset_24dp);
            final Intent reset = new Intent(mContext, HandleDeskClockApiCalls.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                    .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);
            expanded.setOnClickPendingIntent(rightButtonId, pendingActivityIntent(reset));

            // Indicate the stopwatch is paused.
@@ -295,7 +297,7 @@ final class StopwatchModel {
        // Swipe away will reset the stopwatch without bringing forward the app.
        final Intent reset = new Intent(mContext, StopwatchService.class)
                .setAction(HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH)
                .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, eventLabel);

        final Notification notification = new NotificationCompat.Builder(mContext)
                .setLocalOnly(true)
+12 −19
Original line number Diff line number Diff line
@@ -180,13 +180,13 @@ final class TimerModel {
    /**
     * @param length the length of the timer in milliseconds
     * @param label describes the purpose of the timer
     * @param deleteAfterUser {@code true} indicates the timer should be deleted when it is reset
     * @param deleteAfterUse {@code true} indicates the timer should be deleted when it is reset
     * @return the newly added timer
     */
    Timer addTimer(long length, String label, boolean deleteAfterUser) {
    Timer addTimer(long length, String label, boolean deleteAfterUse) {
        // Create the timer instance.
        Timer timer = new Timer(-1, RESET, length, length, Long.MIN_VALUE, length, label,
                deleteAfterUser);
                deleteAfterUse);

        // Add the timer to permanent storage.
        timer = TimerDAO.addTimer(mContext, timer);
@@ -606,15 +606,13 @@ final class TimerModel {
                firstActionTitleId = R.string.timer_pause;
                firstActionIntent = new Intent(mContext, TimerService.class)
                        .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER)
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
                        .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

                secondActionIconId = R.drawable.ic_add_24dp;
                secondActionTitleId = R.string.timer_plus_1_min;
                secondActionIntent = new Intent(mContext, TimerService.class)
                        .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER)
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
                        .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
            } else {
                // Single timer is paused.
                contentTitle = mContext.getString(R.string.timer_paused);
@@ -623,15 +621,13 @@ final class TimerModel {
                firstActionTitleId = R.string.sw_resume_button;
                firstActionIntent = new Intent(mContext, TimerService.class)
                        .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER)
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
                        .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

                secondActionIconId = R.drawable.ic_reset_24dp;
                secondActionTitleId = R.string.sw_reset_button;
                secondActionIntent = new Intent(mContext, TimerService.class)
                        .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER)
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
                        .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
                        .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
            }
        } else {
            if (timer.isRunning()) {
@@ -647,16 +643,15 @@ final class TimerModel {

            firstActionIconId = R.drawable.ic_reset_24dp;
            firstActionTitleId = R.string.timer_reset_all;
            firstActionIntent = TimerService.createResetUnexpiredTimersIntent(mContext)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
            firstActionIntent = TimerService.createResetUnexpiredTimersIntent(mContext);
        }

        // Intent to load the app and show the timer when the notification is tapped.
        final Intent showApp = new Intent(mContext, HandleDeskClockApiCalls.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS)
                .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true)
                .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
                .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
                .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, R.string.label_notification);

        final PendingIntent pendingShowApp = PendingIntent.getActivity(mContext, 0, showApp,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
@@ -767,8 +762,7 @@ final class TimerModel {
                PendingIntent.FLAG_UPDATE_CURRENT);

        // First action intent is either reset single timer or reset all timers.
        final Intent reset = TimerService.createResetExpiredTimersIntent(mContext)
                .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
        final Intent reset = TimerService.createResetExpiredTimersIntent(mContext);
        final PendingIntent pendingReset = PendingIntent.getService(mContext, 0, reset,
                PendingIntent.FLAG_UPDATE_CURRENT);

@@ -789,8 +783,7 @@ final class TimerModel {
        // Add a second action if only a single timer is expired.
        if (expired.size() == 1) {
            // Second action intent adds a minute to a single timer.
            final Intent addMinute = TimerService.createAddMinuteTimerIntent(mContext, timerId)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, true);
            final Intent addMinute = TimerService.createAddMinuteTimerIntent(mContext, timerId);
            final PendingIntent pendingAddMinute = PendingIntent.getService(mContext, 0, addMinute,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            final String addMinuteTitle = mContext.getString(R.string.timer_plus_1_min);
+6 −17
Original line number Diff line number Diff line
@@ -26,11 +26,11 @@ import com.android.deskclock.data.DataModel;
import com.android.deskclock.events.Events;

/**
 * This service exists primarily to allow the stopwatch notification to alter the state of the
 * This service exists solely to allow the stopwatch notification to alter the state of the
 * stopwatch without disturbing the notification shade. If an activity were used instead (even one
 * that is not displayed) the notification manager implicitly closes the notification shade which
 * clashes with the use case of starting/pausing/lapping/resetting the stopwatch without
 * interruption.
 * disturbing the notification shade.
 */
public final class StopwatchService extends Service {

@@ -41,37 +41,26 @@ public final class StopwatchService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final boolean fromNotification =
                intent.getBooleanExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, false);

        switch (intent.getAction()) {
            case HandleDeskClockApiCalls.ACTION_START_STOPWATCH: {
                DataModel.getDataModel().startStopwatch();
                if (fromNotification) {
                Events.sendStopwatchEvent(R.string.action_start, R.string.label_notification);
                }
                break;
            }
            case HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH: {
                DataModel.getDataModel().pauseStopwatch();
                if (fromNotification) {
                Events.sendStopwatchEvent(R.string.action_pause, R.string.label_notification);
                }
                break;
            }
            case HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH: {
                DataModel.getDataModel().addLap();
                if (fromNotification) {
                Events.sendStopwatchEvent(R.string.action_lap, R.string.label_notification);
                }
                break;
            }
            case HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH: {
                DataModel.getDataModel().clearLaps();
                DataModel.getDataModel().resetStopwatch();
                if (fromNotification) {
                Events.sendStopwatchEvent(R.string.action_reset, R.string.label_notification);
                }
                break;
            }
        }
Loading