blob: d80ca104f59ba772525b92f140c7528caeff76bf [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
2 * Copyright (C) 2014 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
Jason Monk3e189872016-01-12 09:10:34 -050019import android.content.Context;
Jason Monk76c67aa2016-02-19 14:49:42 -050020import android.content.Intent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040021import android.content.res.Configuration;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050022
Jason Monk76c67aa2016-02-19 14:49:42 -050023import android.provider.Settings;
Chris Wren457a21c2015-05-06 17:50:34 -040024import com.android.internal.logging.MetricsLogger;
Chris Wrenf6e9228b2016-01-26 18:04:35 -050025import com.android.internal.logging.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026import com.android.systemui.R;
27import com.android.systemui.qs.QSTile;
28import com.android.systemui.statusbar.policy.RotationLockController;
29import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback;
30
31/** Quick settings tile: Rotation **/
32public class RotationLockTile extends QSTile<QSTile.BooleanState> {
John Spurlockc6df3cf2014-11-04 19:36:44 -050033 private final AnimationIcon mPortraitToAuto
Jason Monk1aec93f2016-03-01 09:39:30 -050034 = new AnimationIcon(R.drawable.ic_portrait_to_auto_rotate_animation,
35 R.drawable.ic_portrait_from_auto_rotate);
John Spurlockc6df3cf2014-11-04 19:36:44 -050036 private final AnimationIcon mAutoToPortrait
Jason Monk1aec93f2016-03-01 09:39:30 -050037 = new AnimationIcon(R.drawable.ic_portrait_from_auto_rotate_animation,
38 R.drawable.ic_portrait_to_auto_rotate);
John Spurlock2d695812014-10-30 13:25:21 -040039
John Spurlockc6df3cf2014-11-04 19:36:44 -050040 private final AnimationIcon mLandscapeToAuto
Jason Monk1aec93f2016-03-01 09:39:30 -050041 = new AnimationIcon(R.drawable.ic_landscape_to_auto_rotate_animation,
42 R.drawable.ic_landscape_from_auto_rotate);
John Spurlockc6df3cf2014-11-04 19:36:44 -050043 private final AnimationIcon mAutoToLandscape
Jason Monk1aec93f2016-03-01 09:39:30 -050044 = new AnimationIcon(R.drawable.ic_landscape_from_auto_rotate_animation,
45 R.drawable.ic_landscape_to_auto_rotate);
John Spurlockaf8d6c42014-05-07 17:49:08 -040046
47 private final RotationLockController mController;
48
49 public RotationLockTile(Host host) {
50 super(host);
51 mController = host.getRotationLockController();
John Spurlockaf8d6c42014-05-07 17:49:08 -040052 }
53
54 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050055 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040056 return new BooleanState();
57 }
58
John Spurlockccb6b9a2014-05-17 15:54:40 -040059 public void setListening(boolean listening) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040060 if (mController == null) return;
John Spurlockccb6b9a2014-05-17 15:54:40 -040061 if (listening) {
62 mController.addRotationLockControllerCallback(mCallback);
63 } else {
64 mController.removeRotationLockControllerCallback(mCallback);
65 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040066 }
67
68 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050069 public Intent getLongClickIntent() {
70 return new Intent(Settings.ACTION_DISPLAY_SETTINGS);
71 }
72
73 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040074 protected void handleClick() {
75 if (mController == null) return;
Chris Wren9e7283f2015-05-08 17:23:47 -040076 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
John Spurlock2d695812014-10-30 13:25:21 -040077 final boolean newState = !mState.value;
78 mController.setRotationLocked(newState);
Jason Monk66239fb2015-12-21 14:27:00 -050079 refreshState(newState);
John Spurlockaf8d6c42014-05-07 17:49:08 -040080 }
81
82 @Override
83 protected void handleUpdateState(BooleanState state, Object arg) {
84 if (mController == null) return;
Jason Monk66239fb2015-12-21 14:27:00 -050085 final boolean rotationLocked = arg != null ? (Boolean) arg
John Spurlock2d695812014-10-30 13:25:21 -040086 : mController.isRotationLocked();
Jason Monkba2318e2015-12-08 09:04:23 -050087 // TODO: Handle accessibility rotation lock and whatnot.
88// state.visible = mController.isRotationLockAffordanceVisible();
Jason Monk1634d182015-06-15 10:19:26 -040089 if (state.value == rotationLocked && state.contentDescription != null) {
90 // No change and initialized, no need to update all the values.
Jason Monk62154782015-06-11 09:23:16 -040091 return;
92 }
John Spurlock1a462c12014-07-14 10:52:01 -040093 state.value = rotationLocked;
Jason Monk3e189872016-01-12 09:10:34 -050094 final boolean portrait = isCurrentOrientationLockPortrait(mController, mContext);
John Spurlockaf8d6c42014-05-07 17:49:08 -040095 if (rotationLocked) {
John Spurlock1a462c12014-07-14 10:52:01 -040096 final int label = portrait ? R.string.quick_settings_rotation_locked_portrait_label
97 : R.string.quick_settings_rotation_locked_landscape_label;
John Spurlockaf8d6c42014-05-07 17:49:08 -040098 state.label = mContext.getString(label);
Jason Monk66239fb2015-12-21 14:27:00 -050099 state.icon = portrait ? mAutoToPortrait : mAutoToLandscape;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400100 } else {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400101 state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label);
Jason Monk66239fb2015-12-21 14:27:00 -0500102 state.icon = portrait ? mPortraitToAuto : mLandscapeToAuto;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400103 }
John Spurlock2d695812014-10-30 13:25:21 -0400104 state.contentDescription = getAccessibilityString(rotationLocked,
Selim Cinek4fda7b22014-08-18 22:07:25 +0200105 R.string.accessibility_rotation_lock_on_portrait,
106 R.string.accessibility_rotation_lock_on_landscape,
107 R.string.accessibility_rotation_lock_off);
108 }
109
Jason Monk3e189872016-01-12 09:10:34 -0500110 public static boolean isCurrentOrientationLockPortrait(RotationLockController controller,
111 Context context) {
112 int lockOrientation = controller.getRotationLockOrientation();
Adrian Roos26098832015-07-14 16:41:57 -0700113 if (lockOrientation == Configuration.ORIENTATION_UNDEFINED) {
114 // Freely rotating device; use current rotation
Jason Monk3e189872016-01-12 09:10:34 -0500115 return context.getResources().getConfiguration().orientation
Adrian Roos26098832015-07-14 16:41:57 -0700116 != Configuration.ORIENTATION_LANDSCAPE;
117 } else {
118 return lockOrientation != Configuration.ORIENTATION_LANDSCAPE;
119 }
120 }
121
Chris Wren457a21c2015-05-06 17:50:34 -0400122 @Override
123 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500124 return MetricsEvent.QS_ROTATIONLOCK;
Chris Wren457a21c2015-05-06 17:50:34 -0400125 }
126
Selim Cinek4fda7b22014-08-18 22:07:25 +0200127 /**
128 * Get the correct accessibility string based on the state
129 *
John Spurlock2d695812014-10-30 13:25:21 -0400130 * @param locked Whether or not rotation is locked.
Selim Cinek4fda7b22014-08-18 22:07:25 +0200131 * @param idWhenPortrait The id which should be used when locked in portrait.
132 * @param idWhenLandscape The id which should be used when locked in landscape.
133 * @param idWhenOff The id which should be used when the rotation lock is off.
134 * @return
135 */
John Spurlock2d695812014-10-30 13:25:21 -0400136 private String getAccessibilityString(boolean locked, int idWhenPortrait, int idWhenLandscape,
137 int idWhenOff) {
Selim Cinek4fda7b22014-08-18 22:07:25 +0200138 int stringID;
John Spurlock2d695812014-10-30 13:25:21 -0400139 if (locked) {
Jason Monk3e189872016-01-12 09:10:34 -0500140 stringID = isCurrentOrientationLockPortrait(mController, mContext) ? idWhenPortrait
141 : idWhenLandscape;
Selim Cinek4fda7b22014-08-18 22:07:25 +0200142 } else {
143 stringID = idWhenOff;
144 }
145 return mContext.getString(stringID);
146 }
147
148 @Override
149 protected String composeChangeAnnouncement() {
John Spurlock2d695812014-10-30 13:25:21 -0400150 return getAccessibilityString(mState.value,
Selim Cinek4fda7b22014-08-18 22:07:25 +0200151 R.string.accessibility_rotation_lock_on_portrait_changed,
152 R.string.accessibility_rotation_lock_on_landscape_changed,
153 R.string.accessibility_rotation_lock_off_changed);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400154 }
155
156 private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() {
157 @Override
158 public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) {
Jason Monk66239fb2015-12-21 14:27:00 -0500159 refreshState(rotationLocked);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400160 }
161 };
162}