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