• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 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.systemui.keyboard.shortcut.ui
18 
19 import android.app.Dialog
20 import android.content.ActivityNotFoundException
21 import android.content.Intent
22 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
23 import android.os.UserHandle
24 import android.provider.Settings
25 import androidx.annotation.VisibleForTesting
26 import androidx.compose.foundation.layout.width
27 import androidx.compose.runtime.getValue
28 import androidx.compose.ui.Modifier
29 import androidx.compose.ui.res.stringResource
30 import androidx.lifecycle.compose.collectAsStateWithLifecycle
31 import com.android.systemui.CoreStartable
32 import com.android.systemui.dagger.SysUISingleton
33 import com.android.systemui.dagger.qualifiers.Application
34 import com.android.systemui.keyboard.shortcut.ui.composable.ShortcutHelper
35 import com.android.systemui.keyboard.shortcut.ui.composable.ShortcutHelperBottomSheet
36 import com.android.systemui.keyboard.shortcut.ui.composable.getWidth
37 import com.android.systemui.keyboard.shortcut.ui.viewmodel.ShortcutHelperViewModel
38 import com.android.systemui.lifecycle.rememberActivated
39 import com.android.systemui.plugins.ActivityStarter
40 import com.android.systemui.res.R
41 import com.android.systemui.statusbar.phone.SystemUIDialogFactory
42 import com.android.systemui.statusbar.phone.createBottomSheet
43 import javax.inject.Inject
44 import kotlinx.coroutines.CoroutineScope
45 import kotlinx.coroutines.flow.launchIn
46 import kotlinx.coroutines.flow.map
47 
48 @SysUISingleton
49 class ShortcutHelperDialogStarter
50 @Inject
51 constructor(
52     @Application private val applicationScope: CoroutineScope,
53     private val shortcutHelperViewModel: ShortcutHelperViewModel,
54     private val shortcutCustomizationDialogStarterFactory:
55         ShortcutCustomizationDialogStarter.Factory,
56     private val dialogFactory: SystemUIDialogFactory,
57     private val activityStarter: ActivityStarter,
58 ) : CoreStartable {
59 
60     @VisibleForTesting var dialog: Dialog? = null
61 
62     override fun start() {
63         shortcutHelperViewModel.shouldShow
64             .map { shouldShow ->
65                 if (shouldShow) {
66                     dialog = createShortcutHelperDialog().also { it.show() }
67                 } else {
68                     dialog?.dismiss()
69                 }
70             }
71             .launchIn(applicationScope)
72     }
73 
74     private fun createShortcutHelperDialog(): Dialog {
75         return dialogFactory.createBottomSheet(
76             content = { dialog ->
77                 val shortcutsUiState by
78                     shortcutHelperViewModel.shortcutsUiState.collectAsStateWithLifecycle()
79                 val shortcutCustomizationDialogStarter =
80                     rememberActivated(traceName = "shortcutCustomizationDialogStarter") {
81                         shortcutCustomizationDialogStarterFactory.create()
82                     }
83                 ShortcutHelper(
84                     modifier = Modifier.width(getWidth()),
85                     shortcutsUiState = shortcutsUiState,
86                     onKeyboardSettingsClicked = { onKeyboardSettingsClicked(dialog) },
87                     onSearchQueryChanged = { shortcutHelperViewModel.onSearchQueryChanged(it) },
88                     onShortcutCustomizationRequested = {
89                         shortcutCustomizationDialogStarter.onShortcutCustomizationRequested(it)
90                     },
91                     onCustomizationModeToggled = { isCustomizing ->
92                         shortcutHelperViewModel.toggleCustomizationMode(isCustomizing)
93                     },
94                 )
95                 dialog.setOnDismissListener { shortcutHelperViewModel.onViewClosed() }
96                 dialog.setTitle(stringResource(R.string.shortcut_helper_title))
97             },
98             maxWidth = ShortcutHelperBottomSheet.LargeScreenWidthLandscape,
99         )
100     }
101 
102     private fun onKeyboardSettingsClicked(dialog: Dialog) {
103         try {
104             activityStarter.startActivity(
105                 Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS).addFlags(FLAG_ACTIVITY_NEW_TASK),
106                 /* dismissShade= */ true,
107                 /* animationController = */ null,
108                 /* showOverLockscreenWhenLocked = */ false,
109                 UserHandle.CURRENT,
110             )
111         } catch (e: ActivityNotFoundException) {
112             // From the Settings docs: In some cases, a matching Activity may not exist, so ensure
113             // you safeguard against this.
114             e.printStackTrace()
115             return
116         }
117         dialog.dismiss()
118     }
119 }
120