diff --git a/res/xml/app_picker_prefs.xml b/res/xml/placeholder_prefs.xml similarity index 100% rename from res/xml/app_picker_prefs.xml rename to res/xml/placeholder_prefs.xml diff --git a/src/com/android/settings/applications/assist/DefaultAssistPicker.java b/src/com/android/settings/applications/assist/DefaultAssistPicker.java index 703665a86ed..51cb67740bb 100644 --- a/src/com/android/settings/applications/assist/DefaultAssistPicker.java +++ b/src/com/android/settings/applications/assist/DefaultAssistPicker.java @@ -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; diff --git a/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java b/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java index df345011901..747ce4da596 100644 --- a/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java +++ b/src/com/android/settings/applications/assist/DefaultAssistPreferenceController.java @@ -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); } } diff --git a/src/com/android/settings/applications/assist/DefaultVoiceInputPicker.java b/src/com/android/settings/applications/assist/DefaultVoiceInputPicker.java index 7168aa5cebe..f51275e23ea 100644 --- a/src/com/android/settings/applications/assist/DefaultVoiceInputPicker.java +++ b/src/com/android/settings/applications/assist/DefaultVoiceInputPicker.java @@ -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 { diff --git a/src/com/android/settings/applications/assist/DefaultVoiceInputPreferenceController.java b/src/com/android/settings/applications/assist/DefaultVoiceInputPreferenceController.java index 918ec9c7864..032fb13ad88 100644 --- a/src/com/android/settings/applications/assist/DefaultVoiceInputPreferenceController.java +++ b/src/com/android/settings/applications/assist/DefaultVoiceInputPreferenceController.java @@ -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 */); } } diff --git a/src/com/android/settings/applications/defaultapps/DefaultAppInfo.java b/src/com/android/settings/applications/defaultapps/DefaultAppInfo.java index d276fd241c2..e99b106dfab 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultAppInfo.java +++ b/src/com/android/settings/applications/defaultapps/DefaultAppInfo.java @@ -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(); diff --git a/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java b/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java index 785f7add124..d08ac992b35 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java +++ b/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragment.java @@ -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 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 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 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 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; - } } diff --git a/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java index 4c65fca0a5b..93fc6a601e9 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java @@ -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); diff --git a/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java b/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java index e3a7a2b1c77..ce9344f8b29 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java @@ -56,34 +56,34 @@ public class DefaultAutofillPicker extends DefaultAppPickerFragment { final List 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 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; } diff --git a/src/com/android/settings/applications/defaultapps/DefaultAutofillPreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultAutofillPreferenceController.java index c536dbb9103..161f2df4d80 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultAutofillPreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultAutofillPreferenceController.java @@ -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; } diff --git a/src/com/android/settings/applications/defaultapps/DefaultBrowserPicker.java b/src/com/android/settings/applications/defaultapps/DefaultBrowserPicker.java index 70d94151b52..e9650523bdf 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultBrowserPicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultBrowserPicker.java @@ -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. diff --git a/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java index 9a5c224cef1..661fb5b24cc 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java @@ -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; } diff --git a/src/com/android/settings/applications/defaultapps/DefaultEmergencyPicker.java b/src/com/android/settings/applications/defaultapps/DefaultEmergencyPicker.java index b3cf882868e..1d707dfef6a 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultEmergencyPicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultEmergencyPicker.java @@ -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); diff --git a/src/com/android/settings/applications/defaultapps/DefaultHomePicker.java b/src/com/android/settings/applications/defaultapps/DefaultHomePicker.java index 5e48bf193a4..038267b5d29 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultHomePicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultHomePicker.java @@ -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 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 homeActivities = new ArrayList<>(); diff --git a/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java index 7366d181b2d..2e938766852 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java @@ -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 homeActivities = new ArrayList<>(); final ComponentName currentDefaultHome = mPackageManager.getHomeActivities(homeActivities); - return new DefaultAppInfo(mUserId, currentDefaultHome); + return new DefaultAppInfo(mPackageManager, mUserId, currentDefaultHome); } private String getOnlyAppLabel() { diff --git a/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPicker.java b/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPicker.java index 06c54011fe1..8ffcb68f4d1 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPicker.java @@ -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; diff --git a/src/com/android/settings/applications/defaultapps/DefaultPhonePicker.java b/src/com/android/settings/applications/defaultapps/DefaultPhonePicker.java index 0fa93902b48..fc643c86e89 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultPhonePicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultPhonePicker.java @@ -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; diff --git a/src/com/android/settings/applications/defaultapps/DefaultPhonePreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultPhonePreferenceController.java index 8a7346893d3..712badbc6ef 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultPhonePreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultPhonePreferenceController.java @@ -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; } diff --git a/src/com/android/settings/applications/defaultapps/DefaultSmsPicker.java b/src/com/android/settings/applications/defaultapps/DefaultSmsPicker.java index 31bb6f659d6..966cd430ac5 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultSmsPicker.java +++ b/src/com/android/settings/applications/defaultapps/DefaultSmsPicker.java @@ -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); } diff --git a/src/com/android/settings/applications/defaultapps/DefaultSmsPreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultSmsPreferenceController.java index 81a3af0dd40..63075b903ba 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultSmsPreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultSmsPreferenceController.java @@ -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; } diff --git a/src/com/android/settings/location/LocationSettings.java b/src/com/android/settings/location/LocationSettings.java index e540b2cb5fd..dc321e284b0 100644 --- a/src/com/android/settings/location/LocationSettings.java +++ b/src/com/android/settings/location/LocationSettings.java @@ -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: *
    diff --git a/src/com/android/settings/webview/WebViewAppPicker.java b/src/com/android/settings/webview/WebViewAppPicker.java index f1faf9b2c35..380b964ab7e 100644 --- a/src/com/android/settings/webview/WebViewAppPicker.java +++ b/src/com/android/settings/webview/WebViewAppPicker.java @@ -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 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 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; diff --git a/src/com/android/settings/webview/WebViewAppPreferenceController.java b/src/com/android/settings/webview/WebViewAppPreferenceController.java index 3a181fe7ffa..2f3246c9fde 100644 --- a/src/com/android/settings/webview/WebViewAppPreferenceController.java +++ b/src/com/android/settings/webview/WebViewAppPreferenceController.java @@ -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 diff --git a/src/com/android/settings/widget/RadioButtonPickerFragment.java b/src/com/android/settings/widget/RadioButtonPickerFragment.java new file mode 100644 index 00000000000..52e8c538ef6 --- /dev/null +++ b/src/com/android/settings/widget/RadioButtonPickerFragment.java @@ -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 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 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 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 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(); + } + +} diff --git a/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java b/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java index 544526abead..aca11a673dc 100644 --- a/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/assist/DefaultAssistPickerTest.java @@ -61,12 +61,12 @@ public class DefaultAssistPickerTest { final List assistants = new ArrayList<>(); assistants.add(new DefaultAssistPicker.Info(TEST_ASSIST)); ReflectionHelpers.setField(mPicker, "mAvailableAssistants", assistants); - mPicker.setDefaultAppKey(TEST_ASSIST.flattenToString()); + mPicker.setDefaultKey(TEST_ASSIST.flattenToString()); assertThat(Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ASSISTANT)) .isEqualTo(TEST_ASSIST.flattenToString()); - assertThat(mPicker.getDefaultAppKey()) + assertThat(mPicker.getDefaultKey()) .isEqualTo(TEST_ASSIST.flattenToString()); } @@ -74,12 +74,12 @@ public class DefaultAssistPickerTest { public void setDefaultAppKey_noAvaialbleAssit_shouldClearDefaultAssist() { final List assistants = new ArrayList<>(); ReflectionHelpers.setField(mPicker, "mAvailableAssistants", assistants); - mPicker.setDefaultAppKey(TEST_ASSIST.flattenToString()); + mPicker.setDefaultKey(TEST_ASSIST.flattenToString()); assertThat(Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ASSISTANT)) .isEmpty(); - assertThat(mPicker.getDefaultAppKey()) + assertThat(mPicker.getDefaultKey()) .isNull(); } @@ -88,12 +88,12 @@ public class DefaultAssistPickerTest { final List assistants = new ArrayList<>(); assistants.add(new DefaultAssistPicker.Info(TEST_ASSIST)); ReflectionHelpers.setField(mPicker, "mAvailableAssistants", assistants); - mPicker.setDefaultAppKey(null); + mPicker.setDefaultKey(null); assertThat(Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ASSISTANT)) .isEmpty(); - assertThat(mPicker.getDefaultAppKey()) + assertThat(mPicker.getDefaultKey()) .isNull(); } } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppInfoTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppInfoTest.java index 57668c80fc4..4501a987eac 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppInfoTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppInfoTest.java @@ -17,13 +17,17 @@ package com.android.settings.applications.defaultapps; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import android.content.ComponentName; -import android.content.pm.ActivityInfo; -import android.content.pm.ApplicationInfo; +import android.content.pm.PackageItemInfo; import android.content.pm.PackageManager; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settings.applications.PackageManagerWrapper; import org.junit.Before; import org.junit.Test; @@ -32,60 +36,44 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class DefaultAppInfoTest { @Mock - private ActivityInfo mActivityInfo; - @Mock - private ApplicationInfo mApplicationInfo; + private PackageItemInfo mPackageItemInfo; @Mock private ComponentName mComponentName; @Mock private PackageManager mPackageManager; + @Mock + private PackageManagerWrapper mPackageManagerWrapper; private DefaultAppInfo mInfo; @Before public void setUp() { MockitoAnnotations.initMocks(this); + when(mPackageManagerWrapper.getPackageManager()).thenReturn(mPackageManager); } @Test public void initInfoWithActivityInfo_shouldLoadInfo() { - mActivityInfo.packageName = "test"; - mInfo = new DefaultAppInfo(mActivityInfo); - mInfo.loadLabel(mPackageManager); - mInfo.loadIcon(mPackageManager); + mPackageItemInfo.packageName = "test"; + mInfo = new DefaultAppInfo(mPackageManagerWrapper, mPackageItemInfo); + mInfo.loadLabel(); + mInfo.loadIcon(); - assertThat(mInfo.getKey()).isEqualTo(mActivityInfo.packageName); - verify(mActivityInfo).loadLabel(mPackageManager); - verify(mActivityInfo).loadIcon(mPackageManager); - } - - @Test - public void initInfoWithApplicationInfo_shouldLoadInfo() { - mApplicationInfo.packageName = "test"; - - mInfo = new DefaultAppInfo(mApplicationInfo); - mInfo.loadLabel(mPackageManager); - mInfo.loadIcon(mPackageManager); - - assertThat(mInfo.getKey()).isEqualTo(mApplicationInfo.packageName); - verify(mApplicationInfo).loadLabel(mPackageManager); - verify(mApplicationInfo).loadIcon(mPackageManager); + assertThat(mInfo.getKey()).isEqualTo(mPackageItemInfo.packageName); + verify(mPackageItemInfo).loadLabel(mPackageManager); + verify(mPackageItemInfo).loadIcon(mPackageManager); } @Test public void initInfoWithComponent_shouldLoadInfo() { when(mComponentName.getPackageName()).thenReturn("com.android.settings"); - mInfo = new DefaultAppInfo(0 /* uid */, mComponentName); + mInfo = new DefaultAppInfo(mPackageManagerWrapper, 0 /* uid */, mComponentName); mInfo.getKey(); verify(mComponentName).flattenToString(); diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragmentTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragmentTest.java index 6c44bd51da7..2c8817931a5 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragmentTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPickerFragmentTest.java @@ -17,11 +17,15 @@ package com.android.settings.applications.defaultapps; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + import android.app.Activity; import android.content.Context; -import android.os.Bundle; import android.os.UserManager; -import android.support.v4.app.FragmentManager; import android.support.v7.preference.PreferenceScreen; import com.android.settings.SettingsRobolectricTestRunner; @@ -40,13 +44,6 @@ import org.robolectric.annotation.Config; import java.util.ArrayList; import java.util.List; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class DefaultAppPickerFragmentTest { @@ -57,8 +54,6 @@ public class DefaultAppPickerFragmentTest { private PreferenceScreen mScreen; @Mock private UserManager mUserManager; - @Mock - private FragmentManager mFragmentManager; private TestFragment mFragment; @@ -66,9 +61,6 @@ public class DefaultAppPickerFragmentTest { public void setUp() { MockitoAnnotations.initMocks(this); mFragment = spy(new TestFragment()); - final Bundle bundle = new Bundle(); - bundle.putBoolean(DefaultAppPickerFragment.EXTRA_FOR_WORK, false); - mFragment.setArguments(bundle); when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); doReturn(mActivity).when(mFragment).getContext(); @@ -76,26 +68,7 @@ public class DefaultAppPickerFragmentTest { } @Test - public void onAttach_userIsInitialized() { - mFragment.onAttach((Context) mActivity); - - verify(mActivity).getPackageManager(); - verify(mActivity).getSystemService(Context.USER_SERVICE); - } - - @Test - public void clickPreference_noCofirmation_shouldDirectlyConfirm() { - final RadioButtonPreference pref = - new RadioButtonPreference(RuntimeEnvironment.application); - pref.setKey("TEST"); - - mFragment.onRadioButtonClicked(pref); - - assertThat(mFragment.setDefaultAppKeyCalled).isTrue(); - } - - @Test - public void clickPreference_hasCofirmation_shouldShowConfirmation() { + public void clickPreference_hasConfirmation_shouldShowConfirmation() { final RadioButtonPreference pref = new RadioButtonPreference(RuntimeEnvironment.application); pref.setKey("TEST"); @@ -106,18 +79,6 @@ public class DefaultAppPickerFragmentTest { mFragment.onRadioButtonClicked(pref); } - @Test - public void displaySingleOption_shouldSelectRadioButton() { - final RadioButtonPreference pref = - new RadioButtonPreference(RuntimeEnvironment.application); - when(mScreen.getPreferenceCount()).thenReturn(1); - when(mScreen.getPreference(0)).thenReturn(pref); - - mFragment.mayCheckOnlyRadioButton(); - - assertThat(pref.isChecked()).isTrue(); - } - public static class TestFragment extends DefaultAppPickerFragment { boolean setDefaultAppKeyCalled; @@ -133,12 +94,12 @@ public class DefaultAppPickerFragmentTest { } @Override - protected String getDefaultAppKey() { + protected String getDefaultKey() { return null; } @Override - protected boolean setDefaultAppKey(String key) { + protected boolean setDefaultKey(String key) { setDefaultAppKeyCalled = true; return true; } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceControllerTest.java index 6dc2e64aa1e..fc000c9277d 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceControllerTest.java @@ -17,6 +17,10 @@ package com.android.settings.applications.defaultapps; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import android.content.Context; import android.os.UserManager; import android.support.v7.preference.Preference; @@ -33,10 +37,6 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class DefaultAppPreferenceControllerTest { @@ -62,7 +62,7 @@ public class DefaultAppPreferenceControllerTest { public void updateState_hasDefaultApp_shouldUpdateAppName() { mController = new TestPreferenceController(mContext); - when(mController.mAppInfo.loadLabel(mContext.getPackageManager())) + when(mController.mAppInfo.loadLabel()) .thenReturn(TEST_APP_NAME); mController.updateState(mPreference); diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAutofillPickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAutofillPickerTest.java index 1bd611b5c39..78d0bda1080 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAutofillPickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultAutofillPickerTest.java @@ -16,9 +16,14 @@ package com.android.settings.applications.defaultapps; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + import android.app.Activity; import android.content.Context; -import android.content.pm.PackageManager; import android.os.UserManager; import com.android.settings.SettingsRobolectricTestRunner; @@ -36,13 +41,6 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.util.ReflectionHelpers; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @@ -73,14 +71,14 @@ public class DefaultAutofillPickerTest { @Test public void setAndGetDefaultAppKey_shouldUpdateDefaultAutoFill() { - assertThat(mPicker.setDefaultAppKey(TEST_APP_KEY)).isTrue(); - assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY); + assertThat(mPicker.setDefaultKey(TEST_APP_KEY)).isTrue(); + assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); } @Test public void getConfirmationMessage_shouldNotBeNull() { final DefaultAppInfo info = mock(DefaultAppInfo.class); - when(info.loadLabel(any(PackageManager.class))).thenReturn("test_app_name"); + when(info.loadLabel()).thenReturn("test_app_name"); assertThat(mPicker.getConfirmationMessage(info)).isNotNull(); } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultBrowserPickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultBrowserPickerTest.java index b0231f8fb31..63d8c6daa69 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultBrowserPickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultBrowserPickerTest.java @@ -67,14 +67,14 @@ public class DefaultBrowserPickerTest { @Test public void setDefaultAppKey_shouldUpdateDefaultBrowser() { - mPicker.setDefaultAppKey(TEST_APP_KEY); + mPicker.setDefaultKey(TEST_APP_KEY); verify(mPackageManager) .setDefaultBrowserPackageNameAsUser(eq(TEST_APP_KEY), anyInt()); } @Test public void getDefaultAppKey_shouldReturnDefaultBrowser() { - mPicker.getDefaultAppKey(); + mPicker.getDefaultKey(); verify(mPackageManager) .getDefaultBrowserPackageNameAsUser(anyInt()); } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultEmergencyPickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultEmergencyPickerTest.java index ceccba17e78..6ee9e8606a4 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultEmergencyPickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultEmergencyPickerTest.java @@ -70,8 +70,8 @@ public class DefaultEmergencyPickerTest { @Test public void setDefaultAppKey_shouldUpdateDefault() { - assertThat(mPicker.setDefaultAppKey(TEST_APP_KEY)).isTrue(); - assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY); + assertThat(mPicker.setDefaultKey(TEST_APP_KEY)).isTrue(); + assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); } @Test @@ -80,6 +80,6 @@ public class DefaultEmergencyPickerTest { Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, TEST_APP_KEY); - assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY); + assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); } } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePickerTest.java index d63ae6df551..38104a9e07b 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultHomePickerTest.java @@ -72,7 +72,7 @@ public class DefaultHomePickerTest { @Test public void setDefaultAppKey_shouldUpdateDefault() { - assertThat(mPicker.setDefaultAppKey(TEST_APP_KEY)).isTrue(); + assertThat(mPicker.setDefaultKey(TEST_APP_KEY)).isTrue(); verify(mPackageManager).replacePreferredActivity(any(IntentFilter.class), anyInt(), any(ComponentName[].class), any(ComponentName.class)); @@ -83,7 +83,7 @@ public class DefaultHomePickerTest { final ComponentName cn = mock(ComponentName.class); when(mPackageManager.getHomeActivities(anyList())) .thenReturn(cn); - mPicker.getDefaultAppKey(); + mPicker.getDefaultKey(); verify(cn).flattenToString(); } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPickerTest.java index 322830c108a..acf90d581f3 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultNotificationAssistantPickerTest.java @@ -70,9 +70,9 @@ public class DefaultNotificationAssistantPickerTest { @Test public void setDefaultAppKey_shouldUpdateDefault() { - mPicker.setDefaultAppKey(TEST_APP_KEY); + mPicker.setDefaultKey(TEST_APP_KEY); - assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY); + assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); } @Test @@ -81,6 +81,6 @@ public class DefaultNotificationAssistantPickerTest { Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT, TEST_APP_KEY); - assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY); + assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY); } } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultPhonePickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultPhonePickerTest.java index 0c5d5f1207c..0318a9769b5 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultPhonePickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultPhonePickerTest.java @@ -75,14 +75,14 @@ public class DefaultPhonePickerTest { @Test public void getSystemDefaultPackage_shouldAskDefaultKeyUpdater() { - mPicker.getSystemDefaultAppKey(); + mPicker.getSystemDefaultKey(); verify(mDefaultKeyUpdater).getSystemDialerPackage(); } @Test public void setDefaultAppKey_shouldUpdateDefault() { - mPicker.setDefaultAppKey(TEST_APP_KEY); + mPicker.setDefaultKey(TEST_APP_KEY); verify(mDefaultKeyUpdater).setDefaultDialerApplication( any(Context.class), eq(TEST_APP_KEY), anyInt()); @@ -90,7 +90,7 @@ public class DefaultPhonePickerTest { @Test public void getDefaultAppKey_shouldReturnDefault() { - mPicker.getDefaultAppKey(); + mPicker.getDefaultKey(); verify(mDefaultKeyUpdater).getDefaultDialerApplication(any(Context.class), anyInt()); } } diff --git a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultSmsPickerTest.java b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultSmsPickerTest.java index 3da6fec3738..ba19d83fc4a 100644 --- a/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultSmsPickerTest.java +++ b/tests/robotests/src/com/android/settings/applications/defaultapps/DefaultSmsPickerTest.java @@ -73,14 +73,14 @@ public class DefaultSmsPickerTest { @Test public void setDefaultAppKey_shouldUpdateDefault() { - mPicker.setDefaultAppKey(TEST_APP_KEY); + mPicker.setDefaultKey(TEST_APP_KEY); verify(mDefaultKeyUpdater).setDefaultApplication(any(Context.class), eq(TEST_APP_KEY)); } @Test public void getDefaultAppKey_shouldReturnDefault() { - mPicker.getDefaultAppKey(); + mPicker.getDefaultKey(); verify(mDefaultKeyUpdater).getDefaultApplication(any(Context.class)); } diff --git a/tests/robotests/src/com/android/settings/webview/WebViewAppPickerTest.java b/tests/robotests/src/com/android/settings/webview/WebViewAppPickerTest.java index 728e35797ce..655f35428bc 100644 --- a/tests/robotests/src/com/android/settings/webview/WebViewAppPickerTest.java +++ b/tests/robotests/src/com/android/settings/webview/WebViewAppPickerTest.java @@ -46,8 +46,6 @@ import com.android.settings.applications.PackageManagerWrapper; import com.android.settings.applications.defaultapps.DefaultAppInfo; import com.android.settings.widget.RadioButtonPreference; -import java.util.Arrays; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -58,6 +56,8 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.util.ReflectionHelpers; +import java.util.Arrays; + @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class WebViewAppPickerTest { @@ -162,13 +162,14 @@ public class WebViewAppPickerTest { @Test public void testDisabledPackageShownAsDisabled() { String disabledReason = "disabled"; - DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo( + DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo(mPackageManager, createApplicationInfo(DEFAULT_PACKAGE_NAME), disabledReason); RadioButtonPreference mockPreference = mock(RadioButtonPreference.class); - mPicker.configurePreferenceFromAppInfo(mockPreference, + mPicker.bindPreference(mockPreference, + DEFAULT_PACKAGE_NAME, webviewAppInfo, null); + mPicker.bindPreferenceExtra(mockPreference, DEFAULT_PACKAGE_NAME, webviewAppInfo, null, null); - verify(mockPreference, times(1)).setEnabled(eq(false)); verify(mockPreference, never()).setEnabled(eq(true)); } @@ -176,13 +177,14 @@ public class WebViewAppPickerTest { @Test public void testEnabledPackageShownAsEnabled() { String disabledReason = ""; - DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo( + DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo(mPackageManager, createApplicationInfo(DEFAULT_PACKAGE_NAME), disabledReason); RadioButtonPreference mockPreference = mock(RadioButtonPreference.class); - mPicker.configurePreferenceFromAppInfo(mockPreference, + mPicker.bindPreference(mockPreference, + DEFAULT_PACKAGE_NAME, webviewAppInfo, null); + mPicker.bindPreferenceExtra(mockPreference, DEFAULT_PACKAGE_NAME, webviewAppInfo, null, null); - verify(mockPreference, times(1)).setEnabled(eq(true)); verify(mockPreference, never()).setEnabled(eq(false)); } @@ -190,13 +192,14 @@ public class WebViewAppPickerTest { @Test public void testDisabledPackageShowsDisabledReasonSummary() { String disabledReason = "disabled"; - DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo( + DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo(mPackageManager, createApplicationInfo(DEFAULT_PACKAGE_NAME), disabledReason); RadioButtonPreference mockPreference = mock(RadioButtonPreference.class); - mPicker.configurePreferenceFromAppInfo(mockPreference, + mPicker.bindPreference(mockPreference, + DEFAULT_PACKAGE_NAME, webviewAppInfo, null); + mPicker.bindPreferenceExtra(mockPreference, DEFAULT_PACKAGE_NAME, webviewAppInfo, null, null); - verify(mockPreference, times(1)).setSummary(eq(disabledReason)); // Ensure we haven't called setSummary several times. verify(mockPreference, times(1)).setSummary(any()); @@ -205,13 +208,14 @@ public class WebViewAppPickerTest { @Test public void testEnabledPackageShowsEmptySummary() { String disabledReason = null; - DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo( + DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo(mPackageManager, createApplicationInfo(DEFAULT_PACKAGE_NAME), disabledReason); RadioButtonPreference mockPreference = mock(RadioButtonPreference.class); - mPicker.configurePreferenceFromAppInfo(mockPreference, + mPicker.bindPreference(mockPreference, + DEFAULT_PACKAGE_NAME, webviewAppInfo, null); + mPicker.bindPreferenceExtra(mockPreference, DEFAULT_PACKAGE_NAME, webviewAppInfo, null, null); - verify(mockPreference, never()).setSummary(any()); } @@ -242,7 +246,7 @@ public class WebViewAppPickerTest { WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class); when(wvusWrapper.getPackageInfosAllUsers( any(), eq(DEFAULT_PACKAGE_NAME))).thenReturn( - Arrays.asList(packageForFirstUser, packageForSecondUser)); + Arrays.asList(packageForFirstUser, packageForSecondUser)); assertThat(mPicker.getDisabledReason(wvusWrapper, mContext, DEFAULT_PACKAGE_NAME)).isNull(); } @@ -255,8 +259,8 @@ public class WebViewAppPickerTest { when(packageForFirstUser.getUserInfo()).thenReturn(FIRST_USER); WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class); - when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME) - )).thenReturn(Arrays.asList(packageForFirstUser)); + when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME))) + .thenReturn(Arrays.asList(packageForFirstUser)); final String EXPECTED_DISABLED_REASON = String.format( "(disabled for user %s)", FIRST_USER.name); @@ -272,8 +276,8 @@ public class WebViewAppPickerTest { when(packageForFirstUser.getUserInfo()).thenReturn(FIRST_USER); WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class); - when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME) - )).thenReturn(Arrays.asList(packageForFirstUser)); + when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME))) + .thenReturn(Arrays.asList(packageForFirstUser)); final String EXPECTED_DISABLED_REASON = String.format( "(uninstalled for user %s)", FIRST_USER.name); @@ -294,13 +298,13 @@ public class WebViewAppPickerTest { when(packageForSecondUser.getUserInfo()).thenReturn(SECOND_USER); WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class); - when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME) - )).thenReturn(Arrays.asList(packageForFirstUser, packageForSecondUser)); + when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME))) + .thenReturn(Arrays.asList(packageForFirstUser, packageForSecondUser)); final String EXPECTED_DISABLED_REASON = String.format( "(disabled for user %s)", FIRST_USER.name); assertThat(mPicker.getDisabledReason( - wvusWrapper, mContext,DEFAULT_PACKAGE_NAME)).isEqualTo(EXPECTED_DISABLED_REASON); + wvusWrapper, mContext, DEFAULT_PACKAGE_NAME)).isEqualTo(EXPECTED_DISABLED_REASON); } /** @@ -320,8 +324,8 @@ public class WebViewAppPickerTest { when(packageForSecondUser.getUserInfo()).thenReturn(SECOND_USER); WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class); - when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME) - )).thenReturn(Arrays.asList(packageForFirstUser, packageForSecondUser)); + when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME))) + .thenReturn(Arrays.asList(packageForFirstUser, packageForSecondUser)); final String EXPECTED_DISABLED_REASON = String.format( "(uninstalled for user %s)", FIRST_USER.name); @@ -338,7 +342,7 @@ public class WebViewAppPickerTest { PackageItemInfo mockPackageItemInfo = mock(PackageItemInfo.class); mockPackageItemInfo.packageName = DEFAULT_PACKAGE_NAME; when(mockPackageItemInfo.loadLabel(any())).thenReturn("myPackage"); - DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo( + DefaultAppInfo webviewAppInfo = mPicker.createDefaultAppInfo(mPackageManager, mockPackageItemInfo, "" /* disabledReason */); PackageInfo packageInfo = new PackageInfo(); @@ -348,9 +352,10 @@ public class WebViewAppPickerTest { when(mPackageManager.getPackageManager()).thenReturn(pm); RadioButtonPreference mockPreference = mock(RadioButtonPreference.class); - mPicker.configurePreferenceFromAppInfo(mockPreference, + mPicker.bindPreference(mockPreference, + DEFAULT_PACKAGE_NAME, webviewAppInfo, null); + mPicker.bindPreferenceExtra(mockPreference, DEFAULT_PACKAGE_NAME, webviewAppInfo, null, null); - verify(mockPreference, times(1)).setTitle(eq("myPackage myVersionName")); verify(mockPreference, times(1)).setTitle(any()); } diff --git a/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java b/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java new file mode 100644 index 00000000000..e6d00b38ee3 --- /dev/null +++ b/tests/robotests/src/com/android/settings/widget/RadioButtonPickerFragmentTest.java @@ -0,0 +1,132 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.Activity; +import android.content.Context; +import android.os.UserManager; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; +import com.android.settings.applications.defaultapps.DefaultAppInfo; +import com.android.settings.applications.defaultapps.DefaultAppPickerFragment; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class RadioButtonPickerFragmentTest { + + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private Activity mActivity; + @Mock + private PreferenceScreen mScreen; + @Mock + private UserManager mUserManager; + + private TestFragment mFragment; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mFragment = spy(new TestFragment()); + + when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); + doReturn(mActivity).when(mFragment).getContext(); + doReturn(mScreen).when(mFragment).getPreferenceScreen(); + } + + @Test + public void onAttach_userIsInitialized() { + mFragment.onAttach((Context) mActivity); + + verify(mActivity).getPackageManager(); + verify(mActivity).getSystemService(Context.USER_SERVICE); + } + + @Test + public void displaySingleOption_shouldSelectRadioButton() { + final RadioButtonPreference pref = + new RadioButtonPreference(RuntimeEnvironment.application); + when(mScreen.getPreferenceCount()).thenReturn(1); + when(mScreen.getPreference(0)).thenReturn(pref); + + mFragment.mayCheckOnlyRadioButton(); + + assertThat(pref.isChecked()).isTrue(); + } + + @Test + public void clickPreference_shouldConfirm() { + final RadioButtonPreference pref = + new RadioButtonPreference(RuntimeEnvironment.application); + pref.setKey("TEST"); + + mFragment.onRadioButtonClicked(pref); + + assertThat(mFragment.setDefaultKeyCalled).isTrue(); + } + + public static class TestFragment extends DefaultAppPickerFragment { + + boolean setDefaultKeyCalled; + + @Override + public int getMetricsCategory() { + return 0; + } + + @Override + protected List getCandidates() { + return new ArrayList<>(); + } + + @Override + protected String getDefaultKey() { + return null; + } + + @Override + protected boolean setDefaultKey(String key) { + setDefaultKeyCalled = true; + return true; + } + + @Override + public Context getContext() { + return RuntimeEnvironment.application; + } + } +}