summaryrefslogtreecommitdiff
path: root/src/com/android/systemui/car/systembar/SystemBarUtil.kt
blob: 633b05ce9027d5836677d90f67f866631238e9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.car.systembar

import android.app.ActivityOptions
import android.car.settings.CarSettings.Secure.KEY_UNACCEPTED_TOS_DISABLED_APPS
import android.content.Context
import android.content.Intent
import android.os.UserHandle
import android.provider.Settings
import android.text.TextUtils
import android.util.ArraySet
import android.util.Log
import com.android.systemui.R
import com.android.systemui.settings.UserTracker
import java.net.URISyntaxException

object SystemBarUtil {
    private const val TAG = "SystemBarUtil"
    private const val TOS_DISABLED_APPS_SEPARATOR = ","

    /**
     * Returns a set of packages that are disabled by tos
     *
     * @param context The application context
     * @param uid A user id for a particular user
     *
     * @return Set of packages disabled by tos
     */
    fun getTosDisabledPackages(context: Context, uid: Int?): Set<String> {
        if (uid == null) {
            return ArraySet()
        }
        val settingsValue = Settings.Secure
                .getStringForUser(context.contentResolver, KEY_UNACCEPTED_TOS_DISABLED_APPS, uid)
        return if (TextUtils.isEmpty(settingsValue)) {
            ArraySet()
        } else {
            settingsValue.split(TOS_DISABLED_APPS_SEPARATOR).toSet()
        }
    }

    /**
     * Gets the intent for launching the TOS acceptance flow
     *
     * @param context The app context
     * @param id The desired resource identifier
     *
     * @return TOS intent, or null
     */
    fun getIntentForTosAcceptanceFlow(context: Context, id: Int): Intent? {
        val tosIntentName = context.resources.getString(id)
        return try {
            Intent.parseUri(tosIntentName, Intent.URI_ANDROID_APP_SCHEME)
        } catch (e: URISyntaxException) {
            Log.e(TAG, "Invalid intent URI in user_tos_activity_intent", e)
            null
        }
    }

    /**
     * Helper method that launches an activity with an intent for a particular user.
     *
     * @param context The app context
     * @param intent The description of the activity to start.
     * @param userId The UserHandle of the user to start this activity for.
     */
    fun launchApp(context: Context, intent: Intent, userId: UserHandle) {
        val options = ActivityOptions.makeBasic()
        options.launchDisplayId = context.displayId
        context.startActivityAsUser(intent, options.toBundle(), userId)
    }

    /**
     * Launch the TOS acceptance flow
     *
     * @param context The app context
     * @param userTracker user tracker object
     */
    fun showTosAcceptanceFlow(context: Context, userTracker: UserTracker?) {
        val tosIntent = getIntentForTosAcceptanceFlow(context, R.string.user_tos_activity_intent)
        val userHandle = userTracker?.userHandle
        if (tosIntent == null) {
            Log.w(TAG, "Unable to launch TOS flow from Assistant because intent is null")
            return
        }
        if (userHandle == null) {
            Log.w(TAG, "Unable to launch TOS flow from Assistant because userid is null")
            return
        }
        launchApp(context, tosIntent, userHandle)
    }
}