Home | History | Annotate | Download | only in display
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 package com.android.settings.display;
     15 
     16 import android.content.Context;
     17 import android.content.Intent;
     18 import android.provider.Settings;
     19 import android.text.TextUtils;
     20 
     21 import com.android.settings.DisplaySettings;
     22 import com.android.settings.core.TogglePreferenceController;
     23 import com.android.settings.search.DatabaseIndexingUtils;
     24 import com.android.settings.search.InlineSwitchPayload;
     25 import com.android.settings.search.ResultPayload;
     26 import com.android.settings.R;
     27 
     28 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
     29 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
     30 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
     31 
     32 
     33 public class AutoBrightnessPreferenceController extends TogglePreferenceController {
     34 
     35     private final String SYSTEM_KEY = SCREEN_BRIGHTNESS_MODE;
     36     private final int DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL;
     37 
     38     public AutoBrightnessPreferenceController(Context context, String key) {
     39         super(context, key);
     40     }
     41 
     42     @Override
     43     public boolean isChecked() {
     44         return Settings.System.getInt(mContext.getContentResolver(),
     45                 SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
     46     }
     47 
     48     @Override
     49     public boolean setChecked(boolean isChecked) {
     50         Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
     51                 isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE);
     52         return true;
     53     }
     54 
     55     @Override
     56     @AvailabilityStatus
     57     public int getAvailabilityStatus() {
     58         return mContext.getResources().getBoolean(
     59                 com.android.internal.R.bool.config_automatic_brightness_available)
     60                 ? AVAILABLE
     61                 : UNSUPPORTED_ON_DEVICE;
     62     }
     63 
     64     @Override
     65     public boolean isSliceable() {
     66         return TextUtils.equals(getPreferenceKey(), "auto_brightness");
     67     }
     68 
     69     @Override
     70     public ResultPayload getResultPayload() {
     71         // TODO remove result payload
     72         final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
     73                 DisplaySettings.class.getName(), getPreferenceKey(),
     74                 mContext.getString(R.string.display_settings));
     75 
     76         return new InlineSwitchPayload(SYSTEM_KEY,
     77                 ResultPayload.SettingsSource.SYSTEM, SCREEN_BRIGHTNESS_MODE_AUTOMATIC, intent,
     78                 isAvailable(), DEFAULT_VALUE);
     79     }
     80 }