1 /* 2 * Copyright (C) 2023 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.unfold.progress 17 18 import android.os.Looper 19 import android.view.Choreographer 20 import androidx.dynamicanimation.animation.FrameCallbackScheduler 21 import dagger.assisted.AssistedFactory 22 import dagger.assisted.AssistedInject 23 24 /** 25 * Scheduler that posts animation progresses on a thread different than the ui one. 26 * 27 * The following is taken from [AnimationHandler.FrameCallbackScheduler16]. It is extracted here as 28 * there are no guarantees which implementation the [DynamicAnimation] class would use otherwise. 29 * This allows classes using [DynamicAnimation] to be created in any thread, but still use the 30 * scheduler for a specific thread. 31 * 32 * Technically the [AssistedInject] is not needed: it's just to have a nicer factory with a 33 * documentation snippet instead of using a plain dagger provider. 34 */ 35 class UnfoldFrameCallbackScheduler @AssistedInject constructor() : FrameCallbackScheduler { 36 37 private val choreographer = Choreographer.getInstance() 38 private val looper = 39 Looper.myLooper() ?: error("This should be created in a thread with a looper.") 40 postFrameCallbacknull41 override fun postFrameCallback(frameCallback: Runnable) { 42 choreographer.postFrameCallback { frameCallback.run() } 43 } 44 isCurrentThreadnull45 override fun isCurrentThread(): Boolean { 46 return looper.isCurrentThread 47 } 48 49 @AssistedFactory 50 interface Factory { 51 /** 52 * Creates a [FrameCallbackScheduler] that uses [Choreographer] to post frame callbacks. 53 * 54 * Note that the choreographer used depends on the thread this [create] is called on, as it 55 * is get from a thread static attribute. 56 */ createnull57 fun create(): UnfoldFrameCallbackScheduler 58 } 59 } 60