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 package com.android.systemui.car.systembar
18 
19 import android.app.ActivityOptions
20 import android.car.settings.CarSettings.Secure.KEY_UNACCEPTED_TOS_DISABLED_APPS
21 import android.content.Context
22 import android.content.Intent
23 import android.os.UserHandle
24 import android.provider.Settings
25 import android.text.TextUtils
26 import android.util.ArraySet
27 import android.util.Log
28 import com.android.systemui.R
29 import com.android.systemui.settings.UserTracker
30 import java.net.URISyntaxException
31 
32 object SystemBarUtil {
33     private const val TAG = "SystemBarUtil"
34     private const val TOS_DISABLED_APPS_SEPARATOR = ","
35 
36     /**
37      * Returns a set of packages that are disabled by tos
38      *
39      * @param context The application context
40      * @param uid A user id for a particular user
41      *
42      * @return Set of packages disabled by tos
43      */
getTosDisabledPackagesnull44     fun getTosDisabledPackages(context: Context, uid: Int?): Set<String> {
45         if (uid == null) {
46             return ArraySet()
47         }
48         val settingsValue = Settings.Secure
49                 .getStringForUser(context.contentResolver, KEY_UNACCEPTED_TOS_DISABLED_APPS, uid)
50         return if (TextUtils.isEmpty(settingsValue)) {
51             ArraySet()
52         } else {
53             settingsValue.split(TOS_DISABLED_APPS_SEPARATOR).toSet()
54         }
55     }
56 
57     /**
58      * Gets the intent for launching the TOS acceptance flow
59      *
60      * @param context The app context
61      * @param id The desired resource identifier
62      *
63      * @return TOS intent, or null
64      */
getIntentForTosAcceptanceFlownull65     fun getIntentForTosAcceptanceFlow(context: Context, id: Int): Intent? {
66         val tosIntentName = context.resources.getString(id)
67         return try {
68             Intent.parseUri(tosIntentName, Intent.URI_ANDROID_APP_SCHEME)
69         } catch (e: URISyntaxException) {
70             Log.e(TAG, "Invalid intent URI in user_tos_activity_intent", e)
71             null
72         }
73     }
74 
75     /**
76      * Helper method that launches an activity with an intent for a particular user.
77      *
78      * @param context The app context
79      * @param intent The description of the activity to start.
80      * @param userId The UserHandle of the user to start this activity for.
81      */
launchAppnull82     fun launchApp(context: Context, intent: Intent, userId: UserHandle) {
83         val options = ActivityOptions.makeBasic()
84         options.launchDisplayId = context.displayId
85         context.startActivityAsUser(intent, options.toBundle(), userId)
86     }
87 
88     /**
89      * Launch the TOS acceptance flow
90      *
91      * @param context The app context
92      * @param userTracker user tracker object
93      */
showTosAcceptanceFlownull94     fun showTosAcceptanceFlow(context: Context, userTracker: UserTracker?) {
95         val tosIntent = getIntentForTosAcceptanceFlow(context, R.string.user_tos_activity_intent)
96         val userHandle = userTracker?.userHandle
97         if (tosIntent == null) {
98             Log.w(TAG, "Unable to launch TOS flow from Assistant because intent is null")
99             return
100         }
101         if (userHandle == null) {
102             Log.w(TAG, "Unable to launch TOS flow from Assistant because userid is null")
103             return
104         }
105         launchApp(context, tosIntent, userHandle)
106     }
107 }
108