1 /*
<lambda>null2 * 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.multiuser.widget.data
18
19 import android.content.ComponentName
20 import android.content.Intent
21 import android.content.res.Resources
22 import android.provider.Settings
23 import android.os.UserManager
24 import com.android.multiuser.widget.R
25 import com.android.multiuser.widget.data.model.UserSwitchRestrictions
26
27 class IntentRepository(val res: Resources) {
28 fun getUserSwitchRestrictedIntent(restriction: UserSwitchRestrictions) =
29 Intent(Settings.ACTION_USER_SETTINGS)
30 .setComponent(
31 ComponentName(
32 "com.android.multiuser",
33 "com.android.multiuser.widget.ui.DialogActivity"
34 )
35 )
36 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
37 .putExtra("title", res.getUserSwitchRestrictedTitle(restriction))
38 .putExtra("message", res.getUserSwitchRestrictedMessage(restriction))
39 .multiuserSettingsIntent(restriction)
40
41 private fun Resources.getUserSwitchRestrictedTitle(
42 restriction: UserSwitchRestrictions
43 ): String {
44 return when (restriction) {
45 UserSwitchRestrictions.WORK_POLICY ->
46 getString(R.string.widget_switch_work_restriction_dialog_title)
47 UserSwitchRestrictions.ONCALL_OR_LOCKED ->
48 getString(R.string.widget_switch_not_allowed_dialog_title)
49 UserSwitchRestrictions.DISABLED ->
50 getString(R.string.widget_switch_disabled_dialog_title)
51 else ->
52 getString(R.string.widget_switch_failed_dialog_title)
53 }
54 }
55
56 private fun Resources.getUserSwitchRestrictedMessage(
57 restriction: UserSwitchRestrictions
58 ): String {
59 return when (restriction) {
60 UserSwitchRestrictions.WORK_POLICY ->
61 getString(R.string.widget_switch_work_restriction_dialog_message)
62 // impossible to show widged on locked system?
63 UserSwitchRestrictions.ONCALL_OR_LOCKED ->
64 getString(R.string.widget_switch_not_allowed_dialog_message)
65 UserSwitchRestrictions.DISABLED ->
66 getString(R.string.widget_switch_disabled_dialog_message)
67 else ->
68 getString(R.string.widget_switch_failed_dialog_message)
69 }
70 }
71
72 private fun Intent.multiuserSettingsIntent(
73 restriction: UserSwitchRestrictions
74 ): Intent {
75 if (restriction == UserSwitchRestrictions.DISABLED) {
76 putExtra("intent_action", Settings.ACTION_USER_SETTINGS)
77 putExtra("intent_package", "com.android.settings")
78 putExtra(
79 "intent_class",
80 "com.android.settings.Settings\$UserSettingsActivity"
81 )
82 putExtra(
83 "intent_flags",
84 Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
85 )
86 putExtra(
87 "action_text",
88 res.getString(
89 R.string.widget_switch_disabled_dialog_open_settings_button
90 )
91 )
92 }
93 return this
94 }
95 }
96
Intentnull97 fun Intent.getActionIntent() : Intent? {
98 var intent: Intent? = null
99 getStringExtra("intent_action")?.let { action ->
100 intent = Intent(action)
101 getStringExtra("intent_package")?.let { intentPackage ->
102 getStringExtra("intent_class")?.let { intentClass ->
103 intent.component = ComponentName(intentPackage, intentClass)
104 }
105 }
106 getIntExtra("intent_flags", 0)?.let { intent.flags = it }
107 }
108 return intent
109 }
110
111
112