1 /* 2 * Copyright (C) 2015 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.tv.settings.device.display.daydream; 18 19 import android.content.BroadcastReceiver; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.os.Bundle; 25 import android.provider.Settings; 26 import android.support.annotation.Keep; 27 import android.support.v17.preference.LeanbackPreferenceFragment; 28 import android.support.v7.preference.ListPreference; 29 import android.support.v7.preference.Preference; 30 import android.text.TextUtils; 31 import android.text.format.DateUtils; 32 import android.util.ArrayMap; 33 import android.util.AttributeSet; 34 import android.util.Log; 35 36 import com.android.settingslib.dream.DreamBackend; 37 import com.android.tv.settings.R; 38 39 import java.util.List; 40 import java.util.Map; 41 import java.util.Objects; 42 43 import static android.provider.Settings.Secure.SLEEP_TIMEOUT; 44 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; 45 46 public class DaydreamFragment extends LeanbackPreferenceFragment 47 implements Preference.OnPreferenceChangeListener { 48 49 private static final String TAG = "DaydreamFragment"; 50 51 private static final String KEY_ACTIVE_DREAM = "activeDream"; 52 private static final String KEY_DREAM_TIME = "dreamTime"; 53 private static final String KEY_SLEEP_TIME = "sleepTime"; 54 private static final String KEY_DREAM_NOW = "dreamNow"; 55 56 private static final String DREAM_COMPONENT_NONE = "NONE"; 57 private static final String PACKAGE_SCHEME = "package"; 58 59 private static final int DEFAULT_DREAM_TIME_MS = (int) (30 * DateUtils.MINUTE_IN_MILLIS); 60 private static final int DEFAULT_SLEEP_TIME_MS = (int) (3 * DateUtils.HOUR_IN_MILLIS); 61 62 private final PackageReceiver mPackageReceiver = new PackageReceiver(); 63 64 private DreamBackend mBackend; 65 private final Map<String, DreamBackend.DreamInfo> mDreamInfos = new ArrayMap<>(); 66 67 public static DaydreamFragment newInstance() { 68 return new DaydreamFragment(); 69 } 70 71 @Override 72 public void onCreate(Bundle savedInstanceState) { 73 mBackend = new DreamBackend(getActivity()); 74 super.onCreate(savedInstanceState); 75 } 76 77 @Override 78 public void onResume() { 79 super.onResume(); 80 refreshFromBackend(); 81 82 // listen for package changes 83 IntentFilter filter = new IntentFilter(); 84 filter.addAction(Intent.ACTION_PACKAGE_ADDED); 85 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 86 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 87 filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 88 filter.addDataScheme(PACKAGE_SCHEME); 89 getActivity().registerReceiver(mPackageReceiver, filter); 90 } 91 92 @Override 93 public void onPause() { 94 super.onPause(); 95 96 getActivity().unregisterReceiver(mPackageReceiver); 97 } 98 99 @Override 100 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 101 setPreferencesFromResource(R.xml.daydream, null); 102 103 final ListPreference activeDreamPref = (ListPreference) findPreference(KEY_ACTIVE_DREAM); 104 refreshActiveDreamPref(activeDreamPref); 105 activeDreamPref.setOnPreferenceChangeListener(this); 106 107 final ListPreference dreamTimePref = (ListPreference) findPreference(KEY_DREAM_TIME); 108 dreamTimePref.setValue(Integer.toString(getDreamTime())); 109 dreamTimePref.setOnPreferenceChangeListener(this); 110 111 final ListPreference sleepTimePref = (ListPreference) findPreference(KEY_SLEEP_TIME); 112 sleepTimePref.setValue(Integer.toString(getSleepTime())); 113 sleepTimePref.setOnPreferenceChangeListener(this); 114 115 final Preference dreamNowPref = findPreference(KEY_DREAM_NOW); 116 dreamNowPref.setEnabled(mBackend.isEnabled()); 117 } 118 119 private void refreshActiveDreamPref(ListPreference activeDreamPref) { 120 final List<DreamBackend.DreamInfo> infos = mBackend.getDreamInfos(); 121 final CharSequence[] dreamEntries = new CharSequence[infos.size() + 1]; 122 final CharSequence[] dreamEntryValues = new CharSequence[infos.size() + 1]; 123 refreshDreamInfoMap(infos, dreamEntries, dreamEntryValues); 124 activeDreamPref.setEntries(dreamEntries); 125 activeDreamPref.setEntryValues(dreamEntryValues); 126 final ComponentName currentDreamComponent = mBackend.getActiveDream(); 127 activeDreamPref.setValue(mBackend.isEnabled() && currentDreamComponent != null 128 ? currentDreamComponent.toShortString() : DREAM_COMPONENT_NONE); 129 } 130 131 private void refreshDreamInfoMap(List<DreamBackend.DreamInfo> infos, 132 CharSequence[] listEntries, CharSequence[] listEntryValues) { 133 mDreamInfos.clear(); 134 listEntries[0] = getString(R.string.device_daydreams_none); 135 listEntryValues[0] = DREAM_COMPONENT_NONE; 136 int index = 1; 137 for (final DreamBackend.DreamInfo info : infos) { 138 final String componentNameString = info.componentName.toShortString(); 139 mDreamInfos.put(componentNameString, info); 140 listEntries[index] = info.caption; 141 listEntryValues[index] = componentNameString; 142 index++; 143 } 144 } 145 146 @Override 147 public boolean onPreferenceChange(Preference preference, Object newValue) { 148 switch (preference.getKey()) { 149 case KEY_ACTIVE_DREAM: 150 setActiveDream((String) newValue); 151 break; 152 case KEY_DREAM_TIME: 153 setDreamTime(Integer.parseInt((String) newValue)); 154 break; 155 case KEY_SLEEP_TIME: 156 setSleepTime(Integer.parseInt((String) newValue)); 157 break; 158 } 159 return true; 160 } 161 162 private void setActiveDream(String componentNameString) { 163 final DreamBackend.DreamInfo dreamInfo = mDreamInfos.get(componentNameString); 164 if (dreamInfo != null) { 165 if (dreamInfo.settingsComponentName != null) { 166 startActivity(new Intent().setComponent(dreamInfo.settingsComponentName)); 167 } 168 if (!mBackend.isEnabled()) { 169 mBackend.setEnabled(true); 170 } 171 if (!Objects.equals(mBackend.getActiveDream(), dreamInfo.componentName)) { 172 mBackend.setActiveDream(dreamInfo.componentName); 173 } 174 } else { 175 if (mBackend.isEnabled()) { 176 mBackend.setActiveDream(null); 177 mBackend.setEnabled(false); 178 } 179 } 180 } 181 182 private int getDreamTime() { 183 return Settings.System.getInt(getActivity().getContentResolver(), SCREEN_OFF_TIMEOUT, 184 DEFAULT_DREAM_TIME_MS); 185 } 186 187 private void setDreamTime(int ms) { 188 Settings.System.putInt(getActivity().getContentResolver(), SCREEN_OFF_TIMEOUT, ms); 189 190 } 191 192 private int getSleepTime() { 193 return Settings.Secure.getInt(getActivity().getContentResolver(), SLEEP_TIMEOUT, 194 DEFAULT_SLEEP_TIME_MS); 195 } 196 197 private void setSleepTime(int ms) { 198 Settings.Secure.putInt(getActivity().getContentResolver(), SLEEP_TIMEOUT, ms); 199 } 200 201 @Override 202 public boolean onPreferenceTreeClick(Preference preference) { 203 switch (preference.getKey()) { 204 case KEY_DREAM_NOW: 205 mBackend.startDreaming(); 206 return true; 207 default: 208 return super.onPreferenceTreeClick(preference); 209 } 210 } 211 212 private void refreshFromBackend() { 213 if (getActivity() == null) { 214 Log.d(TAG, "No activity, not refreshing"); 215 return; 216 } 217 218 final ListPreference activeDreamPref = (ListPreference) findPreference(KEY_ACTIVE_DREAM); 219 if (activeDreamPref != null) { 220 refreshActiveDreamPref(activeDreamPref); 221 } 222 223 final ListPreference dreamTimePref = (ListPreference) findPreference(KEY_DREAM_TIME); 224 if (dreamTimePref != null) { 225 dreamTimePref.setValue(Integer.toString(getDreamTime())); 226 } 227 228 final ListPreference sleepTimePref = (ListPreference) findPreference(KEY_SLEEP_TIME); 229 if (sleepTimePref != null) { 230 sleepTimePref.setValue(Integer.toString(getSleepTime())); 231 } 232 233 final Preference dreamNowPref = findPreference(KEY_DREAM_NOW); 234 if (dreamNowPref != null) { 235 dreamNowPref.setEnabled(mBackend.isEnabled()); 236 } 237 } 238 239 private class PackageReceiver extends BroadcastReceiver { 240 @Override 241 public void onReceive(Context context, Intent intent) { 242 refreshFromBackend(); 243 } 244 } 245 246 } 247