Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2e263191 authored by Yiyi Shen's avatar Yiyi Shen Committed by Android (Google) Code Review
Browse files

Merge "[Audiosharing] Format files" into main

parents 27e27c5f 7c00a50b
Loading
Loading
Loading
Loading
+66 −49
Original line number Diff line number Diff line
@@ -130,8 +130,7 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
                public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}

                @Override
                public void onSourceAdded(
                        @NonNull BluetoothDevice sink, int sourceId, int reason) {
                public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
                    Log.d(TAG, "onSourceAdded: updateSummary");
                    updateSummary();
                }
@@ -164,8 +163,10 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
                        int sourceId,
                        @NonNull BluetoothLeBroadcastReceiveState state) {}
            };

    @VisibleForTesting
    final BluetoothLeAudio.Callback mLeAudioCallback = new BluetoothLeAudio.Callback() {
    final BluetoothLeAudio.Callback mLeAudioCallback =
            new BluetoothLeAudio.Callback() {
                @Override
                public void onBroadcastToUnicastFallbackGroupChanged(int groupId) {
                    if (!Flags.adoptPrimaryGroupManagementApiV2()) return;
@@ -176,8 +177,8 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
                }

                @Override
        public void onCodecConfigChanged(int i,
                @NonNull BluetoothLeAudioCodecStatus bluetoothLeAudioCodecStatus) {}
                public void onCodecConfigChanged(
                        int i, @NonNull BluetoothLeAudioCodecStatus bluetoothLeAudioCodecStatus) {}

                @Override
                public void onGroupNodeAdded(@NonNull BluetoothDevice bluetoothDevice, int i) {}
@@ -291,13 +292,17 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
    }

    @Override
    public void onActiveDeviceChanged(@Nullable CachedBluetoothDevice activeDevice,
            int bluetoothProfile) {
        if (activeDevice != null && bluetoothProfile == BluetoothProfile.LE_AUDIO
    public void onActiveDeviceChanged(
            @Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
        if (activeDevice != null
                && bluetoothProfile == BluetoothProfile.LE_AUDIO
                && BluetoothUtils.isBroadcasting(mBtManager)) {
            Log.d(TAG, "onActiveDeviceChanged: update summary, device = "
            Log.d(
                    TAG,
                    "onActiveDeviceChanged: update summary, device = "
                            + activeDevice.getDevice().getAnonymizedAddress()
                    + ", profile = " + bluetoothProfile);
                            + ", profile = "
                            + bluetoothProfile);
            updateSummary();
        }
    }
@@ -332,8 +337,11 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
            Log.d(TAG, "Skip registerCallbacks(). Feature is not available.");
            return;
        }
        if (mEventManager == null || mAssistant == null || (Flags.adoptPrimaryGroupManagementApiV2()
                ? mLeAudio == null : mContentResolver == null)) {
        if (mEventManager == null
                || mAssistant == null
                || (Flags.adoptPrimaryGroupManagementApiV2()
                        ? mLeAudio == null
                        : mContentResolver == null)) {
            Log.d(
                    TAG,
                    "Skip registerCallbacks(). Init is not ready: eventManager = "
@@ -369,8 +377,11 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
            Log.d(TAG, "Skip unregisterCallbacks(). Feature is not available.");
            return;
        }
        if (mEventManager == null || mAssistant == null || (Flags.adoptPrimaryGroupManagementApiV2()
                ? mLeAudio == null : mContentResolver == null)) {
        if (mEventManager == null
                || mAssistant == null
                || (Flags.adoptPrimaryGroupManagementApiV2()
                        ? mLeAudio == null
                        : mContentResolver == null)) {
            Log.d(TAG, "Skip unregisterCallbacks(). Init is not ready.");
            return;
        }
@@ -393,18 +404,19 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP

    private void setCallAudioGroup(int groupId) {
        List<BluetoothDevice> devices =
                mGroupedConnectedDevices.getOrDefault(
                        groupId, ImmutableList.of());
        CachedBluetoothDevice lead =
                AudioSharingUtils.getLeadDevice(
                        mCacheManager, devices);
                mGroupedConnectedDevices.getOrDefault(groupId, ImmutableList.of());
        CachedBluetoothDevice lead = AudioSharingUtils.getLeadDevice(mCacheManager, devices);
        if (lead != null) {
            String addr = lead.getDevice().getAnonymizedAddress();
            Log.d(TAG, "Set call audio device: " + addr);
            if ((Flags.adoptPrimaryGroupManagementApi() || (Flags.audioSharingDeveloperOption()
                    && BluetoothUtils.getAudioSharingPreviewValue(mContentResolver)))
            if ((Flags.adoptPrimaryGroupManagementApi()
                            || (Flags.audioSharingDeveloperOption()
                                    && BluetoothUtils.getAudioSharingPreviewValue(
                                            mContentResolver)))
                    && !mIsAudioModeOngoingCall.get()) {
                LeAudioProfile leaProfile = mBtManager == null ? null
                LeAudioProfile leaProfile =
                        mBtManager == null
                                ? null
                                : mBtManager.getProfileManager().getLeAudioProfile();
                if (leaProfile != null) {
                    leaProfile.setBroadcastToUnicastFallbackGroup(groupId);
@@ -476,8 +488,8 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
    private Pair<Integer, AudioSharingDeviceItem> getActiveItemWithIndex() {
        List<AudioSharingDeviceItem> deviceItems = new ArrayList<>(mDeviceItemsInSharingSession);
        int fallbackActiveGroupId =
                BluetoothUtils.getPrimaryGroupIdForBroadcast(mContext.getContentResolver(),
                        mBtManager);
                BluetoothUtils.getPrimaryGroupIdForBroadcast(
                        mContext.getContentResolver(), mBtManager);
        if (fallbackActiveGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
            for (AudioSharingDeviceItem item : deviceItems) {
                if (item.getGroupId() == fallbackActiveGroupId) {
@@ -532,12 +544,17 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
                                                    : ChangeCallAudioType.CONNECTED_EARLIER;
                                }
                            }
                            Pair<Integer, Object>[] eventData = new Pair[]{
                                    Pair.create(METRIC_KEY_DEVICE_CONNECTION_TYPE.getId(),
                            Pair<Integer, Object>[] eventData =
                                    new Pair[] {
                                        Pair.create(
                                                METRIC_KEY_DEVICE_CONNECTION_TYPE.getId(),
                                                type.getName()),
                                    Pair.create(METRIC_KEY_DEVICE_IS_TEMP_BOND.getId(),
                                            BluetoothUtils.isTemporaryBondDevice(target.getDevice())
                                                    ? 1 : 0)
                                        Pair.create(
                                                METRIC_KEY_DEVICE_IS_TEMP_BOND.getId(),
                                                BluetoothUtils.isTemporaryBondDevice(
                                                                target.getDevice())
                                                        ? 1
                                                        : 0)
                                    };
                            mMetricsFeatureProvider.action(
                                    mContext,
+2 −1
Original line number Diff line number Diff line
@@ -155,7 +155,8 @@ public class AudioSharingCompatibilityPreferenceController extends TogglePrefere

    @Override
    public int getAvailabilityStatus() {
        return BluetoothUtils.isAudioSharingUIAvailable(mContext) ? AVAILABLE
        return BluetoothUtils.isAudioSharingUIAvailable(mContext)
                ? AVAILABLE
                : UNSUPPORTED_ON_DEVICE;
    }

+114 −86
Original line number Diff line number Diff line
@@ -207,8 +207,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                                    + broadcastId);
                    mStoppingSharing.compareAndSet(true, false);
                    updateSwitch();
                    AudioSharingUtils.postOnMainThread(mContext,
                            () -> dismissStaleDialogsOtherThanErrorDialog());
                    AudioSharingUtils.postOnMainThread(
                            mContext, () -> dismissStaleDialogsOtherThanErrorDialog());
                    AudioSharingUtils.toastMessage(
                            mContext,
                            mContext.getString(R.string.audio_sharing_sharing_stopped_label));
@@ -265,13 +265,16 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}

                @Override
                public void onSourceAdded(
                        @NonNull BluetoothDevice sink, int sourceId, int reason) {
                public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
                    if (mSinksInAdding.contains(sink)) {
                        mSinksInAdding.remove(sink);
                    }
                    dismissProgressDialogIfNeeded();
                    Log.d(TAG, "onSourceAdded(), sink = " + sink + ", remaining sinks = "
                    Log.d(
                            TAG,
                            "onSourceAdded(), sink = "
                                    + sink
                                    + ", remaining sinks = "
                                    + mSinksInAdding);
                    if (mSinksToWaitFor.contains(sink)) {
                        mSinksToWaitFor.remove(sink);
@@ -286,8 +289,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                                            /* userTriggered= */ false,
                                            /* deviceCountInSharing= */ 1,
                                            /* candidateDeviceCount= */ 0);
                            showJoinAudioSharingDialog(eventData,
                                    mBroadcast.getLatestBluetoothLeBroadcastMetadata());
                            showJoinAudioSharingDialog(
                                    eventData, mBroadcast.getLatestBluetoothLeBroadcastMetadata());
                        }
                    }
                }
@@ -336,9 +339,14 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                        @NonNull BluetoothDevice sink,
                        int sourceId,
                        @NonNull BluetoothLeBroadcastReceiveState state) {
                    Log.d(TAG,
                            "onReceiveStateChanged(), sink = " + sink + ", sourceId = " + sourceId
                                    + ", state = " + state);
                    Log.d(
                            TAG,
                            "onReceiveStateChanged(), sink = "
                                    + sink
                                    + ", sourceId = "
                                    + sourceId
                                    + ", state = "
                                    + state);
                }
            };

@@ -449,7 +457,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController

    @Override
    public int getAvailabilityStatus() {
        return BluetoothUtils.isAudioSharingUIAvailable(mContext) ? AVAILABLE
        return BluetoothUtils.isAudioSharingUIAvailable(mContext)
                ? AVAILABLE
                : UNSUPPORTED_ON_DEVICE;
    }

@@ -479,12 +488,15 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
    }

    @Override
    public void onActiveDeviceChanged(@Nullable CachedBluetoothDevice activeDevice,
            int bluetoothProfile) {
    public void onActiveDeviceChanged(
            @Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
        if (activeDevice != null) {
            Log.d(TAG, "onActiveDeviceChanged: device = "
            Log.d(
                    TAG,
                    "onActiveDeviceChanged: device = "
                            + activeDevice.getDevice().getAnonymizedAddress()
                    + ", profile = " + bluetoothProfile);
                            + ", profile = "
                            + bluetoothProfile);
            updateSwitch();
        }
    }
@@ -506,7 +518,9 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                deviceManager == null ? null : deviceManager.findDevice(device);
        if (cachedDevice != null && mBroadcast != null) {
            Log.d(TAG, "handleAutoAddSourceAfterPair, device = " + device.getAnonymizedAddress());
            addSourceToTargetSinks(ImmutableList.of(device), cachedDevice.getName(),
            addSourceToTargetSinks(
                    ImmutableList.of(device),
                    cachedDevice.getName(),
                    mBroadcast.getLatestBluetoothLeBroadcastMetadata(),
                    AudioSharingUtils.buildAddSourceEventData(
                            SettingsEnums.BLUETOOTH_PAIRING, /* userTriggered= */ true));
@@ -611,9 +625,9 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                                            && AudioSharingUtils.isAudioSharingProfileReady(
                                                    mProfileManager)
                                            && (isBroadcasting
                                            // Always enable toggle when no connected sink. We have
                                            // dialog to guide users to connect compatible devices
                                            // for audio sharing.
                                                    // Always enable toggle when no connected sink.
                                                    // We have dialog to guide users to connect
                                                    // compatible devices for audio sharing.
                                                    || hasEmptyConnectedDevice
                                                    // Disable toggle till device gets active after
                                                    // broadcast ends.
@@ -646,7 +660,9 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
    }

    private void handleOnBroadcastReady(@NonNull BluetoothLeBroadcastMetadata metadata) {
        List<BluetoothDevice> targetActiveSinks = mTargetActiveItem == null ? ImmutableList.of()
        List<BluetoothDevice> targetActiveSinks =
                mTargetActiveItem == null
                        ? ImmutableList.of()
                        : mGroupedConnectedDevices.getOrDefault(
                                mTargetActiveItem.getGroupId(), ImmutableList.of());
        Pair<Integer, Object>[] eventData =
@@ -659,7 +675,10 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
        // Auto add primary/active sinks w/o user interactions.
        if (!targetActiveSinks.isEmpty() && mTargetActiveItem != null) {
            Log.d(TAG, "handleOnBroadcastReady: automatically add source to active sinks.");
            addSourceToTargetSinks(targetActiveSinks, mTargetActiveItem.getName(), metadata,
            addSourceToTargetSinks(
                    targetActiveSinks,
                    mTargetActiveItem.getName(),
                    metadata,
                    AudioSharingUtils.buildAddSourceEventData(
                            SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING,
                            /* userTriggered= */ false));
@@ -680,9 +699,13 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                    && mDeviceItemsForSharing.size() == 1) {
                Log.d(TAG, "handleOnBroadcastReady: auto add source to the second device");
                AudioSharingDeviceItem target = mDeviceItemsForSharing.get(0);
                List<BluetoothDevice> targetSinks = mGroupedConnectedDevices.getOrDefault(
                List<BluetoothDevice> targetSinks =
                        mGroupedConnectedDevices.getOrDefault(
                                target.getGroupId(), ImmutableList.of());
                addSourceToTargetSinks(targetSinks, target.getName(), metadata,
                addSourceToTargetSinks(
                        targetSinks,
                        target.getName(),
                        metadata,
                        AudioSharingUtils.buildAddSourceEventData(
                                SettingsEnums.ACTION_AUTO_JOIN_AUDIO_SHARING,
                                /* userTriggered= */ true));
@@ -709,8 +732,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
        }
    }

    private void showJoinAudioSharingDialog(Pair<Integer, Object>[] eventData,
            @Nullable BluetoothLeBroadcastMetadata metadata) {
    private void showJoinAudioSharingDialog(
            Pair<Integer, Object>[] eventData, @Nullable BluetoothLeBroadcastMetadata metadata) {
        if (!BluetoothUtils.isBroadcasting(mBtManager)) {
            Log.d(TAG, "Skip showJoinAudioSharingDialog, broadcast is stopped");
            return;
@@ -726,9 +749,13 @@ public class AudioSharingSwitchBarController extends BasePreferenceController

                    @Override
                    public void onItemClick(@NonNull AudioSharingDeviceItem item) {
                        List<BluetoothDevice> targetSinks = mGroupedConnectedDevices.getOrDefault(
                        List<BluetoothDevice> targetSinks =
                                mGroupedConnectedDevices.getOrDefault(
                                        item.getGroupId(), ImmutableList.of());
                        addSourceToTargetSinks(targetSinks, item.getName(), metadata,
                        addSourceToTargetSinks(
                                targetSinks,
                                item.getName(),
                                metadata,
                                AudioSharingUtils.buildAddSourceEventData(
                                        SettingsEnums.DIALOG_AUDIO_SHARING_MAIN,
                                        /* userTriggered= */ true));
@@ -744,16 +771,14 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                };
        AudioSharingUtils.postOnMainThread(
                mContext,
                () -> AudioSharingDialogFragment.show(
                        mFragment,
                        mDeviceItemsForSharing,
                        metadata,
                        listener,
                        eventData));
                () ->
                        AudioSharingDialogFragment.show(
                                mFragment, mDeviceItemsForSharing, metadata, listener, eventData));
    }

    private void showErrorDialog() {
        AudioSharingUtils.postOnMainThread(mContext,
        AudioSharingUtils.postOnMainThread(
                mContext,
                () -> {
                    // Remove all stale dialogs before showing error dialog
                    dismissStaleDialogsOtherThanErrorDialog();
@@ -766,14 +791,14 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
        List<Fragment> fragments = new ArrayList<Fragment>();
        try {
            if (mFragment != null) {
                fragments =
                        mFragment.getChildFragmentManager().getFragments();
                fragments = mFragment.getChildFragmentManager().getFragments();
            }
        } catch (Exception e) {
            Log.e(TAG, "Fail to dismiss stale dialogs: " + e.getMessage());
        }
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment instanceof DialogFragment
            if (fragment != null
                    && fragment instanceof DialogFragment
                    && !(fragment instanceof AudioSharingErrorDialogFragment)
                    && ((DialogFragment) fragment).getDialog() != null) {
                Log.d(TAG, "Remove stale dialog = " + fragment.getTag());
@@ -838,8 +863,10 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                        });
    }

    private void addSourceToTargetSinks(List<BluetoothDevice> targetGroupedSinks,
            @NonNull String targetSinkName, @Nullable BluetoothLeBroadcastMetadata metadata,
    private void addSourceToTargetSinks(
            List<BluetoothDevice> targetGroupedSinks,
            @NonNull String targetSinkName,
            @Nullable BluetoothLeBroadcastMetadata metadata,
            Pair<Integer, Object>[] eventData) {
        if (targetGroupedSinks.isEmpty()) {
            Log.d(TAG, "Skip addSourceToTargetSinks, no sinks.");
@@ -854,25 +881,27 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
            return;
        }
        mSinksInAdding.addAll(targetGroupedSinks);
        String progressMessage = mContext.getString(
        String progressMessage =
                mContext.getString(
                        R.string.audio_sharing_progress_dialog_add_source_content, targetSinkName);
        showProgressDialog(progressMessage);
        for (BluetoothDevice sink : targetGroupedSinks) {
            mAssistant.addSource(sink, metadata, /* isGroupOp= */ false);
        }
        mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE,
                eventData);
        mMetricsFeatureProvider.action(
                mContext, SettingsEnums.ACTION_AUDIO_SHARING_ADD_SOURCE, eventData);
    }

    private void showProgressDialog(@NonNull String progressMessage) {
        AudioSharingUtils.postOnMainThread(mContext,
        AudioSharingUtils.postOnMainThread(
                mContext,
                () -> AudioSharingProgressDialogFragment.show(mFragment, progressMessage));
    }

    private void dismissProgressDialogIfNeeded() {
        if (mSinksInAdding.isEmpty()) {
            AudioSharingUtils.postOnMainThread(mContext,
                    () -> AudioSharingProgressDialogFragment.dismiss(mFragment));
            AudioSharingUtils.postOnMainThread(
                    mContext, () -> AudioSharingProgressDialogFragment.dismiss(mFragment));
        }
    }

@@ -893,15 +922,16 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
            FragmentActivity activity = mFragment.getActivity();
            Intent intent = activity.getIntent();
            if (intent != null) {
                sourceMetric = intent.getIntExtra(EXTRA_SOURCE_METRICS,
                        SettingsEnums.PAGE_UNKNOWN);
                sourceMetric = intent.getIntExtra(EXTRA_SOURCE_METRICS, SettingsEnums.PAGE_UNKNOWN);
            }
            if (activity instanceof SettingsActivity settingsActivity) {
                callingPackage = settingsActivity.getInitialCallingPackage();
            }
        }
        Pair<Integer, Object>[] eventData = new Pair[]{
                Pair.create(AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
        Pair<Integer, Object>[] eventData =
                new Pair[] {
                    Pair.create(
                            AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PAGE_ID.getId(),
                            sourceMetric),
                    Pair.create(
                            AudioSharingUtils.MetricKey.METRIC_KEY_SOURCE_PACKAGE_NAME.getId(),
@@ -911,9 +941,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
                            candidateDeviceCount)
                };
        mMetricsFeatureProvider.action(
                mContext,
                SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_ON,
                eventData);
                mContext, SettingsEnums.ACTION_AUDIO_SHARING_MAIN_SWITCH_ON, eventData);
    }

    private enum StartIntentHandleStage {
+99 −64

File changed.

Preview size limit exceeded, changes collapsed.

+185 −125

File changed.

Preview size limit exceeded, changes collapsed.