ipc/android: Clean up, adjust action name, adjust manifest.

This commit is contained in:
Ryan Pavlik 2020-11-10 15:27:22 +01:00 committed by Lubosz Sarnecki
parent 2c3e0a2bac
commit aa222ebba1
2 changed files with 32 additions and 12 deletions

View file

@ -4,14 +4,15 @@
Copyright 2020, Collabora, Ltd. Copyright 2020, Collabora, Ltd.
SPDX-License-Identifier: BSL-1.0 SPDX-License-Identifier: BSL-1.0
--> -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application> <application>
<service <service
android:name=".MonadoService" android:name=".MonadoService"
android:enabled="true" android:enabled="true"
android:exported="true" android:exported="true"
android:externalService="true"> android:foregroundServiceType="connectedDevice|mediaPlayback">
<intent-filter> <intent-filter>
<action android:name="org.freedesktop.monado.CONNECT" /> <action android:name="org.freedesktop.monado.ipc.CONNECT" />
</intent-filter> </intent-filter>
</service> </service>
</application> </application>

View file

@ -14,6 +14,7 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder; import android.os.IBinder;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.RemoteException; import android.os.RemoteException;
@ -37,6 +38,10 @@ public class Client implements ServiceConnection {
* Used to block native until we have our side of the socket pair. * Used to block native until we have our side of the socket pair.
*/ */
private final Object connectSync = new Object(); private final Object connectSync = new Object();
/**
* Keep track of the ipc_client_android instance over on the native side.
*/
private final NativeCounterpart nativeCounterpart;
/** /**
* Pointer to local IPC proxy: calling methods on it automatically transports arguments across binder IPC. * Pointer to local IPC proxy: calling methods on it automatically transports arguments across binder IPC.
* <p> * <p>
@ -52,10 +57,6 @@ public class Client implements ServiceConnection {
*/ */
@Keep @Keep
public boolean failed = false; public boolean failed = false;
/**
* Keep track of the ipc_client_android instance over on the native side.
*/
private final NativeCounterpart nativeCounterpart;
/** /**
* "Our" side of the socket pair - the other side is sent to the server automatically on connection. * "Our" side of the socket pair - the other side is sent to the server automatically on connection.
*/ */
@ -64,6 +65,10 @@ public class Client implements ServiceConnection {
* Context provided by app. * Context provided by app.
*/ */
private Context context; private Context context;
/**
* Context of the runtime package
*/
private Context runtimePackageContext;
/** /**
* Constructor * Constructor
@ -104,7 +109,10 @@ public class Client implements ServiceConnection {
@Keep @Keep
public int blockingConnect(Context context_, String packageName) { public int blockingConnect(Context context_, String packageName) {
synchronized (connectSync) { synchronized (connectSync) {
bind(context_, packageName); if (!bind(context_, packageName)) {
// Bind failed immediately
return -1;
}
try { try {
while (fd == null) { while (fd == null) {
connectSync.wait(); connectSync.wait();
@ -130,17 +138,28 @@ public class Client implements ServiceConnection {
* must specify the package name explicitly to avoid violating security * must specify the package name explicitly to avoid violating security
* restrictions. * restrictions.
*/ */
public void bind(Context context_, String packageName) { public boolean bind(Context context_, String packageName) {
context = context_.getApplicationContext(); context = context_.getApplicationContext();
if (context == null) { if (context == null) {
// in case app context returned null // in case app context returned null
context = context_; context = context_;
} }
context.bindService( try {
new Intent("org.freedesktop.monado.CONNECT") runtimePackageContext = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
.setPackage(packageName), } catch (PackageManager.NameNotFoundException e) {
this, Context.BIND_AUTO_CREATE); e.printStackTrace();
Log.e(TAG, "bind: Could not find package " + packageName);
return false;
}
Intent intent = new Intent("org.freedesktop.monado.ipc.CONNECT")
.setPackage(packageName);
context.startForegroundService(intent);
context.bindService(intent,
this, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT | Context.BIND_INCLUDE_CAPABILITIES | Context.BIND_ABOVE_CLIENT);
// does not bind right away! This takes some time. // does not bind right away! This takes some time.
return true;
} }
/** /**