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