RotationLockController.java revision 79578b29bf4ba1d210586b9d7bb832eddf0960b7
1/*
2 * Copyright (C) 2010 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.statusbar.policy;
18
19import android.content.Context;
20import android.os.UserHandle;
21
22import com.android.internal.view.RotationPolicy;
23
24import java.util.concurrent.CopyOnWriteArrayList;
25
26public final class RotationLockController {
27    private final Context mContext;
28    private final CopyOnWriteArrayList<RotationLockControllerCallback> mCallbacks =
29            new CopyOnWriteArrayList<RotationLockControllerCallback>();
30
31    private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
32            new RotationPolicy.RotationPolicyListener() {
33        @Override
34        public void onChange() {
35            notifyChanged();
36        }
37    };
38
39    public interface RotationLockControllerCallback {
40        public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible);
41    }
42
43    public RotationLockController(Context context) {
44        mContext = context;
45        notifyChanged();
46        if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
47            RotationPolicy.registerRotationPolicyListener(mContext,
48                    mRotationPolicyListener, UserHandle.USER_ALL);
49        }
50    }
51
52    public void addRotationLockControllerCallback(RotationLockControllerCallback callback) {
53        mCallbacks.add(callback);
54    }
55
56    public boolean isRotationLocked() {
57        if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
58            return RotationPolicy.isRotationLocked(mContext);
59        }
60        return false;
61    }
62
63    public void setRotationLocked(boolean locked) {
64        if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
65            RotationPolicy.setRotationLock(mContext, locked);
66        }
67    }
68
69    public boolean isRotationLockAffordanceVisible() {
70        if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
71            return RotationPolicy.isRotationLockToggleVisible(mContext);
72        }
73        return false;
74    }
75
76    public void release() {
77        if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
78            RotationPolicy.unregisterRotationPolicyListener(mContext,
79                    mRotationPolicyListener);
80        }
81    }
82
83    private void notifyChanged() {
84        for (RotationLockControllerCallback callback : mCallbacks) {
85            callback.onRotationLockStateChanged(RotationPolicy.isRotationLocked(mContext),
86                    RotationPolicy.isRotationLockToggleVisible(mContext));
87        }
88    }
89}
90