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 package com.android.tv.settings.privacy; 18 19 import static android.hardware.SensorPrivacyManager.TOGGLE_TYPE_HARDWARE; 20 21 import android.content.Context; 22 import android.graphics.drawable.Animatable; 23 import android.graphics.drawable.Drawable; 24 import android.hardware.SensorPrivacyManager; 25 import android.os.Bundle; 26 import android.view.View; 27 28 import androidx.annotation.Keep; 29 import androidx.fragment.app.FragmentActivity; 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.tv.settings.R; 34 import com.android.tv.settings.SettingsPreferenceFragment; 35 36 37 /** 38 * A fragment that shows info on how to unblock sensors blocked by a physical privacy switch. 39 * Dismisses itself when physical privacy is disabled. 40 */ 41 @Keep 42 public class PhysicalPrivacyUnblockFragment extends SettingsPreferenceFragment { 43 public static final String TOGGLE_EXTRA = "toggle"; 44 45 private PrivacyToggle mToggle; 46 private SensorPrivacyManager mSensorPrivacyManager; 47 private final SensorPrivacyManager.OnSensorPrivacyChangedListener mPrivacyChangedListener = 48 (sensor, enabled) -> updateSensorPrivacyState(); 49 private Preference mImagePreference; 50 51 @Override onCreate(Bundle savedInstanceState)52 public void onCreate(Bundle savedInstanceState) { 53 mSensorPrivacyManager = (SensorPrivacyManager) 54 getContext().getSystemService(Context.SENSOR_PRIVACY_SERVICE); 55 56 mToggle = (PrivacyToggle) getArguments().get(TOGGLE_EXTRA); 57 if (mToggle == null) { 58 throw new IllegalArgumentException("PrivacyToggle extra missing"); 59 } 60 61 super.onCreate(savedInstanceState); 62 } 63 64 @Override onStart()65 public void onStart() { 66 super.onStart(); 67 if (mImagePreference != null) { 68 Drawable image = mImagePreference.getIcon(); 69 if (image instanceof Animatable) { 70 ((Animatable) image).start(); 71 } 72 } 73 } 74 75 @Override onViewCreated(View view, Bundle savedInstanceState)76 public void onViewCreated(View view, Bundle savedInstanceState) { 77 super.onViewCreated(view, savedInstanceState); 78 mSensorPrivacyManager.addSensorPrivacyListener(mToggle.sensor, 79 mPrivacyChangedListener); 80 } 81 82 @Override onCreatePreferences(Bundle bundle, String s)83 public void onCreatePreferences(Bundle bundle, String s) { 84 Context themedContext = getPreferenceManager().getContext(); 85 PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(themedContext); 86 87 screen.setTitle(mToggle.physicalPrivacyEnabledInfoTitle); 88 89 mImagePreference = new Preference(themedContext); 90 mImagePreference.setLayoutResource(R.layout.image_preference); 91 mImagePreference.setIcon(mToggle.physicalPrivacyEnabledInfoPanelImage); 92 mImagePreference.setSelectable(false); 93 screen.addPreference(mImagePreference); 94 95 Preference preference = new Preference(themedContext); 96 preference.setSummary(mToggle.physicalPrivacyEnabledInfoPanelText); 97 screen.addPreference(preference); 98 99 updateSensorPrivacyState(); 100 101 setPreferenceScreen(screen); 102 } 103 updateSensorPrivacyState()104 private void updateSensorPrivacyState() { 105 boolean physicalPrivacyEnabled = mSensorPrivacyManager.isSensorPrivacyEnabled( 106 TOGGLE_TYPE_HARDWARE, mToggle.sensor); 107 if (!physicalPrivacyEnabled) { 108 FragmentActivity activity = getActivity(); 109 if (isResumed() && !getParentFragmentManager().popBackStackImmediate() 110 && activity != null) { 111 activity.onBackPressed(); 112 } 113 } 114 } 115 116 @Override onDestroyView()117 public void onDestroyView() { 118 mSensorPrivacyManager.removeSensorPrivacyListener(mToggle.sensor, mPrivacyChangedListener); 119 super.onDestroyView(); 120 } 121 } 122