• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2023 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.qs.tiles.impl.saver.domain.interactor
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.provider.Settings
22 import com.android.internal.jank.InteractionJankMonitor
23 import com.android.systemui.animation.DialogCuj
24 import com.android.systemui.animation.DialogTransitionAnimator
25 import com.android.systemui.dagger.qualifiers.Background
26 import com.android.systemui.dagger.qualifiers.Main
27 import com.android.systemui.qs.tiles.base.domain.actions.QSTileIntentUserInputHandler
28 import com.android.systemui.qs.tiles.base.domain.interactor.QSTileUserActionInteractor
29 import com.android.systemui.qs.tiles.base.domain.model.QSTileInput
30 import com.android.systemui.qs.tiles.base.shared.model.QSTileUserAction
31 import com.android.systemui.qs.tiles.impl.saver.domain.DataSaverDialogDelegate
32 import com.android.systemui.qs.tiles.impl.saver.domain.model.DataSaverTileModel
33 import com.android.systemui.settings.UserFileManager
34 import com.android.systemui.shade.ShadeDisplayAware
35 import com.android.systemui.shade.domain.interactor.ShadeDialogContextInteractor
36 import com.android.systemui.statusbar.phone.SystemUIDialog
37 import com.android.systemui.statusbar.policy.DataSaverController
38 import javax.inject.Inject
39 import kotlin.coroutines.CoroutineContext
40 import kotlinx.coroutines.withContext
41 
42 /** Handles data saver tile clicks. */
43 class DataSaverTileUserActionInteractor
44 @Inject
45 constructor(
46     @ShadeDisplayAware private val context: Context,
47     private val contextInteractor: ShadeDialogContextInteractor,
48     @Main private val coroutineContext: CoroutineContext,
49     @Background private val backgroundContext: CoroutineContext,
50     private val dataSaverController: DataSaverController,
51     private val qsTileIntentUserActionHandler: QSTileIntentUserInputHandler,
52     private val dialogTransitionAnimator: DialogTransitionAnimator,
53     private val systemUIDialogFactory: SystemUIDialog.Factory,
54     userFileManager: UserFileManager,
55 ) : QSTileUserActionInteractor<DataSaverTileModel> {
56     companion object {
57         private const val INTERACTION_JANK_TAG = "start_data_saver"
58         const val PREFS = "data_saver"
59         const val DIALOG_SHOWN = "data_saver_dialog_shown"
60     }
61 
62     val sharedPreferences =
63         userFileManager.getSharedPreferences(PREFS, Context.MODE_PRIVATE, context.userId)
64 
65     override suspend fun handleInput(input: QSTileInput<DataSaverTileModel>): Unit =
66         with(input) {
67             when (action) {
68                 is QSTileUserAction.Click -> {
69                     val wasEnabled: Boolean = data.isEnabled
70                     if (wasEnabled || sharedPreferences.getBoolean(DIALOG_SHOWN, false)) {
71                         withContext(backgroundContext) {
72                             dataSaverController.setDataSaverEnabled(!wasEnabled)
73                         }
74                         return@with
75                     }
76                     // Show a dialog to confirm first. Dialogs shown by the DialogTransitionAnimator
77                     // must be created and shown on the main thread, so we post it to the UI
78                     // handler
79                     withContext(coroutineContext) {
80                         val dialogDelegate =
81                             DataSaverDialogDelegate(
82                                 systemUIDialogFactory,
83                                 contextInteractor,
84                                 backgroundContext,
85                                 dataSaverController,
86                                 sharedPreferences,
87                             )
88                         val dialog =
89                             systemUIDialogFactory.create(dialogDelegate, contextInteractor.context)
90 
91                         action.expandable
92                             ?.dialogTransitionController(
93                                 DialogCuj(
94                                     InteractionJankMonitor.CUJ_SHADE_DIALOG_OPEN,
95                                     INTERACTION_JANK_TAG,
96                                 )
97                             )
98                             ?.let { controller ->
99                                 dialogTransitionAnimator.show(dialog, controller)
100                             } ?: dialog.show()
101                     }
102                 }
103                 is QSTileUserAction.LongClick -> {
104                     qsTileIntentUserActionHandler.handle(
105                         action.expandable,
106                         Intent(Settings.ACTION_DATA_SAVER_SETTINGS),
107                     )
108                 }
109                 is QSTileUserAction.ToggleClick -> {}
110             }
111         }
112 }
113