1 /* 2 * Copyright (C) 2019 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.car.developeroptions.display; 18 19 import android.app.UiModeManager; 20 import android.content.Context; 21 import androidx.preference.Preference; 22 import com.android.car.developeroptions.R; 23 import androidx.annotation.VisibleForTesting; 24 25 public class DarkUISettingsRadioButtonsController { 26 27 public static final String KEY_DARK = "key_dark_ui_settings_dark"; 28 public static final String KEY_LIGHT = "key_dark_ui_settings_light"; 29 30 @VisibleForTesting 31 UiModeManager mManager; 32 33 private Preference mFooter; 34 DarkUISettingsRadioButtonsController(Context context, Preference footer)35 public DarkUISettingsRadioButtonsController(Context context, Preference footer) { 36 mManager = context.getSystemService(UiModeManager.class); 37 mFooter = footer; 38 } 39 getDefaultKey()40 public String getDefaultKey() { 41 final int mode = mManager.getNightMode(); 42 updateFooter(); 43 return mode == UiModeManager.MODE_NIGHT_YES ? KEY_DARK : KEY_LIGHT; 44 } 45 setDefaultKey(String key)46 public boolean setDefaultKey(String key) { 47 switch(key) { 48 case KEY_DARK: 49 mManager.setNightMode(UiModeManager.MODE_NIGHT_YES); 50 break; 51 case KEY_LIGHT: 52 mManager.setNightMode(UiModeManager.MODE_NIGHT_NO); 53 break; 54 default: 55 throw new IllegalStateException( 56 "Not a valid key for " + this.getClass().getSimpleName() + ": " + key); 57 } 58 updateFooter(); 59 return true; 60 } 61 updateFooter()62 public void updateFooter() { 63 final int mode = mManager.getNightMode(); 64 switch (mode) { 65 case UiModeManager.MODE_NIGHT_YES: 66 mFooter.setSummary(R.string.dark_ui_settings_dark_summary); 67 break; 68 case UiModeManager.MODE_NIGHT_NO: 69 case UiModeManager.MODE_NIGHT_AUTO: 70 default: 71 mFooter.setSummary(R.string.dark_ui_settings_light_summary); 72 } 73 } 74 modeToDescription(Context context, int mode)75 public static String modeToDescription(Context context, int mode) { 76 final String[] values = context.getResources().getStringArray(R.array.dark_ui_mode_entries); 77 switch (mode) { 78 case UiModeManager.MODE_NIGHT_YES: 79 return values[0]; 80 case UiModeManager.MODE_NIGHT_NO: 81 case UiModeManager.MODE_NIGHT_AUTO: 82 default: 83 return values[1]; 84 } 85 } 86 } 87