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