1 /* 2 * Copyright (C) 2023 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.settings.qc; 18 19 import static android.car.settings.CarSettings.Global.FORCED_DAY_NIGHT_MODE; 20 21 import static com.android.car.qc.QCItem.QC_TYPE_ACTION_TOGGLE; 22 23 import android.app.PendingIntent; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.graphics.drawable.Icon; 27 import android.net.Uri; 28 import android.os.Bundle; 29 import android.provider.Settings; 30 31 import com.android.car.qc.QCActionItem; 32 import com.android.car.qc.QCItem; 33 import com.android.car.qc.QCList; 34 import com.android.car.qc.QCRow; 35 import com.android.car.settings.Flags; 36 import com.android.car.settings.R; 37 38 /** 39 * QC Item to control the day/night theme mode. 40 */ 41 public final class ThemeToggle extends SettingsQCItem { 42 static final String EXTRA_BUTTON_TYPE = "THEME_MODE_EXTRA_BUTTON_TYPE"; 43 static final int FORCED_SENSOR_MODE = 0; 44 static final int FORCED_DAY_MODE = 1; 45 static final int FORCED_NIGHT_MODE = 2; 46 ThemeToggle(Context context)47 public ThemeToggle(Context context) { 48 super(context); 49 } 50 51 @Override getQCItem()52 QCItem getQCItem() { 53 if (!android.car.feature.Flags.carNightGlobalSetting() || !Flags.uiThemeToggle()) { 54 return null; 55 } 56 QCList.Builder listBuilder = new QCList.Builder(); 57 listBuilder.addRow(new QCRow.Builder() 58 .setTitle(getContext().getString(R.string.qc_ui_mode_title)) 59 .addEndItem(createThemeToggleButton(FORCED_SENSOR_MODE)) 60 .addEndItem(createThemeToggleButton(FORCED_DAY_MODE)) 61 .addEndItem(createThemeToggleButton(FORCED_NIGHT_MODE)) 62 .build()); 63 64 return listBuilder.build(); 65 } 66 67 @Override getUri()68 Uri getUri() { 69 return SettingsQCRegistry.THEME_TOGGLE_URI; 70 } 71 72 @Override onNotifyChange(Intent intent)73 void onNotifyChange(Intent intent) { 74 int buttonType = intent.getIntExtra(EXTRA_BUTTON_TYPE, -1); 75 if (buttonType == -1) return; 76 77 if (!(buttonType == getForcedDayNightModeSetting())) { 78 setForcedDayNightModeSetting(buttonType); 79 } 80 } 81 82 @Override getBackgroundWorkerClass()83 Class getBackgroundWorkerClass() { 84 if (!android.car.feature.Flags.carNightGlobalSetting() || !Flags.uiThemeToggle()) { 85 return null; 86 } 87 return ThemeToggleWorker.class; 88 } 89 createThemeToggleButton(int mode)90 private QCActionItem createThemeToggleButton(int mode) { 91 Bundle extras = new Bundle(); 92 extras.putInt(EXTRA_BUTTON_TYPE, mode); 93 PendingIntent action = getBroadcastIntent(extras, mode); 94 boolean isSelected = getForcedDayNightModeSetting() == mode; 95 96 return new QCActionItem.Builder(QC_TYPE_ACTION_TOGGLE) 97 .setAction(action) 98 .setIcon(getThemeModeIcon(mode)) 99 .setChecked(isSelected) 100 .setClickable(!isSelected) 101 .build(); 102 } 103 getForcedDayNightModeSetting()104 private int getForcedDayNightModeSetting() { 105 return Settings.Global.getInt(getContext().getContentResolver(), 106 FORCED_DAY_NIGHT_MODE, FORCED_SENSOR_MODE); 107 } 108 setForcedDayNightModeSetting(int mode)109 private void setForcedDayNightModeSetting(int mode) { 110 Settings.Global.putInt(getContext().getContentResolver(), FORCED_DAY_NIGHT_MODE, mode); 111 } 112 getThemeModeIcon(int mode)113 private Icon getThemeModeIcon(int mode) { 114 switch(mode) { 115 case FORCED_SENSOR_MODE: 116 return Icon.createWithResource(getContext(), R.drawable.ic_qc_ui_mode_auto); 117 case FORCED_DAY_MODE: 118 return Icon.createWithResource(getContext(), R.drawable.ic_qc_ui_mode_day); 119 case FORCED_NIGHT_MODE: 120 return Icon.createWithResource(getContext(), R.drawable.ic_qc_ui_mode_night); 121 } 122 return null; 123 } 124 } 125