Merge VmLauncherServices into VmLauncherService
The former is just a wrapper around the latter.
Bug: N/A
Test: N/A
Change-Id: I665dbacf28cb89b2ff266c0e3ee5fc280fab871b
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java b/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java
index cdab321..1c09728 100644
--- a/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/MainActivity.java
@@ -76,7 +76,7 @@
import java.util.Map;
public class MainActivity extends BaseActivity
- implements VmLauncherServices.VmLauncherServiceCallback, AccessibilityStateChangeListener {
+ implements VmLauncherService.VmLauncherServiceCallback, AccessibilityStateChangeListener {
static final String TAG = "VmTerminalApp";
private static final String VM_ADDR = "192.168.0.2";
private static final int TTYD_PORT = 7681;
@@ -427,7 +427,7 @@
@Override
protected void onDestroy() {
getSystemService(AccessibilityManager.class).removeAccessibilityStateChangeListener(this);
- VmLauncherServices.stopVmLauncherService(this);
+ VmLauncherService.stopVmLauncherService(this);
super.onDestroy();
}
@@ -542,7 +542,7 @@
Intent stopIntent = new Intent();
stopIntent.setClass(this, VmLauncherService.class);
- stopIntent.setAction(VmLauncherServices.ACTION_STOP_VM_LAUNCHER_SERVICE);
+ stopIntent.setAction(VmLauncherService.ACTION_STOP_VM_LAUNCHER_SERVICE);
PendingIntent stopPendingIntent =
PendingIntent.getService(
this,
@@ -580,7 +580,7 @@
.build();
android.os.Trace.beginAsyncSection("executeTerminal", 0);
- VmLauncherServices.startVmLauncherService(this, this, notification);
+ VmLauncherService.startVmLauncherService(this, this, notification);
connectToTerminalService();
}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java b/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java
index 32d534a..a46d0b4 100644
--- a/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherService.java
@@ -29,7 +29,10 @@
import android.content.SharedPreferences;
import android.graphics.drawable.Icon;
import android.os.Bundle;
+import android.os.Handler;
import android.os.IBinder;
+import android.os.Looper;
+import android.os.Parcel;
import android.os.ResultReceiver;
import android.system.virtualmachine.VirtualMachine;
import android.system.virtualmachine.VirtualMachineConfig;
@@ -62,6 +65,11 @@
public class VmLauncherService extends Service implements DebianServiceImpl.DebianServiceCallback {
public static final String EXTRA_NOTIFICATION = "EXTRA_NOTIFICATION";
+ private static final String ACTION_START_VM_LAUNCHER_SERVICE =
+ "android.virtualization.START_VM_LAUNCHER_SERVICE";
+
+ public static final String ACTION_STOP_VM_LAUNCHER_SERVICE =
+ "android.virtualization.STOP_VM_LAUNCHER_SERVICE";
private static final int RESULT_START = 0;
private static final int RESULT_STOP = 1;
@@ -76,6 +84,82 @@
private DebianServiceImpl mDebianService;
private PortForwardingRequestReceiver mPortForwardingReceiver;
+ private static Intent buildVmLauncherServiceIntent(Context context) {
+ Intent i = new Intent();
+ i.setAction(ACTION_START_VM_LAUNCHER_SERVICE);
+
+ Intent intent = new Intent(ACTION_START_VM_LAUNCHER_SERVICE);
+ PackageManager pm = context.getPackageManager();
+ List<ResolveInfo> resolveInfos =
+ pm.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY);
+ if (resolveInfos == null || resolveInfos.size() != 1) {
+ Log.e(TAG, "cannot find a service to handle ACTION_START_VM_LAUNCHER_SERVICE");
+ return null;
+ }
+ String packageName = resolveInfos.get(0).serviceInfo.packageName;
+
+ i.setPackage(packageName);
+ return i;
+ }
+
+ public static void stopVmLauncherService(Context context) {
+ Intent i = buildVmLauncherServiceIntent(context);
+ context.stopService(i);
+ }
+
+ public static void startVmLauncherService(
+ Context context, VmLauncherServiceCallback callback, Notification notification) {
+ Intent i = buildVmLauncherServiceIntent(context);
+ if (i == null) {
+ return;
+ }
+ ResultReceiver resultReceiver =
+ new ResultReceiver(new Handler(Looper.myLooper())) {
+ @Override
+ protected void onReceiveResult(int resultCode, Bundle resultData) {
+ if (callback == null) {
+ return;
+ }
+ switch (resultCode) {
+ case RESULT_START:
+ callback.onVmStart();
+ return;
+ case RESULT_STOP:
+ callback.onVmStop();
+ return;
+ case RESULT_ERROR:
+ callback.onVmError();
+ return;
+ case RESULT_IPADDR:
+ callback.onIpAddrAvailable(resultData.getString(KEY_VM_IP_ADDR));
+ return;
+ }
+ }
+ };
+ i.putExtra(Intent.EXTRA_RESULT_RECEIVER, getResultReceiverForIntent(resultReceiver));
+ i.putExtra(VmLauncherService.EXTRA_NOTIFICATION, notification);
+ 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);
+ parcel.setDataPosition(0);
+ r = ResultReceiver.CREATOR.createFromParcel(parcel);
+ parcel.recycle();
+ return r;
+ }
+
@Override
public IBinder onBind(Intent intent) {
return null;
@@ -87,8 +171,7 @@
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- if (Objects.equals(
- intent.getAction(), VmLauncherServices.ACTION_STOP_VM_LAUNCHER_SERVICE)) {
+ if (Objects.equals(intent.getAction(), ACTION_STOP_VM_LAUNCHER_SERVICE)) {
stopSelf();
return START_NOT_STICKY;
}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherServices.java b/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherServices.java
deleted file mode 100644
index aad95cb..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/VmLauncherServices.java
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright (C) 2024 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.virtualization.terminal;
-
-import static com.android.virtualization.terminal.MainActivity.TAG;
-
-import android.app.Notification;
-import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
-import android.os.Bundle;
-import android.os.Handler;
-import android.os.Looper;
-import android.os.Parcel;
-import android.os.ResultReceiver;
-import android.util.Log;
-
-import java.util.List;
-
-public class VmLauncherServices {
- private static final String ACTION_START_VM_LAUNCHER_SERVICE =
- "android.virtualization.START_VM_LAUNCHER_SERVICE";
-
- public static final String ACTION_STOP_VM_LAUNCHER_SERVICE =
- "android.virtualization.STOP_VM_LAUNCHER_SERVICE";
- private static final int RESULT_START = 0;
- private static final int RESULT_STOP = 1;
- private static final int RESULT_ERROR = 2;
- private static final int RESULT_IPADDR = 3;
- private static final String KEY_VM_IP_ADDR = "ip_addr";
-
- private static Intent buildVmLauncherServiceIntent(Context context) {
- Intent i = new Intent();
- i.setAction(ACTION_START_VM_LAUNCHER_SERVICE);
-
- Intent intent = new Intent(ACTION_START_VM_LAUNCHER_SERVICE);
- PackageManager pm = context.getPackageManager();
- List<ResolveInfo> resolveInfos =
- pm.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY);
- if (resolveInfos == null || resolveInfos.size() != 1) {
- Log.e(TAG, "cannot find a service to handle ACTION_START_VM_LAUNCHER_SERVICE");
- return null;
- }
- String packageName = resolveInfos.get(0).serviceInfo.packageName;
-
- i.setPackage(packageName);
- return i;
- }
-
- public static void stopVmLauncherService(Context context) {
- Intent i = buildVmLauncherServiceIntent(context);
- context.stopService(i);
- }
-
- public static void startVmLauncherService(
- Context context, VmLauncherServiceCallback callback, Notification notification) {
- Intent i = buildVmLauncherServiceIntent(context);
- if (i == null) {
- return;
- }
- ResultReceiver resultReceiver =
- new ResultReceiver(new Handler(Looper.myLooper())) {
- @Override
- protected void onReceiveResult(int resultCode, Bundle resultData) {
- if (callback == null) {
- return;
- }
- switch (resultCode) {
- case RESULT_START:
- callback.onVmStart();
- return;
- case RESULT_STOP:
- callback.onVmStop();
- return;
- case RESULT_ERROR:
- callback.onVmError();
- return;
- case RESULT_IPADDR:
- callback.onIpAddrAvailable(resultData.getString(KEY_VM_IP_ADDR));
- return;
- }
- }
- };
- i.putExtra(Intent.EXTRA_RESULT_RECEIVER, getResultReceiverForIntent(resultReceiver));
- i.putExtra(VmLauncherService.EXTRA_NOTIFICATION, notification);
- 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);
- parcel.setDataPosition(0);
- r = ResultReceiver.CREATOR.createFromParcel(parcel);
- parcel.recycle();
- return r;
- }
-}