• 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 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     const val SYSTEM_BAR_PERSISTENCY_CONFIG_NON_IMMERSIVE = 0
36     const val SYSTEM_BAR_PERSISTENCY_CONFIG_IMMERSIVE = 1
37     const val SYSTEM_BAR_PERSISTENCY_CONFIG_IMMERSIVE_WITH_NAV = 2
38     const val SYSTEM_BAR_PERSISTENCY_CONFIG_BARPOLICY = 3
39     const val VISIBLE_BAR_VISIBILITIES_TYPES_INDEX: Int = 0
40     const val INVISIBLE_BAR_VISIBILITIES_TYPES_INDEX: Int = 1
41 
42     /**
43      * Returns a set of packages that are disabled by tos
44      *
45      * @param context The application context
46      * @param uid A user id for a particular user
47      *
48      * @return Set of packages disabled by tos
49      */
getTosDisabledPackagesnull50     fun getTosDisabledPackages(context: Context, uid: Int?): Set<String> {
51         if (uid == null) {
52             return ArraySet()
53         }
54         val settingsValue = Settings.Secure.getStringForUser(
55             context.contentResolver,
56             KEY_UNACCEPTED_TOS_DISABLED_APPS,
57             uid
58         )
59         return if (TextUtils.isEmpty(settingsValue)) {
60             ArraySet()
61         } else {
62             settingsValue.split(TOS_DISABLED_APPS_SEPARATOR).toSet()
63         }
64     }
65 
66     /**
67      * Gets the intent for launching the TOS acceptance flow
68      *
69      * @param context The app context
70      * @param id The desired resource identifier
71      *
72      * @return TOS intent, or null
73      */
getIntentForTosAcceptanceFlownull74     fun getIntentForTosAcceptanceFlow(context: Context, id: Int): Intent? {
75         val tosIntentName = context.resources.getString(id)
76         return try {
77             Intent.parseUri(tosIntentName, Intent.URI_ANDROID_APP_SCHEME)
78         } catch (e: URISyntaxException) {
79             Log.e(TAG, "Invalid intent URI in user_tos_activity_intent", e)
80             null
81         }
82     }
83 
84     /**
85      * Helper method that launches an activity with an intent for a particular user.
86      *
87      * @param context The app context
88      * @param intent The description of the activity to start.
89      * @param userId The UserHandle of the user to start this activity for.
90      */
launchAppnull91     fun launchApp(context: Context, intent: Intent, userId: UserHandle) {
92         val options = ActivityOptions.makeBasic()
93         options.launchDisplayId = context.displayId
94         context.startActivityAsUser(intent, options.toBundle(), userId)
95     }
96 
97     /**
98      * Launch the TOS acceptance flow
99      *
100      * @param context The app context
101      * @param userTracker user tracker object
102      */
showTosAcceptanceFlownull103     fun showTosAcceptanceFlow(context: Context, userTracker: UserTracker?) {
104         val tosIntent = getIntentForTosAcceptanceFlow(context, R.string.user_tos_activity_intent)
105         val userHandle = userTracker?.userHandle
106         if (tosIntent == null) {
107             Log.w(TAG, "Unable to launch TOS flow from Assistant because intent is null")
108             return
109         }
110         if (userHandle == null) {
111             Log.w(TAG, "Unable to launch TOS flow from Assistant because userid is null")
112             return
113         }
114         launchApp(context, tosIntent, userHandle)
115     }
116 }
117