Skip to content

Commit

Permalink
Restart ADB if the path to its executable is changed by the user
Browse files Browse the repository at this point in the history
Issue: #197
  • Loading branch information
mlopatkin committed Dec 25, 2023
1 parent 4e021d8 commit d5cfa8c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@ public void stop() {
for (AdbBridgeObserver observer : bridgeObservers) {
observer.onAdbBridgeClosed();
}
AndroidDebugBridge.disconnectBridge();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ private Consumer<Throwable> adbErrorHandler() {
}
};
}

/**
* Restarts ADB services (or starts one if it is not yet running).
*/
public void restartAdb() {
bridge.stopAdb();
hasShownErrorMessage = false;
withAdbServicesInteractive(adb -> {}, failure -> {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package name.mlopatkin.andlogview.ui.preferences;

import name.mlopatkin.andlogview.preferences.AdbConfigurationPref;
import name.mlopatkin.andlogview.ui.device.AdbServicesInitializationPresenter;

import java.util.Objects;
import java.util.function.Predicate;
Expand All @@ -43,20 +44,23 @@ interface View {

void showInvalidAdbLocationError();

void showRestartAppWarning();

void show();

void hide();
}

private final View view;
private final AdbConfigurationPref adbConfigurationPref;
private final AdbServicesInitializationPresenter adbServicesPresenter;

@Inject
ConfigurationDialogPresenter(View view, AdbConfigurationPref adbConfigurationPref) {
ConfigurationDialogPresenter(
View view,
AdbConfigurationPref adbConfigurationPref,
AdbServicesInitializationPresenter adbServicesPresenter) {
this.view = view;
this.adbConfigurationPref = adbConfigurationPref;
this.adbServicesPresenter = adbServicesPresenter;
}

public void openDialog() {
Expand All @@ -80,8 +84,9 @@ private void tryCommit() {

adbConfigurationPref.setAutoReconnectEnabled(view.isAutoReconnectEnabled());
view.hide();

if (hasLocationChanged) {
view.showRestartAppWarning();
adbServicesPresenter.restartAdb();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import javax.inject.Inject;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class ConfigurationDialogView implements ConfigurationDialogPresenter.View {
private final DialogFactory dialogFactory;
Expand Down Expand Up @@ -82,14 +81,6 @@ public void showInvalidAdbLocationError() {
ErrorDialogsHelper.showError(dialogFactory.getOwner(), "%s is not a valid adb file", getAdbLocation());
}

@Override
public void showRestartAppWarning() {
JOptionPane.showMessageDialog(dialogFactory.getOwner(),
"You've changed the path to the ADB executable. Please restart the "
+ "application to apply changes.",
"Please restart", JOptionPane.INFORMATION_MESSAGE);
}

@Override
public void show() {
ConfigurationDialogUi dialog = getDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.Mockito.verify;

import name.mlopatkin.andlogview.config.FakeInMemoryConfigStorage;
import name.mlopatkin.andlogview.preferences.AdbConfigurationPref;
import name.mlopatkin.andlogview.test.Expectations;
import name.mlopatkin.andlogview.test.TestActionHandler;
import name.mlopatkin.andlogview.ui.device.AdbServicesInitializationPresenter;
import name.mlopatkin.andlogview.utils.SystemPathResolver;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.File;
Expand All @@ -56,6 +59,9 @@ class ConfigurationDialogPresenterTest {
private final AdbConfigurationPref adbConfiguration = new AdbConfigurationPref(new FakeInMemoryConfigStorage(),
new FakePathResolver(VALID_ADB_LOCATION));

@Mock
private AdbServicesInitializationPresenter adbServicesInitPresenter;

@BeforeEach
void setUp() {
adbConfiguration.setAutoReconnectEnabled(DEFAULT_AUTO_RECONNECT);
Expand All @@ -64,7 +70,7 @@ void setUp() {

@Test
void openDialogShowsViewWithProperLocationSet() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
var presenter = createPresenter();

presenter.openDialog();

Expand All @@ -75,7 +81,7 @@ void openDialogShowsViewWithProperLocationSet() {

@Test
void committingWithInvalidLocationShowsError() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
var presenter = createPresenter();

presenter.openDialog();
fakeView.selectAdbLocation(INVALID_ADB_LOCATION);
Expand All @@ -89,7 +95,7 @@ void committingWithInvalidLocationShowsError() {

@Test
void discardingWithInvalidLocationShowsNoError() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
var presenter = createPresenter();

presenter.openDialog();
fakeView.selectAdbLocation(INVALID_ADB_LOCATION);
Expand All @@ -102,22 +108,21 @@ void discardingWithInvalidLocationShowsNoError() {
}

@Test
void committingWithNewLocationShowsRestartWarning() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
void committingWithNewLocationRestartsAdb() {
var presenter = createPresenter();

presenter.openDialog();
fakeView.selectAdbLocation(VALID_ADB_LOCATION);

Expectations.expect("show restart warning dialog", expectation -> {
fakeView.onRestartWarningShown.setAction(expectation::fulfill);
fakeView.commit();
});
fakeView.commit();

assertFalse(fakeView.isShown(), "configuration dialog is hidden");
verify(adbServicesInitPresenter).restartAdb();
}

@Test
void committingAfterChangingAutoReconnectUpdatesPreference() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
var presenter = createPresenter();

presenter.openDialog();
fakeView.setAutoReconnectEnabled(CHANGED_AUTO_RECONNECT);
Expand All @@ -128,7 +133,7 @@ void committingAfterChangingAutoReconnectUpdatesPreference() {

@Test
void settingInvalidAdbLocationHighlightError() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
var presenter = createPresenter();

presenter.openDialog();
fakeView.selectAdbLocation(INVALID_ADB_LOCATION);
Expand All @@ -137,7 +142,7 @@ void settingInvalidAdbLocationHighlightError() {

@Test
void settingValidAdbLocationClearsError() {
ConfigurationDialogPresenter presenter = new ConfigurationDialogPresenter(fakeView, adbConfiguration);
var presenter = createPresenter();

presenter.openDialog();
fakeView.selectAdbLocation(INVALID_ADB_LOCATION);
Expand All @@ -147,13 +152,16 @@ void settingValidAdbLocationClearsError() {
assertFalse(fakeView.isInvalidAdbLocationHighlighted());
}

private ConfigurationDialogPresenter createPresenter() {
return new ConfigurationDialogPresenter(fakeView, adbConfiguration, adbServicesInitPresenter);
}

static class FakeView implements ConfigurationDialogPresenter.View {
private final TestActionHandler<Runnable> onCommit = TestActionHandler.runnableAction();
private final TestActionHandler<Runnable> onDiscard = TestActionHandler.runnableAction();
private final TestActionHandler<Predicate<String>> checkAdbLocation = TestActionHandler.predicateAction(true);

final TestActionHandler<Runnable> onAdbLocationWarningShown = TestActionHandler.runnableAction();
final TestActionHandler<Runnable> onRestartWarningShown = TestActionHandler.runnableAction();

private String adbLocation = "";
private boolean isShown;
Expand Down Expand Up @@ -231,10 +239,6 @@ public void showInvalidAdbLocationError() {
onAdbLocationWarningShown.action().run();
}

@Override
public void showRestartAppWarning() {
onRestartWarningShown.action().run();
}
}

private static class FakePathResolver extends SystemPathResolver {
Expand Down

0 comments on commit d5cfa8c

Please sign in to comment.