• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 2022 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 
18 package com.android.systemui.keyguard.data.quickaffordance
19 
20 import android.app.StatusBarManager
21 import android.app.admin.DevicePolicyManager
22 import android.content.Context
23 import android.content.pm.PackageManager
24 import com.android.systemui.R
25 import com.android.systemui.animation.Expandable
26 import com.android.systemui.camera.CameraGestureHelper
27 import com.android.systemui.common.shared.model.ContentDescription
28 import com.android.systemui.common.shared.model.Icon
29 import com.android.systemui.dagger.SysUISingleton
30 import com.android.systemui.dagger.qualifiers.Application
31 import com.android.systemui.dagger.qualifiers.Background
32 import com.android.systemui.settings.UserTracker
33 import dagger.Lazy
34 import javax.inject.Inject
35 import kotlinx.coroutines.CoroutineDispatcher
36 import kotlinx.coroutines.flow.Flow
37 import kotlinx.coroutines.flow.flowOf
38 import kotlinx.coroutines.withContext
39 
40 @SysUISingleton
41 class CameraQuickAffordanceConfig
42 @Inject
43 constructor(
44     @Application private val context: Context,
45     private val packageManager: PackageManager,
46     private val cameraGestureHelper: Lazy<CameraGestureHelper>,
47     private val userTracker: UserTracker,
48     private val devicePolicyManager: DevicePolicyManager,
49     @Background private val backgroundDispatcher: CoroutineDispatcher,
50 ) : KeyguardQuickAffordanceConfig {
51 
52     override val key: String
53         get() = BuiltInKeyguardQuickAffordanceKeys.CAMERA
54 
55     override val pickerName: String
56         get() = context.getString(R.string.accessibility_camera_button)
57 
58     override val pickerIconResourceId: Int
59         get() = R.drawable.ic_camera
60 
61     override val lockScreenState: Flow<KeyguardQuickAffordanceConfig.LockScreenState>
62         get() =
63             flowOf(
64                 KeyguardQuickAffordanceConfig.LockScreenState.Visible(
65                     icon =
66                         Icon.Resource(
67                             R.drawable.ic_camera,
68                             ContentDescription.Resource(R.string.accessibility_camera_button)
69                         )
70                 )
71             )
72 
getPickerScreenStatenull73     override suspend fun getPickerScreenState(): KeyguardQuickAffordanceConfig.PickerScreenState {
74         return if (isLaunchable()) {
75             super.getPickerScreenState()
76         } else {
77             KeyguardQuickAffordanceConfig.PickerScreenState.UnavailableOnDevice
78         }
79     }
80 
onTriggerednull81     override fun onTriggered(
82         expandable: Expandable?
83     ): KeyguardQuickAffordanceConfig.OnTriggeredResult {
84         cameraGestureHelper
85             .get()
86             .launchCamera(StatusBarManager.CAMERA_LAUNCH_SOURCE_QUICK_AFFORDANCE)
87         return KeyguardQuickAffordanceConfig.OnTriggeredResult.Handled
88     }
89 
isLaunchablenull90     private suspend fun isLaunchable(): Boolean {
91         return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY) &&
92             withContext(backgroundDispatcher) {
93                 !devicePolicyManager.getCameraDisabled(null, userTracker.userId) &&
94                     devicePolicyManager.getKeyguardDisabledFeatures(null, userTracker.userId) and
95                         DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA == 0
96             }
97     }
98 }
99