blob: 5418dc14e0bb03f2e180707a837bec2c73e001f7 [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
Jason Monk88529052016-11-04 13:29:58 -040045 public void addCallback(RotationLockControllerCallback callback) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040046 mCallbacks.add(callback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040047 notifyChanged(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -040048 }
49
Jason Monk88529052016-11-04 13:29:58 -040050 public void removeCallback(RotationLockControllerCallback callback) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040051 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
Mike Digman6ca87112017-12-06 09:46:52 -080066 public void setRotationLockedAtAngle(boolean locked, int rotation){
67 RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation);
68 }
69
John Spurlockaf8d6c42014-05-07 17:49:08 -040070 public boolean isRotationLockAffordanceVisible() {
71 return RotationPolicy.isRotationLockToggleVisible(mContext);
72 }
73
74 @Override
John Spurlockccb6b9a2014-05-17 15:54:40 -040075 public void setListening(boolean listening) {
76 if (listening) {
77 RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener,
78 UserHandle.USER_ALL);
79 } else {
80 RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
81 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040082 }
83
84 private void notifyChanged() {
85 for (RotationLockControllerCallback callback : mCallbacks) {
John Spurlockccb6b9a2014-05-17 15:54:40 -040086 notifyChanged(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -040087 }
88 }
John Spurlockccb6b9a2014-05-17 15:54:40 -040089
90 private void notifyChanged(RotationLockControllerCallback callback) {
91 callback.onRotationLockStateChanged(RotationPolicy.isRotationLocked(mContext),
92 RotationPolicy.isRotationLockToggleVisible(mContext));
93 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040094}