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