1 /*
<lambda>null2  * 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 package com.android.settings.applications.defaultapps
17 
18 import android.content.ComponentName
19 import android.content.Context
20 import android.content.Intent
21 import android.os.UserHandle
22 import android.provider.Settings
23 import android.text.TextUtils
24 import com.android.settings.Utils
25 import com.android.settings.dashboard.profileselector.ProfileSelectFragment
26 import com.android.settingslib.applications.DefaultAppInfo
27 
28 class DefaultPrivateAutofillPreferenceController(context: Context?) : DefaultAutofillPreferenceController(context) {
29     private val userHandle: UserHandle? = Utils
30             .getProfileOfType(mUserManager, ProfileSelectFragment.ProfileType.PRIVATE)
31 
32     override fun isAvailable(): Boolean {
33         return if (userHandle == null) {
34             false
35         } else super.isAvailable()
36     }
37 
38     override fun getPreferenceKey(): String {
39         return "default_autofill_private"
40     }
41 
42     override fun getDefaultAppInfo(): DefaultAppInfo ? {
43         val flattenComponent = userHandle?.let { handle ->
44             Settings.Secure.getStringForUser(
45                     mContext.contentResolver,
46                     DefaultAutofillPicker.SETTING,
47                     handle.identifier
48             )
49         }
50         return if (!flattenComponent.isNullOrEmpty()) {
51             userHandle?.let {
52                 DefaultAppInfo(
53                         mContext,
54                         mPackageManager,
55                         it.identifier,
56                         ComponentName.unflattenFromString(flattenComponent))
57             }
58         } else null
59     }
60 
61     override fun startActivity(intent: Intent) {
62         if (userHandle == null) {
63             mContext.startActivityAsUser(intent, UserHandle.CURRENT)
64         } else mContext.startActivityAsUser(intent, userHandle)
65     }
66 }