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 static androidx.lifecycle.Lifecycle.Event.ON_START; 23 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 24 25 import android.content.Context; 26 import android.hardware.SensorPrivacyManager; 27 28 import androidx.lifecycle.Lifecycle; 29 import androidx.lifecycle.LifecycleObserver; 30 import androidx.lifecycle.OnLifecycleEvent; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.internal.annotations.VisibleForTesting; 34 import com.android.settings.R; 35 import com.android.settingslib.widget.BannerMessagePreference; 36 37 /** 38 * The controller of Screen attention's camera disabled warning preference. 39 * The preference appears when the camera access is disabled for Screen Attention feature. 40 */ 41 public class AdaptiveSleepCameraStatePreferenceController implements LifecycleObserver { 42 @VisibleForTesting 43 BannerMessagePreference mPreference; 44 private final SensorPrivacyManager mPrivacyManager; 45 private final Context mContext; 46 47 private final SensorPrivacyManager.OnSensorPrivacyChangedListener mPrivacyChangedListener = 48 new SensorPrivacyManager.OnSensorPrivacyChangedListener() { 49 @Override 50 public void onSensorPrivacyChanged(int sensor, boolean enabled) { 51 updateVisibility(); 52 } 53 }; 54 AdaptiveSleepCameraStatePreferenceController(Context context, Lifecycle lifecycle)55 public AdaptiveSleepCameraStatePreferenceController(Context context, Lifecycle lifecycle) { 56 mPrivacyManager = SensorPrivacyManager.getInstance(context); 57 mContext = context; 58 lifecycle.addObserver(this); 59 } 60 61 @OnLifecycleEvent(ON_START) onStart()62 public void onStart() { 63 mPrivacyManager.addSensorPrivacyListener(CAMERA, mPrivacyChangedListener); 64 } 65 66 @OnLifecycleEvent(ON_STOP) onStop()67 public void onStop() { 68 mPrivacyManager.removeSensorPrivacyListener(CAMERA, mPrivacyChangedListener); 69 } 70 71 /** 72 * Adds the controlled preference to the provided preference screen. 73 */ addToScreen(PreferenceScreen screen)74 public void addToScreen(PreferenceScreen screen) { 75 initializePreference(); 76 screen.addPreference(mPreference); 77 updateVisibility(); 78 } 79 80 /** 81 * Need this because all controller tests use Robolectric. No easy way to mock this service, 82 * so we mock the call we need 83 */ 84 @VisibleForTesting isCameraLocked()85 boolean isCameraLocked() { 86 return mPrivacyManager.isSensorPrivacyEnabled(CAMERA); 87 } 88 89 /** 90 * Refreshes the visibility of the preference. 91 */ updateVisibility()92 public void updateVisibility() { 93 initializePreference(); 94 mPreference.setVisible(isCameraLocked()); 95 } 96 initializePreference()97 private void initializePreference() { 98 if (mPreference == null) { 99 mPreference = new BannerMessagePreference(mContext); 100 mPreference.setTitle(R.string.auto_rotate_camera_lock_title); 101 mPreference.setSummary(R.string.adaptive_sleep_camera_lock_summary); 102 mPreference.setPositiveButtonText(R.string.allow); 103 mPreference.setPositiveButtonOnClickListener( 104 p -> mPrivacyManager.setSensorPrivacy(DIALOG, CAMERA, false)); 105 } 106 } 107 } 108