1 /* 2 * Copyright (C) 2017 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.development; 18 19 import android.content.Context; 20 import android.os.SystemProperties; 21 import android.text.TextUtils; 22 23 import androidx.annotation.VisibleForTesting; 24 import androidx.preference.Preference; 25 import androidx.preference.SwitchPreference; 26 27 import com.android.settings.R; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 30 31 public class CameraLaserSensorPreferenceController extends DeveloperOptionsPreferenceController 32 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 33 34 private static final String KEY_CAMERA_LASER_SENSOR_SWITCH = "camera_laser_sensor_switch"; 35 @VisibleForTesting 36 static final String BUILD_TYPE = "ro.build.type"; 37 @VisibleForTesting 38 static final String PROPERTY_CAMERA_LASER_SENSOR = "persist.camera.stats.disablehaf"; 39 @VisibleForTesting 40 static final int ENABLED = 0; 41 @VisibleForTesting 42 static final int DISABLED = 2; 43 @VisibleForTesting 44 static final String USERDEBUG_BUILD = "userdebug"; 45 @VisibleForTesting 46 static final String ENG_BUILD = "eng"; 47 @VisibleForTesting 48 static final String USER_BUILD = "user"; 49 CameraLaserSensorPreferenceController(Context context)50 public CameraLaserSensorPreferenceController(Context context) { 51 super(context); 52 } 53 54 @Override isAvailable()55 public boolean isAvailable() { 56 return mContext.getResources().getBoolean(R.bool.config_show_camera_laser_sensor); 57 } 58 59 @Override getPreferenceKey()60 public String getPreferenceKey() { 61 return KEY_CAMERA_LASER_SENSOR_SWITCH; 62 } 63 64 @Override onPreferenceChange(Preference preference, Object newValue)65 public boolean onPreferenceChange(Preference preference, Object newValue) { 66 final boolean isEnabled = (Boolean) newValue; 67 String value = Integer.toString(isEnabled ? ENABLED : DISABLED); 68 SystemProperties.set(PROPERTY_CAMERA_LASER_SENSOR, value); 69 return true; 70 } 71 72 @Override updateState(Preference preference)73 public void updateState(Preference preference) { 74 final boolean enabled = isLaserSensorEnabled(); 75 ((SwitchPreference) mPreference).setChecked(enabled); 76 } 77 78 @Override onDeveloperOptionsSwitchDisabled()79 protected void onDeveloperOptionsSwitchDisabled() { 80 super.onDeveloperOptionsSwitchDisabled(); 81 SystemProperties.set(PROPERTY_CAMERA_LASER_SENSOR, Integer.toString(DISABLED)); 82 ((SwitchPreference) mPreference).setChecked(false); 83 } 84 isLaserSensorEnabled()85 private boolean isLaserSensorEnabled() { 86 final String prop = SystemProperties.get(PROPERTY_CAMERA_LASER_SENSOR, 87 Integer.toString(ENABLED)); 88 return TextUtils.equals(Integer.toString(ENABLED), prop); 89 } 90 91 } 92