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 19 /** 20 * Copyright (C) 2022 The Android Open Source Project 21 * 22 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 23 * in compliance with the License. You may obtain a copy of the License at 24 * 25 * ``` 26 * http://www.apache.org/licenses/LICENSE-2.0 27 * ``` 28 * 29 * Unless required by applicable law or agreed to in writing, software distributed under the License 30 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 31 * or implied. See the License for the specific language governing permissions and limitations under 32 * the License. 33 */ 34 package com.android.healthconnect.controller.shared.app 35 36 import android.content.Context 37 import android.content.pm.ApplicationInfo 38 import android.content.pm.PackageManager 39 import android.content.pm.PackageManager.ApplicationInfoFlags 40 import dagger.hilt.android.qualifiers.ApplicationContext 41 import javax.inject.Inject 42 import javax.inject.Singleton 43 44 @Singleton 45 class AppInfoReader 46 @Inject 47 constructor( 48 @ApplicationContext private val context: Context, 49 private val applicationsInfoUseCase: GetContributorAppInfoUseCase 50 ) { 51 52 private var cache: HashMap<String, AppMetadata> = HashMap() 53 private val packageManager = context.packageManager 54 getAppMetadatanull55 suspend fun getAppMetadata(packageName: String): AppMetadata { 56 if (cache.containsKey(packageName)) { 57 return cache[packageName]!! 58 } else if (isAppInstalled(packageName)) { 59 val app = 60 AppMetadata( 61 packageName = packageName, 62 appName = 63 packageManager.getApplicationLabel(getPackageInfo(packageName)).toString(), 64 icon = packageManager.getApplicationIcon(packageName)) 65 cache[packageName] = app 66 return app 67 } else { 68 val contributorApps = applicationsInfoUseCase.invoke() 69 cache.putAll(contributorApps) 70 return if (contributorApps.containsKey(packageName)) { 71 contributorApps[packageName]!! 72 } else { 73 AppMetadata(packageName = packageName, appName = "", icon = null) 74 } 75 } 76 } 77 isAppInstallednull78 fun isAppInstalled(packageName: String): Boolean { 79 return try { 80 getPackageInfo(packageName).enabled 81 } catch (e: PackageManager.NameNotFoundException) { 82 false 83 } 84 } 85 getPackageInfonull86 private fun getPackageInfo(packageName: String): ApplicationInfo { 87 return packageManager.getApplicationInfo(packageName, ApplicationInfoFlags.of(0)) 88 } 89 } 90