天天看點

imx8qm平台android 8.1.0 添加系統service

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<HelloWorldManager>() {

            @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 {

      *  <dt> {@link #ALARM_SERVICE} ("alarm")

      *  <dd> A {@link android.app.AlarmManager} for receiving intents at the

      *  time of your choosing.

+     *  <dt> {@link #HELLO_SERVICE} ("hello")

+     *  <dd> A {@link android.app.HelloWorldManager} for receiving intents at the

+     *  time of your choosing.

      *  <dt> {@link #NOTIFICATION_SERVICE} ("notification")

      *  <dd> 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";

+    

+   

+    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. @SystemService(Context.HELLO_SERVICE)

  6. public final class HelloWorldManager{

  7. private final IHelloWorldManager mService;

  8. private Context mContext;

  9. HelloWorldManager(Context context,IHelloWorldManager service){

  10. mContext = context;

  11. mService = service;

  12. }

  13. public void printHello(){

  14. try{

  15. mService.printHello();

  16. }catch (RemoteException ex){

  17. }

  18. }

  19. }

frameworks/base/core/java/android/app/IHelloWorldManager.aidl

  1. package android.app;

  2. interface IHelloWorldManager{

  3. void printHello();

  4. }

frameworks/base/services/core/java/com/android/server/HelloWorldService.java

  1. package com.android.server;

  2. import android.app.IHelloWorldManager;

  3. import android.content.Context;

  4. import android.os.RemoteException;

  5. import android.util.Slog;

  6. import android.content.Context;

  7. import com.android.server.SystemService;

  8. public class HelloWorldService extends IHelloWorldManager.Stub {

  9. private final static String LOG_TAG = "HelloWorldService";

  10. private static final int BACKGROUND_USER_ID = -10;

  11. private final Object mLock = new Object();

  12. private final Context mContext;

  13. HelloWorldService(Context context) {

  14. mContext = context;

  15. }

  16. @Override

  17. public void printHello() throws RemoteException {

  18. Slog.i(LOG_TAG,"xuyong Hello,World!");

  19. }

  20. }

當然不要忘了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;

繼續閱讀