blob: b267ccd1ec46792ee3a4fab7c0bd438268593be8 [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
John Spurlock2d695812014-10-30 13:25:21 -040034 = new AnimationIcon(R.drawable.ic_portrait_to_auto_rotate_animation);
John Spurlockc6df3cf2014-11-04 19:36:44 -050035 private final AnimationIcon mAutoToPortrait
John Spurlock2d695812014-10-30 13:25:21 -040036 = new AnimationIcon(R.drawable.ic_portrait_from_auto_rotate_animation);
37
John Spurlockc6df3cf2014-11-04 19:36:44 -050038 private final AnimationIcon mLandscapeToAuto
John Spurlock2d695812014-10-30 13:25:21 -040039 = new AnimationIcon(R.drawable.ic_landscape_to_auto_rotate_animation);
John Spurlockc6df3cf2014-11-04 19:36:44 -050040 private final AnimationIcon mAutoToLandscape
John Spurlock2d695812014-10-30 13:25:21 -040041 = new AnimationIcon(R.drawable.ic_landscape_from_auto_rotate_animation);
John Spurlockaf8d6c42014-05-07 17:49:08 -040042
43 private final RotationLockController mController;
44
45 public RotationLockTile(Host host) {
46 super(host);
47 mController = host.getRotationLockController();
John Spurlockaf8d6c42014-05-07 17:49:08 -040048 }
49
50 @Override
Jason Monk62b63a02016-02-02 15:15:31 -050051 public BooleanState newTileState() {
John Spurlockaf8d6c42014-05-07 17:49:08 -040052 return new BooleanState();
53 }
54
John Spurlockccb6b9a2014-05-17 15:54:40 -040055 public void setListening(boolean listening) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040056 if (mController == null) return;
John Spurlockccb6b9a2014-05-17 15:54:40 -040057 if (listening) {
58 mController.addRotationLockControllerCallback(mCallback);
59 } else {
60 mController.removeRotationLockControllerCallback(mCallback);
61 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040062 }
63
64 @Override
Jason Monk76c67aa2016-02-19 14:49:42 -050065 public Intent getLongClickIntent() {
66 return new Intent(Settings.ACTION_DISPLAY_SETTINGS);
67 }
68
69 @Override
John Spurlockaf8d6c42014-05-07 17:49:08 -040070 protected void handleClick() {
71 if (mController == null) return;
Chris Wren9e7283f2015-05-08 17:23:47 -040072 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
John Spurlock2d695812014-10-30 13:25:21 -040073 final boolean newState = !mState.value;
74 mController.setRotationLocked(newState);
Jason Monk66239fb2015-12-21 14:27:00 -050075 refreshState(newState);
John Spurlockaf8d6c42014-05-07 17:49:08 -040076 }
77
78 @Override
79 protected void handleUpdateState(BooleanState state, Object arg) {
80 if (mController == null) return;
Jason Monk66239fb2015-12-21 14:27:00 -050081 final boolean rotationLocked = arg != null ? (Boolean) arg
John Spurlock2d695812014-10-30 13:25:21 -040082 : mController.isRotationLocked();
Jason Monkba2318e2015-12-08 09:04:23 -050083 // TODO: Handle accessibility rotation lock and whatnot.
84// state.visible = mController.isRotationLockAffordanceVisible();
Jason Monk1634d182015-06-15 10:19:26 -040085 if (state.value == rotationLocked && state.contentDescription != null) {
86 // No change and initialized, no need to update all the values.
Jason Monk62154782015-06-11 09:23:16 -040087 return;
88 }
John Spurlock1a462c12014-07-14 10:52:01 -040089 state.value = rotationLocked;
Jason Monk3e189872016-01-12 09:10:34 -050090 final boolean portrait = isCurrentOrientationLockPortrait(mController, mContext);
John Spurlockaf8d6c42014-05-07 17:49:08 -040091 if (rotationLocked) {
John Spurlock1a462c12014-07-14 10:52:01 -040092 final int label = portrait ? R.string.quick_settings_rotation_locked_portrait_label
93 : R.string.quick_settings_rotation_locked_landscape_label;
John Spurlockaf8d6c42014-05-07 17:49:08 -040094 state.label = mContext.getString(label);
Jason Monk66239fb2015-12-21 14:27:00 -050095 state.icon = portrait ? mAutoToPortrait : mAutoToLandscape;
John Spurlockaf8d6c42014-05-07 17:49:08 -040096 } else {
John Spurlockaf8d6c42014-05-07 17:49:08 -040097 state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label);
Jason Monk66239fb2015-12-21 14:27:00 -050098 state.icon = portrait ? mPortraitToAuto : mLandscapeToAuto;
John Spurlockaf8d6c42014-05-07 17:49:08 -040099 }
John Spurlock2d695812014-10-30 13:25:21 -0400100 state.contentDescription = getAccessibilityString(rotationLocked,
Selim Cinek4fda7b22014-08-18 22:07:25 +0200101 R.string.accessibility_rotation_lock_on_portrait,
102 R.string.accessibility_rotation_lock_on_landscape,
103 R.string.accessibility_rotation_lock_off);
104 }
105
Jason Monk3e189872016-01-12 09:10:34 -0500106 public static boolean isCurrentOrientationLockPortrait(RotationLockController controller,
107 Context context) {
108 int lockOrientation = controller.getRotationLockOrientation();
Adrian Roos26098832015-07-14 16:41:57 -0700109 if (lockOrientation == Configuration.ORIENTATION_UNDEFINED) {
110 // Freely rotating device; use current rotation
Jason Monk3e189872016-01-12 09:10:34 -0500111 return context.getResources().getConfiguration().orientation
Adrian Roos26098832015-07-14 16:41:57 -0700112 != Configuration.ORIENTATION_LANDSCAPE;
113 } else {
114 return lockOrientation != Configuration.ORIENTATION_LANDSCAPE;
115 }
116 }
117
Chris Wren457a21c2015-05-06 17:50:34 -0400118 @Override
119 public int getMetricsCategory() {
Chris Wrenf6e9228b2016-01-26 18:04:35 -0500120 return MetricsEvent.QS_ROTATIONLOCK;
Chris Wren457a21c2015-05-06 17:50:34 -0400121 }
122
Selim Cinek4fda7b22014-08-18 22:07:25 +0200123 /**
124 * Get the correct accessibility string based on the state
125 *
John Spurlock2d695812014-10-30 13:25:21 -0400126 * @param locked Whether or not rotation is locked.
Selim Cinek4fda7b22014-08-18 22:07:25 +0200127 * @param idWhenPortrait The id which should be used when locked in portrait.
128 * @param idWhenLandscape The id which should be used when locked in landscape.
129 * @param idWhenOff The id which should be used when the rotation lock is off.
130 * @return
131 */
John Spurlock2d695812014-10-30 13:25:21 -0400132 private String getAccessibilityString(boolean locked, int idWhenPortrait, int idWhenLandscape,
133 int idWhenOff) {
Selim Cinek4fda7b22014-08-18 22:07:25 +0200134 int stringID;
John Spurlock2d695812014-10-30 13:25:21 -0400135 if (locked) {
Jason Monk3e189872016-01-12 09:10:34 -0500136 stringID = isCurrentOrientationLockPortrait(mController, mContext) ? idWhenPortrait
137 : idWhenLandscape;
Selim Cinek4fda7b22014-08-18 22:07:25 +0200138 } else {
139 stringID = idWhenOff;
140 }
141 return mContext.getString(stringID);
142 }
143
144 @Override
145 protected String composeChangeAnnouncement() {
John Spurlock2d695812014-10-30 13:25:21 -0400146 return getAccessibilityString(mState.value,
Selim Cinek4fda7b22014-08-18 22:07:25 +0200147 R.string.accessibility_rotation_lock_on_portrait_changed,
148 R.string.accessibility_rotation_lock_on_landscape_changed,
149 R.string.accessibility_rotation_lock_off_changed);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400150 }
151
152 private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() {
153 @Override
154 public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) {
Jason Monk66239fb2015-12-21 14:27:00 -0500155 refreshState(rotationLocked);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400156 }
157 };
158}