FooterPreference directs summary to title of preference

To compatible with preference controller design,
we override implementation of setSummary() which
directs summary to title of preference.

So, footer preference can be simply created in xml
and controlled by BasePreferenceController.

Test: visual, robotest
Bug: 124129485
Change-Id: I453ffc35761ecf24f2ac1415e0d674ac73c9c475
This commit is contained in:
tmfang
2019-05-31 18:33:23 +08:00
parent 2758853c7d
commit aa45b42423
2 changed files with 30 additions and 11 deletions

View File

@@ -17,6 +17,7 @@
package com.android.settingslib.widget;
import android.content.Context;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;
@@ -55,9 +56,26 @@ public class FooterPreference extends Preference {
title.setLongClickable(false);
}
@Override
public void setSummary(CharSequence summary) {
setTitle(summary);
}
@Override
public void setSummary(int summaryResId) {
setTitle(summaryResId);
}
@Override
public CharSequence getSummary() {
return getTitle();
}
private void init() {
setIcon(R.drawable.ic_info_outline_24);
setKey(KEY_FOOTER);
setOrder(ORDER_FOOTER);
if (TextUtils.isEmpty(getKey())) {
setKey(KEY_FOOTER);
}
}
}

View File

@@ -37,28 +37,29 @@ import org.robolectric.RuntimeEnvironment;
public class FooterPreferenceTest {
private Context mContext;
private FooterPreference mFooterPreference;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
}
@Test
public void createNewPreference_shouldSetKeyAndOrder() {
final FooterPreference preference = new FooterPreference(mContext);
assertThat(preference.getKey()).isEqualTo(FooterPreference.KEY_FOOTER);
assertThat(preference.getOrder()).isEqualTo(FooterPreference.ORDER_FOOTER);
mFooterPreference = new FooterPreference(mContext);
}
@Test
public void bindPreference_shouldLinkifyContent() {
final FooterPreference preference = new FooterPreference(mContext);
final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_footer, null));
preference.onBindViewHolder(holder);
mFooterPreference.onBindViewHolder(holder);
assertThat(((TextView) holder.findViewById(android.R.id.title)).getMovementMethod())
.isInstanceOf(LinkMovementMethod.class);
}
@Test
public void setSummary_summarySet_shouldSetAsTitle() {
mFooterPreference.setSummary("summary");
assertThat(mFooterPreference.getTitle()).isEqualTo("summary");
}
}