[Audiosharing] Move getSummary to a bg thread.

Bug: 308368124
Test: Manual
Change-Id: I6d521681ce8835e5187c43f6ec930fb119f46674
This commit is contained in:
chelseahao
2023-11-23 13:13:48 +08:00
committed by Chelsea Hao
parent 2dfae393c4
commit 1e0301d70f
2 changed files with 32 additions and 11 deletions

View File

@@ -25,10 +25,10 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.SummaryUpdater;
public class AudioStreamsActiveDeviceController extends BasePreferenceController
implements SummaryUpdater.OnSummaryChangeListener, DefaultLifecycleObserver {
implements AudioStreamsActiveDeviceSummaryUpdater.OnSummaryChangeListener,
DefaultLifecycleObserver {
public static final String KEY = "audio_streams_active_device";
private final AudioStreamsActiveDeviceSummaryUpdater mSummaryHelper;

View File

@@ -19,28 +19,30 @@ package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.annotation.Nullable;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settings.widget.SummaryUpdater;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import java.util.Optional;
public class AudioStreamsActiveDeviceSummaryUpdater extends SummaryUpdater
implements BluetoothCallback {
private static final String TAG = "AudioStreamsListenWithSummaryUpdater";
public class AudioStreamsActiveDeviceSummaryUpdater implements BluetoothCallback {
private static final String TAG = "AudioStreamsActiveDeviceSummaryUpdater";
private static final boolean DEBUG = BluetoothUtils.D;
private final LocalBluetoothManager mBluetoothManager;
private String mSummary;
private OnSummaryChangeListener mListener;
public AudioStreamsActiveDeviceSummaryUpdater(
Context context, OnSummaryChangeListener listener) {
super(context, listener);
mBluetoothManager = Utils.getLocalBluetoothManager(context);
mListener = listener;
}
@Override
@@ -59,8 +61,7 @@ public class AudioStreamsActiveDeviceSummaryUpdater extends SummaryUpdater
}
}
@Override
public void register(boolean register) {
void register(boolean register) {
if (register) {
notifyChangeIfNeeded();
mBluetoothManager.getEventManager().registerCallback(this);
@@ -69,8 +70,18 @@ public class AudioStreamsActiveDeviceSummaryUpdater extends SummaryUpdater
}
}
@Override
protected String getSummary() {
private void notifyChangeIfNeeded() {
ThreadUtils.postOnBackgroundThread(
() -> {
String summary = getSummary();
if (!TextUtils.equals(mSummary, summary)) {
mSummary = summary;
ThreadUtils.postOnMainThread(() -> mListener.onSummaryChanged(summary));
}
});
}
private String getSummary() {
var activeSink = getActiveSinkOnAssistant(mBluetoothManager);
if (activeSink.isEmpty()) {
return "No active LE Audio device";
@@ -95,4 +106,14 @@ public class AudioStreamsActiveDeviceSummaryUpdater extends SummaryUpdater
}
return Optional.empty();
}
/** Interface definition for a callback to be invoked when the summary has been changed. */
interface OnSummaryChangeListener {
/**
* Called when summary has changed.
*
* @param summary The new summary.
*/
void onSummaryChanged(String summary);
}
}