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