blob: c3bcd946c70cdc182f5a8ebcaea677c51db51e06 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
26/** Platform implementation of the rotation lock controller. **/
27public final class RotationLockControllerImpl implements RotationLockController {
28 private final Context mContext;
29 private final CopyOnWriteArrayList<RotationLockControllerCallback> mCallbacks =
30 new CopyOnWriteArrayList<RotationLockControllerCallback>();
31
32 private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
33 new RotationPolicy.RotationPolicyListener() {
34 @Override
35 public void onChange() {
36 notifyChanged();
37 }
38 };
39
40 public RotationLockControllerImpl(Context context) {
41 mContext = context;
John Spurlockccb6b9a2014-05-17 15:54:40 -040042 setListening(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -040043 }
44
45 public void addRotationLockControllerCallback(RotationLockControllerCallback callback) {
46 mCallbacks.add(callback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040047 notifyChanged(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -040048 }
49
50 public void removeRotationLockControllerCallback(RotationLockControllerCallback callback) {
51 mCallbacks.remove(callback);
52 }
53
54 public int getRotationLockOrientation() {
55 return RotationPolicy.getRotationLockOrientation(mContext);
56 }
57
58 public boolean isRotationLocked() {
59 return RotationPolicy.isRotationLocked(mContext);
60 }
61
62 public void setRotationLocked(boolean locked) {
63 RotationPolicy.setRotationLock(mContext, locked);
64 }
65
66 public boolean isRotationLockAffordanceVisible() {
67 return RotationPolicy.isRotationLockToggleVisible(mContext);
68 }
69
70 @Override
John Spurlockccb6b9a2014-05-17 15:54:40 -040071 public void setListening(boolean listening) {
72 if (listening) {
73 RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener,
74 UserHandle.USER_ALL);
75 } else {
76 RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
77 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040078 }
79
80 private void notifyChanged() {
81 for (RotationLockControllerCallback callback : mCallbacks) {
John Spurlockccb6b9a2014-05-17 15:54:40 -040082 notifyChanged(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -040083 }
84 }
John Spurlockccb6b9a2014-05-17 15:54:40 -040085
86 private void notifyChanged(RotationLockControllerCallback callback) {
87 callback.onRotationLockStateChanged(RotationPolicy.isRotationLocked(mContext),
88 RotationPolicy.isRotationLockToggleVisible(mContext));
89 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040090}