Use FooterPreference in xml explicitly

- Removed the FooterPreferenceMixin from the BluetoothDetailsMacAddressController.
 - Added the common api addFooterPreference.

Fixes: 139104386
Test: manual test
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.bluetooth
Change-Id: Iab40fa4c5be674290b1dc6d372c84609ccc2ea49
This commit is contained in:
Sunny Shao
2019-08-08 15:43:38 +08:00
parent 656e4ef701
commit 660b36e061
4 changed files with 83 additions and 48 deletions

View File

@@ -47,4 +47,10 @@
<PreferenceCategory <PreferenceCategory
android:key="bluetooth_profiles"/> android:key="bluetooth_profiles"/>
<com.android.settingslib.widget.FooterPreference
android:key="device_details_footer"
android:selectable="false"
settings:searchable="false"
settings:controller="com.android.settings.bluetooth.BluetoothDetailsMacAddressController"/>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -92,6 +92,7 @@ import android.widget.ListView;
import android.widget.TabWidget; import android.widget.TabWidget;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.IconCompat; import androidx.core.graphics.drawable.IconCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
@@ -105,6 +106,7 @@ import com.android.settings.core.FeatureFlags;
import com.android.settings.development.featureflags.FeatureFlagPersistent; import com.android.settings.development.featureflags.FeatureFlagPersistent;
import com.android.settings.password.ChooseLockSettingsHelper; import com.android.settings.password.ChooseLockSettingsHelper;
import com.android.settingslib.widget.ActionBarShadowController; import com.android.settingslib.widget.ActionBarShadowController;
import com.android.settingslib.widget.FooterPreference;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Iterator; import java.util.Iterator;
@@ -221,6 +223,7 @@ public final class Utils extends com.android.settingslib.Utils {
/** /**
* Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses.
*
* @param context the application context * @param context the application context
* @return the formatted and newline-separated IP addresses, or null if none. * @return the formatted and newline-separated IP addresses, or null if none.
*/ */
@@ -257,8 +260,9 @@ public final class Utils extends com.android.settingslib.Utils {
// many cases, because the constructor takes the only string as the language // many cases, because the constructor takes the only string as the language
// code. So : new Locale("en", "US").toString() => "en_US" // code. So : new Locale("en", "US").toString() => "en_US"
// And : new Locale("en_US").toString() => "en_us" // And : new Locale("en_US").toString() => "en_us"
if (null == localeStr) if (null == localeStr) {
return Locale.getDefault(); return Locale.getDefault();
}
String[] brokenDownLocale = localeStr.split("_", 3); String[] brokenDownLocale = localeStr.split("_", 3);
// split may not return a 0-length array. // split may not return a 0-length array.
if (1 == brokenDownLocale.length) { if (1 == brokenDownLocale.length) {
@@ -500,7 +504,8 @@ public final class Utils extends com.android.settingslib.Utils {
* Lookup both {@link Intent#EXTRA_USER} and {@link Intent#EXTRA_USER_ID} in the bundle * 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. * 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) { if (bundle == null) {
return null; return null;
} }
@@ -541,6 +546,7 @@ 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). * Queries for the UserInfo of a user. Returns null if the user doesn't exist (was removed).
*
* @param userManager Instance of UserManager * @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. * @return UserInfo of the user or null for non-existent user.
@@ -625,6 +631,7 @@ public final class Utils extends com.android.settingslib.Utils {
/** /**
* Returns an accessible SpannableString. * 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 * @param accessibileText the text text-to-speech engines should read
*/ */
@@ -835,6 +842,7 @@ public final class Utils extends com.android.settingslib.Utils {
/** /**
* Launches an intent which may optionally have a user id defined. * Launches an intent which may optionally have a user id defined.
*
* @param fragment Fragment to use to launch the activity. * @param fragment Fragment to use to launch the activity.
* @param intent Intent to launch. * @param intent Intent to launch.
*/ */
@@ -864,6 +872,7 @@ public final class Utils extends com.android.settingslib.Utils {
/** /**
* Returns if a given user is a profile of another user. * 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. * @param profile The (potential) profile.
* @return if the profile is actually a profile * @return if the profile is actually a profile
@@ -889,6 +898,7 @@ public final class Utils extends com.android.settingslib.Utils {
/** /**
* Return {@code true} if the supplied package is device owner or profile owner of at * Return {@code true} if the supplied package is device owner or profile owner of at
* least one user. * 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 devicePolicyManager used to check whether it is device owner app
* @param packageName package to check about * @param packageName package to check about
@@ -1065,4 +1075,24 @@ public final class Utils extends com.android.settingslib.Utils {
ActionBarShadowController.attachToView(activity, lifecycle, scrollView); 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);
}
} }

View File

@@ -25,13 +25,13 @@ import com.android.settings.R;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.FooterPreference; import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.widget.FooterPreferenceMixinCompat;
/** /**
* This class adds the device MAC address to a footer. * This class adds the device MAC address to a footer.
*/ */
public class BluetoothDetailsMacAddressController extends BluetoothDetailsController { public class BluetoothDetailsMacAddressController extends BluetoothDetailsController {
FooterPreferenceMixinCompat mFooterPreferenceMixin; public static final String KEY_DEVICE_DETAILS_FOOTER = "device_details_footer";
FooterPreference mFooterPreference; FooterPreference mFooterPreference;
public BluetoothDetailsMacAddressController(Context context, public BluetoothDetailsMacAddressController(Context context,
@@ -39,12 +39,11 @@ public class BluetoothDetailsMacAddressController extends BluetoothDetailsContro
CachedBluetoothDevice device, CachedBluetoothDevice device,
Lifecycle lifecycle) { Lifecycle lifecycle) {
super(context, fragment, device, lifecycle); super(context, fragment, device, lifecycle);
mFooterPreferenceMixin = new FooterPreferenceMixinCompat(fragment, lifecycle);
} }
@Override @Override
protected void init(PreferenceScreen screen) { protected void init(PreferenceScreen screen) {
mFooterPreference = mFooterPreferenceMixin.createFooterPreference(); mFooterPreference = screen.findPreference(KEY_DEVICE_DETAILS_FOOTER);
mFooterPreference.setTitle(mContext.getString( mFooterPreference.setTitle(mContext.getString(
R.string.bluetooth_device_mac_address, mCachedDevice.getAddress())); R.string.bluetooth_device_mac_address, mCachedDevice.getAddress()));
} }
@@ -57,9 +56,6 @@ public class BluetoothDetailsMacAddressController extends BluetoothDetailsContro
@Override @Override
public String getPreferenceKey() { public String getPreferenceKey() {
if (mFooterPreference == null) { return KEY_DEVICE_DETAILS_FOOTER;
return null;
}
return mFooterPreference.getKey();
} }
} }

View File

@@ -15,8 +15,11 @@
*/ */
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import static com.android.settings.bluetooth.BluetoothDetailsMacAddressController.KEY_DEVICE_DETAILS_FOOTER;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.android.settings.Utils;
import com.android.settingslib.widget.FooterPreference; import com.android.settingslib.widget.FooterPreference;
import org.junit.Test; import org.junit.Test;
@@ -25,7 +28,6 @@ import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class BluetoothDetailsMacAddressControllerTest extends BluetoothDetailsControllerTestBase { public class BluetoothDetailsMacAddressControllerTest extends BluetoothDetailsControllerTestBase {
private BluetoothDetailsMacAddressController mController; private BluetoothDetailsMacAddressController mController;
@Override @Override
@@ -34,6 +36,7 @@ public class BluetoothDetailsMacAddressControllerTest extends BluetoothDetailsCo
mController = mController =
new BluetoothDetailsMacAddressController(mContext, mFragment, mCachedDevice, mLifecycle); new BluetoothDetailsMacAddressController(mContext, mFragment, mCachedDevice, mLifecycle);
setupDevice(mDeviceConfig); setupDevice(mDeviceConfig);
Utils.addFooterPreference(mContext, mScreen, KEY_DEVICE_DETAILS_FOOTER, null /* title */);
} }
@Test @Test