Loading packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionAssociationActivity.java +4 −1 Original line number Diff line number Diff line Loading @@ -211,7 +211,10 @@ public class CompanionAssociationActivity extends FragmentActivity implements // Start discovery services if needed. if (!mRequest.isSelfManaged()) { CompanionDeviceDiscoveryService.startForRequest(this, mRequest); boolean started = CompanionDeviceDiscoveryService.startForRequest(this, mRequest); if (!started) { return; } // TODO(b/217749191): Create the ViewModel for the LiveData CompanionDeviceDiscoveryService.getDiscoveryState().observe( /* LifeCycleOwner */ this, this::onDiscoveryStateChanged); Loading packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java +36 −17 Original line number Diff line number Diff line Loading @@ -60,6 +60,8 @@ import android.util.Slog; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import com.android.internal.annotations.GuardedBy; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; Loading Loading @@ -88,6 +90,9 @@ public class CompanionDeviceDiscoveryService extends Service { new MutableLiveData<>(Collections.emptyList()); private static MutableLiveData<DiscoveryState> sStateLiveData = new MutableLiveData<>(DiscoveryState.NOT_STARTED); private static final Object LOCK = new Object(); @GuardedBy("LOCK") private static boolean sDiscoveryStarted = false; private BluetoothManager mBtManager; private BluetoothAdapter mBtAdapter; Loading @@ -98,8 +103,6 @@ public class CompanionDeviceDiscoveryService extends Service { private BluetoothBroadcastReceiver mBtReceiver; private WifiBroadcastReceiver mWifiReceiver; private boolean mDiscoveryStarted = false; private boolean mDiscoveryStopped = false; private final List<DeviceFilterPair<?>> mDevicesFound = new ArrayList<>(); private final Runnable mTimeoutRunnable = this::timeout; Loading @@ -111,22 +114,27 @@ public class CompanionDeviceDiscoveryService extends Service { */ enum DiscoveryState { NOT_STARTED, STARTING, DISCOVERY_IN_PROGRESS, IN_PROGRESS, FINISHED_STOPPED, FINISHED_TIMEOUT } static void startForRequest( static boolean startForRequest( @NonNull Context context, @NonNull AssociationRequest associationRequest) { synchronized (LOCK) { if (sDiscoveryStarted) { Slog.e(TAG, "Discovery is already started. Ignoring this request..."); return false; } } requireNonNull(associationRequest); final Intent intent = new Intent(context, CompanionDeviceDiscoveryService.class); intent.setAction(ACTION_START_DISCOVERY); intent.putExtra(EXTRA_ASSOCIATION_REQUEST, associationRequest); sStateLiveData.setValue(DiscoveryState.STARTING); sScanResultsLiveData.setValue(Collections.emptyList()); context.startService(intent); return true; } static void stop(@NonNull Context context) { Loading Loading @@ -176,10 +184,16 @@ public class CompanionDeviceDiscoveryService extends Service { Slog.d(TAG, "startDiscovery() request=" + request); requireNonNull(request); if (mDiscoveryStarted) throw new RuntimeException("Discovery in progress."); synchronized (LOCK) { if (sDiscoveryStarted) { Slog.e(TAG, "Discovery is already started. Returning..."); return; } sDiscoveryStarted = true; } mStopAfterFirstMatch = request.isSingleDevice(); mDiscoveryStarted = true; sStateLiveData.setValue(DiscoveryState.DISCOVERY_IN_PROGRESS); sScanResultsLiveData.setValue(Collections.emptyList()); sStateLiveData.setValue(DiscoveryState.IN_PROGRESS); final List<DeviceFilter<?>> allFilters = request.getDeviceFilters(); final List<BluetoothDeviceFilter> btFilters = Loading Loading @@ -211,13 +225,12 @@ public class CompanionDeviceDiscoveryService extends Service { private void stopDiscoveryAndFinish(boolean timeout) { Slog.d(TAG, "stopDiscoveryAndFinish(" + timeout + ")"); if (!mDiscoveryStarted) { synchronized (LOCK) { if (!sDiscoveryStarted) { stopSelf(); return; } if (mDiscoveryStopped) return; mDiscoveryStopped = true; } // Stop BT discovery. if (mBtReceiver != null) { Loading Loading @@ -249,6 +262,10 @@ public class CompanionDeviceDiscoveryService extends Service { sStateLiveData.setValue(DiscoveryState.FINISHED_STOPPED); } synchronized (LOCK) { sDiscoveryStarted = false; } // "Finish". stopSelf(); } Loading Loading @@ -340,7 +357,9 @@ public class CompanionDeviceDiscoveryService extends Service { private void onDeviceFound(@NonNull DeviceFilterPair<?> device) { runOnMainThread(() -> { if (mDiscoveryStopped) return; synchronized (LOCK) { if (!sDiscoveryStarted) 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). Loading Loading
packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionAssociationActivity.java +4 −1 Original line number Diff line number Diff line Loading @@ -211,7 +211,10 @@ public class CompanionAssociationActivity extends FragmentActivity implements // Start discovery services if needed. if (!mRequest.isSelfManaged()) { CompanionDeviceDiscoveryService.startForRequest(this, mRequest); boolean started = CompanionDeviceDiscoveryService.startForRequest(this, mRequest); if (!started) { return; } // TODO(b/217749191): Create the ViewModel for the LiveData CompanionDeviceDiscoveryService.getDiscoveryState().observe( /* LifeCycleOwner */ this, this::onDiscoveryStateChanged); Loading
packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java +36 −17 Original line number Diff line number Diff line Loading @@ -60,6 +60,8 @@ import android.util.Slog; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import com.android.internal.annotations.GuardedBy; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; Loading Loading @@ -88,6 +90,9 @@ public class CompanionDeviceDiscoveryService extends Service { new MutableLiveData<>(Collections.emptyList()); private static MutableLiveData<DiscoveryState> sStateLiveData = new MutableLiveData<>(DiscoveryState.NOT_STARTED); private static final Object LOCK = new Object(); @GuardedBy("LOCK") private static boolean sDiscoveryStarted = false; private BluetoothManager mBtManager; private BluetoothAdapter mBtAdapter; Loading @@ -98,8 +103,6 @@ public class CompanionDeviceDiscoveryService extends Service { private BluetoothBroadcastReceiver mBtReceiver; private WifiBroadcastReceiver mWifiReceiver; private boolean mDiscoveryStarted = false; private boolean mDiscoveryStopped = false; private final List<DeviceFilterPair<?>> mDevicesFound = new ArrayList<>(); private final Runnable mTimeoutRunnable = this::timeout; Loading @@ -111,22 +114,27 @@ public class CompanionDeviceDiscoveryService extends Service { */ enum DiscoveryState { NOT_STARTED, STARTING, DISCOVERY_IN_PROGRESS, IN_PROGRESS, FINISHED_STOPPED, FINISHED_TIMEOUT } static void startForRequest( static boolean startForRequest( @NonNull Context context, @NonNull AssociationRequest associationRequest) { synchronized (LOCK) { if (sDiscoveryStarted) { Slog.e(TAG, "Discovery is already started. Ignoring this request..."); return false; } } requireNonNull(associationRequest); final Intent intent = new Intent(context, CompanionDeviceDiscoveryService.class); intent.setAction(ACTION_START_DISCOVERY); intent.putExtra(EXTRA_ASSOCIATION_REQUEST, associationRequest); sStateLiveData.setValue(DiscoveryState.STARTING); sScanResultsLiveData.setValue(Collections.emptyList()); context.startService(intent); return true; } static void stop(@NonNull Context context) { Loading Loading @@ -176,10 +184,16 @@ public class CompanionDeviceDiscoveryService extends Service { Slog.d(TAG, "startDiscovery() request=" + request); requireNonNull(request); if (mDiscoveryStarted) throw new RuntimeException("Discovery in progress."); synchronized (LOCK) { if (sDiscoveryStarted) { Slog.e(TAG, "Discovery is already started. Returning..."); return; } sDiscoveryStarted = true; } mStopAfterFirstMatch = request.isSingleDevice(); mDiscoveryStarted = true; sStateLiveData.setValue(DiscoveryState.DISCOVERY_IN_PROGRESS); sScanResultsLiveData.setValue(Collections.emptyList()); sStateLiveData.setValue(DiscoveryState.IN_PROGRESS); final List<DeviceFilter<?>> allFilters = request.getDeviceFilters(); final List<BluetoothDeviceFilter> btFilters = Loading Loading @@ -211,13 +225,12 @@ public class CompanionDeviceDiscoveryService extends Service { private void stopDiscoveryAndFinish(boolean timeout) { Slog.d(TAG, "stopDiscoveryAndFinish(" + timeout + ")"); if (!mDiscoveryStarted) { synchronized (LOCK) { if (!sDiscoveryStarted) { stopSelf(); return; } if (mDiscoveryStopped) return; mDiscoveryStopped = true; } // Stop BT discovery. if (mBtReceiver != null) { Loading Loading @@ -249,6 +262,10 @@ public class CompanionDeviceDiscoveryService extends Service { sStateLiveData.setValue(DiscoveryState.FINISHED_STOPPED); } synchronized (LOCK) { sDiscoveryStarted = false; } // "Finish". stopSelf(); } Loading Loading @@ -340,7 +357,9 @@ public class CompanionDeviceDiscoveryService extends Service { private void onDeviceFound(@NonNull DeviceFilterPair<?> device) { runOnMainThread(() -> { if (mDiscoveryStopped) return; synchronized (LOCK) { if (!sDiscoveryStarted) 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). Loading