1 /* 2 * Copyright (C) 2018 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.homepage.contextualcards.conditional; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.hardware.display.ColorDisplayManager; 22 import android.hardware.display.NightDisplayListener; 23 24 import com.android.settings.R; 25 import com.android.settings.core.SubSettingLauncher; 26 import com.android.settings.display.NightDisplaySettings; 27 import com.android.settings.homepage.contextualcards.ContextualCard; 28 29 import java.util.Objects; 30 31 public class NightDisplayConditionController implements ConditionalCardController, 32 NightDisplayListener.Callback { 33 34 static final int ID = Objects.hash("NightDisplayConditionController"); 35 36 private final Context mAppContext; 37 private final ConditionManager mConditionManager; 38 private final ColorDisplayManager mColorDisplayManager; 39 private final NightDisplayListener mNightDisplayListener; 40 NightDisplayConditionController(Context appContext, ConditionManager manager)41 public NightDisplayConditionController(Context appContext, ConditionManager manager) { 42 mAppContext = appContext; 43 mConditionManager = manager; 44 mColorDisplayManager = appContext.getSystemService(ColorDisplayManager.class); 45 mNightDisplayListener = new NightDisplayListener(appContext); 46 } 47 48 @Override getId()49 public long getId() { 50 return ID; 51 } 52 53 @Override isDisplayable()54 public boolean isDisplayable() { 55 return mColorDisplayManager.isNightDisplayActivated(); 56 } 57 58 @Override onPrimaryClick(Context context)59 public void onPrimaryClick(Context context) { 60 new SubSettingLauncher(context) 61 .setDestination(NightDisplaySettings.class.getName()) 62 .setSourceMetricsCategory(SettingsEnums.SETTINGS_HOMEPAGE) 63 .setTitleRes(R.string.night_display_title) 64 .launch(); 65 } 66 67 @Override onActionClick()68 public void onActionClick() { 69 mColorDisplayManager.setNightDisplayActivated(false); 70 } 71 72 @Override buildContextualCard()73 public ContextualCard buildContextualCard() { 74 return new ConditionalContextualCard.Builder() 75 .setConditionId(ID) 76 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_NIGHT_DISPLAY) 77 .setActionText(mAppContext.getText(R.string.condition_turn_off)) 78 .setName(mAppContext.getPackageName() + "/" 79 + mAppContext.getText(R.string.condition_night_display_title)) 80 .setTitleText(mAppContext.getText( 81 R.string.condition_night_display_title).toString()) 82 .setSummaryText( 83 mAppContext.getText(R.string.condition_night_display_summary).toString()) 84 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_settings_night_display)) 85 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH) 86 .build(); 87 } 88 89 @Override startMonitoringStateChange()90 public void startMonitoringStateChange() { 91 mNightDisplayListener.setCallback(this); 92 } 93 94 @Override stopMonitoringStateChange()95 public void stopMonitoringStateChange() { 96 mNightDisplayListener.setCallback(null); 97 } 98 99 @Override onActivated(boolean activated)100 public void onActivated(boolean activated) { 101 mConditionManager.onConditionChanged(); 102 } 103 } 104