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 import android.support.v7.preference.Preference; 21 import com.android.settings.core.PreferenceControllerMixin; 22 import com.android.settings.widget.GearPreference; 23 import com.android.settingslib.core.AbstractPreferenceController; 24 import com.android.settingslib.dream.DreamBackend; 25 import com.android.settingslib.dream.DreamBackend.DreamInfo; 26 import java.util.Optional; 27 28 public class CurrentDreamPreferenceController extends AbstractPreferenceController implements 29 PreferenceControllerMixin { 30 private final DreamBackend mBackend; 31 private final static String TAG = "CurrentDreamPreferenceController"; 32 private final static String CURRENT_SCREENSAVER = "current_screensaver"; 33 CurrentDreamPreferenceController(Context context)34 public CurrentDreamPreferenceController(Context context) { 35 super(context); 36 mBackend = DreamBackend.getInstance(context); 37 } 38 39 @Override isAvailable()40 public boolean isAvailable() { 41 return mBackend.getDreamInfos().size() > 0; 42 } 43 44 @Override getPreferenceKey()45 public String getPreferenceKey() { 46 return CURRENT_SCREENSAVER; 47 } 48 49 @Override updateState(Preference preference)50 public void updateState(Preference preference) { 51 super.updateState(preference); 52 53 preference.setSummary(mBackend.getActiveDreamName()); 54 setGearClickListenerForPreference(preference); 55 } 56 setGearClickListenerForPreference(Preference preference)57 private void setGearClickListenerForPreference(Preference preference) { 58 if (!(preference instanceof GearPreference)) return; 59 60 GearPreference gearPreference = (GearPreference)preference; 61 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 Optional<DreamInfo> info = getActiveDreamInfo(); 71 if (!info.isPresent()) return; 72 mBackend.launchSettings(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