• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.systemui.demomode
18 
19 import android.content.Context
20 import android.database.ContentObserver
21 import android.os.Handler
22 import android.os.Looper
23 import android.provider.Settings
24 
25 /**
26  * Class to track the availability of [DemoMode]. Use this class to track the availability and
27  * on/off state for DemoMode
28  *
29  * This class works by wrapping a content observer for the relevant keys related to DemoMode
30  * availability and current on/off state, and triggering callbacks.
31  */
32 abstract class DemoModeAvailabilityTracker(val context: Context) {
33     var isInDemoMode = false
34     var isDemoModeAvailable = false
35 
36     init {
37         isInDemoMode = checkIsDemoModeOn()
38         isDemoModeAvailable = checkIsDemoModeAllowed()
39     }
40 
startTrackingnull41     fun startTracking() {
42         val resolver = context.contentResolver
43         resolver.registerContentObserver(
44                 Settings.Global.getUriFor(DEMO_MODE_ALLOWED), false, allowedObserver)
45         resolver.registerContentObserver(
46                 Settings.Global.getUriFor(DEMO_MODE_ON), false, onObserver)
47     }
48 
stopTrackingnull49     fun stopTracking() {
50         val resolver = context.contentResolver
51         resolver.unregisterContentObserver(allowedObserver)
52         resolver.unregisterContentObserver(onObserver)
53     }
54 
onDemoModeAvailabilityChangednull55     abstract fun onDemoModeAvailabilityChanged()
56     abstract fun onDemoModeStarted()
57     abstract fun onDemoModeFinished()
58 
59     private fun checkIsDemoModeAllowed(): Boolean {
60         return Settings.Global
61                 .getInt(context.contentResolver, DEMO_MODE_ALLOWED, 0) != 0
62     }
63 
checkIsDemoModeOnnull64     private fun checkIsDemoModeOn(): Boolean {
65         return Settings.Global.getInt(context.contentResolver, DEMO_MODE_ON, 0) != 0
66     }
67 
68     private val allowedObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
onChangenull69         override fun onChange(selfChange: Boolean) {
70             val allowed = checkIsDemoModeAllowed()
71             if (DEBUG) {
72                 android.util.Log.d(TAG, "onChange: DEMO_MODE_ALLOWED changed: $allowed")
73             }
74 
75             if (isDemoModeAvailable == allowed) {
76                 return
77             }
78 
79             isDemoModeAvailable = allowed
80             onDemoModeAvailabilityChanged()
81         }
82     }
83 
84     private val onObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
onChangenull85         override fun onChange(selfChange: Boolean) {
86             val on = checkIsDemoModeOn()
87 
88             if (DEBUG) {
89                 android.util.Log.d(TAG, "onChange: DEMO_MODE_ON changed: $on")
90             }
91 
92             if (isInDemoMode == on) {
93                 return
94             }
95 
96             isInDemoMode = on
97             if (on) {
98                 onDemoModeStarted()
99             } else {
100                 onDemoModeFinished()
101             }
102         }
103     }
104 }
105 
106 private const val TAG = "DemoModeAvailabilityTracker"
107 private const val DEMO_MODE_ALLOWED = "sysui_demo_allowed"
108 private const val DEMO_MODE_ON = "sysui_tuner_demo_on"
109 private const val DEBUG = false
110