1 /*
2  * Copyright (C) 2017 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.dream;
18 
19 import static com.android.settingslib.dream.DreamBackend.EITHER;
20 import static com.android.settingslib.dream.DreamBackend.NEVER;
21 import static com.android.settingslib.dream.DreamBackend.WHILE_CHARGING;
22 import static com.android.settingslib.dream.DreamBackend.WHILE_DOCKED;
23 
24 import android.app.settings.SettingsEnums;
25 import android.content.Context;
26 import android.provider.SearchIndexableResource;
27 
28 import androidx.annotation.VisibleForTesting;
29 
30 import com.android.settings.R;
31 import com.android.settings.dashboard.DashboardFragment;
32 import com.android.settings.search.BaseSearchIndexProvider;
33 import com.android.settingslib.core.AbstractPreferenceController;
34 import com.android.settingslib.dream.DreamBackend;
35 import com.android.settingslib.dream.DreamBackend.WhenToDream;
36 import com.android.settingslib.search.SearchIndexable;
37 
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 
42 @SearchIndexable
43 public class DreamSettings extends DashboardFragment {
44 
45     private static final String TAG = "DreamSettings";
46     static final String WHILE_CHARGING_ONLY = "while_charging_only";
47     static final String WHILE_DOCKED_ONLY = "while_docked_only";
48     static final String EITHER_CHARGING_OR_DOCKED = "either_charging_or_docked";
49     static final String NEVER_DREAM = "never";
50 
51     @WhenToDream
getSettingFromPrefKey(String key)52     static int getSettingFromPrefKey(String key) {
53         switch (key) {
54             case WHILE_CHARGING_ONLY:
55                 return WHILE_CHARGING;
56             case WHILE_DOCKED_ONLY:
57                 return WHILE_DOCKED;
58             case EITHER_CHARGING_OR_DOCKED:
59                 return EITHER;
60             case NEVER_DREAM:
61             default:
62                 return NEVER;
63         }
64     }
65 
getKeyFromSetting(@henToDream int dreamSetting)66     static String getKeyFromSetting(@WhenToDream int dreamSetting) {
67         switch (dreamSetting) {
68             case WHILE_CHARGING:
69                 return WHILE_CHARGING_ONLY;
70             case WHILE_DOCKED:
71                 return WHILE_DOCKED_ONLY;
72             case EITHER:
73                 return EITHER_CHARGING_OR_DOCKED;
74             case NEVER:
75             default:
76                 return NEVER_DREAM;
77         }
78     }
79 
getDreamSettingDescriptionResId(@henToDream int dreamSetting)80     static int getDreamSettingDescriptionResId(@WhenToDream int dreamSetting) {
81         switch (dreamSetting) {
82             case WHILE_CHARGING:
83                 return R.string.screensaver_settings_summary_sleep;
84             case WHILE_DOCKED:
85                 return R.string.screensaver_settings_summary_dock;
86             case EITHER:
87                 return R.string.screensaver_settings_summary_either_long;
88             case NEVER:
89             default:
90                 return R.string.screensaver_settings_summary_never;
91         }
92     }
93 
94     @Override
getMetricsCategory()95     public int getMetricsCategory() {
96         return SettingsEnums.DREAM;
97     }
98 
99     @Override
getPreferenceScreenResId()100     protected int getPreferenceScreenResId() {
101         return R.xml.dream_fragment_overview;
102     }
103 
104     @Override
getHelpResource()105     public int getHelpResource() {
106         return R.string.help_url_screen_saver;
107     }
108 
109     @Override
getLogTag()110     protected String getLogTag() {
111         return TAG;
112     }
113 
114     @Override
createPreferenceControllers(Context context)115     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
116         return buildPreferenceControllers(context);
117     }
118 
getSummaryTextWithDreamName(Context context)119     public static CharSequence getSummaryTextWithDreamName(Context context) {
120         DreamBackend backend = DreamBackend.getInstance(context);
121         return getSummaryTextFromBackend(backend, context);
122     }
123 
124     @VisibleForTesting
getSummaryTextFromBackend(DreamBackend backend, Context context)125     static CharSequence getSummaryTextFromBackend(DreamBackend backend, Context context) {
126         if (!backend.isEnabled()) {
127             return context.getString(R.string.screensaver_settings_summary_off);
128         } else {
129             return backend.getActiveDreamName();
130         }
131     }
132 
buildPreferenceControllers(Context context)133     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
134         List<AbstractPreferenceController> controllers = new ArrayList<>();
135         controllers.add(new WhenToDreamPreferenceController(context));
136         controllers.add(new StartNowPreferenceController(context));
137         return controllers;
138     }
139 
140     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER
141             = new BaseSearchIndexProvider() {
142         @Override
143         public List<SearchIndexableResource> getXmlResourcesToIndex(
144                 Context context, boolean enabled) {
145             final SearchIndexableResource sir = new SearchIndexableResource(context);
146             sir.xmlResId = R.xml.dream_fragment_overview;
147             return Arrays.asList(sir);
148         }
149 
150         @Override
151         public List<AbstractPreferenceController> createPreferenceControllers(Context context) {
152             return buildPreferenceControllers(context);
153         }
154     };
155 }
156 
157