blob: 7d58032c12c80daf219652a1ab231ac92b1236dd [file] [log] [blame]
Joe Onoratoa8e34182010-11-26 13:19:58 -08001/*
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
Jeff Brown207673cd2012-06-05 17:47:11 -070019import com.android.internal.view.RotationPolicy;
20
Joe Onoratoa8e34182010-11-26 13:19:58 -080021import android.content.Context;
Christopher Tate5e08af02012-09-21 17:17:22 -070022import android.os.UserHandle;
Joe Onoratoa8e34182010-11-26 13:19:58 -080023import android.widget.CompoundButton;
24
Jeff Brown207673cd2012-06-05 17:47:11 -070025public final class AutoRotateController implements CompoundButton.OnCheckedChangeListener {
John Spurlock781f0f42012-05-22 10:12:09 -040026 private final Context mContext;
27 private final CompoundButton mCheckbox;
Jeff Brown207673cd2012-06-05 17:47:11 -070028 private final RotationLockCallbacks mCallbacks;
Joe Onoratoa8e34182010-11-26 13:19:58 -080029
Daniel Sandler5920f152011-03-02 15:19:54 -050030 private boolean mAutoRotation;
Joe Onoratoa8e34182010-11-26 13:19:58 -080031
Jeff Brown207673cd2012-06-05 17:47:11 -070032 private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
33 new RotationPolicy.RotationPolicyListener() {
John Spurlock781f0f42012-05-22 10:12:09 -040034 @Override
Jeff Brown207673cd2012-06-05 17:47:11 -070035 public void onChange() {
36 updateState();
John Spurlock781f0f42012-05-22 10:12:09 -040037 }
38 };
39
Jeff Brown207673cd2012-06-05 17:47:11 -070040 public AutoRotateController(Context context, CompoundButton checkbox,
41 RotationLockCallbacks callbacks) {
Joe Onoratoa8e34182010-11-26 13:19:58 -080042 mContext = context;
John Spurlock781f0f42012-05-22 10:12:09 -040043 mCheckbox = checkbox;
Jeff Brown207673cd2012-06-05 17:47:11 -070044 mCallbacks = callbacks;
45
John Spurlock781f0f42012-05-22 10:12:09 -040046 mCheckbox.setOnCheckedChangeListener(this);
47
Christopher Tate5e08af02012-09-21 17:17:22 -070048 RotationPolicy.registerRotationPolicyListener(context, mRotationPolicyListener,
49 UserHandle.USER_ALL);
Jeff Brown207673cd2012-06-05 17:47:11 -070050 updateState();
Joe Onoratoa8e34182010-11-26 13:19:58 -080051 }
52
53 public void onCheckedChanged(CompoundButton view, boolean checked) {
Daniel Sandler5920f152011-03-02 15:19:54 -050054 if (checked != mAutoRotation) {
Jeff Brown207673cd2012-06-05 17:47:11 -070055 mAutoRotation = checked;
56 RotationPolicy.setRotationLock(mContext, !checked);
Joe Onoratoa8e34182010-11-26 13:19:58 -080057 }
58 }
59
John Spurlock781f0f42012-05-22 10:12:09 -040060 public void release() {
Jeff Brown207673cd2012-06-05 17:47:11 -070061 RotationPolicy.unregisterRotationPolicyListener(mContext,
62 mRotationPolicyListener);
John Spurlock781f0f42012-05-22 10:12:09 -040063 }
64
Jeff Brown207673cd2012-06-05 17:47:11 -070065 private void updateState() {
66 mAutoRotation = !RotationPolicy.isRotationLocked(mContext);
John Spurlock781f0f42012-05-22 10:12:09 -040067 mCheckbox.setChecked(mAutoRotation);
Jeff Brown207673cd2012-06-05 17:47:11 -070068
69 boolean visible = RotationPolicy.isRotationLockToggleVisible(mContext);
70 mCallbacks.setRotationLockControlVisibility(visible);
71 mCheckbox.setEnabled(visible);
John Spurlock781f0f42012-05-22 10:12:09 -040072 }
73
Jeff Brown207673cd2012-06-05 17:47:11 -070074 public interface RotationLockCallbacks {
75 void setRotationLockControlVisibility(boolean show);
Joe Onoratoa8e34182010-11-26 13:19:58 -080076 }
77}