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 19 import android.content.res.Configuration; 20 21 import com.android.systemui.R; 22 import com.android.systemui.qs.QSTile; 23 import com.android.systemui.statusbar.policy.RotationLockController; 24 import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback; 25 26 /** Quick settings tile: Rotation **/ 27 public class RotationLockTile extends QSTile<QSTile.BooleanState> { 28 private final AnimationIcon mPortraitToAuto 29 = new AnimationIcon(R.drawable.ic_portrait_to_auto_rotate_animation); 30 private final AnimationIcon mAutoToPortrait 31 = new AnimationIcon(R.drawable.ic_portrait_from_auto_rotate_animation); 32 33 private final AnimationIcon mLandscapeToAuto 34 = new AnimationIcon(R.drawable.ic_landscape_to_auto_rotate_animation); 35 private final AnimationIcon mAutoToLandscape 36 = new AnimationIcon(R.drawable.ic_landscape_from_auto_rotate_animation); 37 38 private final RotationLockController mController; 39 40 public RotationLockTile(Host host) { 41 super(host); 42 mController = host.getRotationLockController(); 43 } 44 45 @Override 46 protected BooleanState newTileState() { 47 return new BooleanState(); 48 } 49 50 public void setListening(boolean listening) { 51 if (mController == null) return; 52 if (listening) { 53 mController.addRotationLockControllerCallback(mCallback); 54 } else { 55 mController.removeRotationLockControllerCallback(mCallback); 56 } 57 } 58 59 @Override 60 protected void handleClick() { 61 if (mController == null) return; 62 final boolean newState = !mState.value; 63 mController.setRotationLocked(newState); 64 refreshState(newState ? UserBoolean.USER_TRUE : UserBoolean.USER_FALSE); 65 } 66 67 @Override 68 protected void handleUpdateState(BooleanState state, Object arg) { 69 if (mController == null) return; 70 final boolean rotationLocked = arg != null ? ((UserBoolean) arg).value 71 : mController.isRotationLocked(); 72 final boolean userInitiated = arg != null ? ((UserBoolean) arg).userInitiated : false; 73 state.visible = mController.isRotationLockAffordanceVisible(); 74 state.value = rotationLocked; 75 final boolean portrait = mContext.getResources().getConfiguration().orientation 76 != Configuration.ORIENTATION_LANDSCAPE; 77 final AnimationIcon icon; 78 if (rotationLocked) { 79 final int label = portrait ? R.string.quick_settings_rotation_locked_portrait_label 80 : R.string.quick_settings_rotation_locked_landscape_label; 81 state.label = mContext.getString(label); 82 icon = portrait ? mAutoToPortrait : mAutoToLandscape; 83 } else { 84 state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label); 85 icon = portrait ? mPortraitToAuto : mLandscapeToAuto; 86 } 87 icon.setAllowAnimation(userInitiated); 88 state.icon = icon; 89 state.contentDescription = getAccessibilityString(rotationLocked, 90 R.string.accessibility_rotation_lock_on_portrait, 91 R.string.accessibility_rotation_lock_on_landscape, 92 R.string.accessibility_rotation_lock_off); 93 } 94 95 /** 96 * Get the correct accessibility string based on the state 97 * 98 * @param locked Whether or not rotation is locked. 99 * @param idWhenPortrait The id which should be used when locked in portrait. 100 * @param idWhenLandscape The id which should be used when locked in landscape. 101 * @param idWhenOff The id which should be used when the rotation lock is off. 102 * @return 103 */ 104 private String getAccessibilityString(boolean locked, int idWhenPortrait, int idWhenLandscape, 105 int idWhenOff) { 106 int stringID; 107 if (locked) { 108 final boolean portrait = mContext.getResources().getConfiguration().orientation 109 != Configuration.ORIENTATION_LANDSCAPE; 110 stringID = portrait ? idWhenPortrait: idWhenLandscape; 111 } else { 112 stringID = idWhenOff; 113 } 114 return mContext.getString(stringID); 115 } 116 117 @Override 118 protected String composeChangeAnnouncement() { 119 return getAccessibilityString(mState.value, 120 R.string.accessibility_rotation_lock_on_portrait_changed, 121 R.string.accessibility_rotation_lock_on_landscape_changed, 122 R.string.accessibility_rotation_lock_off_changed); 123 } 124 125 private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() { 126 @Override 127 public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) { 128 refreshState(rotationLocked ? UserBoolean.BACKGROUND_TRUE 129 : UserBoolean.BACKGROUND_FALSE); 130 } 131 }; 132 } 133