• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
17  */
18 package com.android.healthconnect.controller.shared.app
19 
20 import android.content.Context
21 import android.content.pm.ApplicationInfo
22 import android.content.pm.PackageManager
23 import android.content.pm.PackageManager.ApplicationInfoFlags
24 import dagger.hilt.android.qualifiers.ApplicationContext
25 import javax.inject.Inject
26 import javax.inject.Singleton
27 
28 @Singleton
29 class AppInfoReader
30 @Inject
31 constructor(
32     @ApplicationContext private val context: Context,
33     private val applicationsInfoUseCase: IGetContributorAppInfoUseCase,
34 ) {
35 
36     private var cache: HashMap<String, AppMetadata> = HashMap()
37     private val packageManager = context.packageManager
38 
getAppMetadatanull39     suspend fun getAppMetadata(packageName: String, isSystem: Boolean = false): AppMetadata {
40         if (cache.containsKey(packageName)) {
41             return cache[packageName]!!
42         }
43         try {
44             val app =
45                 AppMetadata(
46                     packageName = packageName,
47                     appName =
48                         packageManager.getApplicationLabel(getPackageInfo(packageName)).toString(),
49                     icon = packageManager.getApplicationIcon(packageName),
50                     isSystem = isSystem,
51                 )
52             cache[packageName] = app
53             return app
54         } catch (e: PackageManager.NameNotFoundException) {
55             // Fallthrough to reading from storage.
56         }
57         val contributorApps = applicationsInfoUseCase.invoke()
58         cache.putAll(contributorApps)
59         return if (contributorApps.containsKey(packageName)) {
60             contributorApps[packageName]!!
61         } else {
62             AppMetadata(packageName = packageName, appName = "", icon = null)
63         }
64     }
65 
isAppEnablednull66     fun isAppEnabled(packageName: String): Boolean {
67         return try {
68             getPackageInfo(packageName).enabled
69         } catch (e: PackageManager.NameNotFoundException) {
70             false
71         }
72     }
73 
getPackageInfonull74     private fun getPackageInfo(packageName: String): ApplicationInfo {
75         return packageManager.getApplicationInfo(packageName, ApplicationInfoFlags.of(0))
76     }
77 }
78