• 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.content.Context;
20 
21 import androidx.preference.Preference;
22 
23 import com.android.settings.Utils;
24 import com.android.settings.core.BasePreferenceController;
25 import com.android.settings.widget.GearPreference;
26 import com.android.settingslib.RestrictedPreference;
27 import com.android.settingslib.dream.DreamBackend;
28 import com.android.settingslib.dream.DreamBackend.DreamInfo;
29 
30 import java.util.Optional;
31 
32 public class CurrentDreamPreferenceController extends BasePreferenceController {
33 
34     private final DreamBackend mBackend;
35 
CurrentDreamPreferenceController(Context context, String key)36     public CurrentDreamPreferenceController(Context context, String key) {
37         super(context, key);
38         mBackend = DreamBackend.getInstance(context);
39     }
40 
41     @Override
getAvailabilityStatus()42     public int getAvailabilityStatus() {
43         return mBackend.getDreamInfos().size() > 0 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
44     }
45 
46     @Override
updateState(Preference preference)47     public void updateState(Preference preference) {
48         super.updateState(preference);
49         setGearClickListenerForPreference(preference);
50         setActiveDreamIcon(preference);
51     }
52 
53     @Override
getSummary()54     public CharSequence getSummary() {
55         return mBackend.getActiveDreamName();
56     }
57 
setGearClickListenerForPreference(Preference preference)58     private void setGearClickListenerForPreference(Preference preference) {
59         if (!(preference instanceof GearPreference)) {
60             return;
61         }
62 
63         final GearPreference gearPreference = (GearPreference) preference;
64         final Optional<DreamInfo> info = getActiveDreamInfo();
65         if (!info.isPresent() || info.get().settingsComponentName == null) {
66             gearPreference.setOnGearClickListener(null);
67             return;
68         }
69         gearPreference.setOnGearClickListener(gearPref -> launchScreenSaverSettings());
70     }
71 
launchScreenSaverSettings()72     private void launchScreenSaverSettings() {
73         final Optional<DreamInfo> info = getActiveDreamInfo();
74         if (!info.isPresent()) return;
75         mBackend.launchSettings(mContext, info.get());
76     }
77 
getActiveDreamInfo()78     private Optional<DreamInfo> getActiveDreamInfo() {
79         return mBackend.getDreamInfos()
80                 .stream()
81                 .filter((info) -> info.isActive)
82                 .findFirst();
83     }
84 
setActiveDreamIcon(Preference preference)85     private void setActiveDreamIcon(Preference preference) {
86         if (!(preference instanceof GearPreference)) {
87             return;
88         }
89         final GearPreference gearPref = (GearPreference) preference;
90         gearPref.setIconSize(RestrictedPreference.ICON_SIZE_SMALL);
91         gearPref.setIcon(Utils.getSafeIcon(mBackend.getActiveIcon()));
92     }
93 }
94