1 /* 2 * Copyright (C) 2018 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.shared.system; 17 18 import android.content.Context; 19 import android.os.RemoteException; 20 import android.util.Log; 21 import android.view.IRotationWatcher; 22 import android.view.WindowManagerGlobal; 23 24 public abstract class RotationWatcher { 25 26 private static final String TAG = "RotationWatcher"; 27 28 private final Context mContext; 29 30 private final IRotationWatcher mWatcher = new IRotationWatcher.Stub() { 31 32 @Override 33 public void onRotationChanged(int rotation) { 34 RotationWatcher.this.onRotationChanged(rotation); 35 36 } 37 }; 38 39 private boolean mIsWatching = false; 40 RotationWatcher(Context context)41 public RotationWatcher(Context context) { 42 mContext = context; 43 } 44 onRotationChanged(int rotation)45 protected abstract void onRotationChanged(int rotation); 46 enable()47 public void enable() { 48 if (!mIsWatching) { 49 try { 50 WindowManagerGlobal.getWindowManagerService().watchRotation(mWatcher, 51 mContext.getDisplayId()); 52 mIsWatching = true; 53 } catch (RemoteException e) { 54 Log.w(TAG, "Failed to set rotation watcher", e); 55 } 56 } 57 } 58 disable()59 public void disable() { 60 if (mIsWatching) { 61 try { 62 WindowManagerGlobal.getWindowManagerService().removeRotationWatcher(mWatcher); 63 mIsWatching = false; 64 } catch (RemoteException e) { 65 Log.w(TAG, "Failed to remove rotation watcher", e); 66 } 67 } 68 } 69 } 70