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

Commit 13404277 authored by chelseahao's avatar chelseahao Committed by Chelsea Hao
Browse files

[Audiosharing] Send intent to the qr code scanner.

Bug: 308368124
Test: Manual
Change-Id: I45abe790caecc6eb86e4ada2827ed740a5ee6a46
parent de3fe374
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -24,7 +24,8 @@
        android:key="audio_streams_scan_qr_code"
        android:title="@string/bluetooth_find_broadcast_button_scan"
        android:icon="@drawable/ic_add_24dp"
        android:summary="@string/audio_streams_qr_code_summary" />
        android:summary="@string/audio_streams_qr_code_summary"
        settings:controller="com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsScanQrCodeController" />

    <Preference
        android:key="audio_streams_active_device"
+8 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.connecteddevice.audiosharing.audiostreams;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.android.settings.R;
@@ -58,10 +59,17 @@ public class AudioStreamsDashboardFragment extends DashboardFragment {
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        use(AudioStreamsScanQrCodeController.class).setFragment(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // TODO(chelseahao): implementation.
    }
}
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.connecteddevice.audiosharing.audiostreams;

import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode.QrCodeScanModeActivity;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.bluetooth.BluetoothBroadcastUtils;
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;

public class AudioStreamsScanQrCodeController extends BasePreferenceController
        implements DefaultLifecycleObserver {
    private static final String TAG = "AudioStreamsProgressCategoryController";
    private static final boolean DEBUG = BluetoothUtils.D;
    private static final String KEY = "audio_streams_scan_qr_code";
    private static final int REQUEST_SCAN_BT_BROADCAST_QR_CODE = 0;
    private final BluetoothCallback mBluetoothCallback =
            new BluetoothCallback() {
                @Override
                public void onActiveDeviceChanged(
                        @Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
                    if (bluetoothProfile == BluetoothProfile.LE_AUDIO) {
                        updateVisibility();
                    }
                }
            };

    private final LocalBluetoothManager mLocalBtManager;
    private AudioStreamsDashboardFragment mFragment;
    private Preference mPreference;

    public AudioStreamsScanQrCodeController(Context context, String preferenceKey) {
        super(context, preferenceKey);
        mLocalBtManager = Utils.getLocalBtManager(mContext);
    }

    public void setFragment(AudioStreamsDashboardFragment fragment) {
        mFragment = fragment;
    }

    @Override
    public void onStart(@NonNull LifecycleOwner owner) {
        if (mLocalBtManager != null) {
            mLocalBtManager.getEventManager().registerCallback(mBluetoothCallback);
        }
    }

    @Override
    public void onStop(@NonNull LifecycleOwner owner) {
        if (mLocalBtManager != null) {
            mLocalBtManager.getEventManager().unregisterCallback(mBluetoothCallback);
        }
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public String getPreferenceKey() {
        return KEY;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
        if (mPreference == null) {
            Log.w(TAG, "displayPreference() mPreference is null!");
            return;
        }
        mPreference.setOnPreferenceClickListener(
                preference -> {
                    if (mFragment == null) {
                        Log.w(TAG, "displayPreference() mFragment is null!");
                        return false;
                    }
                    if (preference.getKey().equals(KEY)) {
                        Intent intent = new Intent(mContext, QrCodeScanModeActivity.class);
                        intent.setAction(
                                BluetoothBroadcastUtils.ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER);
                        mFragment.startActivityForResult(intent, REQUEST_SCAN_BT_BROADCAST_QR_CODE);
                        if (DEBUG) {
                            Log.w(TAG, "displayPreference() sent intent : " + intent);
                        }
                        return true;
                    }
                    return false;
                });
    }

    private void updateVisibility() {
        ThreadUtils.postOnBackgroundThread(
                () -> {
                    boolean hasActiveLe =
                            AudioSharingUtils.getActiveSinkOnAssistant(mLocalBtManager).isPresent();
                    ThreadUtils.postOnMainThread(() -> mPreference.setVisible(hasActiveLe));
                });
    }
}
+0 −9
Original line number Diff line number Diff line
@@ -16,10 +16,6 @@

package com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode;

import static com.android.settingslib.bluetooth.BluetoothBroadcastUtils.EXTRA_BLUETOOTH_DEVICE_SINK;
import static com.android.settingslib.bluetooth.BluetoothBroadcastUtils.EXTRA_BLUETOOTH_SINK_IS_GROUP;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
@@ -44,9 +40,6 @@ public class QrCodeScanModeActivity extends QrCodeScanModeBaseActivity {
    private static final boolean DEBUG = BluetoothUtils.D;
    private static final String TAG = "QrCodeScanModeActivity";

    private boolean mIsGroupOp;
    private BluetoothDevice mSink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -88,8 +81,6 @@ public class QrCodeScanModeActivity extends QrCodeScanModeBaseActivity {
            Log.d(TAG, "showQrCodeScannerFragment");
        }

        mSink = intent.getParcelableExtra(EXTRA_BLUETOOTH_DEVICE_SINK);
        mIsGroupOp = intent.getBooleanExtra(EXTRA_BLUETOOTH_SINK_IS_GROUP, false);
        if (DEBUG) {
            Log.d(TAG, "get extra from intent");
        }