1 /* 2 * Copyright (C) 2022 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.unfold.updates 18 19 import android.content.Context 20 import android.hardware.display.DisplayManager 21 import android.os.Handler 22 import android.os.RemoteException 23 import com.android.systemui.unfold.dagger.UnfoldMain 24 import com.android.systemui.unfold.util.CallbackController 25 import javax.inject.Inject 26 27 /** 28 * Allows to subscribe to rotation changes. Updates are provided for the display associated 29 * to [context]. 30 */ 31 class RotationChangeProvider 32 @Inject 33 constructor( 34 private val displayManager: DisplayManager, 35 private val context: Context, 36 @UnfoldMain private val mainHandler: Handler, 37 ) : CallbackController<RotationChangeProvider.RotationListener> { 38 39 private val listeners = mutableListOf<RotationListener>() 40 41 private val displayListener = RotationDisplayListener() 42 private var lastRotation: Int? = null 43 addCallbacknull44 override fun addCallback(listener: RotationListener) { 45 mainHandler.post { 46 if (listeners.isEmpty()) { 47 subscribeToRotation() 48 } 49 listeners += listener 50 } 51 } 52 removeCallbacknull53 override fun removeCallback(listener: RotationListener) { 54 mainHandler.post { 55 listeners -= listener 56 if (listeners.isEmpty()) { 57 unsubscribeToRotation() 58 lastRotation = null 59 } 60 } 61 } 62 subscribeToRotationnull63 private fun subscribeToRotation() { 64 try { 65 displayManager.registerDisplayListener(displayListener, mainHandler) 66 } catch (e: RemoteException) { 67 throw e.rethrowFromSystemServer() 68 } 69 } 70 unsubscribeToRotationnull71 private fun unsubscribeToRotation() { 72 try { 73 displayManager.unregisterDisplayListener(displayListener) 74 } catch (e: RemoteException) { 75 throw e.rethrowFromSystemServer() 76 } 77 } 78 79 /** Gets notified of rotation changes. */ interfacenull80 fun interface RotationListener { 81 /** Called once rotation changes. */ 82 fun onRotationChanged(newRotation: Int) 83 } 84 85 private inner class RotationDisplayListener : DisplayManager.DisplayListener { 86 onDisplayChangednull87 override fun onDisplayChanged(displayId: Int) { 88 val display = context.display ?: return 89 90 if (displayId == display.displayId) { 91 val currentRotation = display.rotation 92 if (lastRotation == null || lastRotation != currentRotation) { 93 listeners.forEach { it.onRotationChanged(currentRotation) } 94 lastRotation = currentRotation 95 } 96 } 97 } 98 onDisplayAddednull99 override fun onDisplayAdded(displayId: Int) {} 100 onDisplayRemovednull101 override fun onDisplayRemoved(displayId: Int) {} 102 } 103 } 104