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

Commit c6a12c14 authored by Evan Chen's avatar Evan Chen
Browse files

Enable singleDevice for CDM UI

Test: atest CtsCompanionDeviceManagerCoreTestCases
      atest CtsCompanionDeviceManagerUiAutomationTestCases
      atest CtsOsTestCases:CompanionDeviceManagerTest

Bug: 211722613
Change-Id: I1d3105070c24a8df3e4112a2e0d368cfe4df139f
parent b1101d1a
Loading
Loading
Loading
Loading
+27 −17
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

/**
 *  A CompanionDevice activity response for showing the available
 *  nearby devices to be associated with.
@@ -204,10 +206,7 @@ public class CompanionDeviceActivity extends AppCompatActivity {
        if (mRequest.isSelfManaged()) {
            initUiForSelfManagedAssociation(appLabel);
        } else if (mRequest.isSingleDevice()) {
            // TODO(b/211722613)
            // Treat singleDevice as the multipleDevices for now
            // initUiForSingleDevice(appLabel);
            initUiForMultipleDevices(appLabel);
            initUiForSingleDevice(appLabel);
        } else {
            initUiForMultipleDevices(appLabel);
        }
@@ -221,12 +220,6 @@ public class CompanionDeviceActivity extends AppCompatActivity {
    }

    private void onUserSelectedDevice(@NonNull DeviceFilterPair<?> selectedDevice) {
        if (mSelectedDevice != null) {
            if (DEBUG) Log.w(TAG, "Already selected.");
            return;
        }
        mSelectedDevice = requireNonNull(selectedDevice);

        final MacAddress macAddress = selectedDevice.getMacAddress();
        onAssociationApproved(macAddress);
    }
@@ -346,13 +339,26 @@ public class CompanionDeviceActivity extends AppCompatActivity {
    private void initUiForSingleDevice(CharSequence appLabel) {
        if (DEBUG) Log.i(TAG, "initUiFor_SingleDevice()");

        // TODO: use real name
        final String deviceName = "<device>";
        final String deviceProfile = mRequest.getDeviceProfile();

        CompanionDeviceDiscoveryService.getScanResult().observe(this,
                deviceFilterPairs -> updateSingleDeviceUi(
                        deviceFilterPairs, deviceProfile, appLabel));

        mListView.setVisibility(View.GONE);
    }

    private void updateSingleDeviceUi(List<DeviceFilterPair<?>> deviceFilterPairs,
            String deviceProfile, CharSequence appLabel) {
        // Ignore "empty" scan repots.
        if (deviceFilterPairs.isEmpty()) return;
        mSelectedDevice = requireNonNull(deviceFilterPairs.get(0));

        final String deviceName = mSelectedDevice.getDisplayName();
        final Spanned title = getHtmlFromResources(
                this, R.string.confirmation_title, appLabel, deviceName);
        final Spanned summary;

        if (deviceProfile == null) {
            summary = getHtmlFromResources(this, R.string.summary_generic);
        } else if (deviceProfile.equals(DEVICE_PROFILE_WATCH)) {
@@ -363,8 +369,6 @@ public class CompanionDeviceActivity extends AppCompatActivity {

        mTitle.setText(title);
        mSummary.setText(summary);

        mListView.setVisibility(View.GONE);
    }

    private void initUiForMultipleDevices(CharSequence appLabel) {
@@ -405,6 +409,14 @@ public class CompanionDeviceActivity extends AppCompatActivity {
        if (DEBUG) Log.d(TAG, "onListItemClick() " + position);

        final DeviceFilterPair<?> selectedDevice = mAdapter.getItem(position);

        if (mSelectedDevice != null) {
            if (DEBUG) Log.w(TAG, "Already selected.");
            return;
        }

        mSelectedDevice = requireNonNull(selectedDevice);

        onUserSelectedDevice(selectedDevice);
    }

@@ -417,9 +429,7 @@ public class CompanionDeviceActivity extends AppCompatActivity {
        if (mRequest.isSelfManaged()) {
            onAssociationApproved(null);
        } else {
            // TODO(b/211722613): call onUserSelectedDevice().
            throw new UnsupportedOperationException(
                    "isSingleDevice() requests are not supported yet.");
            onUserSelectedDevice(mSelectedDevice);
        }
    }

+17 −5
Original line number Diff line number Diff line
@@ -103,6 +103,8 @@ public class CompanionDeviceDiscoveryService extends Service {

    private final Runnable mTimeoutRunnable = this::timeout;

    private boolean mStopAfterFirstMatch;;

    /**
     * A state enum for devices' discovery.
     */
@@ -163,8 +165,7 @@ public class CompanionDeviceDiscoveryService extends Service {
                break;

            case ACTION_STOP_DISCOVERY:
                stopDiscoveryAndFinish();
                sStateLiveData.setValue(DiscoveryState.FINISHED_STOPPED);
                stopDiscoveryAndFinish(/* timeout */ false);
                break;
        }
        return START_NOT_STICKY;
@@ -182,6 +183,7 @@ public class CompanionDeviceDiscoveryService extends Service {
        requireNonNull(request);

        if (mDiscoveryStarted) throw new RuntimeException("Discovery in progress.");
        mStopAfterFirstMatch = request.isSingleDevice();
        mDiscoveryStarted = true;
        sStateLiveData.setValue(DiscoveryState.DISCOVERY_IN_PROGRESS);
        sScanResultsLiveData.setValue(Collections.emptyList());
@@ -208,7 +210,7 @@ public class CompanionDeviceDiscoveryService extends Service {
    }

    @MainThread
    private void stopDiscoveryAndFinish() {
    private void stopDiscoveryAndFinish(boolean timeout) {
        if (DEBUG) Log.i(TAG, "stopDiscovery()");

        if (!mDiscoveryStarted) {
@@ -243,6 +245,12 @@ public class CompanionDeviceDiscoveryService extends Service {

        Handler.getMain().removeCallbacks(mTimeoutRunnable);

        if (timeout) {
            sStateLiveData.setValue(DiscoveryState.FINISHED_TIMEOUT);
        } else {
            sStateLiveData.setValue(DiscoveryState.FINISHED_STOPPED);
        }

        // "Finish".
        stopSelf();
    }
@@ -332,6 +340,7 @@ public class CompanionDeviceDiscoveryService extends Service {
    private void onDeviceFound(@NonNull DeviceFilterPair<?> device) {
        runOnMainThread(() -> {
            if (DEBUG) Log.v(TAG, "onDeviceFound() " + device);
            if (mDiscoveryStopped) return;
            if (mDevicesFound.contains(device)) {
                // TODO: update the device instead of ignoring (new found device may contain
                //  additional/updated info, eg. name of the device).
@@ -347,6 +356,10 @@ public class CompanionDeviceDiscoveryService extends Service {
            mDevicesFound.add(device);
            // Then: notify observers.
            sScanResultsLiveData.setValue(mDevicesFound);
            // Stop discovery when there's one device found for singleDevice.
            if (mStopAfterFirstMatch) {
                stopDiscoveryAndFinish(/* timeout */ false);
            }
        });
    }

@@ -378,8 +391,7 @@ public class CompanionDeviceDiscoveryService extends Service {

    private void timeout() {
        if (DEBUG) Log.i(TAG, "timeout()");
        stopDiscoveryAndFinish();
        sStateLiveData.setValue(DiscoveryState.FINISHED_TIMEOUT);
        stopDiscoveryAndFinish(/* timeout */ true);
    }

    @Override