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; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 24 | import android.service.quicksettings.Tile; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 25 | import android.widget.Switch; |
| 26 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 27 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 28 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 29 | import com.android.systemui.Dependency; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | import com.android.systemui.R; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 31 | import com.android.systemui.qs.QSHost; |
| 32 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
| 33 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 34 | import com.android.systemui.statusbar.policy.RotationLockController; |
| 35 | import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback; |
| 36 | |
| 37 | /** Quick settings tile: Rotation **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 38 | public class RotationLockTile extends QSTileImpl<BooleanState> { |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 39 | private final AnimationIcon mPortraitToAuto |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 40 | = new AnimationIcon(R.drawable.ic_portrait_to_auto_rotate_animation, |
| 41 | R.drawable.ic_portrait_from_auto_rotate); |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 42 | private final AnimationIcon mAutoToPortrait |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 43 | = new AnimationIcon(R.drawable.ic_portrait_from_auto_rotate_animation, |
| 44 | R.drawable.ic_portrait_to_auto_rotate); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 45 | |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 46 | private final AnimationIcon mLandscapeToAuto |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 47 | = new AnimationIcon(R.drawable.ic_landscape_to_auto_rotate_animation, |
| 48 | R.drawable.ic_landscape_from_auto_rotate); |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 49 | private final AnimationIcon mAutoToLandscape |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 50 | = new AnimationIcon(R.drawable.ic_landscape_from_auto_rotate_animation, |
| 51 | R.drawable.ic_landscape_to_auto_rotate); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | |
| 53 | private final RotationLockController mController; |
| 54 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 55 | public RotationLockTile(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 56 | super(host); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 57 | mController = Dependency.get(RotationLockController.class); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 61 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | return new BooleanState(); |
| 63 | } |
| 64 | |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 65 | public void handleSetListening(boolean listening) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | if (mController == null) return; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 67 | if (listening) { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 68 | mController.addCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 69 | } else { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 70 | mController.removeCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 71 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 75 | public Intent getLongClickIntent() { |
| 76 | return new Intent(Settings.ACTION_DISPLAY_SETTINGS); |
| 77 | } |
| 78 | |
| 79 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 80 | protected void handleClick() { |
| 81 | if (mController == null) return; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 82 | final boolean newState = !mState.value; |
Julia Reynolds | 68221f5 | 2016-06-14 16:47:53 -0400 | [diff] [blame] | 83 | mController.setRotationLocked(!newState); |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 84 | refreshState(newState); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 88 | public CharSequence getTileLabel() { |
| 89 | return getState().label; |
| 90 | } |
| 91 | |
| 92 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 93 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 94 | if (mController == null) return; |
Julia Reynolds | 68221f5 | 2016-06-14 16:47:53 -0400 | [diff] [blame] | 95 | final boolean rotationLocked = mController.isRotationLocked(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 96 | // TODO: Handle accessibility rotation lock and whatnot. |
Julia Reynolds | 68221f5 | 2016-06-14 16:47:53 -0400 | [diff] [blame] | 97 | |
| 98 | state.value = !rotationLocked; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 99 | final boolean portrait = isCurrentOrientationLockPortrait(mController, mContext); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 100 | if (rotationLocked) { |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 101 | final int label = portrait ? R.string.quick_settings_rotation_locked_portrait_label |
| 102 | : R.string.quick_settings_rotation_locked_landscape_label; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 103 | state.label = mContext.getString(label); |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 104 | state.icon = portrait ? mAutoToPortrait : mAutoToLandscape; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 105 | } else { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 106 | state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label); |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 107 | state.icon = portrait ? mPortraitToAuto : mLandscapeToAuto; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 108 | } |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 109 | state.contentDescription = getAccessibilityString(rotationLocked); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 110 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 111 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 114 | public static boolean isCurrentOrientationLockPortrait(RotationLockController controller, |
| 115 | Context context) { |
| 116 | int lockOrientation = controller.getRotationLockOrientation(); |
Adrian Roos | 2609883 | 2015-07-14 16:41:57 -0700 | [diff] [blame] | 117 | if (lockOrientation == Configuration.ORIENTATION_UNDEFINED) { |
| 118 | // Freely rotating device; use current rotation |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 119 | return context.getResources().getConfiguration().orientation |
Adrian Roos | 2609883 | 2015-07-14 16:41:57 -0700 | [diff] [blame] | 120 | != Configuration.ORIENTATION_LANDSCAPE; |
| 121 | } else { |
| 122 | return lockOrientation != Configuration.ORIENTATION_LANDSCAPE; |
| 123 | } |
| 124 | } |
| 125 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 126 | @Override |
| 127 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 128 | return MetricsEvent.QS_ROTATIONLOCK; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 131 | /** |
| 132 | * Get the correct accessibility string based on the state |
| 133 | * |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 134 | * @param locked Whether or not rotation is locked. |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 135 | */ |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 136 | private String getAccessibilityString(boolean locked) { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 137 | if (locked) { |
Jason Monk | 92de719 | 2017-03-23 10:05:18 -0400 | [diff] [blame] | 138 | return mContext.getString(R.string.accessibility_quick_settings_rotation_value, |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 139 | isCurrentOrientationLockPortrait(mController, mContext) |
| 140 | ? mContext.getString( |
| 141 | R.string.quick_settings_rotation_locked_portrait_label) |
| 142 | : mContext.getString( |
Jason Monk | 92de719 | 2017-03-23 10:05:18 -0400 | [diff] [blame] | 143 | R.string.quick_settings_rotation_locked_landscape_label)) |
| 144 | + "," + mContext.getString(R.string.accessibility_quick_settings_rotation); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 145 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 146 | } else { |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 147 | return mContext.getString(R.string.accessibility_quick_settings_rotation); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 148 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected String composeChangeAnnouncement() { |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 153 | return getAccessibilityString(mState.value); |
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 | } |