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