From 7e866b670e268b30fb22f20cd870406aebba87e7 Mon Sep 17 00:00:00 2001 From: Sunny Shao Date: Fri, 9 Aug 2019 11:42:04 +0800 Subject: [PATCH] Use Builder to replace the Uitls.addFooterPreference Fixes: 139163212 Test: manual test Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.bluetooth Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.accounts Change-Id: I861434b81c07db785e6f1cfb3e34871ffba41e5f --- src/com/android/settings/Utils.java | 108 +++++++---------- .../accounts/AccountSyncSettings.java | 11 +- .../fingerprint/FingerprintSettings.java | 109 +++++++++--------- .../accounts/AccountSyncSettingsTest.java | 19 --- ...etoothDetailsMacAddressControllerTest.java | 35 +++--- 5 files changed, 113 insertions(+), 169 deletions(-) diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java index 00542cad017..75db3e18188 100644 --- a/src/com/android/settings/Utils.java +++ b/src/com/android/settings/Utils.java @@ -92,7 +92,6 @@ import android.widget.ListView; import android.widget.TabWidget; import androidx.annotation.StringRes; -import androidx.annotation.NonNull; import androidx.core.graphics.drawable.IconCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.Lifecycle; @@ -106,7 +105,6 @@ import com.android.settings.core.FeatureFlags; import com.android.settings.development.featureflags.FeatureFlagPersistent; import com.android.settings.password.ChooseLockSettingsHelper; import com.android.settingslib.widget.ActionBarShadowController; -import com.android.settingslib.widget.FooterPreference; import java.net.InetAddress; import java.util.Iterator; @@ -141,16 +139,16 @@ public final class Utils extends com.android.settingslib.Utils { * Finds a matching activity for a preference's intent. If a matching * activity is not found, it will remove the preference. * - * @param context The context. + * @param context The context. * @param parentPreferenceGroup The preference group that contains the - * preference whose intent is being resolved. - * @param preferenceKey The key of the preference whose intent is being - * resolved. - * @param flags 0 or one or more of - * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} - * . + * preference whose intent is being resolved. + * @param preferenceKey The key of the preference whose intent is being + * resolved. + * @param flags 0 or one or more of + * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} + * . * @return Whether an activity was found. If false, the preference was - * removed. + * removed. */ public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { @@ -223,7 +221,6 @@ public final class Utils extends com.android.settingslib.Utils { /** * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. - * * @param context the application context * @return the formatted and newline-separated IP addresses, or null if none. */ @@ -232,7 +229,7 @@ public final class Utils extends com.android.settingslib.Utils { Network currentNetwork = wifiManager.getCurrentNetwork(); if (currentNetwork != null) { ConnectivityManager cm = (ConnectivityManager) - context.getSystemService(Context.CONNECTIVITY_SERVICE); + context.getSystemService(Context.CONNECTIVITY_SERVICE); LinkProperties prop = cm.getLinkProperties(currentNetwork); return formatIpAddresses(prop); } @@ -260,9 +257,8 @@ public final class Utils extends com.android.settingslib.Utils { // many cases, because the constructor takes the only string as the language // code. So : new Locale("en", "US").toString() => "en_US" // And : new Locale("en_US").toString() => "en_us" - if (null == localeStr) { + if (null == localeStr) return Locale.getDefault(); - } String[] brokenDownLocale = localeStr.split("_", 3); // split may not return a 0-length array. if (1 == brokenDownLocale.length) { @@ -333,7 +329,7 @@ public final class Utils extends com.android.settingslib.Utils { final long localRowProfileId; final Cursor localRawProfile = cr.query( Profile.CONTENT_RAW_CONTACTS_URI, - new String[]{RawContacts._ID}, + new String[] {RawContacts._ID}, RawContacts.ACCOUNT_TYPE + " IS NULL AND " + RawContacts.ACCOUNT_NAME + " IS NULL", null, null); @@ -351,8 +347,8 @@ public final class Utils extends com.android.settingslib.Utils { // Find the structured name for the raw contact. final Cursor structuredName = cr.query( Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), - new String[]{CommonDataKinds.StructuredName.GIVEN_NAME, - CommonDataKinds.StructuredName.FAMILY_NAME}, + new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, + CommonDataKinds.StructuredName.FAMILY_NAME}, Data.RAW_CONTACT_ID + "=" + localRowProfileId, null, null); if (structuredName == null) return null; @@ -374,7 +370,7 @@ public final class Utils extends com.android.settingslib.Utils { private static final String getProfileDisplayName(Context context) { final ContentResolver cr = context.getContentResolver(); final Cursor profile = cr.query(Profile.CONTENT_URI, - new String[]{Profile.DISPLAY_NAME}, null, null, null); + new String[] {Profile.DISPLAY_NAME}, null, null, null); if (profile == null) return null; try { @@ -455,7 +451,7 @@ public final class Utils extends com.android.settingslib.Utils { * *

@@ -504,8 +500,7 @@ public final class Utils extends com.android.settingslib.Utils { * Lookup both {@link Intent#EXTRA_USER} and {@link Intent#EXTRA_USER_ID} in the bundle * and return the {@link UserHandle} object. Return {@code null} if nothing is found. */ - private static @Nullable - UserHandle getUserHandleFromBundle(Bundle bundle) { + private static @Nullable UserHandle getUserHandleFromBundle(Bundle bundle) { if (bundle == null) { return null; } @@ -520,14 +515,14 @@ public final class Utils extends com.android.settingslib.Utils { return null; } - /** - * Returns true if the user provided is in the same profiles group as the current user. - */ - private static boolean isProfileOf(UserManager um, UserHandle otherUser) { - if (um == null || otherUser == null) return false; - return (UserHandle.myUserId() == otherUser.getIdentifier()) - || um.getUserProfiles().contains(otherUser); - } + /** + * Returns true if the user provided is in the same profiles group as the current user. + */ + private static boolean isProfileOf(UserManager um, UserHandle otherUser) { + if (um == null || otherUser == null) return false; + return (UserHandle.myUserId() == otherUser.getIdentifier()) + || um.getUserProfiles().contains(otherUser); + } /** * Return whether or not the user should have a SIM Cards option in Settings. @@ -546,9 +541,8 @@ public final class Utils extends com.android.settingslib.Utils { /** * Queries for the UserInfo of a user. Returns null if the user doesn't exist (was removed). - * * @param userManager Instance of UserManager - * @param checkUser The user to check the existence of. + * @param checkUser The user to check the existence of. * @return UserInfo of the user or null for non-existent user. */ public static UserInfo getExistingUser(UserManager userManager, UserHandle checkUser) { @@ -588,7 +582,7 @@ public final class Utils extends com.android.settingslib.Utils { for (IntentFilter filter : filters) { if (filter.hasCategory(Intent.CATEGORY_BROWSABLE) && (filter.hasDataScheme(IntentFilter.SCHEME_HTTP) || - filter.hasDataScheme(IntentFilter.SCHEME_HTTPS))) { + filter.hasDataScheme(IntentFilter.SCHEME_HTTPS))) { result.addAll(filter.getHostsList()); } } @@ -631,8 +625,7 @@ public final class Utils extends com.android.settingslib.Utils { /** * Returns an accessible SpannableString. - * - * @param displayText the text to display + * @param displayText the text to display * @param accessibileText the text text-to-speech engines should read */ public static SpannableString createAccessibleSequence(CharSequence displayText, @@ -649,7 +642,7 @@ public final class Utils extends com.android.settingslib.Utils { * {@link Intent#EXTRA_USER_ID} if it belongs to the current user. * * @throws SecurityException if the given userId does not belong to the - * current user group. + * current user group. */ public static int getUserIdFromBundle(Context context, Bundle bundle) { return getUserIdFromBundle(context, bundle, false); @@ -660,10 +653,10 @@ public final class Utils extends com.android.settingslib.Utils { * {@link Intent#EXTRA_USER_ID} if it belongs to the current user. * * @param isInternal indicating if the caller is "internal" to the system, - * meaning we're willing to trust extras like - * {@link ChooseLockSettingsHelper#EXTRA_ALLOW_ANY_USER}. + * meaning we're willing to trust extras like + * {@link ChooseLockSettingsHelper#EXTRA_ALLOW_ANY_USER}. * @throws SecurityException if the given userId does not belong to the - * current user group. + * current user group. */ public static int getUserIdFromBundle(Context context, Bundle bundle, boolean isInternal) { if (bundle == null) { @@ -783,7 +776,7 @@ public final class Utils extends com.android.settingslib.Utils { final ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo( packageName, PackageManager.MATCH_DISABLED_COMPONENTS - | PackageManager.MATCH_ANY_USER); + | PackageManager.MATCH_ANY_USER); return appInfo.loadLabel(context.getPackageManager()); } catch (PackageManager.NameNotFoundException e) { Log.w(TAG, "Unable to find info for package: " + packageName); @@ -842,9 +835,8 @@ public final class Utils extends com.android.settingslib.Utils { /** * Launches an intent which may optionally have a user id defined. - * * @param fragment Fragment to use to launch the activity. - * @param intent Intent to launch. + * @param intent Intent to launch. */ public static void launchIntent(Fragment fragment, Intent intent) { try { @@ -872,8 +864,7 @@ public final class Utils extends com.android.settingslib.Utils { /** * Returns if a given user is a profile of another user. - * - * @param user The user whose profiles wibe checked. + * @param user The user whose profiles wibe checked. * @param profile The (potential) profile. * @return if the profile is actually a profile */ @@ -898,10 +889,9 @@ public final class Utils extends com.android.settingslib.Utils { /** * Return {@code true} if the supplied package is device owner or profile owner of at * least one user. - * - * @param userManager used to get profile owner app for each user + * @param userManager used to get profile owner app for each user * @param devicePolicyManager used to check whether it is device owner app - * @param packageName package to check about + * @param packageName package to check about */ public static boolean isProfileOrDeviceOwner(UserManager userManager, DevicePolicyManager devicePolicyManager, String packageName) { @@ -953,8 +943,8 @@ public final class Utils extends com.android.settingslib.Utils { /** * Gets a drawable with a limited size to avoid crashing Settings if it's too big. * - * @param original original drawable, typically an app icon. - * @param maxWidth maximum width, in pixels. + * @param original original drawable, typically an app icon. + * @param maxWidth maximum width, in pixels. * @param maxHeight maximum height, in pixels. */ public static Drawable getSafeDrawable(Drawable original, int maxWidth, int maxHeight) { @@ -987,7 +977,7 @@ public final class Utils extends com.android.settingslib.Utils { public static IconCompat createIconWithDrawable(Drawable drawable) { Bitmap bitmap; if (drawable instanceof BitmapDrawable) { - bitmap = ((BitmapDrawable) drawable).getBitmap(); + bitmap = ((BitmapDrawable)drawable).getBitmap(); } else { final int width = drawable.getIntrinsicWidth(); final int height = drawable.getIntrinsicHeight(); @@ -1075,24 +1065,4 @@ public final class Utils extends com.android.settingslib.Utils { ActionBarShadowController.attachToView(activity, lifecycle, scrollView); } } - - /** - * Add a footer preference into preference group. - * - * @param group The parent {@link PreferenceGroup} of the footer preference. - * @param key The key value of the footer preference. - * @param title The title of the footer preference. - */ - public static void addFooterPreference(@NonNull Context context, @NonNull PreferenceGroup group, - String key, CharSequence title) { - final FooterPreference footerPreference = new FooterPreference(context); - footerPreference.setSelectable(false); - if (!TextUtils.isEmpty(key)) { - footerPreference.setKey(key); - } - if (!TextUtils.isEmpty(title)) { - footerPreference.setTitle(title); - } - group.addPreference(footerPreference); - } } diff --git a/src/com/android/settings/accounts/AccountSyncSettings.java b/src/com/android/settings/accounts/AccountSyncSettings.java index a652056cf33..992cc67ce22 100644 --- a/src/com/android/settings/accounts/AccountSyncSettings.java +++ b/src/com/android/settings/accounts/AccountSyncSettings.java @@ -42,7 +42,6 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import androidx.annotation.VisibleForTesting; import androidx.appcompat.app.AlertDialog; import androidx.preference.Preference; @@ -460,7 +459,8 @@ public class AccountSyncSettings extends AccountPreferenceBase { syncPref.setChecked(oneTimeSyncMode || syncEnabled); } if (syncIsFailing) { - createFooterPreference(); + getPreferenceScreen().addPreference(new FooterPreference.Builder( + getActivity()).setTitle(R.string.sync_is_failing).build()); } } @@ -553,11 +553,4 @@ public class AccountSyncSettings extends AccountPreferenceBase { | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_TIME); } - - @VisibleForTesting - void createFooterPreference() { - final FooterPreference footerPreference = new FooterPreference(getActivity()); - footerPreference.setTitle(R.string.sync_is_failing); - getPreferenceScreen().addPreference(footerPreference); - } } diff --git a/src/com/android/settings/biometrics/fingerprint/FingerprintSettings.java b/src/com/android/settings/biometrics/fingerprint/FingerprintSettings.java index c90a0ded752..68b21f5aef5 100644 --- a/src/com/android/settings/biometrics/fingerprint/FingerprintSettings.java +++ b/src/com/android/settings/biometrics/fingerprint/FingerprintSettings.java @@ -141,56 +141,57 @@ public class FingerprintSettings extends SubSettings { private HashMap mFingerprintsRenaming; FingerprintAuthenticateSidecar.Listener mAuthenticateListener = - new FingerprintAuthenticateSidecar.Listener() { - @Override - public void onAuthenticationSucceeded( - FingerprintManager.AuthenticationResult result) { - int fingerId = result.getFingerprint().getBiometricId(); - mHandler.obtainMessage(MSG_FINGER_AUTH_SUCCESS, fingerId, 0).sendToTarget(); - } + new FingerprintAuthenticateSidecar.Listener() { + @Override + public void onAuthenticationSucceeded( + FingerprintManager.AuthenticationResult result) { + int fingerId = result.getFingerprint().getBiometricId(); + mHandler.obtainMessage(MSG_FINGER_AUTH_SUCCESS, fingerId, 0).sendToTarget(); + } - @Override - public void onAuthenticationFailed() { - mHandler.obtainMessage(MSG_FINGER_AUTH_FAIL).sendToTarget(); - } + @Override + public void onAuthenticationFailed() { + mHandler.obtainMessage(MSG_FINGER_AUTH_FAIL).sendToTarget(); + } - @Override - public void onAuthenticationError(int errMsgId, CharSequence errString) { - mHandler.obtainMessage(MSG_FINGER_AUTH_ERROR, errMsgId, 0, errString) - .sendToTarget(); - } + @Override + public void onAuthenticationError(int errMsgId, CharSequence errString) { + mHandler.obtainMessage(MSG_FINGER_AUTH_ERROR, errMsgId, 0, errString) + .sendToTarget(); + } - @Override - public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { - mHandler.obtainMessage(MSG_FINGER_AUTH_HELP, helpMsgId, 0, helpString) - .sendToTarget(); - } - }; + @Override + public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { + mHandler.obtainMessage(MSG_FINGER_AUTH_HELP, helpMsgId, 0, helpString) + .sendToTarget(); + } + }; FingerprintRemoveSidecar.Listener mRemovalListener = new FingerprintRemoveSidecar.Listener() { - public void onRemovalSucceeded(Fingerprint fingerprint) { - mHandler.obtainMessage(MSG_REFRESH_FINGERPRINT_TEMPLATES, - fingerprint.getBiometricId(), 0).sendToTarget(); - updateDialog(); - } + public void onRemovalSucceeded(Fingerprint fingerprint) { + mHandler.obtainMessage(MSG_REFRESH_FINGERPRINT_TEMPLATES, + fingerprint.getBiometricId(), 0).sendToTarget(); + updateDialog(); + } - public void onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString) { - final Activity activity = getActivity(); - if (activity != null) { - Toast.makeText(activity, errString, Toast.LENGTH_SHORT); - } - updateDialog(); - } + public void onRemovalError(Fingerprint fp, int errMsgId, + CharSequence errString) { + final Activity activity = getActivity(); + if (activity != null) { + Toast.makeText(activity, errString, Toast.LENGTH_SHORT); + } + updateDialog(); + } - private void updateDialog() { - RenameDialog renameDialog = (RenameDialog) getFragmentManager(). - findFragmentByTag(RenameDialog.class.getName()); - if (renameDialog != null) { - renameDialog.enableDelete(); - } - } - }; + private void updateDialog() { + RenameDialog renameDialog = (RenameDialog) getFragmentManager(). + findFragmentByTag(RenameDialog.class.getName()); + if (renameDialog != null) { + renameDialog.enableDelete(); + } + } + }; private final Handler mHandler = new Handler() { @Override @@ -200,17 +201,17 @@ public class FingerprintSettings extends SubSettings { removeFingerprintPreference(msg.arg1); updateAddPreference(); retryFingerprint(); - break; + break; case MSG_FINGER_AUTH_SUCCESS: highlightFingerprintItem(msg.arg1); retryFingerprint(); - break; + break; case MSG_FINGER_AUTH_FAIL: // No action required... fingerprint will allow up to 5 of these - break; + break; case MSG_FINGER_AUTH_ERROR: - handleError(msg.arg1 /* errMsgId */, (CharSequence) msg.obj /* errStr */ ); - break; + handleError(msg.arg1 /* errMsgId */, (CharSequence) msg.obj /* errStr */); + break; case MSG_FINGER_AUTH_HELP: { // Not used } @@ -220,7 +221,7 @@ public class FingerprintSettings extends SubSettings { }; /** - * @param errMsgId + * */ protected void handleError(int errMsgId, CharSequence msg) { switch (errMsgId) { @@ -243,7 +244,7 @@ public class FingerprintSettings extends SubSettings { // Activity can be null on a screen rotation. final Activity activity = getActivity(); if (activity != null) { - Toast.makeText(activity, msg , Toast.LENGTH_SHORT).show(); + Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show(); } } retryFingerprint(); // start again @@ -425,11 +426,8 @@ public class FingerprintSettings extends SubSettings { if (context == null) { return; } - - final FooterPreference footerPreference = new FooterPreference(context); - footerPreference.setTitle(mFooterTitle); - footerPreference.setSelectable(false); - root.addPreference(footerPreference); + root.addPreference(new FooterPreference.Builder(context).setTitle( + mFooterTitle).build()); } private static String genKey(int id) { @@ -522,7 +520,8 @@ public class FingerprintSettings extends SubSettings { RenameDialog renameDialog = new RenameDialog(); Bundle args = new Bundle(); if (mFingerprintsRenaming.containsKey(fp.getBiometricId())) { - final Fingerprint f = new Fingerprint(mFingerprintsRenaming.get(fp.getBiometricId()), + final Fingerprint f = new Fingerprint( + mFingerprintsRenaming.get(fp.getBiometricId()), fp.getGroupId(), fp.getBiometricId(), fp.getDeviceId()); args.putParcelable("fingerprint", f); } else { diff --git a/tests/robotests/src/com/android/settings/accounts/AccountSyncSettingsTest.java b/tests/robotests/src/com/android/settings/accounts/AccountSyncSettingsTest.java index 59d1f60afbb..dd1778f95fc 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountSyncSettingsTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountSyncSettingsTest.java @@ -15,8 +15,6 @@ */ package com.android.settings.accounts; -import static com.google.common.truth.Truth.assertThat; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @@ -26,9 +24,7 @@ import android.content.Context; import android.os.UserHandle; import androidx.fragment.app.FragmentActivity; -import androidx.preference.Preference; -import com.android.settings.testutils.shadow.ShadowAuthenticationHelper; import com.android.settings.testutils.shadow.ShadowContentResolver; import org.junit.After; @@ -37,7 +33,6 @@ import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; -import org.robolectric.shadows.androidx.fragment.FragmentController; import org.robolectric.util.ReflectionHelpers; @RunWith(RobolectricTestRunner.class) @@ -62,18 +57,4 @@ public class AccountSyncSettingsTest { settings.onPreferenceTreeClick(preference); // no crash } - - @Test - @Config(shadows = {ShadowAuthenticationHelper.class}) - public void createFooterPreference_shouldReturnFooter() { - final AccountSyncSettings settings = FragmentController.of(new AccountSyncSettings()) - .create() - .get(); - - settings.createFooterPreference(); - - final Preference footer = settings.getPreferenceScreen().findPreference( - "footer_preference"); - assertThat(footer).isNotNull(); - } } diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsMacAddressControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsMacAddressControllerTest.java index ec1549fd8e5..c7eb1bbea3f 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsMacAddressControllerTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsMacAddressControllerTest.java @@ -19,7 +19,6 @@ import static com.android.settings.bluetooth.BluetoothDetailsMacAddressControlle import static com.google.common.truth.Truth.assertThat; -import com.android.settings.Utils; import com.android.settingslib.widget.FooterPreference; import org.junit.Test; @@ -28,22 +27,24 @@ import org.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) public class BluetoothDetailsMacAddressControllerTest extends BluetoothDetailsControllerTestBase { - private BluetoothDetailsMacAddressController mController; + private BluetoothDetailsMacAddressController mController; - @Override - public void setUp() { - super.setUp(); - mController = - new BluetoothDetailsMacAddressController(mContext, mFragment, mCachedDevice, mLifecycle); - setupDevice(mDeviceConfig); - Utils.addFooterPreference(mContext, mScreen, KEY_DEVICE_DETAILS_FOOTER, null /* title */); - } + @Override + public void setUp() { + super.setUp(); + mController = + new BluetoothDetailsMacAddressController(mContext, mFragment, mCachedDevice, + mLifecycle); + setupDevice(mDeviceConfig); + mScreen.addPreference(new FooterPreference.Builder(mContext).setKey( + KEY_DEVICE_DETAILS_FOOTER).build()); + } - @Test - public void macAddress() { - showScreen(mController); - FooterPreference footer = - (FooterPreference) mScreen.findPreference(mController.getPreferenceKey()); - assertThat(footer.getTitle().toString()).endsWith(mDeviceConfig.getAddress()); - } + @Test + public void macAddress() { + showScreen(mController); + FooterPreference footer = + (FooterPreference) mScreen.findPreference(mController.getPreferenceKey()); + assertThat(footer.getTitle().toString()).endsWith(mDeviceConfig.getAddress()); + } }