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