diff --git a/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceController.java b/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceController.java index fab6e477402..b5101b7dd6d 100644 --- a/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceController.java +++ b/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceController.java @@ -43,6 +43,7 @@ import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStop; +import com.android.settingslib.utils.ThreadUtils; import java.util.Set; @@ -120,7 +121,20 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC } @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(); if (device == null) { return mContext.getText(R.string.accessibility_hearingaid_not_connected_summary); diff --git a/tests/robotests/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceControllerTest.java index 743f8843ced..c521f2545e6 100644 --- a/tests/robotests/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/AccessibilityHearingAidPreferenceControllerTest.java @@ -60,6 +60,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.shadow.api.Shadow; import org.robolectric.shadows.ShadowApplication; +import org.robolectric.shadows.ShadowLooper; import java.util.ArrayList; import java.util.HashSet; @@ -132,6 +133,7 @@ public class AccessibilityHearingAidPreferenceControllerTest { Intent intent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); intent.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString().contentEquals( "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.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString().contentEquals( "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.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString().contentEquals( "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.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString().contentEquals( "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.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString().contentEquals( "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.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHapClient.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo( "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.putExtra(BluetoothHearingAid.EXTRA_STATE, BluetoothHearingAid.STATE_CONNECTED); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString().contentEquals( "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.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF); sendIntent(intent); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary()).isEqualTo( mContext.getText(R.string.accessibility_hearingaid_not_connected_summary)); @@ -252,6 +261,7 @@ public class AccessibilityHearingAidPreferenceControllerTest { mPreferenceController.onStart(); mPreferenceController.onServiceConnected(); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo( "TEST_HEARING_AID_BT_DEVICE_NAME / Left only"); @@ -265,6 +275,7 @@ public class AccessibilityHearingAidPreferenceControllerTest { mPreferenceController.onStart(); mPreferenceController.onServiceConnected(); + ShadowLooper.idleMainLooper(); assertThat(mHearingAidPreference.getSummary().toString()).isEqualTo( "TEST_HEARING_AID_BT_DEVICE_NAME / Right only");