• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.widget.Switch;
25 
26 import com.android.internal.logging.MetricsLogger;
27 import com.android.internal.logging.MetricsProto.MetricsEvent;
28 import com.android.systemui.R;
29 import com.android.systemui.qs.QSTile;
30 import com.android.systemui.statusbar.policy.RotationLockController;
31 import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback;
32 
33 /** Quick settings tile: Rotation **/
34 public class RotationLockTile extends QSTile<QSTile.BooleanState> {
35     private final AnimationIcon mPortraitToAuto
36             = new AnimationIcon(R.drawable.ic_portrait_to_auto_rotate_animation,
37             R.drawable.ic_portrait_from_auto_rotate);
38     private final AnimationIcon mAutoToPortrait
39             = new AnimationIcon(R.drawable.ic_portrait_from_auto_rotate_animation,
40             R.drawable.ic_portrait_to_auto_rotate);
41 
42     private final AnimationIcon mLandscapeToAuto
43             = new AnimationIcon(R.drawable.ic_landscape_to_auto_rotate_animation,
44             R.drawable.ic_landscape_from_auto_rotate);
45     private final AnimationIcon mAutoToLandscape
46             = new AnimationIcon(R.drawable.ic_landscape_from_auto_rotate_animation,
47             R.drawable.ic_landscape_to_auto_rotate);
48 
49     private final RotationLockController mController;
50 
RotationLockTile(Host host)51     public RotationLockTile(Host host) {
52         super(host);
53         mController = host.getRotationLockController();
54     }
55 
56     @Override
newTileState()57     public BooleanState newTileState() {
58         return new BooleanState();
59     }
60 
setListening(boolean listening)61     public void setListening(boolean listening) {
62         if (mController == null) return;
63         if (listening) {
64             mController.addRotationLockControllerCallback(mCallback);
65         } else {
66             mController.removeRotationLockControllerCallback(mCallback);
67         }
68     }
69 
70     @Override
getLongClickIntent()71     public Intent getLongClickIntent() {
72         return new Intent(Settings.ACTION_DISPLAY_SETTINGS);
73     }
74 
75     @Override
handleClick()76     protected void handleClick() {
77         if (mController == null) return;
78         MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
79         final boolean newState = !mState.value;
80         mController.setRotationLocked(!newState);
81         refreshState(newState);
82     }
83 
84     @Override
getTileLabel()85     public CharSequence getTileLabel() {
86         return getState().label;
87     }
88 
89     @Override
handleUpdateState(BooleanState state, Object arg)90     protected void handleUpdateState(BooleanState state, Object arg) {
91         if (mController == null) return;
92         final boolean rotationLocked = mController.isRotationLocked();
93         // TODO: Handle accessibility rotation lock and whatnot.
94 
95         state.value = !rotationLocked;
96         final boolean portrait = isCurrentOrientationLockPortrait(mController, mContext);
97         if (rotationLocked) {
98             final int label = portrait ? R.string.quick_settings_rotation_locked_portrait_label
99                     : R.string.quick_settings_rotation_locked_landscape_label;
100             state.label = mContext.getString(label);
101             state.icon = portrait ? mAutoToPortrait : mAutoToLandscape;
102         } else {
103             state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label);
104             state.icon = portrait ? mPortraitToAuto : mLandscapeToAuto;
105         }
106         state.contentDescription = getAccessibilityString(rotationLocked);
107         state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
108                 = Switch.class.getName();
109     }
110 
isCurrentOrientationLockPortrait(RotationLockController controller, Context context)111     public static boolean isCurrentOrientationLockPortrait(RotationLockController controller,
112             Context context) {
113         int lockOrientation = controller.getRotationLockOrientation();
114         if (lockOrientation == Configuration.ORIENTATION_UNDEFINED) {
115             // Freely rotating device; use current rotation
116             return context.getResources().getConfiguration().orientation
117                     != Configuration.ORIENTATION_LANDSCAPE;
118         } else {
119             return lockOrientation != Configuration.ORIENTATION_LANDSCAPE;
120         }
121     }
122 
123     @Override
getMetricsCategory()124     public int getMetricsCategory() {
125         return MetricsEvent.QS_ROTATIONLOCK;
126     }
127 
128     /**
129      * Get the correct accessibility string based on the state
130      *
131      * @param locked Whether or not rotation is locked.
132      */
getAccessibilityString(boolean locked)133     private String getAccessibilityString(boolean locked) {
134         if (locked) {
135             return mContext.getString(R.string.accessibility_quick_settings_rotation) + ","
136                     + mContext.getString(R.string.accessibility_quick_settings_rotation_value,
137                     isCurrentOrientationLockPortrait(mController, mContext)
138                             ? mContext.getString(
139                                     R.string.quick_settings_rotation_locked_portrait_label)
140                             : mContext.getString(
141                                     R.string.quick_settings_rotation_locked_landscape_label));
142 
143         } else {
144             return mContext.getString(R.string.accessibility_quick_settings_rotation);
145         }
146     }
147 
148     @Override
composeChangeAnnouncement()149     protected String composeChangeAnnouncement() {
150         return getAccessibilityString(mState.value);
151     }
152 
153     private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() {
154         @Override
155         public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) {
156             refreshState(rotationLocked);
157         }
158     };
159 }
160