1 /*
2  * Copyright (C) 2016 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 package com.android.systemui.statusbar.policy
17 
18 import android.app.StatusBarManager
19 import android.content.Context
20 import android.content.res.Configuration
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.statusbar.CommandQueue
23 import com.android.systemui.util.Utils
24 import javax.inject.Inject
25 
26 /**
27  * Controls whether the disable flag [StatusBarManager.DISABLE2_QUICK_SETTINGS] should be set.
28  * This would happen when a [RemoteInputView] is active, the device is in landscape and not using
29  * split shade.
30  */
31 @SysUISingleton
32 class RemoteInputQuickSettingsDisabler @Inject constructor(
33     private val context: Context,
34     private val commandQueue: CommandQueue,
35     configController: ConfigurationController
36 ) : ConfigurationController.ConfigurationListener {
37 
38     private var remoteInputActive = false
39     private var isLandscape: Boolean
40     private var shouldUseSplitNotificationShade: Boolean
41 
42     init {
43         isLandscape =
44             context.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
45         shouldUseSplitNotificationShade = Utils.shouldUseSplitNotificationShade(context.resources)
46         configController.addCallback(this)
47     }
48 
49     fun adjustDisableFlags(state: Int): Int {
50         var mutableState = state
51         if (remoteInputActive &&
52             isLandscape &&
53             !shouldUseSplitNotificationShade
54         ) {
55             mutableState = state or StatusBarManager.DISABLE2_QUICK_SETTINGS
56         }
57         return mutableState
58     }
59 
60     fun setRemoteInputActive(active: Boolean) {
61         if (remoteInputActive != active) {
62             remoteInputActive = active
63             recomputeDisableFlags()
64         }
65     }
66 
67     override fun onConfigChanged(newConfig: Configuration) {
68         var needToRecompute = false
69 
70         val newIsLandscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
71         if (newIsLandscape != isLandscape) {
72             isLandscape = newIsLandscape
73             needToRecompute = true
74         }
75 
76         val newSplitShadeFlag = Utils.shouldUseSplitNotificationShade(context.resources)
77         if (newSplitShadeFlag != shouldUseSplitNotificationShade) {
78             shouldUseSplitNotificationShade = newSplitShadeFlag
79             needToRecompute = true
80         }
81         if (needToRecompute) {
82             recomputeDisableFlags()
83         }
84     }
85 
86     /**
87      * Called in order to trigger a refresh of the disable flags after a relevant configuration
88      * change or when a [RemoteInputView] has changed its active state. The method
89      * [adjustDisableFlags] will be invoked to modify the disable flags according to
90      * [remoteInputActive], [isLandscape] and [shouldUseSplitNotificationShade].
91      */
92     private fun recomputeDisableFlags() {
93         commandQueue.recomputeDisableFlags(context.displayId, true)
94     }
95 }