Skip to content

Commit

Permalink
Handle app closing in AdbDataSource
Browse files Browse the repository at this point in the history
As device listeners are now also ui-thread-confined, there is no need in
ThreadSafeObservable anymore. Without the async disconnect
notifications, the AdbDataSource properly handles device list shutdown.

Issue: fixes #332
  • Loading branch information
mlopatkin committed Jan 1, 2024
1 parent 4021eb1 commit 3c128ce
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package name.mlopatkin.andlogview.device;

import name.mlopatkin.andlogview.utils.events.ThreadSafeObservable;
import name.mlopatkin.andlogview.utils.events.Observable;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -68,5 +68,5 @@ default Command command(String... commandLine) {
*
* @return the observable
*/
ThreadSafeObservable<DeviceChangeObserver> asObservable();
Observable<DeviceChangeObserver> asObservable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package name.mlopatkin.andlogview.device;

import name.mlopatkin.andlogview.utils.events.ThreadSafeObservable;
import name.mlopatkin.andlogview.utils.events.ThreadSafeSubject;
import name.mlopatkin.andlogview.utils.events.Observable;
import name.mlopatkin.andlogview.utils.events.Subject;

import java.util.List;

class DeviceImpl implements DeviceInternal {
private final DeviceKey deviceKey;
private final LoggingDevice device;
private final DeviceProperties deviceProperties;
private final ThreadSafeSubject<DeviceChangeObserver> observers = new ThreadSafeSubject<>();
private final Subject<DeviceChangeObserver> observers = new Subject<>();

public DeviceImpl(DeviceKey deviceKey, LoggingDevice device, DeviceProperties deviceProperties) {
this.deviceKey = deviceKey;
Expand Down Expand Up @@ -86,27 +86,35 @@ public Command command(List<String> commandLine) {
}

@Override
public ThreadSafeObservable<DeviceChangeObserver> asObservable() {
public Observable<DeviceChangeObserver> asObservable() {
return observers.asObservable();
}

@Override
public void notifyProvisioned() {
observers.forEach(obs -> obs.onDeviceConnected(this));
for (var obs : observers) {
obs.onDeviceConnected(this);
}
}

@Override
public void notifyChanged() {
observers.forEach(obs -> obs.onDeviceChanged(this));
for (var obs : observers) {
obs.onDeviceChanged(this);
}
}

@Override
public void notifyConnected() {
observers.forEach(obs -> obs.onProvisionalDeviceConnected(this));
for (var obs : observers) {
obs.onProvisionalDeviceConnected(this);
}
}

@Override
public void notifyDisconnected() {
observers.forEach(obs -> obs.onDeviceDisconnected(this));
for (var obs : observers) {
obs.onDeviceDisconnected(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import name.mlopatkin.andlogview.test.StrictMock;
import name.mlopatkin.andlogview.thirdparty.device.AndroidVersionCodes;
import name.mlopatkin.andlogview.utils.events.ThreadSafeObservable;
import name.mlopatkin.andlogview.utils.events.ThreadSafeSubject;
import name.mlopatkin.andlogview.utils.events.Observable;
import name.mlopatkin.andlogview.utils.events.Subject;

import com.android.ddmlib.IDevice;

Expand Down Expand Up @@ -88,9 +88,9 @@ public Command command(List<String> commandLine) {
}

@Override
public ThreadSafeObservable<DeviceChangeObserver> asObservable() {
public Observable<DeviceChangeObserver> asObservable() {
// TODO(mlopatkin) implement it?
return new ThreadSafeSubject<DeviceChangeObserver>().asObservable();
return new Subject<DeviceChangeObserver>().asObservable();
}

protected FakeCommand onCommand(List<String> commandLine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;

public class AdbDataSource implements DataSource, BufferReceiver {
/**
Expand Down Expand Up @@ -78,7 +77,7 @@ default void onDataSourceClosed() {}
private @Nullable RecordListener<LogRecord> listener;
private boolean closed = false;

public AdbDataSource(Device device, Executor uiExecutor) {
public AdbDataSource(Device device) {
assert device != null;
assert device.isOnline();
this.device = device;
Expand All @@ -101,7 +100,7 @@ public void onDeviceChanged(Device device) {
invalidateAndClose(InvalidationReason.OFFLINE);
}
}
}, uiExecutor);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,22 @@

package name.mlopatkin.andlogview.ui.device;

import name.mlopatkin.andlogview.AppExecutors;
import name.mlopatkin.andlogview.device.Device;
import name.mlopatkin.andlogview.liblogcat.ddmlib.AdbDataSource;
import name.mlopatkin.andlogview.liblogcat.ddmlib.DeviceDisconnectedHandler;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.concurrent.Executor;
import java.util.function.Consumer;

import javax.inject.Inject;
import javax.inject.Named;

public class AdbDataSourceFactory {
private final DeviceDisconnectedHandler deviceDisconnectedHandler;
private final Executor uiExecutor;

@Inject
AdbDataSourceFactory(
DeviceDisconnectedHandler deviceDisconnectedHandler,
@Named(AppExecutors.UI_EXECUTOR) Executor uiExecutor) {
AdbDataSourceFactory(DeviceDisconnectedHandler deviceDisconnectedHandler) {
this.deviceDisconnectedHandler = deviceDisconnectedHandler;
this.uiExecutor = uiExecutor;
}

public void selectDeviceAndOpenAsDataSource(
Expand All @@ -54,7 +47,7 @@ public void selectDeviceAndOpenAsDataSource(
}

public void openDeviceAsDataSource(Device device, Consumer<? super AdbDataSource> callback) {
AdbDataSource dataSource = new AdbDataSource(device, uiExecutor);
AdbDataSource dataSource = new AdbDataSource(device);
deviceDisconnectedHandler.startWatching(dataSource);
callback.accept(dataSource);
}
Expand Down

0 comments on commit 3c128ce

Please sign in to comment.