• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.settings.display
18 
19 import android.content.Context
20 import android.hardware.devicestate.DeviceStateManager
21 import android.os.Build
22 import android.os.Handler
23 import android.os.Looper
24 import com.android.internal.annotations.VisibleForTesting
25 import com.android.settingslib.devicestate.AndroidSecureSettings
26 import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager
27 import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManagerProvider.createInstance
28 import com.android.settingslib.devicestate.PosturesHelper
29 import com.android.settingslib.utils.ThreadUtils
30 import com.android.window.flags.Flags
31 
32 /**
33  * Provides appropriate instance of [DeviceStateAutoRotateSettingManager], based on the value of
34  * [Flags.FLAG_ENABLE_DEVICE_STATE_AUTO_ROTATE_SETTING_REFACTOR].
35  */
36 object DeviceStateAutoRotateSettingManagerProvider {
37     private var nullableSingletonSettingManager: DeviceStateAutoRotateSettingManager? = null
38 
39     /**
40      * Provides a singleton instance of [DeviceStateAutoRotateSettingManager], based on the
41      * value of[Flags.FLAG_ENABLE_DEVICE_STATE_AUTO_ROTATE_SETTING_REFACTOR]. It is supposed to
42      * be used by apps that don't support dagger to provide and manager instance.
43      */
44     @JvmStatic
getSingletonInstancenull45     fun getSingletonInstance(context: Context) =
46         nullableSingletonSettingManager ?: createInstance(
47             context,
48             ThreadUtils.getBackgroundExecutor(),
49             AndroidSecureSettings(context.contentResolver),
50             Handler(Looper.getMainLooper()),
51             PosturesHelper(context, context.getSystemService(DeviceStateManager::class.java))
52         ).also {
53             nullableSingletonSettingManager = it
54         }
55 
56     /** Resets the singleton instance of [DeviceStateAutoRotateSettingManager]. */
57     @JvmStatic
58     @VisibleForTesting
resetInstancenull59     fun resetInstance() {
60         nullableSingletonSettingManager = null
61     }
62 }
63