1/* 2 * Copyright (c) 2016, 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 17package com.android.systemui.qs.tiles; 18 19import android.app.ActivityManager; 20import android.content.Intent; 21import android.provider.Settings; 22import android.widget.Switch; 23 24import com.android.internal.app.NightDisplayController; 25import com.android.internal.logging.MetricsLogger; 26import com.android.internal.logging.MetricsProto.MetricsEvent; 27import com.android.systemui.R; 28import com.android.systemui.qs.QSTile; 29 30public class NightDisplayTile extends QSTile<QSTile.BooleanState> 31 implements NightDisplayController.Callback { 32 33 private NightDisplayController mController; 34 private boolean mIsListening; 35 36 public NightDisplayTile(Host host) { 37 super(host); 38 mController = new NightDisplayController(mContext, ActivityManager.getCurrentUser()); 39 } 40 41 @Override 42 public boolean isAvailable() { 43 return NightDisplayController.isAvailable(mContext); 44 } 45 46 @Override 47 public BooleanState newTileState() { 48 return new BooleanState(); 49 } 50 51 @Override 52 protected void handleClick() { 53 final boolean activated = !mState.value; 54 MetricsLogger.action(mContext, getMetricsCategory(), activated); 55 mController.setActivated(activated); 56 } 57 58 @Override 59 protected void handleUserSwitch(int newUserId) { 60 // Stop listening to the old controller. 61 if (mIsListening) { 62 mController.setListener(null); 63 } 64 65 // Make a new controller for the new user. 66 mController = new NightDisplayController(mContext, newUserId); 67 if (mIsListening) { 68 mController.setListener(this); 69 } 70 71 super.handleUserSwitch(newUserId); 72 } 73 74 @Override 75 protected void handleUpdateState(BooleanState state, Object arg) { 76 final boolean isActivated = mController.isActivated(); 77 state.value = isActivated; 78 state.label = mContext.getString(R.string.quick_settings_night_display_label); 79 state.icon = ResourceIcon.get(isActivated ? R.drawable.ic_qs_night_display_on 80 : R.drawable.ic_qs_night_display_off); 81 state.contentDescription = mContext.getString(isActivated 82 ? R.string.quick_settings_night_display_summary_on 83 : R.string.quick_settings_night_display_summary_off); 84 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName 85 = Switch.class.getName(); 86 } 87 88 @Override 89 public int getMetricsCategory() { 90 return MetricsEvent.QS_NIGHT_DISPLAY; 91 } 92 93 @Override 94 public Intent getLongClickIntent() { 95 return new Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS); 96 } 97 98 @Override 99 protected void setListening(boolean listening) { 100 mIsListening = listening; 101 if (listening) { 102 mController.setListener(this); 103 refreshState(); 104 } else { 105 mController.setListener(null); 106 } 107 } 108 109 @Override 110 public CharSequence getTileLabel() { 111 return mContext.getString(R.string.quick_settings_night_display_label); 112 } 113 114 @Override 115 public void onActivated(boolean activated) { 116 refreshState(); 117 } 118} 119