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 static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable; 26 27 import android.content.Context; 28 import android.hardware.SensorPrivacyManager; 29 30 import androidx.lifecycle.LifecycleObserver; 31 import androidx.lifecycle.OnLifecycleEvent; 32 import androidx.preference.Preference; 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.internal.annotations.VisibleForTesting; 36 import com.android.settings.R; 37 import com.android.settings.core.BasePreferenceController; 38 import com.android.settingslib.widget.BannerMessagePreference; 39 40 /** 41 * The controller of camera based rotate privacy sensor warning preference. The preference appears 42 * when the privacy sensor service disables camera functionality completely. 43 */ 44 public class SmartAutoRotateCameraStateController extends BasePreferenceController implements 45 LifecycleObserver { 46 47 private final SensorPrivacyManager mPrivacyManager; 48 private Preference mPreference; 49 50 private final SensorPrivacyManager.OnSensorPrivacyChangedListener mPrivacyChangedListener = 51 new SensorPrivacyManager.OnSensorPrivacyChangedListener() { 52 @Override 53 public void onSensorPrivacyChanged(int sensor, boolean enabled) { 54 if (mPreference != null) { 55 mPreference.setVisible(isAvailable()); 56 } 57 updateState(mPreference); 58 } 59 }; 60 SmartAutoRotateCameraStateController(Context context, String key)61 public SmartAutoRotateCameraStateController(Context context, String key) { 62 super(context, key); 63 mPrivacyManager = SensorPrivacyManager.getInstance(context); 64 } 65 66 @OnLifecycleEvent(ON_START) onStart()67 public void onStart() { 68 mPrivacyManager.addSensorPrivacyListener(CAMERA, mPrivacyChangedListener); 69 70 } 71 72 @OnLifecycleEvent(ON_STOP) onStop()73 public void onStop() { 74 mPrivacyManager.removeSensorPrivacyListener(CAMERA, mPrivacyChangedListener); 75 } 76 77 @VisibleForTesting isCameraLocked()78 boolean isCameraLocked() { 79 return mPrivacyManager.isSensorPrivacyEnabled(SensorPrivacyManager.Sensors.CAMERA); 80 } 81 82 @Override displayPreference(PreferenceScreen screen)83 public void displayPreference(PreferenceScreen screen) { 84 super.displayPreference(screen); 85 mPreference = screen.findPreference(getPreferenceKey()); 86 ((BannerMessagePreference) mPreference) 87 .setPositiveButtonText(R.string.allow) 88 .setPositiveButtonOnClickListener(v -> { 89 mPrivacyManager.setSensorPrivacy(DIALOG, CAMERA, false); 90 }); 91 } 92 93 @Override 94 @AvailabilityStatus getAvailabilityStatus()95 public int getAvailabilityStatus() { 96 return isRotationResolverServiceAvailable(mContext) 97 && isCameraLocked() ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE; 98 } 99 } 100