NXP

imx8qm平台android 8.1.0 添加系统service

2019-07-12 11:53发布

8.1中添加系统service与之前有差异,涉及到te文件。网上找了些例子没有实现。特记录总结!

1.frameworks/base 目录下添加对应的文件清单如下
frameworks/base/core/java/android/app/HelloWorldManager.java
frameworks/base/core/java/android/app/IHelloWorldManager.aidl
frameworks/base/services/core/java/com/android/server/HelloWorldService.java
很好理解 一个service 一个manager 一个aidl文件
功能很简单就打印 Hello,World!
再修改
frameworks/base/core/java/android/app/SystemServiceRegistry.java
frameworks/base/core/java/android/content/Context.java
frameworks/base/services/java/com/android/server/SystemServer.java
很显然是为了添加及注册系统级service "hello"
注意修改完成后使用make update-api
特别提醒如果此步骤中 make update-api 不成功一定要修改成编译成功
make update-api 成功后会修改几个txt文件
frameworks/base/Android.mk
frameworks/base/api/current.txt
frameworks/base/api/system-current.txt
frameworks/base/api/test-current.txt


记得在修改SystemServiceRegistry.java Context.java SystemServer.java 参考Vibrator添加


SystemServiceRegistry.java
registerService(Context.HELLO_SERVICE, HelloWorldManager.class,
                new CachedServiceFetcher() {
            @Override
            public HelloWorldManager createService(ContextImpl ctx) throws ServiceNotFoundException {
                  IBinder binder;
                if (true){//ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
                    binder = ServiceManager.getServiceOrThrow(Context.HELLO_SERVICE);
                } else {
                    binder = ServiceManager.getService(Context.HELLO_SERVICE);
                }
                return new HelloWorldManager(ctx,IHelloWorldManager.Stub.asInterface(binder));
            }});
 


a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/
old mode 100644
new mode 100755
index df351ba..584a8c4
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -2911,6 +2911,7 @@ public abstract class Context {
             ACCOUNT_SERVICE,
             ACTIVITY_SERVICE,
             ALARM_SERVICE,
+            HELLO_SERVICE,
             NOTIFICATION_SERVICE,
             ACCESSIBILITY_SERVICE,
             CAPTIONING_SERVICE,
@@ -3018,6 +3019,9 @@ public abstract class Context {
      * 
{@link #ALARM_SERVICE} ("alarm")
      * 
A {@link android.app.AlarmManager} for receiving intents at the
      *  time of your choosing.
+     * 
{@link #HELLO_SERVICE} ("hello")
+     * 
A {@link android.app.HelloWorldManager} for receiving intents at the
+     *  time of your choosing.
      * 
{@link #NOTIFICATION_SERVICE} ("notification")
      * 
A {@link android.app.NotificationManager} for informing the user
      *   of background events.
@@ -3082,6 +3086,8 @@ public abstract class Context {
      * @see android.os.PowerManager
      * @see #ALARM_SERVICE
      * @see android.app.AlarmManager
+     * @see #HELLO_SERVICE
+     * @see android.app.HelloWorldManager
      * @see #NOTIFICATION_SERVICE
      * @see android.app.NotificationManager
      * @see #KEYGUARD_SERVICE
@@ -3133,7 +3139,8 @@ public abstract class Context {
      * Currently available classes are:
      * {@link android.view.WindowManager}, {@link android.view.LayoutInflater},
      * {@link android.app.ActivityManager}, {@link android.os.PowerManager},
-     * {@link android.app.AlarmManager}, {@link android.app.NotificationManager},
+     * {@link android.app.AlarmManager},
+     * {@link android.app.HelloWorldManager}, {@link android.app.NotificationManager},
      * {@link android.app.KeyguardManager}, {@link android.location.LocationManager},
      * {@link android.app.SearchManager}, {@link android.os.Vibrator},
      * {@link android.net.ConnectivityManager},
@@ -3238,6 +3245,16 @@ public abstract class Context {
      * @see android.app.AlarmManager
      */
     public static final String ALARM_SERVICE = "alarm";
+    
+    /**
+     * Use with {@link #getSystemService} to retrieve a
+     * {@link android.app.HelloWorldManager} for receiving intents at a
+     * time of your choosing.
+     *
+     * @see #getSystemService
+     * @see android.app.HelloWorldManager
+     */
+    public static final String HELLO_SERVICE = "hello"; 




a/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -140,6 +140,7 @@ import static android.view.Display.DEFAULT_DISPLAY;
 import java.lang.reflect.Constructor;
 import android.os.IInterface;
 import java.lang.reflect.InvocationTargetException;
+import com.android.server.HelloWorldService;
 //import com.mediatek.fullscreenswitch.FullscreenSwitchService;
 /// @}
 
@@ -175,6 +176,8 @@ public final class SystemServer {
             "com.android.server.voiceinteraction.VoiceInteractionManagerService";
     private static final String PRINT_MANAGER_SERVICE_CLASS =
             "com.android.server.print.PrintManagerService";
+    private static final String Hello_MANAGER_SERVICE_CLASS =
+            "com.android.server.HelloWorldService";
     private static final String COMPANION_DEVICE_MANAGER_SERVICE_CLASS =
             "com.android.server.companion.CompanionDeviceManagerService";
     private static final String USB_SERVICE_CLASS =
@@ -713,6 +716,7 @@ public final class SystemServer {
         NsdService serviceDiscovery= null;
         WindowManagerService wm = null;
         SerialService serial = null;
+        HelloWorldService hello = null;
         NetworkTimeUpdateService networkTimeUpdater = null;
         CommonTimeManagementService commonTimeMgmtService = null;
         InputManagerService inputManager = null;
@@ -1357,6 +1361,19 @@ public final class SystemServer {
                     traceEnd();
                 }
 
startOtherServices()下添加:
+                    traceBeginAndSlog("StartHelloService");
+                    try {
+                        // Serial port support
+                        hello = new HelloWorldService(context);
+                        ServiceManager.addService(Context.HELLO_SERVICE, hello);
+                    } catch (Throwable e) {
+                        Slog.e(TAG, "Failure starting StartHelloService", e);
+                    }
+                    traceEnd(); frameworks/base/core/java/android/app/HelloWorldManager.java  
  1. package android.app;
  2. import android.os.RemoteException;
  3. import android.annotation.SystemService;
  4. import android.content.Context;
  5.  
  6. @SystemService(Context.HELLO_SERVICE)
  7. public final class HelloWorldManager{
  8. private final IHelloWorldManager mService;
  9. private Context mContext;
  10. /**
  11. * @hide to prevent subclassing from outside of the framework
  12. */
  13. HelloWorldManager(Context context,IHelloWorldManager service){
  14. mContext = context;
  15. mService = service;
  16. }
  17. /**
  18. * Like {@link #HelloWorldManager( )}, but allowing the caller to specify
  19. * that the vibration is owned by someone else.
  20. * @hide
  21. */
  22. public void printHello(){
  23. try{
  24. mService.printHello();
  25. }catch (RemoteException ex){
  26. }
  27. }
  28. }
frameworks/base/core/java/android/app/IHelloWorldManager.aidl  
  1. package android.app;
  2. /**
  3. * System private API for talking with the helloworld service.
  4. * {@hide}
  5. */
  6. interface IHelloWorldManager{
  7. void printHello();
  8. }
frameworks/base/services/core/java/com/android/server/HelloWorldService.java  
  1. package com.android.server;
  2.  
  3. import android.app.IHelloWorldManager;
  4. import android.content.Context;
  5. import android.os.RemoteException;
  6. import android.util.Slog;
  7. import android.content.Context;
  8. import com.android.server.SystemService;
  9. public class HelloWorldService extends IHelloWorldManager.Stub {
  10. private final static String LOG_TAG = "HelloWorldService";
  11. private static final int BACKGROUND_USER_ID = -10;
  12.  
  13. private final Object mLock = new Object();
  14.  
  15. private final Context mContext;
  16.  
  17. HelloWorldService(Context context) {
  18. mContext = context;
  19. }
  20. @Override
  21. public void printHello() throws RemoteException {
  22. Slog.i(LOG_TAG,"xuyong Hello,World!");
  23. }
  24. }
当然不要忘了frameworks/base/Android.mk 中添加aidl  core/java/android/app/IHelloWorldManager.aidl 如果上述操作做完编译后无误的话至少hello service现在是在系统中的 在添加te文件时有两种思路 一种是按照system/sepolicy 目录中添加vibrator service的步骤添加hello service 另一种是在device 目录下添加hello.te 文件等操作实现。下面先说简单的:在system/sepolicy 目录中添加 system/sepolicy/prebuilts/api/26.0/nonplat_sepolicy.cil
system/sepolicy/prebuilts/api/26.0/private/service_contexts
system/sepolicy/prebuilts/api/26.0/public/service.te
system/sepolicy/private/compat/26.0/26.0.cil
system/sepolicy/private/service_contexts system/sepolicy/public/service.te 此步骤很简单 就完整照Vibrator 添加 注意一点 :hello" 对应 Context 中的HELLO_SERVICE system/sepolicy/private/service_contexts hello                                     u:object_r:hello_service:s0 system/sepolicy/prebuilts/api/26.0/public/service.te type hello_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type; 文件修改后可以mmm system/sepolicy/ 验证语法或规则是否符合要求。 下面介绍另一种添加te文件方式 我们是以sprd的代码为例 device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts
device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te
device/sprd/sharkl2/common/plat_sepolicy/public/service.te device/sprd/sharkl2/common/sepolicy/system_server.te device/sprd/sharkl2/common/plat_sepolicy/private/service_contexts hello                               u:object_r:hello_service:s0 device/sprd/sharkl2/common/plat_sepolicy/private/system_server.te添加 allow system_server hello_service:service_manager { add }; device/sprd/sharkl2/common/plat_sepolicy/public/service.te添加 type hello_service,             service_manager_type; device/sprd/sharkl2/common/sepolicy/system_server.te添加 allow system_server hello_service:service_manager add; 添加两个文件   device/sprd/sharkl2/common/plat_sepolicy/private/hello.te typeattribute hello coredomain;
init_daemon_domain(hello) device/sprd/sharkl2/common/plat_sepolicy/public/hello.te type hello, domain; type hello_exec, exec_type, file_type; 文件修改后可以mmm system/sepolicy/ 验证语法或规则是否符合要求. 2.完整编译 刷机 开机后 使用adb shell 进入后 手机  service list | grep hello 如果能显示说明系统中已添加成功hello service 3.编写测试程序 使用hello service 我是在Settings 中添加的  
  1. private void callHelloService(){
  2. Log.d("xuyong","onclick begin");
  3. HelloWorldManager hello = ((HelloWorldManager)mContext.getSystemService(Context.HELLO_SERVICE));
  4. if (hello != null){
  5. Log.d("xuyong","callHelloService successful!");
  6. hello.printHello();
  7. }
  8. Log.d("xuyong","onclick end");
  9. }
记得import android.app.HelloWorldManager;