该原创文章首发于微信公众号:字节流动
cd AOSP root dir
source build/envsetup.sh
lunch
2
path:frameworks/base/core/java/android/os/
Create IHaoHaoService.aidl
file
package android.os;
interface IHaoHaoService {
void setVal(String key,String value);
String getVal(String key);
}
path: frameworks/base/Android.mk
......
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += \
......
core/java/android/os/IPowerManager.aidl \
core/java/android/os/IRemoteCallback.aidl \
core/java/android/os/ISchedulingPolicyService.aidl \
core/java/android/os/IUpdateLock.aidl \
core/java/android/os/IUserManager.aidl \
core/java/android/os/IVibratorService.aidl \
core/java/android/os/IHaoHaoService.aidl \
......
进行模块编译:
~/aosp/android-6.0.1_r1$ mmm frameworks/base
generate IHaoHaoService.java
file.
path: frameworks/base/services/core/java/com/android/server/
Create HaoHaoService.java file.
package com.android.server;
import android.util.Slog;
import android.os.RemoteException;
import android.os.IHaoHaoService;
import java.util.HashMap;
public class HaoHaoService extends IHaoHaoService.Stub {
private static HashMap<String,String> mCache = null;
private static final String TAG="HaoHaoService";
public HaoHaoService() {
mCache = new HashMap<>();
Slog.d(TAG, "HaoHaoService starting.");
}
public void setVal(String key, String value) throws RemoteException {
mCache.put(key, value);
}
public String getVal(String key) throws RemoteException {
return mCache.get(key);
}
}
Modify frameworks/base/core/java/android/content/Context.java
Add:
/**
* Use with {@link #getSystemService} to retrieve a
* {@link android.os.IHaoHaoService} for accessing the HaoHao service.
*
* @see #getSystemService
* @hide
*/
public static final String HAOHAO_SERVICE = "haohao";
Modify frameworks/base/services/java/com/android/server/SystemServer.java
Find function startOtherServices()
Add:
try {
Slog.i(TAG, "***HaoHao Service***");
ServiceManager.addService(Context.HAOHAO_SERVICE, new HaoHaoService());
} catch (Throwable e) {
Slog.e(TAG, "Failure starting HaoHao Service", e);
}
path: frameworks/base/core/java/android/app/
package android.app;
/**
* Created by haohao on 17/10/2.
*/
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.IHaoHaoService;
import android.util.Log;
public class HaoHaoServiceManager {
private static final String TAG = "HaoHaoServiceManager";
private IHaoHaoService mService;
public HaoHaoServiceManager(Context context, IHaoHaoService service){
mService = service;
}
public void setVal(String key,String value){
try {
mService.setVal(key,value);
} catch(Exception e){
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
public String getVal(String key){
try {
return mService.getVal(key);
} catch(Exception e){
Log.e(TAG, e.toString());
e.printStackTrace();
}
return null;
}
}
path: frameworks/base/core/java/android/app/SystemServiceRegistry.java
Add:
import android.os.IHaoHaoService;
......
registerService(Context.HAOHAO_SERVICE, HaoHaoServiceManager.class,
new CachedServiceFetcher<HaoHaoServiceManager>() {
@Override
public HaoHaoServiceManager createService(ContextImpl ctx) {
IBinder binder = ServiceManager.getService(Context.HAOHAO_SERVICE);
IHaoHaoService service = IHaoHaoService.Stub.asInterface(binder);
return new HaoHaoServiceManager(ctx, service);
}});
......
path: external/sepolicy/service.te
Add:
type haohao_service, system_api_service, system_server_service, service_manager_type;
path: external/sepolicy/service_contexts
Add:
haohao u:object_r:haohao_service:s0
service_contexts
accessibility u:object_r:accessibility_service:s0
account u:object_r:account_service:s0
activity u:object_r:activity_service:s0
alarm u:object_r:alarm_service:s0
haohao u:object_r:haohao_service:s0
android.security.keystore u:object_r:keystore_service:s0
android.service.gatekeeper.IGateKeeperService u:object_r:gatekeeper_service:s0
~/aosp/android-6.0.1_r1$ make update-api -j8
~/aosp/android-6.0.1_r1$ make -j8
@Override
public void onCreate() {
super.onCreate();
mHaoHaoServiceManager = (HaoHaoServiceManager)getSystemService(Context.HAOHAO_SERVICE);
mHaoHaoServiceManager.setVal("haohao", "Android Developer");