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
18 
19 import android.app.Activity
20 import android.content.Intent
21 import android.os.Bundle
22 
23 import com.android.deskclock.events.Events
24 import com.android.deskclock.stopwatch.StopwatchService
25 import com.android.deskclock.uidata.UiDataModel
26 
27 class HandleShortcuts : Activity() {
28     override fun onCreate(savedInstanceState: Bundle?) {
29         super.onCreate(savedInstanceState)
30 
31         val intent = intent
32 
33         try {
34             when (val action = intent.action) {
35                 StopwatchService.ACTION_PAUSE_STOPWATCH -> {
36                     Events.sendStopwatchEvent(R.string.action_pause, R.string.label_shortcut)
37 
38                     // Open DeskClock positioned on the stopwatch tab.
39                     UiDataModel.uiDataModel.selectedTab = UiDataModel.Tab.STOPWATCH
40                     startActivity(Intent(this, DeskClock::class.java)
41                             .setAction(StopwatchService.ACTION_PAUSE_STOPWATCH))
42                     setResult(RESULT_OK)
43                 }
44                 StopwatchService.ACTION_START_STOPWATCH -> {
45                     Events.sendStopwatchEvent(R.string.action_start, R.string.label_shortcut)
46 
47                     // Open DeskClock positioned on the stopwatch tab.
48                     UiDataModel.uiDataModel.selectedTab = UiDataModel.Tab.STOPWATCH
49                     startActivity(Intent(this, DeskClock::class.java)
50                             .setAction(StopwatchService.ACTION_START_STOPWATCH))
51                     setResult(RESULT_OK)
52                 }
53                 else -> throw IllegalArgumentException("Unsupported action: $action")
54             }
55         } catch (e: Exception) {
56             LOGGER.e("Error handling intent: $intent", e)
57             setResult(RESULT_CANCELED)
58         } finally {
59             finish()
60         }
61     }
62 
63     companion object {
64         private val LOGGER = LogUtils.Logger("HandleShortcuts")
65     }
66 }