Home | History | Annotate | Download | only in policy
      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 /** Platform implementation of the rotation lock controller. **/
     27 public 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;
     42         setListening(true);
     43     }
     44 
     45     public void addRotationLockControllerCallback(RotationLockControllerCallback callback) {
     46         mCallbacks.add(callback);
     47         notifyChanged(callback);
     48     }
     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
     71     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         }
     78     }
     79 
     80     private void notifyChanged() {
     81         for (RotationLockControllerCallback callback : mCallbacks) {
     82             notifyChanged(callback);
     83         }
     84     }
     85 
     86     private void notifyChanged(RotationLockControllerCallback callback) {
     87         callback.onRotationLockStateChanged(RotationPolicy.isRotationLocked(mContext),
     88                 RotationPolicy.isRotationLockToggleVisible(mContext));
     89     }
     90 }
     91