Refactor DefaultAppPicker to a general radio button picker
Bug: 36557316 Test: make RunSettingsRoboTests Change-Id: I52985e527e55697cf29f8d97d775d4bbed025beb
This commit is contained in:
@@ -79,31 +79,30 @@ public class DefaultAssistPicker extends DefaultAppPickerFragment {
|
||||
continue;
|
||||
}
|
||||
packages.add(packageName);
|
||||
candidates.add(new DefaultAppInfo(mUserId, info.component));
|
||||
candidates.add(new DefaultAppInfo(mPm, mUserId, info.component));
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
final ComponentName cn = getCurrentAssist();
|
||||
if (cn != null) {
|
||||
return new DefaultAppInfo(mUserId, cn).getKey();
|
||||
return new DefaultAppInfo(mPm, mUserId, cn).getKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getConfirmationMessage(DefaultAppInfo appInfo) {
|
||||
protected String getConfirmationMessage(CandidateInfo appInfo) {
|
||||
if (appInfo == null) {
|
||||
return null;
|
||||
}
|
||||
return getContext().getString(R.string.assistant_security_warning,
|
||||
appInfo.loadLabel(mPm.getPackageManager()));
|
||||
return getContext().getString(R.string.assistant_security_warning, appInfo.loadLabel());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
if (TextUtils.isEmpty(key)) {
|
||||
setAssistNone();
|
||||
return true;
|
||||
|
@@ -83,6 +83,6 @@ public class DefaultAssistPreferenceController extends DefaultAppPreferenceContr
|
||||
if (cn == null) {
|
||||
return null;
|
||||
}
|
||||
return new DefaultAppInfo(mUserId, cn);
|
||||
return new DefaultAppInfo(mPackageManager, mUserId, cn);
|
||||
}
|
||||
}
|
||||
|
@@ -19,12 +19,12 @@ package com.android.settings.applications.assist;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.internal.app.AssistUtils;
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.applications.PackageManagerWrapper;
|
||||
import com.android.settings.applications.defaultapps.DefaultAppInfo;
|
||||
import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
|
||||
|
||||
@@ -61,19 +61,19 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
|
||||
for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
|
||||
final boolean enabled = TextUtils.equals(info.key, mAssistRestrict);
|
||||
hasEnabled |= enabled;
|
||||
candidates.add(new VoiceInputDefaultAppInfo(mUserId, info, enabled));
|
||||
candidates.add(new VoiceInputDefaultAppInfo(mPm, mUserId, info, enabled));
|
||||
}
|
||||
|
||||
final boolean assistIsService = !hasEnabled;
|
||||
for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
|
||||
final boolean enabled = !assistIsService;
|
||||
candidates.add(new VoiceInputDefaultAppInfo(mUserId, info, enabled));
|
||||
candidates.add(new VoiceInputDefaultAppInfo(mPm, mUserId, info, enabled));
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
final ComponentName currentService = getCurrentService(mHelper);
|
||||
if (currentService == null) {
|
||||
return null;
|
||||
@@ -82,7 +82,7 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String value) {
|
||||
protected boolean setDefaultKey(String value) {
|
||||
for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
|
||||
if (TextUtils.equals(value, info.key)) {
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
@@ -132,9 +132,9 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
|
||||
|
||||
public VoiceInputHelper.BaseInfo mInfo;
|
||||
|
||||
public VoiceInputDefaultAppInfo(int userId, VoiceInputHelper.BaseInfo info,
|
||||
boolean enabled) {
|
||||
super(userId, info.componentName, null /* summary */, enabled);
|
||||
public VoiceInputDefaultAppInfo(PackageManagerWrapper pm, int userId,
|
||||
VoiceInputHelper.BaseInfo info, boolean enabled) {
|
||||
super(pm, userId, info.componentName, null /* summary */, enabled);
|
||||
mInfo = info;
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence loadLabel(PackageManager pm) {
|
||||
public CharSequence loadLabel() {
|
||||
if (mInfo instanceof VoiceInputHelper.InteractionInfo) {
|
||||
return mInfo.appLabel;
|
||||
} else {
|
||||
|
@@ -104,14 +104,14 @@ public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceC
|
||||
}
|
||||
for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
|
||||
if (TextUtils.equals(defaultKey, info.key)) {
|
||||
return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(
|
||||
return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mPackageManager,
|
||||
mUserId, info, true /* enabled */);
|
||||
}
|
||||
}
|
||||
|
||||
for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
|
||||
if (TextUtils.equals(defaultKey, info.key)) {
|
||||
return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(
|
||||
return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mPackageManager,
|
||||
mUserId, info, true /* enabled */);
|
||||
}
|
||||
}
|
||||
|
@@ -26,91 +26,101 @@ import android.graphics.drawable.Drawable;
|
||||
import android.os.RemoteException;
|
||||
import android.os.UserHandle;
|
||||
|
||||
import com.android.settings.applications.PackageManagerWrapper;
|
||||
import com.android.settings.widget.RadioButtonPickerFragment;
|
||||
|
||||
/**
|
||||
* Data model representing an app in DefaultAppPicker UI.
|
||||
*/
|
||||
public class DefaultAppInfo {
|
||||
public class DefaultAppInfo extends RadioButtonPickerFragment.CandidateInfo {
|
||||
|
||||
public final int userId;
|
||||
public final ComponentName componentName;
|
||||
public final PackageItemInfo packageItemInfo;
|
||||
public final String summary;
|
||||
public final boolean enabled;
|
||||
protected final PackageManagerWrapper mPm;
|
||||
|
||||
public DefaultAppInfo(int uid, ComponentName cn) {
|
||||
this(uid, cn, null /* summary */);
|
||||
public DefaultAppInfo(PackageManagerWrapper pm, int uid, ComponentName cn) {
|
||||
this(pm, uid, cn, null /* summary */);
|
||||
}
|
||||
|
||||
public DefaultAppInfo(int uid, ComponentName cn, String summary) {
|
||||
this(uid, cn, summary, true /* enabled */);
|
||||
public DefaultAppInfo(PackageManagerWrapper pm, int uid, ComponentName cn, String summary) {
|
||||
this(pm, uid, cn, summary, true /* enabled */);
|
||||
}
|
||||
|
||||
public DefaultAppInfo(int uid, ComponentName cn, String summary, boolean enabled) {
|
||||
public DefaultAppInfo(PackageManagerWrapper pm, int uid, ComponentName cn, String summary,
|
||||
boolean enabled) {
|
||||
super(enabled);
|
||||
mPm = pm;
|
||||
packageItemInfo = null;
|
||||
userId = uid;
|
||||
componentName = cn;
|
||||
this.summary = summary;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public DefaultAppInfo(PackageItemInfo info, String summary, boolean enabled) {
|
||||
public DefaultAppInfo(PackageManagerWrapper pm, PackageItemInfo info, String summary,
|
||||
boolean enabled) {
|
||||
super(enabled);
|
||||
mPm = pm;
|
||||
userId = UserHandle.myUserId();
|
||||
packageItemInfo = info;
|
||||
componentName = null;
|
||||
this.summary = summary;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public DefaultAppInfo(PackageItemInfo info) {
|
||||
this(info, null /* summary */, true /* enabled */);
|
||||
public DefaultAppInfo(PackageManagerWrapper pm, PackageItemInfo info) {
|
||||
this(pm, info, null /* summary */, true /* enabled */);
|
||||
}
|
||||
|
||||
public CharSequence loadLabel(PackageManager pm) {
|
||||
@Override
|
||||
public CharSequence loadLabel() {
|
||||
if (componentName != null) {
|
||||
try {
|
||||
final ActivityInfo actInfo = AppGlobals.getPackageManager().getActivityInfo(
|
||||
componentName, 0, userId);
|
||||
if (actInfo != null) {
|
||||
return actInfo.loadLabel(pm);
|
||||
return actInfo.loadLabel(mPm.getPackageManager());
|
||||
} else {
|
||||
final ApplicationInfo appInfo = pm.getApplicationInfoAsUser(
|
||||
final ApplicationInfo appInfo = mPm.getApplicationInfoAsUser(
|
||||
componentName.getPackageName(), 0, userId);
|
||||
return appInfo.loadLabel(pm);
|
||||
return appInfo.loadLabel(mPm.getPackageManager());
|
||||
}
|
||||
} catch (RemoteException | PackageManager.NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
} else if (packageItemInfo != null) {
|
||||
return packageItemInfo.loadLabel(pm);
|
||||
return packageItemInfo.loadLabel(mPm.getPackageManager());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Drawable loadIcon(PackageManager pm) {
|
||||
@Override
|
||||
public Drawable loadIcon() {
|
||||
if (componentName != null) {
|
||||
try {
|
||||
final ActivityInfo actInfo = AppGlobals.getPackageManager().getActivityInfo(
|
||||
componentName, 0, userId);
|
||||
if (actInfo != null) {
|
||||
return actInfo.loadIcon(pm);
|
||||
return actInfo.loadIcon(mPm.getPackageManager());
|
||||
} else {
|
||||
final ApplicationInfo appInfo = pm.getApplicationInfoAsUser(
|
||||
final ApplicationInfo appInfo = mPm.getApplicationInfoAsUser(
|
||||
componentName.getPackageName(), 0, userId);
|
||||
return appInfo.loadIcon(pm);
|
||||
return appInfo.loadIcon(mPm.getPackageManager());
|
||||
}
|
||||
} catch (RemoteException | PackageManager.NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (packageItemInfo != null) {
|
||||
return packageItemInfo.loadIcon(pm);
|
||||
return packageItemInfo.loadIcon(mPm.getPackageManager());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
if (componentName != null) {
|
||||
return componentName.flattenToString();
|
||||
|
@@ -24,113 +24,36 @@ import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.applications.PackageManagerWrapper;
|
||||
import com.android.settings.applications.PackageManagerWrapperImpl;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||
import com.android.settings.widget.RadioButtonPickerFragment;
|
||||
import com.android.settings.widget.RadioButtonPreference;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A generic app picker fragment that shows a list of app as radio button group.
|
||||
*/
|
||||
public abstract class DefaultAppPickerFragment extends InstrumentedPreferenceFragment implements
|
||||
RadioButtonPreference.OnClickListener {
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
static final String EXTRA_FOR_WORK = "for_work";
|
||||
|
||||
private final Map<String, DefaultAppInfo> mCandidates = new ArrayMap<>();
|
||||
public abstract class DefaultAppPickerFragment extends RadioButtonPickerFragment {
|
||||
|
||||
protected PackageManagerWrapper mPm;
|
||||
protected UserManager mUserManager;
|
||||
protected int mUserId;
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mPm = new PackageManagerWrapperImpl(context.getPackageManager());
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
final Bundle arguments = getArguments();
|
||||
|
||||
boolean mForWork = false;
|
||||
if (arguments != null) {
|
||||
mForWork = arguments.getBoolean(EXTRA_FOR_WORK);
|
||||
}
|
||||
final UserHandle managedProfile = Utils.getManagedProfile(mUserManager);
|
||||
mUserId = mForWork && managedProfile != null
|
||||
? managedProfile.getIdentifier()
|
||||
: UserHandle.myUserId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
final View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
super.onCreatePreferences(savedInstanceState, rootKey);
|
||||
addPreferencesFromResource(R.xml.app_picker_prefs);
|
||||
updateCandidates();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void updateCandidates() {
|
||||
mCandidates.clear();
|
||||
final List<? extends DefaultAppInfo> candidateList = getCandidates();
|
||||
if (candidateList != null) {
|
||||
for (DefaultAppInfo info : candidateList) {
|
||||
mCandidates.put(info.getKey(), info);
|
||||
}
|
||||
}
|
||||
final String defaultAppKey = getDefaultAppKey();
|
||||
final String systemDefaultAppKey = getSystemDefaultAppKey();
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
screen.removeAll();
|
||||
if (shouldShowItemNone()) {
|
||||
final RadioButtonPreference nonePref = new RadioButtonPreference(getPrefContext());
|
||||
nonePref.setIcon(R.drawable.ic_remove_circle);
|
||||
nonePref.setTitle(R.string.app_list_preference_none);
|
||||
nonePref.setChecked(TextUtils.isEmpty(defaultAppKey));
|
||||
nonePref.setOnClickListener(this);
|
||||
screen.addPreference(nonePref);
|
||||
}
|
||||
for (Map.Entry<String, DefaultAppInfo> app : mCandidates.entrySet()) {
|
||||
RadioButtonPreference pref = new RadioButtonPreference(getPrefContext());
|
||||
configurePreferenceFromAppInfo(
|
||||
pref, app.getKey(), app.getValue(), defaultAppKey, systemDefaultAppKey);
|
||||
screen.addPreference(pref);
|
||||
}
|
||||
mayCheckOnlyRadioButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRadioButtonClicked(RadioButtonPreference selected) {
|
||||
final String selectedKey = selected.getKey();
|
||||
final String confirmationMessage = getConfirmationMessage(mCandidates.get(selectedKey));
|
||||
final String confirmationMessage = getConfirmationMessage(getCandidate(selectedKey));
|
||||
final Activity activity = getActivity();
|
||||
if (TextUtils.isEmpty(confirmationMessage)) {
|
||||
onRadioButtonConfirmed(selectedKey);
|
||||
super.onRadioButtonClicked(selected);
|
||||
} else if (activity != null) {
|
||||
final DialogFragment fragment = ConfirmationDialogFragment.newInstance(
|
||||
this, selectedKey, confirmationMessage);
|
||||
@@ -138,63 +61,21 @@ public abstract class DefaultAppPickerFragment extends InstrumentedPreferenceFra
|
||||
}
|
||||
}
|
||||
|
||||
private void onRadioButtonConfirmed(String selectedKey) {
|
||||
final boolean success = setDefaultAppKey(selectedKey);
|
||||
if (success) {
|
||||
updateCheckedState(selectedKey);
|
||||
}
|
||||
onSelectionPerformed(success);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void updateCheckedState(String selectedKey) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
if (screen != null) {
|
||||
final int count = screen.getPreferenceCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
final Preference pref = screen.getPreference(i);
|
||||
if (pref instanceof RadioButtonPreference) {
|
||||
final RadioButtonPreference radioPref = (RadioButtonPreference) pref;
|
||||
final boolean newCheckedState = TextUtils.equals(pref.getKey(), selectedKey);
|
||||
if (radioPref.isChecked() != newCheckedState) {
|
||||
radioPref.setChecked(TextUtils.equals(pref.getKey(), selectedKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void bindPreferenceExtra(RadioButtonPreference pref,
|
||||
String key, CandidateInfo info, String defaultKey, String systemDefaultKey) {
|
||||
if (!(info instanceof DefaultAppInfo)) {
|
||||
return;
|
||||
}
|
||||
if (TextUtils.equals(systemDefaultKey, key)) {
|
||||
pref.setSummary(R.string.system_app);
|
||||
} else if (!TextUtils.isEmpty(((DefaultAppInfo) info).summary)) {
|
||||
pref.setSummary(((DefaultAppInfo) info).summary);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
void mayCheckOnlyRadioButton() {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
// If there is only 1 thing on screen, select it.
|
||||
if (screen != null && screen.getPreferenceCount() == 1) {
|
||||
final Preference onlyPref = screen.getPreference(0);
|
||||
if (onlyPref instanceof RadioButtonPreference) {
|
||||
((RadioButtonPreference) onlyPref).setChecked(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean shouldShowItemNone() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected String getSystemDefaultAppKey() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract List<? extends DefaultAppInfo> getCandidates();
|
||||
|
||||
protected abstract String getDefaultAppKey();
|
||||
|
||||
protected abstract boolean setDefaultAppKey(String key);
|
||||
|
||||
// Called after the user tries to select an item.
|
||||
protected void onSelectionPerformed(boolean success) {
|
||||
}
|
||||
|
||||
protected String getConfirmationMessage(DefaultAppInfo appInfo) {
|
||||
protected String getConfirmationMessage(CandidateInfo info) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -241,23 +122,4 @@ public abstract class DefaultAppPickerFragment extends InstrumentedPreferenceFra
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public RadioButtonPreference configurePreferenceFromAppInfo(RadioButtonPreference pref,
|
||||
String appKey, DefaultAppInfo info, String defaultAppKey, String systemDefaultAppKey) {
|
||||
pref.setTitle(info.loadLabel(mPm.getPackageManager()));
|
||||
pref.setIcon(info.loadIcon(mPm.getPackageManager()));
|
||||
pref.setKey(appKey);
|
||||
if (TextUtils.equals(defaultAppKey, appKey)) {
|
||||
pref.setChecked(true);
|
||||
}
|
||||
if (TextUtils.equals(systemDefaultAppKey, appKey)) {
|
||||
pref.setSummary(R.string.system_app);
|
||||
} else if (!TextUtils.isEmpty(info.summary)) {
|
||||
pref.setSummary(info.summary);
|
||||
}
|
||||
pref.setEnabled(info.enabled);
|
||||
pref.setOnClickListener(this);
|
||||
return pref;
|
||||
}
|
||||
}
|
||||
|
@@ -51,7 +51,7 @@ public abstract class DefaultAppPreferenceController extends PreferenceControlle
|
||||
final DefaultAppInfo app = getDefaultAppInfo();
|
||||
CharSequence defaultAppLabel = null;
|
||||
if (app != null) {
|
||||
defaultAppLabel = app.loadLabel(mPackageManager.getPackageManager());
|
||||
defaultAppLabel = app.loadLabel();
|
||||
}
|
||||
if (!TextUtils.isEmpty(defaultAppLabel)) {
|
||||
preference.setSummary(defaultAppLabel);
|
||||
|
@@ -56,34 +56,34 @@ public class DefaultAutofillPicker extends DefaultAppPickerFragment {
|
||||
final List<ResolveInfo> resolveInfos = mPm.getPackageManager()
|
||||
.queryIntentServices(AUTOFILL_PROBE, PackageManager.GET_META_DATA);
|
||||
for (ResolveInfo info : resolveInfos) {
|
||||
candidates.add(new DefaultAppInfo(mUserId, new ComponentName(
|
||||
candidates.add(new DefaultAppInfo(mPm, mUserId, new ComponentName(
|
||||
info.serviceInfo.packageName, info.serviceInfo.name)));
|
||||
}
|
||||
final List<ResolveInfo> oldResolveInfos = mPm.getPackageManager()
|
||||
.queryIntentServices(OLD_AUTO_FILL_PROBE, PackageManager.GET_META_DATA);
|
||||
for (ResolveInfo info : oldResolveInfos) {
|
||||
candidates.add(new DefaultAppInfo(mUserId, new ComponentName(
|
||||
candidates.add(new DefaultAppInfo(mPm, mUserId, new ComponentName(
|
||||
info.serviceInfo.packageName, info.serviceInfo.name)));
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
return Settings.Secure.getString(getContext().getContentResolver(), SETTING);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getConfirmationMessage(DefaultAppInfo appInfo) {
|
||||
protected String getConfirmationMessage(CandidateInfo appInfo) {
|
||||
if (appInfo == null) {
|
||||
return null;
|
||||
}
|
||||
final CharSequence appName = appInfo.loadLabel(mPm.getPackageManager());
|
||||
final CharSequence appName = appInfo.loadLabel();
|
||||
return getContext().getString(R.string.autofill_confirmation_message, appName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
Settings.Secure.putString(getContext().getContentResolver(), SETTING, key);
|
||||
return true;
|
||||
}
|
||||
|
@@ -54,7 +54,7 @@ public class DefaultAutofillPreferenceController extends DefaultAppPreferenceCon
|
||||
final String flattenComponent = Settings.Secure.getString(mContext.getContentResolver(),
|
||||
DefaultAutofillPicker.SETTING);
|
||||
if (!TextUtils.isEmpty(flattenComponent)) {
|
||||
DefaultAppInfo appInfo = new DefaultAppInfo(
|
||||
DefaultAppInfo appInfo = new DefaultAppInfo(mPackageManager,
|
||||
mUserId, ComponentName.unflattenFromString(flattenComponent));
|
||||
return appInfo;
|
||||
}
|
||||
|
@@ -35,12 +35,12 @@ public class DefaultBrowserPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
return mPm.getDefaultBrowserPackageNameAsUser(mUserId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String packageName) {
|
||||
protected boolean setDefaultKey(String packageName) {
|
||||
return mPm.setDefaultBrowserPackageNameAsUser(packageName, mUserId);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public class DefaultBrowserPicker extends DefaultAppPickerFragment {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
candidates.add(new DefaultAppInfo(
|
||||
candidates.add(new DefaultAppInfo(mPm,
|
||||
mPm.getApplicationInfoAsUser(info.activityInfo.packageName, 0, mUserId)));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
// Skip unknown packages.
|
||||
|
@@ -52,8 +52,7 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
final DefaultAppInfo defaultApp = getDefaultAppInfo();
|
||||
final CharSequence defaultAppLabel = defaultApp != null
|
||||
? defaultApp.loadLabel(mPackageManager.getPackageManager()) : null;
|
||||
final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null;
|
||||
if (TextUtils.isEmpty(defaultAppLabel)) {
|
||||
final String onlyAppLabel = getOnlyAppLabel();
|
||||
if (!TextUtils.isEmpty(onlyAppLabel)) {
|
||||
@@ -65,8 +64,9 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont
|
||||
@Override
|
||||
protected DefaultAppInfo getDefaultAppInfo() {
|
||||
try {
|
||||
return new DefaultAppInfo(mPackageManager.getPackageManager().getApplicationInfo(
|
||||
mPackageManager.getDefaultBrowserPackageNameAsUser(mUserId), 0));
|
||||
return new DefaultAppInfo(mPackageManager,
|
||||
mPackageManager.getPackageManager().getApplicationInfo(
|
||||
mPackageManager.getDefaultBrowserPackageNameAsUser(mUserId), 0));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
@@ -49,7 +49,7 @@ public class DefaultEmergencyPicker extends DefaultAppPickerFragment {
|
||||
final PackageInfo packageInfo =
|
||||
mPm.getPackageManager().getPackageInfo(info.activityInfo.packageName, 0);
|
||||
final ApplicationInfo appInfo = packageInfo.applicationInfo;
|
||||
candidates.add(new DefaultAppInfo(appInfo));
|
||||
candidates.add(new DefaultAppInfo(mPm, appInfo));
|
||||
// Get earliest installed system app.
|
||||
if (isSystemApp(appInfo) && (bestMatch == null ||
|
||||
bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
|
||||
@@ -59,9 +59,9 @@ public class DefaultEmergencyPicker extends DefaultAppPickerFragment {
|
||||
// Skip unknown packages.
|
||||
}
|
||||
if (bestMatch != null) {
|
||||
final String defaultKey = getDefaultAppKey();
|
||||
final String defaultKey = getDefaultKey();
|
||||
if (TextUtils.isEmpty(defaultKey)) {
|
||||
setDefaultAppKey(bestMatch.packageName);
|
||||
setDefaultKey(bestMatch.packageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,19 +69,19 @@ public class DefaultEmergencyPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getConfirmationMessage(DefaultAppInfo info) {
|
||||
protected String getConfirmationMessage(CandidateInfo info) {
|
||||
return Utils.isPackageDirectBootAware(getContext(), info.getKey()) ? null
|
||||
: getContext().getString(R.string.direct_boot_unaware_dialog_message);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
return Settings.Secure.getString(getContext().getContentResolver(),
|
||||
Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
final ContentResolver contentResolver = getContext().getContentResolver();
|
||||
final String previousValue = Settings.Secure.getString(contentResolver,
|
||||
Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
|
||||
|
@@ -68,14 +68,15 @@ public class DefaultHomePicker extends DefaultAppPickerFragment {
|
||||
} else {
|
||||
summary = null;
|
||||
}
|
||||
final DefaultAppInfo candidate = new DefaultAppInfo(mUserId, activityName, summary);
|
||||
final DefaultAppInfo candidate =
|
||||
new DefaultAppInfo(mPm, mUserId, activityName, summary);
|
||||
candidates.add(candidate);
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
final ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
|
||||
final ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);
|
||||
if (currentDefaultHome != null) {
|
||||
@@ -85,7 +86,7 @@ public class DefaultHomePicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
if (!TextUtils.isEmpty(key)) {
|
||||
final ComponentName component = ComponentName.unflattenFromString(key);
|
||||
final List<ResolveInfo> homeActivities = new ArrayList<>();
|
||||
|
@@ -60,8 +60,7 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
final DefaultAppInfo defaultApp = getDefaultAppInfo();
|
||||
final CharSequence defaultAppLabel = defaultApp != null
|
||||
? defaultApp.loadLabel(mPackageManager.getPackageManager()) : null;
|
||||
final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null;
|
||||
if (TextUtils.isEmpty(defaultAppLabel)) {
|
||||
final String onlyAppLabel = getOnlyAppLabel();
|
||||
if (!TextUtils.isEmpty(onlyAppLabel)) {
|
||||
@@ -75,7 +74,7 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
|
||||
final ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
|
||||
final ComponentName currentDefaultHome = mPackageManager.getHomeActivities(homeActivities);
|
||||
|
||||
return new DefaultAppInfo(mUserId, currentDefaultHome);
|
||||
return new DefaultAppInfo(mPackageManager, mUserId, currentDefaultHome);
|
||||
}
|
||||
|
||||
private String getOnlyAppLabel() {
|
||||
|
@@ -43,12 +43,12 @@ public class DefaultNotificationAssistantPicker extends DefaultAppPickerFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
return Settings.Secure.getString(getContext().getContentResolver(), mConfig.setting);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String value) {
|
||||
protected boolean setDefaultKey(String value) {
|
||||
Settings.Secure.putString(getContext().getContentResolver(), mConfig.setting, value);
|
||||
return true;
|
||||
}
|
||||
@@ -75,7 +75,7 @@ public class DefaultNotificationAssistantPicker extends DefaultAppPickerFragment
|
||||
continue;
|
||||
}
|
||||
|
||||
candidates.add(new DefaultAppInfo(
|
||||
candidates.add(new DefaultAppInfo(mPm,
|
||||
mUserId, new ComponentName(info.packageName, info.name)));
|
||||
}
|
||||
return candidates;
|
||||
|
@@ -50,7 +50,7 @@ public class DefaultPhonePicker extends DefaultAppPickerFragment {
|
||||
DefaultDialerManager.getInstalledDialerApplications(getContext(), mUserId);
|
||||
for (String packageName : dialerPackages) {
|
||||
try {
|
||||
candidates.add(new DefaultAppInfo(
|
||||
candidates.add(new DefaultAppInfo(mPm,
|
||||
mPm.getApplicationInfoAsUser(packageName, 0, mUserId)));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
// Skip unknown packages.
|
||||
@@ -60,18 +60,18 @@ public class DefaultPhonePicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
return mDefaultKeyUpdater.getDefaultDialerApplication(getContext(), mUserId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSystemDefaultAppKey() {
|
||||
protected String getSystemDefaultKey() {
|
||||
return mDefaultKeyUpdater.getSystemDialerPackage();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultAppKey())) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultKey())) {
|
||||
return mDefaultKeyUpdater.setDefaultDialerApplication(getContext(), key, mUserId);
|
||||
}
|
||||
return false;
|
||||
|
@@ -57,8 +57,10 @@ public class DefaultPhonePreferenceController extends DefaultAppPreferenceContro
|
||||
@Override
|
||||
protected DefaultAppInfo getDefaultAppInfo() {
|
||||
try {
|
||||
return new DefaultAppInfo(mPackageManager.getPackageManager().getApplicationInfo(
|
||||
DefaultDialerManager.getDefaultDialerApplication(mContext, mUserId), 0));
|
||||
return new DefaultAppInfo(mPackageManager,
|
||||
mPackageManager.getPackageManager().getApplicationInfo(
|
||||
DefaultDialerManager.getDefaultDialerApplication(mContext, mUserId),
|
||||
0));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ public class DefaultSmsPicker extends DefaultAppPickerFragment {
|
||||
|
||||
for (SmsApplication.SmsApplicationData smsApplicationData : smsApplications) {
|
||||
try {
|
||||
candidates.add(new DefaultAppInfo(
|
||||
candidates.add(new DefaultAppInfo(mPm,
|
||||
mPm.getApplicationInfoAsUser(smsApplicationData.mPackageName, 0, mUserId)));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
// Skip unknown packages.
|
||||
@@ -58,13 +58,13 @@ public class DefaultSmsPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
return mDefaultKeyUpdater.getDefaultApplication(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultAppKey())) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultKey())) {
|
||||
mDefaultKeyUpdater.setDefaultApplication(getContext(), key);
|
||||
return true;
|
||||
}
|
||||
@@ -72,7 +72,7 @@ public class DefaultSmsPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getConfirmationMessage(DefaultAppInfo info) {
|
||||
protected String getConfirmationMessage(CandidateInfo info) {
|
||||
return Utils.isPackageDirectBootAware(getContext(), info.getKey()) ? null
|
||||
: getContext().getString(R.string.direct_boot_unaware_dialog_message);
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ public class DefaultSmsPreferenceController extends DefaultAppPreferenceControll
|
||||
protected DefaultAppInfo getDefaultAppInfo() {
|
||||
final ComponentName app = SmsApplication.getDefaultSmsApplication(mContext, true);
|
||||
if (app != null) {
|
||||
return new DefaultAppInfo(mUserId, app);
|
||||
return new DefaultAppInfo(mPackageManager, mUserId, app);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.android.settings.location;
|
||||
|
||||
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
@@ -49,8 +51,6 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
|
||||
/**
|
||||
* System location settings (Settings > Location). The screen has three parts:
|
||||
* <ul>
|
||||
|
@@ -19,17 +19,18 @@ package com.android.settings.webview;
|
||||
import static android.provider.Settings.ACTION_WEBVIEW_SETTINGS;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageItemInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageItemInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.applications.PackageManagerWrapper;
|
||||
import com.android.settings.applications.defaultapps.DefaultAppInfo;
|
||||
import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
|
||||
|
||||
@@ -61,7 +62,7 @@ public class WebViewAppPicker extends DefaultAppPickerFragment {
|
||||
List<ApplicationInfo> pkgs =
|
||||
getWebViewUpdateServiceWrapper().getValidWebViewApplicationInfos(getContext());
|
||||
for (ApplicationInfo ai : pkgs) {
|
||||
packageInfoList.add(createDefaultAppInfo(ai,
|
||||
packageInfoList.add(createDefaultAppInfo(mPm, ai,
|
||||
getDisabledReason(getWebViewUpdateServiceWrapper(),
|
||||
getContext(), ai.packageName)));
|
||||
}
|
||||
@@ -69,12 +70,12 @@ public class WebViewAppPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultAppKey() {
|
||||
protected String getDefaultKey() {
|
||||
PackageInfo currentPackage = getWebViewUpdateServiceWrapper().getCurrentWebViewPackage();
|
||||
return currentPackage == null ? null : currentPackage.packageName;
|
||||
}
|
||||
|
||||
protected boolean setDefaultAppKey(String key) {
|
||||
protected boolean setDefaultKey(String key) {
|
||||
boolean success = getWebViewUpdateServiceWrapper().setWebViewProvider(key);
|
||||
return success;
|
||||
}
|
||||
@@ -110,25 +111,28 @@ public class WebViewAppPicker extends DefaultAppPickerFragment {
|
||||
}
|
||||
|
||||
private static class WebViewAppInfo extends DefaultAppInfo {
|
||||
public WebViewAppInfo(PackageItemInfo packageItemInfo, String summary, boolean enabled) {
|
||||
super(packageItemInfo, summary, enabled);
|
||||
public WebViewAppInfo(PackageManagerWrapper pm, PackageItemInfo packageItemInfo,
|
||||
String summary, boolean enabled) {
|
||||
super(pm, packageItemInfo, summary, enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence loadLabel(PackageManager pm) {
|
||||
public CharSequence loadLabel() {
|
||||
String versionName = "";
|
||||
try {
|
||||
versionName = pm.getPackageInfo(packageItemInfo.packageName, 0).versionName;
|
||||
versionName = mPm.getPackageManager().
|
||||
getPackageInfo(packageItemInfo.packageName, 0).versionName;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
}
|
||||
return String.format("%s %s", super.loadLabel(pm), versionName);
|
||||
return String.format("%s %s", super.loadLabel(), versionName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@VisibleForTesting
|
||||
DefaultAppInfo createDefaultAppInfo(PackageItemInfo packageItemInfo, String disabledReason) {
|
||||
return new WebViewAppInfo(packageItemInfo, disabledReason,
|
||||
DefaultAppInfo createDefaultAppInfo(PackageManagerWrapper pm, PackageItemInfo packageItemInfo,
|
||||
String disabledReason) {
|
||||
return new WebViewAppInfo(pm, packageItemInfo, disabledReason,
|
||||
TextUtils.isEmpty(disabledReason) /* enabled */);
|
||||
}
|
||||
|
||||
@@ -139,7 +143,6 @@ public class WebViewAppPicker extends DefaultAppPickerFragment {
|
||||
@VisibleForTesting
|
||||
String getDisabledReason(WebViewUpdateServiceWrapper webviewUpdateServiceWrapper,
|
||||
Context context, String packageName) {
|
||||
StringBuilder disabledReason = new StringBuilder();
|
||||
List<UserPackageWrapper> userPackages =
|
||||
webviewUpdateServiceWrapper.getPackageInfosAllUsers(context, packageName);
|
||||
for (UserPackageWrapper userPackage : userPackages) {
|
||||
@@ -150,7 +153,7 @@ public class WebViewAppPicker extends DefaultAppPickerFragment {
|
||||
} else if (!userPackage.isEnabledPackage()) {
|
||||
// Package disabled
|
||||
return context.getString(
|
||||
R.string.webview_disabled_for_user, userPackage.getUserInfo().name);
|
||||
R.string.webview_disabled_for_user, userPackage.getUserInfo().name);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@@ -26,9 +26,9 @@ public class WebViewAppPreferenceController extends DefaultAppPreferenceControll
|
||||
|
||||
private static final String WEBVIEW_APP_KEY = "select_webview_provider";
|
||||
|
||||
private Context mContext;
|
||||
private Preference mPreference;
|
||||
private final Context mContext;
|
||||
private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
|
||||
private Preference mPreference;
|
||||
|
||||
public WebViewAppPreferenceController(Context context) {
|
||||
this(context, new WebViewUpdateServiceWrapper());
|
||||
@@ -44,7 +44,8 @@ public class WebViewAppPreferenceController extends DefaultAppPreferenceControll
|
||||
@Override
|
||||
public DefaultAppInfo getDefaultAppInfo() {
|
||||
PackageInfo currentPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
|
||||
return new DefaultAppInfo(currentPackage == null ? null : currentPackage.applicationInfo);
|
||||
return new DefaultAppInfo(mPackageManager,
|
||||
currentPackage == null ? null : currentPackage.applicationInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
221
src/com/android/settings/widget/RadioButtonPickerFragment.java
Normal file
221
src/com/android/settings/widget/RadioButtonPickerFragment.java
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class RadioButtonPickerFragment extends InstrumentedPreferenceFragment implements
|
||||
RadioButtonPreference.OnClickListener {
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
static final String EXTRA_FOR_WORK = "for_work";
|
||||
|
||||
private final Map<String, CandidateInfo> mCandidates = new ArrayMap<>();
|
||||
|
||||
protected UserManager mUserManager;
|
||||
protected int mUserId;
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
final Bundle arguments = getArguments();
|
||||
|
||||
boolean mForWork = false;
|
||||
if (arguments != null) {
|
||||
mForWork = arguments.getBoolean(EXTRA_FOR_WORK);
|
||||
}
|
||||
final UserHandle managedProfile = Utils.getManagedProfile(mUserManager);
|
||||
mUserId = mForWork && managedProfile != null
|
||||
? managedProfile.getIdentifier()
|
||||
: UserHandle.myUserId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
super.onCreatePreferences(savedInstanceState, rootKey);
|
||||
addPreferencesFromResource(R.xml.placeholder_prefs);
|
||||
updateCandidates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
final View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRadioButtonClicked(RadioButtonPreference selected) {
|
||||
final String selectedKey = selected.getKey();
|
||||
onRadioButtonConfirmed(selectedKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after the user tries to select an item.
|
||||
*/
|
||||
protected void onSelectionPerformed(boolean success) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the UI should show a "None" item selection.
|
||||
*/
|
||||
protected boolean shouldShowItemNone() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected CandidateInfo getCandidate(String key) {
|
||||
return mCandidates.get(key);
|
||||
}
|
||||
|
||||
protected void onRadioButtonConfirmed(String selectedKey) {
|
||||
final boolean success = setDefaultKey(selectedKey);
|
||||
if (success) {
|
||||
updateCheckedState(selectedKey);
|
||||
}
|
||||
onSelectionPerformed(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* A chance for subclasses to bind additional things to the preference.
|
||||
*/
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
|
||||
public void bindPreferenceExtra(RadioButtonPreference pref,
|
||||
String key, CandidateInfo info, String defaultKey, String systemDefaultKey) {
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
public void updateCandidates() {
|
||||
mCandidates.clear();
|
||||
final List<? extends CandidateInfo> candidateList = getCandidates();
|
||||
if (candidateList != null) {
|
||||
for (CandidateInfo info : candidateList) {
|
||||
mCandidates.put(info.getKey(), info);
|
||||
}
|
||||
}
|
||||
final String defaultKey = getDefaultKey();
|
||||
final String systemDefaultKey = getSystemDefaultKey();
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
screen.removeAll();
|
||||
if (shouldShowItemNone()) {
|
||||
final RadioButtonPreference nonePref = new RadioButtonPreference(getPrefContext());
|
||||
nonePref.setIcon(R.drawable.ic_remove_circle);
|
||||
nonePref.setTitle(R.string.app_list_preference_none);
|
||||
nonePref.setChecked(TextUtils.isEmpty(defaultKey));
|
||||
nonePref.setOnClickListener(this);
|
||||
screen.addPreference(nonePref);
|
||||
}
|
||||
for (Map.Entry<String, CandidateInfo> app : mCandidates.entrySet()) {
|
||||
RadioButtonPreference pref = new RadioButtonPreference(getPrefContext());
|
||||
bindPreference(
|
||||
pref, app.getKey(), app.getValue(), defaultKey);
|
||||
bindPreferenceExtra(pref, app.getKey(), app.getValue(), defaultKey, systemDefaultKey);
|
||||
screen.addPreference(pref);
|
||||
}
|
||||
mayCheckOnlyRadioButton();
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
public RadioButtonPreference bindPreference(RadioButtonPreference pref,
|
||||
String key, CandidateInfo info, String defaultKey) {
|
||||
pref.setTitle(info.loadLabel());
|
||||
pref.setIcon(info.loadIcon());
|
||||
pref.setKey(key);
|
||||
if (TextUtils.equals(defaultKey, key)) {
|
||||
pref.setChecked(true);
|
||||
}
|
||||
pref.setEnabled(info.enabled);
|
||||
pref.setOnClickListener(this);
|
||||
return pref;
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
public void updateCheckedState(String selectedKey) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
if (screen != null) {
|
||||
final int count = screen.getPreferenceCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
final Preference pref = screen.getPreference(i);
|
||||
if (pref instanceof RadioButtonPreference) {
|
||||
final RadioButtonPreference radioPref = (RadioButtonPreference) pref;
|
||||
final boolean newCheckedState = TextUtils.equals(pref.getKey(), selectedKey);
|
||||
if (radioPref.isChecked() != newCheckedState) {
|
||||
radioPref.setChecked(TextUtils.equals(pref.getKey(), selectedKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
public void mayCheckOnlyRadioButton() {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
// If there is only 1 thing on screen, select it.
|
||||
if (screen != null && screen.getPreferenceCount() == 1) {
|
||||
final Preference onlyPref = screen.getPreference(0);
|
||||
if (onlyPref instanceof RadioButtonPreference) {
|
||||
((RadioButtonPreference) onlyPref).setChecked(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract List<? extends CandidateInfo> getCandidates();
|
||||
|
||||
protected abstract String getDefaultKey();
|
||||
|
||||
protected abstract boolean setDefaultKey(String key);
|
||||
|
||||
protected String getSystemDefaultKey() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static abstract class CandidateInfo {
|
||||
|
||||
public final boolean enabled;
|
||||
|
||||
public CandidateInfo(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public abstract CharSequence loadLabel();
|
||||
|
||||
public abstract Drawable loadIcon();
|
||||
|
||||
public abstract String getKey();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user