Reorder methods in VmLauncherService in the order of their execution
.. from start to stop.
Bug: N/A
Test: N/A
Change-Id: Ief78260a3e847663ad209c692f47673f188aa19e
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java b/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java
index 9ce9ac2..38dd7e9 100644
--- a/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java
@@ -88,9 +88,14 @@
return new Intent(context.getApplicationContext(), VmLauncherService.class);
}
- public static void stopVmLauncherService(Context context) {
- Intent i = getMyIntent(context);
- context.stopService(i);
+ public interface VmLauncherServiceCallback {
+ void onVmStart();
+
+ void onVmStop();
+
+ void onVmError();
+
+ void onIpAddrAvailable(String ipAddr);
}
public static void startVmLauncherService(
@@ -127,16 +132,6 @@
context.startForegroundService(i);
}
- public interface VmLauncherServiceCallback {
- void onVmStart();
-
- void onVmStop();
-
- void onVmError();
-
- void onIpAddrAvailable(String ipAddr);
- }
-
private static ResultReceiver getResultReceiverForIntent(ResultReceiver r) {
Parcel parcel = Parcel.obtain();
r.writeToParcel(parcel, 0);
@@ -151,10 +146,6 @@
return null;
}
- private void startForeground(Notification notification) {
- startForeground(this.hashCode(), notification);
- }
-
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Objects.equals(intent.getAction(), ACTION_STOP_VM_LAUNCHER_SERVICE)) {
@@ -220,25 +211,8 @@
return START_NOT_STICKY;
}
- @Override
- public void onDestroy() {
- unregisterReceiver(mPortForwardingReceiver);
- getSystemService(NotificationManager.class).cancelAll();
- stopDebianServer();
- if (mVirtualMachine != null) {
- if (mVirtualMachine.getStatus() == VirtualMachine.STATUS_RUNNING) {
- try {
- mVirtualMachine.stop();
- stopForeground(STOP_FOREGROUND_REMOVE);
- } catch (VirtualMachineException e) {
- Log.e(TAG, "failed to stop a VM instance", e);
- }
- }
- mExecutorService.shutdownNow();
- mExecutorService = null;
- mVirtualMachine = null;
- }
- super.onDestroy();
+ private void startForeground(Notification notification) {
+ startForeground(this.hashCode(), notification);
}
private void startDebianServer() {
@@ -295,6 +269,40 @@
});
}
+ @Override
+ public void onIpAddressAvailable(String ipAddr) {
+ android.os.Trace.endAsyncSection("debianBoot", 0);
+ Bundle b = new Bundle();
+ b.putString(VmLauncherService.KEY_VM_IP_ADDR, ipAddr);
+ mResultReceiver.send(VmLauncherService.RESULT_IPADDR, b);
+ }
+
+ public static void stopVmLauncherService(Context context) {
+ Intent i = getMyIntent(context);
+ context.stopService(i);
+ }
+
+ @Override
+ public void onDestroy() {
+ unregisterReceiver(mPortForwardingReceiver);
+ getSystemService(NotificationManager.class).cancelAll();
+ stopDebianServer();
+ if (mVirtualMachine != null) {
+ if (mVirtualMachine.getStatus() == VirtualMachine.STATUS_RUNNING) {
+ try {
+ mVirtualMachine.stop();
+ stopForeground(STOP_FOREGROUND_REMOVE);
+ } catch (VirtualMachineException e) {
+ Log.e(TAG, "failed to stop a VM instance", e);
+ }
+ }
+ mExecutorService.shutdownNow();
+ mExecutorService = null;
+ mVirtualMachine = null;
+ }
+ super.onDestroy();
+ }
+
private void stopDebianServer() {
if (mDebianService != null) {
mDebianService.killForwarderHost();
@@ -305,14 +313,6 @@
}
@Override
- public void onIpAddressAvailable(String ipAddr) {
- android.os.Trace.endAsyncSection("debianBoot", 0);
- Bundle b = new Bundle();
- b.putString(VmLauncherService.KEY_VM_IP_ADDR, ipAddr);
- mResultReceiver.send(VmLauncherService.RESULT_IPADDR, b);
- }
-
- @Override
public void onActivePortsChanged(Set<String> oldPorts, Set<String> newPorts) {
Set<String> union = new HashSet<>(oldPorts);
union.addAll(newPorts);