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");
      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.display;
     18 
     19 import android.app.Dialog;
     20 import android.app.TimePickerDialog;
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 import android.support.v7.preference.DropDownPreference;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v7.preference.TwoStatePreference;
     26 import android.widget.TimePicker;
     27 
     28 import com.android.internal.app.NightDisplayController;
     29 import com.android.internal.logging.MetricsProto.MetricsEvent;
     30 import com.android.settings.R;
     31 import com.android.settings.SettingsPreferenceFragment;
     32 
     33 import java.text.DateFormat;
     34 import java.util.Calendar;
     35 import java.util.TimeZone;
     36 
     37 /**
     38  * Settings screen for Night display.
     39  */
     40 public class NightDisplaySettings extends SettingsPreferenceFragment
     41         implements NightDisplayController.Callback, Preference.OnPreferenceChangeListener {
     42 
     43     private static final String KEY_NIGHT_DISPLAY_AUTO_MODE = "night_display_auto_mode";
     44     private static final String KEY_NIGHT_DISPLAY_START_TIME = "night_display_start_time";
     45     private static final String KEY_NIGHT_DISPLAY_END_TIME = "night_display_end_time";
     46     private static final String KEY_NIGHT_DISPLAY_ACTIVATED = "night_display_activated";
     47 
     48     private static final int DIALOG_START_TIME = 0;
     49     private static final int DIALOG_END_TIME = 1;
     50 
     51     private NightDisplayController mController;
     52     private DateFormat mTimeFormatter;
     53 
     54     private DropDownPreference mAutoModePreference;
     55     private Preference mStartTimePreference;
     56     private Preference mEndTimePreference;
     57     private TwoStatePreference mActivatedPreference;
     58 
     59     @Override
     60     public void onCreate(Bundle savedInstanceState) {
     61         super.onCreate(savedInstanceState);
     62 
     63         final Context context = getContext();
     64         mController = new NightDisplayController(context);
     65 
     66         mTimeFormatter = android.text.format.DateFormat.getTimeFormat(context);
     67         mTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
     68     }
     69 
     70     @Override
     71     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
     72         super.onCreatePreferences(savedInstanceState, rootKey);
     73 
     74         // Load the preferences from xml.
     75         addPreferencesFromResource(R.xml.night_display_settings);
     76 
     77         mAutoModePreference = (DropDownPreference) findPreference(KEY_NIGHT_DISPLAY_AUTO_MODE);
     78         mStartTimePreference = findPreference(KEY_NIGHT_DISPLAY_START_TIME);
     79         mEndTimePreference = findPreference(KEY_NIGHT_DISPLAY_END_TIME);
     80         mActivatedPreference = (TwoStatePreference) findPreference(KEY_NIGHT_DISPLAY_ACTIVATED);
     81 
     82         mAutoModePreference.setEntries(new CharSequence[] {
     83                 getString(R.string.night_display_auto_mode_never),
     84                 getString(R.string.night_display_auto_mode_custom),
     85                 getString(R.string.night_display_auto_mode_twilight)
     86         });
     87         mAutoModePreference.setEntryValues(new CharSequence[] {
     88                 String.valueOf(NightDisplayController.AUTO_MODE_DISABLED),
     89                 String.valueOf(NightDisplayController.AUTO_MODE_CUSTOM),
     90                 String.valueOf(NightDisplayController.AUTO_MODE_TWILIGHT)
     91         });
     92         mAutoModePreference.setOnPreferenceChangeListener(this);
     93         mActivatedPreference.setOnPreferenceChangeListener(this);
     94     }
     95 
     96     @Override
     97     public void onStart() {
     98         super.onStart();
     99 
    100         // Listen for changes only while visible.
    101         mController.setListener(this);
    102 
    103         // Update the current state since it have changed while not visible.
    104         onActivated(mController.isActivated());
    105         onAutoModeChanged(mController.getAutoMode());
    106         onCustomStartTimeChanged(mController.getCustomStartTime());
    107         onCustomEndTimeChanged(mController.getCustomEndTime());
    108     }
    109 
    110     @Override
    111     public void onStop() {
    112         super.onStop();
    113 
    114         // Stop listening for state changes.
    115         mController.setListener(null);
    116     }
    117 
    118     @Override
    119     public boolean onPreferenceTreeClick(Preference preference) {
    120         if (preference == mStartTimePreference) {
    121             showDialog(DIALOG_START_TIME);
    122             return true;
    123         } else if (preference == mEndTimePreference) {
    124             showDialog(DIALOG_END_TIME);
    125             return true;
    126         }
    127         return super.onPreferenceTreeClick(preference);
    128     }
    129 
    130     @Override
    131     public Dialog onCreateDialog(final int dialogId) {
    132         if (dialogId == DIALOG_START_TIME || dialogId == DIALOG_END_TIME) {
    133             final NightDisplayController.LocalTime initialTime;
    134             if (dialogId == DIALOG_START_TIME) {
    135                 initialTime = mController.getCustomStartTime();
    136             } else {
    137                 initialTime = mController.getCustomEndTime();
    138             }
    139 
    140             final Context context = getContext();
    141             final boolean use24HourFormat = android.text.format.DateFormat.is24HourFormat(context);
    142             return new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
    143                 @Override
    144                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    145                     final NightDisplayController.LocalTime time =
    146                             new NightDisplayController.LocalTime(hourOfDay, minute);
    147                     if (dialogId == DIALOG_START_TIME) {
    148                         mController.setCustomStartTime(time);
    149                     } else {
    150                         mController.setCustomEndTime(time);
    151                     }
    152                 }
    153             }, initialTime.hourOfDay, initialTime.minute, use24HourFormat);
    154         }
    155         return super.onCreateDialog(dialogId);
    156     }
    157 
    158     @Override
    159     public void onActivated(boolean activated) {
    160         mActivatedPreference.setChecked(activated);
    161     }
    162 
    163     @Override
    164     public void onAutoModeChanged(int autoMode) {
    165         mAutoModePreference.setValue(String.valueOf(autoMode));
    166 
    167         final boolean showCustomSchedule = autoMode == NightDisplayController.AUTO_MODE_CUSTOM;
    168         mStartTimePreference.setVisible(showCustomSchedule);
    169         mEndTimePreference.setVisible(showCustomSchedule);
    170     }
    171 
    172     private String getFormattedTimeString(NightDisplayController.LocalTime localTime) {
    173         final Calendar c = Calendar.getInstance();
    174         c.setTimeZone(mTimeFormatter.getTimeZone());
    175         c.set(Calendar.HOUR_OF_DAY, localTime.hourOfDay);
    176         c.set(Calendar.MINUTE, localTime.minute);
    177         c.set(Calendar.SECOND, 0);
    178         c.set(Calendar.MILLISECOND, 0);
    179         return mTimeFormatter.format(c.getTime());
    180     }
    181 
    182     @Override
    183     public void onCustomStartTimeChanged(NightDisplayController.LocalTime startTime) {
    184         mStartTimePreference.setSummary(getFormattedTimeString(startTime));
    185     }
    186 
    187     @Override
    188     public void onCustomEndTimeChanged(NightDisplayController.LocalTime endTime) {
    189         mEndTimePreference.setSummary(getFormattedTimeString(endTime));
    190     }
    191 
    192     @Override
    193     public boolean onPreferenceChange(Preference preference, Object newValue) {
    194         if (preference == mAutoModePreference) {
    195             return mController.setAutoMode(Integer.parseInt((String) newValue));
    196         } else if (preference == mActivatedPreference) {
    197             return mController.setActivated((Boolean) newValue);
    198         }
    199         return false;
    200     }
    201 
    202     @Override
    203     protected int getMetricsCategory() {
    204         return MetricsEvent.NIGHT_DISPLAY_SETTINGS;
    205     }
    206 }
    207