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 com.android.internal.view.RotationPolicy;
     20 
     21 import android.content.Context;
     22 import android.os.UserHandle;
     23 import android.widget.CompoundButton;
     24 
     25 public final class AutoRotateController implements CompoundButton.OnCheckedChangeListener {
     26     private final Context mContext;
     27     private final CompoundButton mCheckbox;
     28     private final RotationLockCallbacks mCallbacks;
     29 
     30     private boolean mAutoRotation;
     31 
     32     private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
     33             new RotationPolicy.RotationPolicyListener() {
     34         @Override
     35         public void onChange() {
     36             updateState();
     37         }
     38     };
     39 
     40     public AutoRotateController(Context context, CompoundButton checkbox,
     41             RotationLockCallbacks callbacks) {
     42         mContext = context;
     43         mCheckbox = checkbox;
     44         mCallbacks = callbacks;
     45 
     46         mCheckbox.setOnCheckedChangeListener(this);
     47 
     48         RotationPolicy.registerRotationPolicyListener(context, mRotationPolicyListener,
     49                 UserHandle.USER_ALL);
     50         updateState();
     51     }
     52 
     53     public void onCheckedChanged(CompoundButton view, boolean checked) {
     54         if (checked != mAutoRotation) {
     55             mAutoRotation = checked;
     56             RotationPolicy.setRotationLock(mContext, !checked);
     57         }
     58     }
     59 
     60     public void release() {
     61         RotationPolicy.unregisterRotationPolicyListener(mContext,
     62                 mRotationPolicyListener);
     63     }
     64 
     65     private void updateState() {
     66         mAutoRotation = !RotationPolicy.isRotationLocked(mContext);
     67         mCheckbox.setChecked(mAutoRotation);
     68 
     69         boolean visible = RotationPolicy.isRotationLockToggleVisible(mContext);
     70         mCallbacks.setRotationLockControlVisibility(visible);
     71         mCheckbox.setEnabled(visible);
     72     }
     73 
     74     public interface RotationLockCallbacks {
     75         void setRotationLockControlVisibility(boolean show);
     76     }
     77 }
     78