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