/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockController.java

https://github.com/aizuzi/platform_frameworks_base · Java · 89 lines · 60 code · 14 blank · 15 comment · 6 complexity · ab602aabff16610a1d204e9288c7fabc MD5 · raw file

  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. package com.android.systemui.statusbar.policy;
  17. import android.content.Context;
  18. import android.os.UserHandle;
  19. import com.android.internal.view.RotationPolicy;
  20. import java.util.concurrent.CopyOnWriteArrayList;
  21. public final class RotationLockController {
  22. private final Context mContext;
  23. private final CopyOnWriteArrayList<RotationLockControllerCallback> mCallbacks =
  24. new CopyOnWriteArrayList<RotationLockControllerCallback>();
  25. private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
  26. new RotationPolicy.RotationPolicyListener() {
  27. @Override
  28. public void onChange() {
  29. notifyChanged();
  30. }
  31. };
  32. public interface RotationLockControllerCallback {
  33. public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible);
  34. }
  35. public RotationLockController(Context context) {
  36. mContext = context;
  37. notifyChanged();
  38. if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
  39. RotationPolicy.registerRotationPolicyListener(mContext,
  40. mRotationPolicyListener, UserHandle.USER_ALL);
  41. }
  42. }
  43. public void addRotationLockControllerCallback(RotationLockControllerCallback callback) {
  44. mCallbacks.add(callback);
  45. }
  46. public boolean isRotationLocked() {
  47. if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
  48. return RotationPolicy.isRotationLocked(mContext);
  49. }
  50. return false;
  51. }
  52. public void setRotationLocked(boolean locked) {
  53. if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
  54. RotationPolicy.setRotationLock(mContext, locked);
  55. }
  56. }
  57. public boolean isRotationLockAffordanceVisible() {
  58. if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
  59. return RotationPolicy.isRotationLockToggleVisible(mContext);
  60. }
  61. return false;
  62. }
  63. public void release() {
  64. if (RotationPolicy.isRotationLockToggleSupported(mContext)) {
  65. RotationPolicy.unregisterRotationPolicyListener(mContext,
  66. mRotationPolicyListener);
  67. }
  68. }
  69. private void notifyChanged() {
  70. for (RotationLockControllerCallback callback : mCallbacks) {
  71. callback.onRotationLockStateChanged(RotationPolicy.isRotationLocked(mContext),
  72. RotationPolicy.isRotationLockToggleVisible(mContext));
  73. }
  74. }
  75. }