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