1 /*
2  * Copyright (C) 2019 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.privacy;
18 
19 import android.annotation.NonNull;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.pm.UserInfo;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.util.Log;
28 
29 import androidx.appcompat.app.AlertDialog;
30 import androidx.preference.Preference;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.TogglePreferenceController;
34 import com.android.settings.dashboard.profileselector.UserAdapter;
35 import com.android.settings.utils.ContentCaptureUtils;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 public final class EnableContentCaptureWithServiceSettingsPreferenceController
41         extends TogglePreferenceController {
42 
43     private static final String TAG = "ContentCaptureController";
44 
45     private final UserManager mUserManager;
46 
EnableContentCaptureWithServiceSettingsPreferenceController(@onNull Context context, @NonNull String key)47     public EnableContentCaptureWithServiceSettingsPreferenceController(@NonNull Context context,
48             @NonNull String key) {
49         super(context, key);
50 
51         mUserManager = UserManager.get(context);
52     }
53 
54     @Override
isChecked()55     public boolean isChecked() {
56         return ContentCaptureUtils.isEnabledForUser(mContext);
57     }
58 
59     @Override
setChecked(boolean isChecked)60     public boolean setChecked(boolean isChecked) {
61         ContentCaptureUtils.setEnabledForUser(mContext, isChecked);
62         return true;
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         super.updateState(preference);
68 
69         ComponentName componentName = ContentCaptureUtils.getServiceSettingsComponentName();
70         if (componentName != null) {
71             preference.setIntent(new Intent(Intent.ACTION_MAIN).setComponent(componentName));
72         } else {
73             // Should not happen - preference should be disabled by controller
74             Log.w(TAG, "No component name for custom service settings");
75             preference.setSelectable(false);
76         }
77 
78         preference.setOnPreferenceClickListener((pref) -> {
79             ProfileSelectDialog.show(mContext, pref);
80             return true;
81         });
82     }
83 
84     @Override
getAvailabilityStatus()85     public int getAvailabilityStatus() {
86         boolean available = ContentCaptureUtils.isFeatureAvailable()
87                 && ContentCaptureUtils.getServiceSettingsComponentName() != null;
88         return available ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
89     }
90 
91     @Override
getSliceHighlightMenuRes()92     public int getSliceHighlightMenuRes() {
93         return R.string.menu_key_privacy;
94     }
95 
96     private static final class ProfileSelectDialog {
show(Context context, Preference pref)97         public static void show(Context context, Preference pref) {
98             final UserManager userManager = UserManager.get(context);
99             final List<UserInfo> userInfos = userManager.getUsers();
100             final ArrayList<UserHandle> userHandles = new ArrayList<>(userInfos.size());
101             for (UserInfo info: userInfos) {
102                 userHandles.add(info.getUserHandle());
103             }
104             if (userHandles.size() == 1) {
105                 final Intent intent = pref.getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
106                 context.startActivityAsUser(intent, userHandles.get(0));
107             } else {
108                 AlertDialog.Builder builder = new AlertDialog.Builder(context);
109                 UserAdapter adapter = UserAdapter.createUserAdapter(userManager, context,
110                         userHandles);
111                 builder.setTitle(com.android.settingslib.R.string.choose_profile)
112                         .setAdapter(adapter, (DialogInterface dialog, int which) -> {
113                             final UserHandle user = userHandles.get(which);
114                             // Show menu on top level items.
115                             final Intent intent = pref.getIntent()
116                                     .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
117                             context.startActivityAsUser(intent, user);
118                         })
119                         .show();
120             }
121         }
122     }
123 
124 }
125