• 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.ComponentName;
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 
24 import com.android.settings.R;
25 import com.android.settings.widget.RadioButtonPickerFragment;
26 import com.android.settingslib.dream.DreamBackend;
27 import com.android.settingslib.dream.DreamBackend.DreamInfo;
28 import com.android.settingslib.widget.CandidateInfo;
29 
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.stream.Collectors;
34 
35 public final class CurrentDreamPicker extends RadioButtonPickerFragment {
36 
37     private DreamBackend mBackend;
38 
39     @Override
onAttach(Context context)40     public void onAttach(Context context) {
41         super.onAttach(context);
42 
43         mBackend = DreamBackend.getInstance(context);
44     }
45 
46     @Override
getPreferenceScreenResId()47     protected int getPreferenceScreenResId() {
48         return R.xml.current_dream_settings;
49     }
50 
51     @Override
getMetricsCategory()52     public int getMetricsCategory() {
53         return SettingsEnums.DREAM;
54     }
55 
56     @Override
setDefaultKey(String key)57     protected boolean setDefaultKey(String key) {
58         Map<String, ComponentName> componentNameMap = getDreamComponentsMap();
59         if (componentNameMap.get(key) != null) {
60             mBackend.setActiveDream(componentNameMap.get(key));
61             return true;
62         }
63         return false;
64     }
65 
66     @Override
getDefaultKey()67     protected String getDefaultKey() {
68         return mBackend.getActiveDream().flattenToString();
69     }
70 
71     @Override
getCandidates()72     protected List<? extends CandidateInfo> getCandidates() {
73         final List<DreamCandidateInfo> candidates;
74         candidates = mBackend.getDreamInfos().stream()
75                 .map(DreamCandidateInfo::new)
76                 .collect(Collectors.toList());
77 
78         return candidates;
79     }
80 
81     @Override
onSelectionPerformed(boolean success)82     protected void onSelectionPerformed(boolean success) {
83         super.onSelectionPerformed(success);
84 
85         getActivity().finish();
86     }
87 
getDreamComponentsMap()88     private Map<String, ComponentName> getDreamComponentsMap() {
89         Map<String, ComponentName> comps = new HashMap<>();
90         mBackend.getDreamInfos()
91                 .forEach((info) ->
92                         comps.put(info.componentName.flattenToString(), info.componentName));
93 
94         return comps;
95     }
96 
97     private static final class DreamCandidateInfo extends CandidateInfo {
98         private final CharSequence name;
99         private final Drawable icon;
100         private final String key;
101 
DreamCandidateInfo(DreamInfo info)102         DreamCandidateInfo(DreamInfo info) {
103             super(true);
104 
105             name = info.caption;
106             icon = info.icon;
107             key = info.componentName.flattenToString();
108         }
109 
110         @Override
loadLabel()111         public CharSequence loadLabel() {
112             return name;
113         }
114 
115         @Override
loadIcon()116         public Drawable loadIcon() {
117             return icon;
118         }
119 
120         @Override
getKey()121         public String getKey() {
122             return key;
123         }
124     }
125 }
126