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 
17 package com.android.systemui.statusbar.policy;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import com.android.internal.view.RotationPolicy;
23 
24 import java.util.concurrent.CopyOnWriteArrayList;
25 
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 
29 /** Platform implementation of the rotation lock controller. **/
30 @Singleton
31 public final class RotationLockControllerImpl implements RotationLockController {
32     private final Context mContext;
33     private final CopyOnWriteArrayList<RotationLockControllerCallback> mCallbacks =
34             new CopyOnWriteArrayList<RotationLockControllerCallback>();
35 
36     private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
37             new RotationPolicy.RotationPolicyListener() {
38         @Override
39         public void onChange() {
40             notifyChanged();
41         }
42     };
43 
44     @Inject
RotationLockControllerImpl(Context context)45     public RotationLockControllerImpl(Context context) {
46         mContext = context;
47         setListening(true);
48     }
49 
addCallback(RotationLockControllerCallback callback)50     public void addCallback(RotationLockControllerCallback callback) {
51         mCallbacks.add(callback);
52         notifyChanged(callback);
53     }
54 
removeCallback(RotationLockControllerCallback callback)55     public void removeCallback(RotationLockControllerCallback callback) {
56         mCallbacks.remove(callback);
57     }
58 
getRotationLockOrientation()59     public int getRotationLockOrientation() {
60         return RotationPolicy.getRotationLockOrientation(mContext);
61     }
62 
isRotationLocked()63     public boolean isRotationLocked() {
64         return RotationPolicy.isRotationLocked(mContext);
65     }
66 
setRotationLocked(boolean locked)67     public void setRotationLocked(boolean locked) {
68         RotationPolicy.setRotationLock(mContext, locked);
69     }
70 
setRotationLockedAtAngle(boolean locked, int rotation)71     public void setRotationLockedAtAngle(boolean locked, int rotation){
72         RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation);
73     }
74 
isRotationLockAffordanceVisible()75     public boolean isRotationLockAffordanceVisible() {
76         return RotationPolicy.isRotationLockToggleVisible(mContext);
77     }
78 
79     @Override
setListening(boolean listening)80     public void setListening(boolean listening) {
81         if (listening) {
82             RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener,
83                     UserHandle.USER_ALL);
84         } else {
85             RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
86         }
87     }
88 
notifyChanged()89     private void notifyChanged() {
90         for (RotationLockControllerCallback callback : mCallbacks) {
91             notifyChanged(callback);
92         }
93     }
94 
notifyChanged(RotationLockControllerCallback callback)95     private void notifyChanged(RotationLockControllerCallback callback) {
96         callback.onRotationLockStateChanged(RotationPolicy.isRotationLocked(mContext),
97                 RotationPolicy.isRotationLockToggleVisible(mContext));
98     }
99 }
100