1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.deskclock.stopwatch; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.IBinder; 22 23 import com.android.deskclock.DeskClock; 24 import com.android.deskclock.R; 25 import com.android.deskclock.data.DataModel; 26 import com.android.deskclock.events.Events; 27 import com.android.deskclock.uidata.UiDataModel; 28 29 import static com.android.deskclock.uidata.UiDataModel.Tab.STOPWATCH; 30 31 /** 32 * This service exists solely to allow the stopwatch notification to alter the state of the 33 * stopwatch without disturbing the notification shade. If an activity were used instead (even one 34 * that is not displayed) the notification manager implicitly closes the notification shade which 35 * clashes with the use case of starting/pausing/lapping/resetting the stopwatch without 36 * disturbing the notification shade. 37 */ 38 public final class StopwatchService extends Service { 39 40 private static final String ACTION_PREFIX = "com.android.deskclock.action."; 41 42 // shows the tab with the stopwatch 43 public static final String ACTION_SHOW_STOPWATCH = ACTION_PREFIX + "SHOW_STOPWATCH"; 44 // starts the current stopwatch 45 public static final String ACTION_START_STOPWATCH = ACTION_PREFIX + "START_STOPWATCH"; 46 // pauses the current stopwatch that's currently running 47 public static final String ACTION_PAUSE_STOPWATCH = ACTION_PREFIX + "PAUSE_STOPWATCH"; 48 // laps the stopwatch that's currently running 49 public static final String ACTION_LAP_STOPWATCH = ACTION_PREFIX + "LAP_STOPWATCH"; 50 // resets the stopwatch if it's stopped 51 public static final String ACTION_RESET_STOPWATCH = ACTION_PREFIX + "RESET_STOPWATCH"; 52 53 @Override onBind(Intent intent)54 public IBinder onBind(Intent intent) { 55 return null; 56 } 57 58 @Override onStartCommand(Intent intent, int flags, int startId)59 public int onStartCommand(Intent intent, int flags, int startId) { 60 final String action = intent.getAction(); 61 final int label = intent.getIntExtra(Events.EXTRA_EVENT_LABEL, R.string.label_intent); 62 switch (action) { 63 case ACTION_SHOW_STOPWATCH: { 64 Events.sendStopwatchEvent(R.string.action_show, label); 65 66 // Open DeskClock positioned on the stopwatch tab. 67 UiDataModel.getUiDataModel().setSelectedTab(STOPWATCH); 68 final Intent showStopwatch = new Intent(this, DeskClock.class) 69 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 70 startActivity(showStopwatch); 71 break; 72 } 73 case ACTION_START_STOPWATCH: { 74 Events.sendStopwatchEvent(R.string.action_start, label); 75 DataModel.getDataModel().startStopwatch(); 76 break; 77 } 78 case ACTION_PAUSE_STOPWATCH: { 79 Events.sendStopwatchEvent(R.string.action_pause, label); 80 DataModel.getDataModel().pauseStopwatch(); 81 break; 82 } 83 case ACTION_RESET_STOPWATCH: { 84 Events.sendStopwatchEvent(R.string.action_reset, label); 85 DataModel.getDataModel().resetStopwatch(); 86 break; 87 } 88 case ACTION_LAP_STOPWATCH: { 89 Events.sendStopwatchEvent(R.string.action_lap, label); 90 DataModel.getDataModel().addLap(); 91 break; 92 } 93 } 94 95 return START_NOT_STICKY; 96 } 97 } 98