1 /*
2  * Copyright (C) 2024 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.car.carlauncher.repositories.appactions
18 
19 import android.car.media.CarMediaManager
20 import android.content.pm.PackageManager
21 import android.util.Log
22 import com.android.car.carlauncher.repositories.appactions.AppLaunchProvider.DisabledAppLaunchProvider
23 import com.android.car.carlauncher.repositories.appactions.AppLaunchProvider.LauncherActivityLaunchProvider
24 import com.android.car.carlauncher.repositories.appactions.AppLaunchProvider.MediaServiceLaunchProvider
25 import com.android.car.carlauncher.repositories.appactions.AppLaunchProvider.MirroringAppLaunchProvider
26 import com.android.car.carlauncher.repositories.appactions.AppLaunchProvider.TosDisabledAppLaunchProvider
27 
28 /**
29  * Acts as a central factory for obtaining specialized AppLaunchProvider instances.
30  * It maintains an internal mapping between AppLauncherProviderType enum values and
31  * the corresponding provider objects. The factory is responsible for:
32  *
33  * * Registering available AppLaunchProvider implementations
34  * * Providing access to providers based on the requested AppLauncherProviderType
35  */
36 class AppLaunchProviderFactory(
37     carMediaManager: CarMediaManager,
38     launchMediaCenter: Boolean,
39     onMediaSelected: () -> Unit,
40     packageManager: PackageManager
41 ) {
42 
43     private val providerMap = mutableMapOf<AppLauncherProviderType, AppLaunchProvider>()
44 
45     /**
46      * Enumerates the supported types of AppLaunchProvider implementations. Used
47      * internally by the factory to differentiate and retrieve providers.
48      */
49     enum class AppLauncherProviderType {
50         LAUNCHER, MEDIA, DISABLED, TOS_DISABLED, MIRRORING
51     }
52 
53     init {
54         // Add all providers.
55         addProvider(LauncherActivityLaunchProvider)
56         addProvider(MediaServiceLaunchProvider(carMediaManager, launchMediaCenter, onMediaSelected))
57         addProvider(DisabledAppLaunchProvider(packageManager))
58         addProvider(TosDisabledAppLaunchProvider)
59         addProvider(MirroringAppLaunchProvider)
60     }
61 
62     /**
63      * Retrieves the AppLaunchProvider associated with the specified AppLauncherProviderType.
64      *
65      * @param providerType The type of AppLaunchProvider to retrieve.
66      * @return The corresponding AppLaunchProvider if registered, otherwise null.
67      */
getnull68     fun get(providerType: AppLauncherProviderType): AppLaunchProvider? {
69         return providerMap[providerType].also {
70             if (it == null) {
71                 Log.i(TAG, "Launch provider for ${providerType.name} missing")
72             }
73         }
74     }
75 
76     /**
77      * Registers an AppLaunchProvider within the factory. Providers are associated with
78      * their corresponding AppLauncherProviderType.
79      *
80      * @param provider The AppLaunchProvider instance to register.
81      */
addProvidernull82     private fun addProvider(provider: AppLaunchProvider) {
83         when (provider) {
84             is DisabledAppLaunchProvider -> {
85                 providerMap[AppLauncherProviderType.DISABLED] = provider
86             }
87 
88             LauncherActivityLaunchProvider -> {
89                 providerMap[AppLauncherProviderType.LAUNCHER] = provider
90             }
91 
92             is MediaServiceLaunchProvider -> {
93                 providerMap[AppLauncherProviderType.MEDIA] = provider
94             }
95 
96             is MirroringAppLaunchProvider -> {
97                 providerMap[AppLauncherProviderType.MIRRORING] = provider
98             }
99 
100             is TosDisabledAppLaunchProvider -> {
101                 providerMap[AppLauncherProviderType.TOS_DISABLED] = provider
102             }
103         }
104     }
105 
106     companion object {
107         const val TAG = "AppLaunchProviderFactory"
108     }
109 }
110