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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 package com.android.systemui.unfold.system
16 
17 import android.os.Handler
18 import android.os.HandlerThread
19 import android.os.Looper
20 import android.os.Process
21 import com.android.systemui.dagger.qualifiers.Main
22 import com.android.systemui.dagger.qualifiers.UiBackground
23 import com.android.systemui.unfold.config.ResourceUnfoldTransitionConfig
24 import com.android.systemui.unfold.config.UnfoldTransitionConfig
25 import com.android.systemui.unfold.dagger.UnfoldBg
26 import com.android.systemui.unfold.dagger.UnfoldMain
27 import com.android.systemui.unfold.dagger.UnfoldSingleThreadBg
28 import com.android.systemui.unfold.updates.FoldProvider
29 import com.android.systemui.unfold.util.CurrentActivityTypeProvider
30 import dagger.Binds
31 import dagger.Module
32 import dagger.Provides
33 import java.util.concurrent.Executor
34 import javax.inject.Singleton
35 import kotlinx.coroutines.CoroutineDispatcher
36 import kotlinx.coroutines.android.asCoroutineDispatcher
37 
38 /**
39  * Dagger module with system-only dependencies for the unfold animation. The code that is used to
40  * calculate unfold transition progress depends on some hidden APIs that are not available in normal
41  * apps. In order to re-use this code and use alternative implementations of these classes in other
42  * apps and hidden APIs here.
43  */
44 @Module
45 abstract class SystemUnfoldSharedModule {
46 
47     @Binds
activityTypeProvidernull48     abstract fun activityTypeProvider(executor: ActivityManagerActivityTypeProvider):
49             CurrentActivityTypeProvider
50 
51     @Binds
52     abstract fun config(config: ResourceUnfoldTransitionConfig): UnfoldTransitionConfig
53 
54     @Binds
55     abstract fun foldState(provider: DeviceStateManagerFoldProvider): FoldProvider
56 
57     @Binds
58     abstract fun deviceStateRepository(provider: DeviceStateRepositoryImpl): DeviceStateRepository
59 
60     @Binds
61     @UnfoldMain
62     abstract fun mainExecutor(@Main executor: Executor): Executor
63 
64     @Binds
65     @UnfoldMain
66     abstract fun mainHandler(@Main handler: Handler): Handler
67 
68     @Binds
69     @UnfoldSingleThreadBg
70     abstract fun backgroundExecutor(@UiBackground executor: Executor): Executor
71 
72     companion object {
73         @Provides
74         @UnfoldBg
75         @Singleton
76         fun unfoldBgProgressHandler(@UnfoldBg looper: Looper): Handler {
77             return Handler(looper)
78         }
79 
80         @Provides
81         @UnfoldBg
82         @Singleton
83         fun unfoldBgDispatcher(@UnfoldBg handler: Handler): CoroutineDispatcher {
84             return handler.asCoroutineDispatcher("@UnfoldBg Dispatcher")
85         }
86 
87         @Provides
88         @UnfoldBg
89         @Singleton
90         fun provideBgLooper(): Looper {
91             return HandlerThread("UnfoldBg", Process.THREAD_PRIORITY_FOREGROUND)
92                 .apply { start() }
93                 .looper
94         }
95     }
96 }
97