Merge "Loading the A11yHearingAidPreference summary in the background" into main

This commit is contained in:
Treehugger Robot
2024-06-07 00:26:38 +00:00
committed by Android (Google) Code Review
2 changed files with 26 additions and 1 deletions

View File

@@ -43,6 +43,7 @@ import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.utils.ThreadUtils;
import java.util.Set; import java.util.Set;
@@ -120,7 +121,20 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
} }
@Override @Override
public CharSequence getSummary() { protected void refreshSummary(Preference preference) {
if (preference == null) {
return;
}
// Loading the hearing aids summary requires IPC call, which can block the UI thread.
// To reduce page loading latency, move loadSummary in the background thread.
ThreadUtils.postOnBackgroundThread(() -> {
CharSequence summary = loadSummary();
ThreadUtils.getUiThreadHandler().post(() -> preference.setSummary(summary));
});
}
private CharSequence loadSummary() {
final CachedBluetoothDevice device = mHelper.getConnectedHearingAidDevice(); final CachedBluetoothDevice device = mHelper.getConnectedHearingAidDevice();
if (device == null) { if (device == null) {
return mContext.getText(R.string.accessibility_hearingaid_not_connected_summary); return mContext.getText(R.string.accessibility_hearingaid_not_connected_summary);

View File

@@ -60,6 +60,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow; import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowLooper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@@ -132,6 +133,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString().contentEquals( assertThat(mHearingAidPreference.getSummary().toString().contentEquals(
"TEST_HEARING_AID_BT_DEVICE_NAME / Right only")).isTrue(); "TEST_HEARING_AID_BT_DEVICE_NAME / Right only")).isTrue();
@@ -149,6 +151,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString().contentEquals( assertThat(mHearingAidPreference.getSummary().toString().contentEquals(
"TEST_HEARING_AID_BT_DEVICE_NAME / Left and right")).isTrue(); "TEST_HEARING_AID_BT_DEVICE_NAME / Left and right")).isTrue();
@@ -165,6 +168,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString().contentEquals( assertThat(mHearingAidPreference.getSummary().toString().contentEquals(
"TEST_HEARING_AID_BT_DEVICE_NAME / Left only")).isTrue(); "TEST_HEARING_AID_BT_DEVICE_NAME / Left only")).isTrue();
@@ -181,6 +185,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString().contentEquals( assertThat(mHearingAidPreference.getSummary().toString().contentEquals(
"TEST_HEARING_AID_BT_DEVICE_NAME / Right only")).isTrue(); "TEST_HEARING_AID_BT_DEVICE_NAME / Right only")).isTrue();
@@ -197,6 +202,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString().contentEquals( assertThat(mHearingAidPreference.getSummary().toString().contentEquals(
"TEST_HEARING_AID_BT_DEVICE_NAME / Left and right")).isTrue(); "TEST_HEARING_AID_BT_DEVICE_NAME / Left and right")).isTrue();
@@ -212,6 +218,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHapClient.ACTION_HAP_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo( assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo(
"TEST_HEARING_AID_BT_DEVICE_NAME / Left and right"); "TEST_HEARING_AID_BT_DEVICE_NAME / Left and right");
@@ -228,6 +235,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString().contentEquals( assertThat(mHearingAidPreference.getSummary().toString().contentEquals(
"TEST_HEARING_AID_BT_DEVICE_NAME +1 more")).isTrue(); "TEST_HEARING_AID_BT_DEVICE_NAME +1 more")).isTrue();
@@ -239,6 +247,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF); intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
sendIntent(intent); sendIntent(intent);
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary()).isEqualTo( assertThat(mHearingAidPreference.getSummary()).isEqualTo(
mContext.getText(R.string.accessibility_hearingaid_not_connected_summary)); mContext.getText(R.string.accessibility_hearingaid_not_connected_summary));
@@ -252,6 +261,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
mPreferenceController.onStart(); mPreferenceController.onStart();
mPreferenceController.onServiceConnected(); mPreferenceController.onServiceConnected();
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo( assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo(
"TEST_HEARING_AID_BT_DEVICE_NAME / Left only"); "TEST_HEARING_AID_BT_DEVICE_NAME / Left only");
@@ -265,6 +275,7 @@ public class AccessibilityHearingAidPreferenceControllerTest {
mPreferenceController.onStart(); mPreferenceController.onStart();
mPreferenceController.onServiceConnected(); mPreferenceController.onServiceConnected();
ShadowLooper.idleMainLooper();
assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo( assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo(
"TEST_HEARING_AID_BT_DEVICE_NAME / Right only"); "TEST_HEARING_AID_BT_DEVICE_NAME / Right only");