Hi,
For Adding a system service i did following steps -
NOTE - I am trying to add my own system service named - gea to AOSP Android 9 framework.
Adding system services to AOSP framework
#####################################################
System Services
##############################
1. Copy GeaService.java to
AOSP frameworks/base/services/core/java/com/android/server of your AOSP
GeaService.java
#################
package com.android.server;
import android.content.Context;
import android.os.Handler;
import android.os.IGeaService;;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
public class GeaService extends IGeaService.Stub {
private static final String TAG = "GeaService";
private Context mContext;
private long mNativePointer;
private int mValue = 0;
public GeaService(Context context) {
super();
mContext = context;
Log.i(TAG, "System service initialized");
mNativePointer = init_native();
Log.i(TAG, "test() returns " + test_native(mNativePointer, 20));
write("HelloFromGEAService");
Log.i(TAG, "read() returns " + read(50));
}
protected void finalize() throws Throwable {
finalize_native(mNativePointer);
super.finalize();
}
public String read(int maxLength)
{
int length;
byte[] buffer = new byte[maxLength];
length = read_native(mNativePointer, buffer);
return new String(buffer, 0, length);
}
public int write(String mString)
{
byte[] buffer = mString.getBytes();
return write_native(mNativePointer, buffer);
}
private static native long init_native();
private static native void finalize_native(long ptr);
private static native int read_native(long ptr, byte[] buffer);
private static native int write_native(long ptr, byte[] buffer);
private static native int test_native(long ptr, int value);
public void test(int val) {
Log.i(TAG, "test " + val);
mValue = val;
}
}
2. Add below lines in
AOSP frameworks/base/services/java/com/android/server/SystemServer.java
import com.android.server.GeaService;
import android.os.GeaManager;
try {
Slog.i(TAG, "GEA Service");
ServiceManager.addService(Context.GEA_SERVICE, new GeaService(context));
} catch (Throwable e) {
Slog.e(TAG, "Failure starting Gea Service", e);
}
3. Copy IGeaService.aidl to
AOSP frameworks/base/core/java/android/os/
package android.os;
/**
* {@hide}
*/
interface IGeaService {
String read(int maxLength);
int write(String mString);
void test(int val);
}
4. Copy GeaManager.java to
AOSP frameworks/base/core/java/android/os/
package android.os;
import android.os.IGeaService;
import android.util.Log;
public class GeaManager
{
IGeaService mService;
private static final String TAG = "GeaManager"; // this is for Log only..
public String read(int maxLength) {
try {
return mService.read(maxLength);
} catch (RemoteException e) {
Log.e(TAG, "Excpetion in read: " + e.toString());
return null;
}
}
public int write(String mString) {
try {
return mService.write(mString);
} catch (RemoteException e) {
Log.e(TAG, "Excpetion in write: " + e.toString());
return 0;
}
}
public void test(int val) {
try {
mService.test(val);
return;
} catch (RemoteException e) {
Log.e(TAG, "Excpetion in test: " + e.toString());
return;
}
}
public GeaManager(IGeaService service) {
mService = service;
}
}
5. Add below line to -> AOSP framework/base/Android.bp
"core/java/android/os/IGeaService.aidl",
6. Add below lines to AOSP frameworks/base/core/java/android/app/SystemServiceRegistry.java
import android.os.IGeaService;
import android.os.GeaManager;
registerService(Context.GEA_SERVICE, GeaManager.class,
new CachedServiceFetcher<GeaManager>() {
@Override
public GeaManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow(Context.GEA_SERVICE);
IGeaService service = IGeaService.Stub.asInterface(b);
return new GeaManager(service);
}});
7. Add below lines to AOSP frameworks/base/core/java/android/app/ContextImpl.java
import android.os.IGeaService;
import android.os.GeaManager;
8. Add below lines to AOSP frameworks/base/core/java/android/content/Context.java
/**
* Use with {@link #getSystemService} to retrieve a
* {@link android.os.GeaManager} for using Gea Service.
*
* @see #getSystemService
*/
public static final String GEA_SERVICE = "gea";
But In my Android App (This App I am building inside AOSP / package / app ) when I try to get this
system service i always get toast msg - "FAILED to call system service".
package com.gea.hellointernal;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.os.ServiceManager; // Will only work in AOSP
import android.os.IGeaService; // Interface "hidden" in SDK
import android.os.GeaManager;
public class HelloGeaInternalActivity extends Activity {
private static final String DTAG = "HelloGeaInternal";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IGeaService om = IGeaService.Stub.asInterface(ServiceManager.getService("gea"));
try {
Log.d(DTAG, "Going to write to the \"gea\" service");
int ret = om.write("Hello World!");
Log.d(DTAG, "GEAManager.read returned: " + om.read(20) + " ret = " + ret);
Toast.makeText(getApplicationContext(),"Going to write to the service",Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Log.d(DTAG, "FAILED to call service : " + e.toString());
Toast.makeText(getApplicationContext(),"FAILED to call system service : "+e.getMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
NOTE - With All the changes I am able to rebuild the AOSP and I am able to see my App but when i open the App i always see
FAILED to call system service toast msg... it means My system service is not working or not discoverable to my app.
I am not understanding what I am missing or what changes i need to do to make my system service available in my app.
Any help or suggestion for what wrong I am doing here or how can i write simple custom system service and test it for Android 9 framework.
Note - I am using iMX8M Mini EVK and Android AOSP 9
Thanks & Regards,
Maneesh