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.Context; 20 import android.content.Intent; 21 import android.content.res.Configuration; 22 23 import android.provider.Settings; 24 import android.service.quicksettings.Tile; 25 import android.widget.Switch; 26 27 import com.android.internal.logging.MetricsLogger; 28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 29 import com.android.systemui.Dependency; 30 import com.android.systemui.R; 31 import com.android.systemui.qs.QSHost; 32 import com.android.systemui.plugins.qs.QSTile.BooleanState; 33 import com.android.systemui.qs.tileimpl.QSTileImpl; 34 import com.android.systemui.statusbar.policy.RotationLockController; 35 import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback; 36 37 /** Quick settings tile: Rotation **/ 38 public class RotationLockTile extends QSTileImpl<BooleanState> { 39 40 private final Icon mIcon = ResourceIcon.get(R.drawable.ic_qs_auto_rotate); 41 private final RotationLockController mController; 42 43 public RotationLockTile(QSHost host) { 44 super(host); 45 mController = Dependency.get(RotationLockController.class); 46 } 47 48 @Override 49 public BooleanState newTileState() { 50 return new BooleanState(); 51 } 52 53 public void handleSetListening(boolean listening) { 54 if (listening) { 55 mController.addCallback(mCallback); 56 } else { 57 mController.removeCallback(mCallback); 58 } 59 } 60 61 @Override 62 public Intent getLongClickIntent() { 63 return new Intent(Settings.ACTION_DISPLAY_SETTINGS); 64 } 65 66 @Override 67 protected void handleClick() { 68 final boolean newState = !mState.value; 69 mController.setRotationLocked(!newState); 70 refreshState(newState); 71 } 72 73 @Override 74 public CharSequence getTileLabel() { 75 return getState().label; 76 } 77 78 @Override 79 protected void handleUpdateState(BooleanState state, Object arg) { 80 final boolean rotationLocked = mController.isRotationLocked(); 81 82 state.value = !rotationLocked; 83 state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label); 84 state.icon = mIcon; 85 state.contentDescription = getAccessibilityString(rotationLocked); 86 state.expandedAccessibilityClassName = Switch.class.getName(); 87 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; 88 } 89 90 public static boolean isCurrentOrientationLockPortrait(RotationLockController controller, 91 Context context) { 92 int lockOrientation = controller.getRotationLockOrientation(); 93 if (lockOrientation == Configuration.ORIENTATION_UNDEFINED) { 94 // Freely rotating device; use current rotation 95 return context.getResources().getConfiguration().orientation 96 != Configuration.ORIENTATION_LANDSCAPE; 97 } else { 98 return lockOrientation != Configuration.ORIENTATION_LANDSCAPE; 99 } 100 } 101 102 @Override 103 public int getMetricsCategory() { 104 return MetricsEvent.QS_ROTATIONLOCK; 105 } 106 107 /** 108 * Get the correct accessibility string based on the state 109 * 110 * @param locked Whether or not rotation is locked. 111 */ 112 private String getAccessibilityString(boolean locked) { 113 return mContext.getString(R.string.accessibility_quick_settings_rotation); 114 } 115 116 @Override 117 protected String composeChangeAnnouncement() { 118 return getAccessibilityString(mState.value); 119 } 120 121 private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() { 122 @Override 123 public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) { 124 refreshState(rotationLocked); 125 } 126 }; 127 } 128