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