RotationLockTile.java revision af8d6c44f06d2f8baac2c5774a9efdae3fc36797
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
17package com.android.systemui.qs.tiles;
18
19import android.content.res.Configuration;
20
21import com.android.systemui.R;
22import com.android.systemui.qs.QSTile;
23import com.android.systemui.statusbar.policy.RotationLockController;
24import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback;
25
26/** Quick settings tile: Rotation **/
27public class RotationLockTile extends QSTile<QSTile.BooleanState> {
28
29    private final RotationLockController mController;
30
31    public RotationLockTile(Host host) {
32        super(host);
33        mController = host.getRotationLockController();
34        if (mController == null) return;
35        mController.addRotationLockControllerCallback(mCallback);
36    }
37
38    @Override
39    protected BooleanState newTileState() {
40        return new BooleanState();
41    }
42
43    public void dispose() {
44        if (mController == null) return;
45        mController.removeRotationLockControllerCallback(mCallback);
46    }
47
48    @Override
49    protected void handleClick() {
50        if (mController == null) return;
51        mController.setRotationLocked(mState.value);
52    }
53
54    @Override
55    protected void handleUpdateState(BooleanState state, Object arg) {
56        if (mController == null) return;
57        final boolean rotationLocked = mController.isRotationLocked();
58        state.visible = mController.isRotationLockAffordanceVisible();
59        state.value = !rotationLocked;
60        if (rotationLocked) {
61            final int lockOrientation = mController.getRotationLockOrientation();
62            final int label = lockOrientation == Configuration.ORIENTATION_PORTRAIT
63                    ? R.string.quick_settings_rotation_locked_portrait_label
64                    : lockOrientation == Configuration.ORIENTATION_LANDSCAPE
65                    ? R.string.quick_settings_rotation_locked_landscape_label
66                    : R.string.quick_settings_rotation_locked_label;
67            state.iconId = R.drawable.ic_qs_rotation_lock;
68            state.label = mContext.getString(label);
69        } else {
70            state.iconId = R.drawable.ic_qs_rotation;
71            state.label = mContext.getString(R.string.quick_settings_rotation_unlocked_label);
72        }
73    }
74
75    private final RotationLockControllerCallback mCallback = new RotationLockControllerCallback() {
76        @Override
77        public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) {
78            refreshState();
79        }
80    };
81}
82