Home | History | Annotate | Download | only in development
      1 /*
      2  * Copyright (C) 2018 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.app.UiModeManager;
     20 import android.content.Context;
     21 import android.support.annotation.VisibleForTesting;
     22 import android.support.v7.preference.ListPreference;
     23 import android.support.v7.preference.Preference;
     24 
     25 import com.android.settings.R;
     26 import com.android.settings.core.PreferenceControllerMixin;
     27 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
     28 
     29 public class DarkUIPreferenceController extends DeveloperOptionsPreferenceController
     30         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
     31 
     32     private static final String DARK_UI_KEY = "dark_ui_mode";
     33     private final UiModeManager mUiModeManager;
     34 
     35     public DarkUIPreferenceController(Context context) {
     36         this(context, context.getSystemService(UiModeManager.class));
     37     }
     38 
     39     @VisibleForTesting
     40     DarkUIPreferenceController(Context context, UiModeManager uiModeManager) {
     41         super(context);
     42         mUiModeManager = uiModeManager;
     43     }
     44 
     45     @Override
     46     public String getPreferenceKey() {
     47         return DARK_UI_KEY;
     48     }
     49 
     50     @Override
     51     public boolean onPreferenceChange(Preference preference, Object newValue) {
     52         mUiModeManager.setNightMode(modeToInt((String) newValue));
     53         updateSummary(preference);
     54         return true;
     55     }
     56 
     57     @Override
     58     public void updateState(Preference preference) {
     59         updateSummary(preference);
     60     }
     61 
     62     private void updateSummary(Preference preference) {
     63         int mode = mUiModeManager.getNightMode();
     64         ((ListPreference) preference).setValue(modeToString(mode));
     65         preference.setSummary(modeToDescription(mode));
     66     }
     67 
     68     private String modeToDescription(int mode) {
     69         String[] values = mContext.getResources().getStringArray(R.array.dark_ui_mode_entries);
     70         switch (mode) {
     71             case UiModeManager.MODE_NIGHT_AUTO:
     72                 return values[0];
     73             case UiModeManager.MODE_NIGHT_YES:
     74                 return values[1];
     75             case UiModeManager.MODE_NIGHT_NO:
     76             default:
     77                 return values[2];
     78 
     79         }
     80     }
     81 
     82     private String modeToString(int mode) {
     83         switch (mode) {
     84             case UiModeManager.MODE_NIGHT_AUTO:
     85                 return "auto";
     86             case UiModeManager.MODE_NIGHT_YES:
     87                 return "yes";
     88             case UiModeManager.MODE_NIGHT_NO:
     89             default:
     90                 return "no";
     91 
     92         }
     93     }
     94 
     95     private int modeToInt(String mode) {
     96         switch (mode) {
     97             case "auto":
     98                 return UiModeManager.MODE_NIGHT_AUTO;
     99             case "yes":
    100                 return UiModeManager.MODE_NIGHT_YES;
    101             case "no":
    102             default:
    103                 return UiModeManager.MODE_NIGHT_NO;
    104         }
    105     }
    106 }
    107