John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 19 | import android.content.Context; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 20 | import android.content.Intent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 22 | |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 23 | import android.provider.Settings; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 24 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 25 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 26 | import com.android.systemui.R; |
| 27 | import com.android.systemui.qs.QSTile; |
| 28 | import com.android.systemui.statusbar.policy.RotationLockController; |
| 29 | import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback; |
| 30 | |
| 31 | /** Quick settings tile: Rotation **/ |
| 32 | public class RotationLockTile extends QSTile<QSTile.BooleanState> { |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 33 | private final AnimationIcon mPortraitToAuto |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 34 | = new AnimationIcon(R.drawable.ic_portrait_to_auto_rotate_animation, |
| 35 | R.drawable.ic_portrait_from_auto_rotate); |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 36 | private final AnimationIcon mAutoToPortrait |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 37 | = new AnimationIcon(R.drawable.ic_portrait_from_auto_rotate_animation, |
| 38 | R.drawable.ic_portrait_to_auto_rotate); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 39 | |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 40 | private final AnimationIcon mLandscapeToAuto |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 41 | = new AnimationIcon(R.drawable.ic_landscape_to_auto_rotate_animation, |
| 42 | R.drawable.ic_landscape_from_auto_rotate); |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 43 | private final AnimationIcon mAutoToLandscape |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 44 | = new AnimationIcon(R.drawable.ic_landscape_from_auto_rotate_animation, |
| 45 | R.drawable.ic_landscape_to_auto_rotate); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 46 | |
| 47 | private final RotationLockController mController; |
| 48 | |
| 49 | public RotationLockTile(Host host) { |
| 50 | super(host); |
| 51 | mController = host.getRotationLockController(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 55 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 56 | return new BooleanState(); |
| 57 | } |
| 58 | |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 59 | public void setListening(boolean listening) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | if (mController == null) return; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 61 | if (listening) { |
| 62 | mController.addRotationLockControllerCallback(mCallback); |
| 63 | } else { |
| 64 | mController.removeRotationLockControllerCallback(mCallback); |
| 65 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 69 | public Intent getLongClickIntent() { |
| 70 | return new Intent(Settings.ACTION_DISPLAY_SETTINGS); |
| 71 | } |
| 72 | |
| 73 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | protected void handleClick() { |
| 75 | if (mController == null) return; |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 76 | MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 77 | final boolean newState = !mState.value; |
| 78 | mController.setRotationLocked(newState); |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 79 | refreshState(newState); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 84 | if (mController == null) return; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 85 | final boolean rotationLocked = arg != null ? (Boolean) arg |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 86 | : mController.isRotationLocked(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 87 | // TODO: Handle accessibility rotation lock and whatnot. |
| 88 | // state.visible = mController.isRotationLockAffordanceVisible(); |
Jason Monk | 1634d18 | 2015-06-15 10:19:26 -0400 | [diff] [blame] | 89 | if (state.value == rotationLocked && state.contentDescription != null) { |
| 90 | // No change and initialized, no need to update all the values. |
Jason Monk | 6215478 | 2015-06-11 09:23:16 -0400 | [diff] [blame] | 91 | return; |
| 92 | } |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 93 | state.value = rotationLocked; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 94 | final boolean portrait = isCurrentOrientationLockPortrait(mController, mContext); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 95 | if (rotationLocked) { |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 96 | final int label = portrait ? R.string.quick_settings_rotation_locked_portrait_label |
| 97 | : R.string.quick_settings_rotation_locked_landscape_label; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 98 | state.label = mContext.getString(label); |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 99 | state.icon = portrait ? mAutoToPortrait : mAutoToLandscape; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 100 | } else { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 101 | state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label); |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 102 | state.icon = portrait ? mPortraitToAuto : mLandscapeToAuto; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 103 | } |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 104 | state.contentDescription = getAccessibilityString(rotationLocked, |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 105 | 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 Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 110 | public static boolean isCurrentOrientationLockPortrait(RotationLockController controller, |
| 111 | Context context) { |
| 112 | int lockOrientation = controller.getRotationLockOrientation(); |
Adrian Roos | 2609883 | 2015-07-14 16:41:57 -0700 | [diff] [blame] | 113 | if (lockOrientation == Configuration.ORIENTATION_UNDEFINED) { |
| 114 | // Freely rotating device; use current rotation |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 115 | return context.getResources().getConfiguration().orientation |
Adrian Roos | 2609883 | 2015-07-14 16:41:57 -0700 | [diff] [blame] | 116 | != Configuration.ORIENTATION_LANDSCAPE; |
| 117 | } else { |
| 118 | return lockOrientation != Configuration.ORIENTATION_LANDSCAPE; |
| 119 | } |
| 120 | } |
| 121 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 122 | @Override |
| 123 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 124 | return MetricsEvent.QS_ROTATIONLOCK; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 127 | /** |
| 128 | * Get the correct accessibility string based on the state |
| 129 | * |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 130 | * @param locked Whether or not rotation is locked. |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 131 | * @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 Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 136 | private String getAccessibilityString(boolean locked, int idWhenPortrait, int idWhenLandscape, |
| 137 | int idWhenOff) { |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 138 | int stringID; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 139 | if (locked) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 140 | stringID = isCurrentOrientationLockPortrait(mController, mContext) ? idWhenPortrait |
| 141 | : idWhenLandscape; |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 142 | } else { |
| 143 | stringID = idWhenOff; |
| 144 | } |
| 145 | return mContext.getString(stringID); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | protected String composeChangeAnnouncement() { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 150 | return getAccessibilityString(mState.value, |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 151 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() { |
| 157 | @Override |
| 158 | public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 159 | refreshState(rotationLocked); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 160 | } |
| 161 | }; |
| 162 | } |