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 com.android.systemui.util.settings.GlobalSettings 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( 33 val context: Context, 34 val globalSettings: GlobalSettings, 35 ) { 36 var isInDemoMode = false 37 var isDemoModeAvailable = false 38 39 init { 40 isInDemoMode = checkIsDemoModeOn() 41 isDemoModeAvailable = checkIsDemoModeAllowed() 42 } 43 startTrackingnull44 fun startTracking() { 45 val resolver = context.contentResolver 46 resolver.registerContentObserver( 47 globalSettings.getUriFor(DEMO_MODE_ALLOWED), false, allowedObserver) 48 resolver.registerContentObserver( 49 globalSettings.getUriFor(DEMO_MODE_ON), false, onObserver) 50 } 51 stopTrackingnull52 fun stopTracking() { 53 val resolver = context.contentResolver 54 resolver.unregisterContentObserver(allowedObserver) 55 resolver.unregisterContentObserver(onObserver) 56 } 57 onDemoModeAvailabilityChangednull58 abstract fun onDemoModeAvailabilityChanged() 59 abstract fun onDemoModeStarted() 60 abstract fun onDemoModeFinished() 61 62 private fun checkIsDemoModeAllowed(): Boolean { 63 return globalSettings.getInt(DEMO_MODE_ALLOWED, 0) != 0 64 } 65 checkIsDemoModeOnnull66 private fun checkIsDemoModeOn(): Boolean { 67 return globalSettings.getInt(DEMO_MODE_ON, 0) != 0 68 } 69 70 private val allowedObserver = object : ContentObserver(Handler(Looper.getMainLooper())) { onChangenull71 override fun onChange(selfChange: Boolean) { 72 val allowed = checkIsDemoModeAllowed() 73 if (DEBUG) { 74 android.util.Log.d(TAG, "onChange: DEMO_MODE_ALLOWED changed: $allowed") 75 } 76 77 if (isDemoModeAvailable == allowed) { 78 return 79 } 80 81 isDemoModeAvailable = allowed 82 onDemoModeAvailabilityChanged() 83 } 84 } 85 86 private val onObserver = object : ContentObserver(Handler(Looper.getMainLooper())) { onChangenull87 override fun onChange(selfChange: Boolean) { 88 val on = checkIsDemoModeOn() 89 90 if (DEBUG) { 91 android.util.Log.d(TAG, "onChange: DEMO_MODE_ON changed: $on") 92 } 93 94 if (isInDemoMode == on) { 95 return 96 } 97 98 isInDemoMode = on 99 if (on) { 100 onDemoModeStarted() 101 } else { 102 onDemoModeFinished() 103 } 104 } 105 } 106 } 107 108 private const val TAG = "DemoModeAvailabilityTracker" 109 private const val DEMO_MODE_ALLOWED = "sysui_demo_allowed" 110 private const val DEMO_MODE_ON = "sysui_tuner_demo_on" 111 private const val DEBUG = false 112