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 import androidx.preference.PreferenceScreen; 32 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 private static final class ProfileSelectDialog { show(Context context, Preference pref)92 public static void show(Context context, Preference pref) { 93 final UserManager userManager = UserManager.get(context); 94 final List<UserInfo> userInfos = userManager.getUsers(); 95 final ArrayList<UserHandle> userHandles = new ArrayList<>(userInfos.size()); 96 for (UserInfo info: userInfos) { 97 userHandles.add(info.getUserHandle()); 98 } 99 100 AlertDialog.Builder builder = new AlertDialog.Builder(context); 101 UserAdapter adapter = UserAdapter.createUserAdapter(userManager, context, userHandles); 102 builder.setTitle(com.android.settingslib.R.string.choose_profile) 103 .setAdapter(adapter, (DialogInterface dialog, int which) -> { 104 final UserHandle user = userHandles.get(which); 105 // Show menu on top level items. 106 final Intent intent = pref.getIntent(); 107 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 108 context.startActivityAsUser(intent, user); 109 }) 110 .show(); 111 } 112 } 113 114 } 115