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