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