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