Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 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 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 19 | import com.android.internal.view.RotationPolicy; |
| 20 | |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 22 | import android.os.UserHandle; |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 23 | import android.widget.CompoundButton; |
| 24 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 25 | public final class AutoRotateController implements CompoundButton.OnCheckedChangeListener { |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 26 | private final Context mContext; |
| 27 | private final CompoundButton mCheckbox; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 28 | private final RotationLockCallbacks mCallbacks; |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 29 | |
Daniel Sandler | 5920f15 | 2011-03-02 15:19:54 -0500 | [diff] [blame] | 30 | private boolean mAutoRotation; |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 31 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 32 | private final RotationPolicy.RotationPolicyListener mRotationPolicyListener = |
| 33 | new RotationPolicy.RotationPolicyListener() { |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 34 | @Override |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 35 | public void onChange() { |
| 36 | updateState(); |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 37 | } |
| 38 | }; |
| 39 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 40 | public AutoRotateController(Context context, CompoundButton checkbox, |
| 41 | RotationLockCallbacks callbacks) { |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 42 | mContext = context; |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 43 | mCheckbox = checkbox; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 44 | mCallbacks = callbacks; |
| 45 | |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 46 | mCheckbox.setOnCheckedChangeListener(this); |
| 47 | |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 48 | RotationPolicy.registerRotationPolicyListener(context, mRotationPolicyListener, |
| 49 | UserHandle.USER_ALL); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 50 | updateState(); |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public void onCheckedChanged(CompoundButton view, boolean checked) { |
Daniel Sandler | 5920f15 | 2011-03-02 15:19:54 -0500 | [diff] [blame] | 54 | if (checked != mAutoRotation) { |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 55 | mAutoRotation = checked; |
| 56 | RotationPolicy.setRotationLock(mContext, !checked); |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 60 | public void release() { |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 61 | RotationPolicy.unregisterRotationPolicyListener(mContext, |
| 62 | mRotationPolicyListener); |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 65 | private void updateState() { |
| 66 | mAutoRotation = !RotationPolicy.isRotationLocked(mContext); |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 67 | mCheckbox.setChecked(mAutoRotation); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 68 | |
| 69 | boolean visible = RotationPolicy.isRotationLockToggleVisible(mContext); |
| 70 | mCallbacks.setRotationLockControlVisibility(visible); |
| 71 | mCheckbox.setEnabled(visible); |
John Spurlock | 781f0f4 | 2012-05-22 10:12:09 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 74 | public interface RotationLockCallbacks { |
| 75 | void setRotationLockControlVisibility(boolean show); |
Joe Onorato | a8e3418 | 2010-11-26 13:19:58 -0800 | [diff] [blame] | 76 | } |
| 77 | } |