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