1 /* 2 * Copyright (C) 2021 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 static android.hardware.SensorPrivacyManager.Sensors.CAMERA; 20 import static android.hardware.SensorPrivacyManager.Sources.DIALOG; 21 22 import android.content.Context; 23 import android.hardware.SensorPrivacyManager; 24 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.internal.annotations.VisibleForTesting; 28 import com.android.settings.R; 29 import com.android.settingslib.widget.BannerMessagePreference; 30 31 /** 32 * The controller of Screen attention's camera disabled warning preference. 33 * The preference appears when the camera access is disabled for Screen Attention feature. 34 */ 35 public class AdaptiveSleepCameraStatePreferenceController { 36 @VisibleForTesting 37 BannerMessagePreference mPreference; 38 private final SensorPrivacyManager mPrivacyManager; 39 private final Context mContext; 40 AdaptiveSleepCameraStatePreferenceController(Context context)41 public AdaptiveSleepCameraStatePreferenceController(Context context) { 42 mPrivacyManager = SensorPrivacyManager.getInstance(context); 43 mPrivacyManager.addSensorPrivacyListener(CAMERA, 44 (sensor, enabled) -> updateVisibility()); 45 mContext = context; 46 } 47 48 /** 49 * Adds the controlled preference to the provided preference screen. 50 */ addToScreen(PreferenceScreen screen)51 public void addToScreen(PreferenceScreen screen) { 52 initializePreference(); 53 screen.addPreference(mPreference); 54 updateVisibility(); 55 } 56 57 /** 58 * Need this because all controller tests use RoboElectric. No easy way to mock this service, 59 * so we mock the call we need 60 */ 61 @VisibleForTesting isCameraLocked()62 boolean isCameraLocked() { 63 return mPrivacyManager.isSensorPrivacyEnabled(CAMERA); 64 } 65 66 /** 67 * Refreshes the visibility of the preference. 68 */ updateVisibility()69 public void updateVisibility() { 70 initializePreference(); 71 mPreference.setVisible(isCameraLocked()); 72 } 73 initializePreference()74 private void initializePreference() { 75 if (mPreference == null) { 76 mPreference = new BannerMessagePreference(mContext); 77 mPreference.setTitle(R.string.auto_rotate_camera_lock_title); 78 mPreference.setSummary(R.string.adaptive_sleep_camera_lock_summary); 79 mPreference.setPositiveButtonText(R.string.allow); 80 mPreference.setPositiveButtonOnClickListener( 81 p -> mPrivacyManager.setSensorPrivacy(DIALOG, CAMERA, false)); 82 } 83 } 84 } 85