Home | History | Annotate | Download | only in development
      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.support.v7.preference.PreferenceScreen;
     24 import android.widget.Toast;
     25 
     26 import com.android.internal.annotations.VisibleForTesting;
     27 import com.android.settings.R;
     28 import com.android.settings.core.PreferenceControllerMixin;
     29 import com.android.settingslib.core.AbstractPreferenceController;
     30 
     31 public class CameraHalHdrplusPreferenceController extends AbstractPreferenceController
     32         implements PreferenceControllerMixin {
     33 
     34     private static final String KEY_CAMERA_HAL_HDRPLUS_SWITCH = "camera_hal_hdrplus_switch";
     35     @VisibleForTesting
     36     static final String BUILD_TYPE = "ro.build.type";
     37     @VisibleForTesting
     38     static final String PROPERTY_CAMERA_HAL_HDRPLUS = "persist.camera.hdrplus.enable";
     39     @VisibleForTesting
     40     static final String ENABLED = "1";
     41     @VisibleForTesting
     42     static final String DISABLED = "0";
     43 
     44     private SwitchPreference mPreference;
     45 
     46     public CameraHalHdrplusPreferenceController(Context context) {
     47         super(context);
     48     }
     49 
     50     @Override
     51     public void displayPreference(PreferenceScreen screen) {
     52         super.displayPreference(screen);
     53         if (isAvailable()) {
     54             mPreference = (SwitchPreference) screen.findPreference(KEY_CAMERA_HAL_HDRPLUS_SWITCH);
     55             mPreference.setChecked(isHalHdrplusEnabled());
     56         }
     57     }
     58 
     59     @Override
     60     public String getPreferenceKey() {
     61         return KEY_CAMERA_HAL_HDRPLUS_SWITCH;
     62     }
     63 
     64     @Override
     65     public boolean isAvailable() {
     66         return mContext.getResources().getBoolean(R.bool.config_show_camera_hal_hdrplus);
     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_HAL_HDRPLUS_SWITCH.equals(preference.getKey())) {
     77             final SwitchPreference switchPreference = (SwitchPreference)preference;
     78             SystemProperties.set(PROPERTY_CAMERA_HAL_HDRPLUS,
     79                     switchPreference.isChecked() ? ENABLED : DISABLED);
     80             Toast.makeText(mContext, R.string.camera_hal_hdrplus_toast,
     81                     Toast.LENGTH_LONG).show();
     82             return true;
     83         }
     84         return false;
     85     }
     86 
     87     public void enablePreference(boolean enabled) {
     88         if (isAvailable()) {
     89             mPreference.setEnabled(enabled);
     90         }
     91     }
     92 
     93     public boolean updatePreference() {
     94         if (!isAvailable()) {
     95             return false;
     96         }
     97         final boolean enabled = isHalHdrplusEnabled();
     98         mPreference.setChecked(enabled);
     99         return enabled;
    100     }
    101 
    102     private boolean isHalHdrplusEnabled() {
    103         return SystemProperties.getBoolean(PROPERTY_CAMERA_HAL_HDRPLUS, false);
    104     }
    105 }
    106