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 17package com.android.settings.development; 18 19import android.content.Context; 20import android.os.SystemProperties; 21import android.support.v14.preference.SwitchPreference; 22import android.support.v7.preference.Preference; 23import android.support.v7.preference.PreferenceScreen; 24import android.widget.Toast; 25 26import com.android.internal.annotations.VisibleForTesting; 27import com.android.settings.R; 28import com.android.settings.core.PreferenceControllerMixin; 29import com.android.settingslib.core.AbstractPreferenceController; 30 31public class CameraLaserSensorPreferenceController extends AbstractPreferenceController 32 implements 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 44 private SwitchPreference mPreference; 45 46 public CameraLaserSensorPreferenceController(Context context) { 47 super(context); 48 } 49 50 @Override 51 public void displayPreference(PreferenceScreen screen) { 52 super.displayPreference(screen); 53 mPreference = (SwitchPreference) screen.findPreference(KEY_CAMERA_LASER_SENSOR_SWITCH); 54 updatePreference(); 55 } 56 57 @Override 58 public String getPreferenceKey() { 59 return KEY_CAMERA_LASER_SENSOR_SWITCH; 60 } 61 62 @Override 63 public boolean isAvailable() { 64 String buildType = SystemProperties.get(BUILD_TYPE); 65 return mContext.getResources().getBoolean(R.bool.config_show_camera_laser_sensor) && 66 (buildType.equals("userdebug") || buildType.equals("eng")); 67 } 68 69 @Override 70 public void updateState(Preference preference) { 71 updatePreference(); 72 } 73 74 @Override 75 public boolean handlePreferenceTreeClick(Preference preference) { 76 if (KEY_CAMERA_LASER_SENSOR_SWITCH.equals(preference.getKey())) { 77 final SwitchPreference switchPreference = (SwitchPreference)preference; 78 String value = Integer.toString(switchPreference.isChecked() ? ENABLED : DISABLED); 79 SystemProperties.set(PROPERTY_CAMERA_LASER_SENSOR, value); 80 return true; 81 } 82 return false; 83 } 84 85 public void enablePreference(boolean enabled) { 86 if (isAvailable()) { 87 mPreference.setEnabled(enabled); 88 } 89 } 90 91 public boolean updatePreference() { 92 if (!isAvailable()) { 93 return false; 94 } 95 final boolean enabled = isLaserSensorEnabled(); 96 mPreference.setChecked(enabled); 97 return enabled; 98 } 99 100 private boolean isLaserSensorEnabled() { 101 String prop = SystemProperties.get(PROPERTY_CAMERA_LASER_SENSOR, Integer.toString(ENABLED)); 102 return prop.equals(Integer.toString(ENABLED)); 103 } 104} 105