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 android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 23 import com.android.settings.R; 24 import com.android.settings.widget.RadioButtonPickerFragment; 25 import com.android.settingslib.dream.DreamBackend; 26 import com.android.settingslib.widget.CandidateInfo; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 public class WhenToDreamPicker extends RadioButtonPickerFragment { 32 33 private static final String TAG = "WhenToDreamPicker"; 34 private DreamBackend mBackend; 35 private boolean mDreamsSupportedOnBattery; 36 37 @Override onAttach(Context context)38 public void onAttach(Context context) { 39 super.onAttach(context); 40 41 mBackend = DreamBackend.getInstance(context); 42 mDreamsSupportedOnBattery = getResources().getBoolean( 43 com.android.internal.R.bool.config_dreamsEnabledOnBattery); 44 } 45 46 @Override getPreferenceScreenResId()47 protected int getPreferenceScreenResId() { 48 return R.xml.when_to_dream_settings; 49 } 50 51 @Override getMetricsCategory()52 public int getMetricsCategory() { 53 return SettingsEnums.SETTINGS_WHEN_TO_DREAM; 54 } 55 56 @Override getCandidates()57 protected List<? extends CandidateInfo> getCandidates() { 58 final String[] entries = entries(); 59 final String[] values = keys(); 60 final List<WhenToDreamCandidateInfo> candidates = new ArrayList<>(); 61 62 if (entries == null || entries.length <= 0) return null; 63 if (values == null || values.length != entries.length) { 64 throw new IllegalArgumentException("Entries and values must be of the same length."); 65 } 66 67 for (int i = 0; i < entries.length; i++) { 68 candidates.add(new WhenToDreamCandidateInfo(entries[i], values[i])); 69 } 70 71 return candidates; 72 } 73 entries()74 private String[] entries() { 75 if (mDreamsSupportedOnBattery) { 76 return getResources().getStringArray(R.array.when_to_start_screensaver_entries); 77 } 78 return getResources().getStringArray(R.array.when_to_start_screensaver_entries_no_battery); 79 } 80 keys()81 private String[] keys() { 82 if (mDreamsSupportedOnBattery) { 83 return getResources().getStringArray(R.array.when_to_start_screensaver_values); 84 } 85 return getResources().getStringArray(R.array.when_to_start_screensaver_values_no_battery); 86 } 87 88 @Override getDefaultKey()89 protected String getDefaultKey() { 90 return DreamSettings.getKeyFromSetting(mBackend.getWhenToDreamSetting()); 91 } 92 93 @Override setDefaultKey(String key)94 protected boolean setDefaultKey(String key) { 95 mBackend.setWhenToDream(DreamSettings.getSettingFromPrefKey(key)); 96 return true; 97 } 98 99 @Override onSelectionPerformed(boolean success)100 protected void onSelectionPerformed(boolean success) { 101 super.onSelectionPerformed(success); 102 103 getActivity().finish(); 104 } 105 106 private final class WhenToDreamCandidateInfo extends CandidateInfo { 107 private final String name; 108 private final String key; 109 WhenToDreamCandidateInfo(String title, String value)110 WhenToDreamCandidateInfo(String title, String value) { 111 super(true); 112 113 name = title; 114 key = value; 115 } 116 117 @Override loadLabel()118 public CharSequence loadLabel() { 119 return name; 120 } 121 122 @Override loadIcon()123 public Drawable loadIcon() { 124 return null; 125 } 126 127 @Override getKey()128 public String getKey() { 129 return key; 130 } 131 } 132 } 133