1 /*
2  * Copyright (C) 2018 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.settings.applications.defaultapps;
18 
19 
20 import android.content.ComponentName;
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 
27 import com.android.settings.Utils;
28 import com.android.settingslib.applications.DefaultAppInfo;
29 
30 public class DefaultWorkAutofillPreferenceController extends DefaultAutofillPreferenceController {
31     private final UserHandle mUserHandle;
32 
DefaultWorkAutofillPreferenceController(Context context)33     public DefaultWorkAutofillPreferenceController(Context context) {
34         super(context);
35         mUserHandle = Utils.getManagedProfile(mUserManager);
36     }
37 
38     @Override
isAvailable()39     public boolean isAvailable() {
40         if (mUserHandle == null) {
41             return false;
42         }
43         return super.isAvailable();
44     }
45 
46     @Override
getPreferenceKey()47     public String getPreferenceKey() {
48         return "default_autofill_work";
49     }
50 
51     @Override
getDefaultAppInfo()52     protected DefaultAppInfo getDefaultAppInfo() {
53         final String flattenComponent = Settings.Secure.getStringForUser(
54                 mContext.getContentResolver(),
55                 DefaultAutofillPicker.SETTING,
56                 mUserHandle.getIdentifier());
57         if (!TextUtils.isEmpty(flattenComponent)) {
58             DefaultAppInfo appInfo = new DefaultAppInfo(
59                     mContext,
60                     mPackageManager,
61                     mUserHandle.getIdentifier(),
62                     ComponentName.unflattenFromString(flattenComponent));
63             return appInfo;
64         }
65         return null;
66     }
67 
68     @Override
getSettingIntent(DefaultAppInfo info)69     protected Intent getSettingIntent(DefaultAppInfo info) {
70         if (info == null) {
71             return null;
72         }
73         final DefaultAutofillPicker.AutofillSettingIntentProvider intentProvider =
74                 new DefaultAutofillPicker.AutofillSettingIntentProvider(
75                         mContext, mUserHandle.getIdentifier(), info.getKey());
76         return intentProvider.getIntent();
77     }
78 
79     @Override
startActivity(Intent intent)80     protected void startActivity(Intent intent) {
81         mContext.startActivityAsUser(intent, mUserHandle);
82     }
83 }
84