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.datasources.restricted
18 
19 import android.content.ContentResolver
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import android.content.pm.PackageManager.GET_RESOLVED_FILTER
23 import android.content.pm.ResolveInfo
24 import android.provider.Settings
25 import android.text.TextUtils
26 import android.util.ArraySet
27 
28 /**
29  * Helper class for Restricted category of launcher apps.
30  */
31 internal object RestrictedAppsUtils {
32 
33     /**
34      * @param contentResolver required to retrieve secure strings from Settings.
35      * @param secureKey key used to store the list of packages.
36      * @param separator separator used for packages in the stored string.
37      *
38      * @return Set of packages stored in [Settings.Secure] with key [secureKey]
39      */
getRestrictedPackagesnull40     fun getRestrictedPackages(
41         contentResolver: ContentResolver,
42         secureKey: String,
43         separator: String
44     ): Set<String> {
45         val settingsValue = Settings.Secure.getString(
46             contentResolver,
47             secureKey
48         )
49 
50         return if (TextUtils.isEmpty(settingsValue)) {
51             ArraySet()
52         } else {
53             ArraySet(
54             listOf(
55                 *settingsValue.split(
56                     separator.toRegex()
57                 ).dropLastWhile { it.isEmpty() }.toTypedArray()
58             )
59         )
60         }
61     }
62 
63     /**
64      * @param packageManager required to queryIntentActivities category [Intent.CATEGORY_LAUNCHER].
65      * @param contentResolver required to retrieve secure string from [Settings.Secure].
66      * @param secureKey key used to store the list of packages.
67      * @param separator separator used for packages in the stored string.
68      *
69      * @return List of ResolveInfo for restricted launcher activities filtered by packages found at
70      *  [Settings.Secure] with key [secureKey].
71      */
getLauncherActivitiesForRestrictedAppsnull72     fun getLauncherActivitiesForRestrictedApps(
73         packageManager: PackageManager,
74         contentResolver: ContentResolver,
75         secureKey: String,
76         separator: String,
77         filter: Int
78     ): List<ResolveInfo> {
79         return packageManager.queryIntentActivities(
80             Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER),
81             PackageManager.ResolveInfoFlags.of(
82                 (GET_RESOLVED_FILTER or filter).toLong()
83             )
84         ).filter {
85             getRestrictedPackages(
86                 contentResolver,
87                 secureKey,
88                 separator
89             ).contains(it.activityInfo.packageName)
90         }
91     }
92 }
93