Context: implement startService() and bindService()

This commit is contained in:
Julian Winkler
2023-09-19 23:11:56 +02:00
parent 299a474aa0
commit 2013024971
3 changed files with 47 additions and 5 deletions

View File

@@ -1,5 +1,15 @@
package android.app; package android.app;
public class Service { import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public abstract class Service extends Context {
public abstract void onCreate();
public abstract IBinder onBind(Intent intent);
public abstract int onStartCommand(Intent intent, int flags, int startId);
} }

View File

@@ -6,6 +6,7 @@ import android.app.AlarmManager;
import android.app.Application; import android.app.Application;
import android.app.KeyguardManager; import android.app.KeyguardManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service;
import android.app.SharedPreferencesImpl; import android.app.SharedPreferencesImpl;
import android.app.UiModeManager; import android.app.UiModeManager;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
@@ -44,6 +45,8 @@ import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException; import java.io.IOException;
public class Context extends Object { public class Context extends Object {
@@ -63,6 +66,7 @@ public class Context extends Object {
static Resources r; static Resources r;
static ApplicationInfo application_info; static ApplicationInfo application_info;
static Resources.Theme theme; static Resources.Theme theme;
private static Map<Class<? extends Service>,Service> runningServices = new HashMap<>();
static String apk_path = "/tmp/APK_PATH_SHOULD_HAVE_BEEN_FILLED_IN_BY_CODE_IN_main.c/"; static String apk_path = "/tmp/APK_PATH_SHOULD_HAVE_BEEN_FILLED_IN_BY_CODE_IN_main.c/";
@@ -319,8 +323,20 @@ public class Context extends Object {
return ClassLoader.getSystemClassLoader(); return ClassLoader.getSystemClassLoader();
} }
public ComponentName startService(Intent service) { public ComponentName startService(Intent intent) {
return new ComponentName("", ""); ComponentName component = intent.getComponent();
try {
Class<? extends Service> cls = Class.forName(component.getClassName()).asSubclass(Service.class);
if (!runningServices.containsKey(cls)) {
Service service = cls.getConstructor().newInstance();
service.onCreate();
runningServices.put(cls, service);
}
runningServices.get(cls).onStartCommand(intent, 0, 0);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
return component;
} }
// TODO: do these both work? make them look more alike // TODO: do these both work? make them look more alike
@@ -344,8 +360,18 @@ public class Context extends Object {
public void registerComponentCallbacks(ComponentCallbacks callbacks) {} public void registerComponentCallbacks(ComponentCallbacks callbacks) {}
public boolean bindService(Intent dummy, ServiceConnection dummy2, int dummy3) { public boolean bindService(Intent intent, ServiceConnection serviceConnection, int dummy3) {
Slog.w(TAG, "bindService("+dummy+", "+dummy2+", "+dummy3+")"); try {
Class<? extends Service> cls = Class.forName(intent.getComponent().getClassName()).asSubclass(Service.class);
if (!runningServices.containsKey(cls)) {
Service service = cls.getConstructor().newInstance();
service.onCreate();
runningServices.put(cls, service);
}
serviceConnection.onServiceConnected(intent.getComponent(), runningServices.get(cls).onBind(intent));
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
return false; // maybe? return false; // maybe?
} }
@@ -410,4 +436,6 @@ public class Context extends Object {
public boolean stopService(Intent intent) {return false;} public boolean stopService(Intent intent) {return false;}
public void unbindService(ServiceConnection serviceConnection) {} public void unbindService(ServiceConnection serviceConnection) {}
public void unregisterReceiver(BroadcastReceiver receiver) {}
} }

View File

@@ -1,4 +1,8 @@
package android.content; package android.content;
import android.os.IBinder;
public interface ServiceConnection { public interface ServiceConnection {
public void onServiceConnected (ComponentName name, IBinder service);
} }