1 /* 2 * Copyright (C) 2020 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 /** 30 * This service exists solely to allow the stopwatch notification to alter the state of the 31 * stopwatch without disturbing the notification shade. If an activity were used instead (even one 32 * that is not displayed) the notification manager implicitly closes the notification shade which 33 * clashes with the use case of starting/pausing/lapping/resetting the stopwatch without 34 * disturbing the notification shade. 35 */ 36 class StopwatchService : Service() { 37 onBindnull38 override fun onBind(intent: Intent?): IBinder? = null 39 40 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 41 val action: String? = intent.getAction() 42 val label: Int = intent.getIntExtra(Events.EXTRA_EVENT_LABEL, R.string.label_intent) 43 when (action) { 44 ACTION_SHOW_STOPWATCH -> { 45 Events.sendStopwatchEvent(R.string.action_show, label) 46 47 // Open DeskClock positioned on the stopwatch tab. 48 UiDataModel.uiDataModel.selectedTab = UiDataModel.Tab.STOPWATCH 49 val showStopwatch: Intent = Intent(this, DeskClock::class.java) 50 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 51 startActivity(showStopwatch) 52 } 53 ACTION_START_STOPWATCH -> { 54 Events.sendStopwatchEvent(R.string.action_start, label) 55 DataModel.dataModel.startStopwatch() 56 } 57 ACTION_PAUSE_STOPWATCH -> { 58 Events.sendStopwatchEvent(R.string.action_pause, label) 59 DataModel.dataModel.pauseStopwatch() 60 } 61 ACTION_RESET_STOPWATCH -> { 62 Events.sendStopwatchEvent(R.string.action_reset, label) 63 DataModel.dataModel.resetStopwatch() 64 } 65 ACTION_LAP_STOPWATCH -> { 66 Events.sendStopwatchEvent(R.string.action_lap, label) 67 DataModel.dataModel.addLap() 68 } 69 } 70 71 return START_NOT_STICKY 72 } 73 74 companion object { 75 private const val ACTION_PREFIX = "com.android.deskclock.action." 76 77 // shows the tab with the stopwatch 78 const val ACTION_SHOW_STOPWATCH = ACTION_PREFIX + "SHOW_STOPWATCH" 79 80 // starts the current stopwatch 81 const val ACTION_START_STOPWATCH = ACTION_PREFIX + "START_STOPWATCH" 82 83 // pauses the current stopwatch that's currently running 84 const val ACTION_PAUSE_STOPWATCH = ACTION_PREFIX + "PAUSE_STOPWATCH" 85 86 // laps the stopwatch that's currently running 87 const val ACTION_LAP_STOPWATCH = ACTION_PREFIX + "LAP_STOPWATCH" 88 89 // resets the stopwatch if it's stopped 90 const val ACTION_RESET_STOPWATCH = ACTION_PREFIX + "RESET_STOPWATCH" 91 } 92 }