RK3568 Android 11新增系统服务

1.编写.aidl文件

存放位置:frameworks/base/core/java/android/os下,我新建test文件目录frameworks/base/core/java/android/os/test/ISystemTestService.aidl

package android.os.test;
 
interface ISystemTestService {
 
    void setTestOne(int flag);
 
    int setTestTwo();
 
    boolean setTestThree(int flag, String path);

    String setTestFour(boolean flag);
}

2.将.aidl文件添加到frameworks/base/Android.bp

filegroup {
    name: "framework-core-sources",
    srcs: [
        "core/java/**/*.java",
        "core/java/**/*.aidl",
    ],
    path: "core/java",
}

说明:android.bp文件中默认把core/java/目录下的aidl文件添加到编译文件中,所以这一步不需要操作.由于Android 11对语法检测比较严格,所以针对我们新增的文件先加入忽略:

    "--hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo " +
    "--force-convert-to-warning-nullability-annotations +*:-android.*:+android.icu.*:-dalvik.* " +
    "--api-lint-ignore-prefix android.icu. " +
    "--api-lint-ignore-prefix java. " +
    "--api-lint-ignore-prefix android.os. " +   //新增这一行
    "--api-lint-ignore-prefix android.app. " +  //新增这一行
    "--api-lint-ignore-prefix junit. " +
    "--api-lint-ignore-prefix org. "

3、添加服务注册名称, 添加该服务名称, 用于快捷注册和快捷引用

修改位置:frameworks/base/core/java/android/content/Context.java

4、新建SystemTestService.java和SystemTestManager.java

存放位置:

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

package com.android.server;
 
    import android.app.SystemTestManager;
    import android.content.Context;
    import android.os.test.ISystemTestService;
    import android.os.RemoteException;
    import com.android.server.SystemService;
    import com.android.internal.app.IAppOpsService;
    import android.util.Slog;
     
     
    public class SystemTestService extends SystemService {
        private final String TAG = "SystemTestService";
     
        private Context mContext;
        private IAppOpsService mAppOps;
        private SystemTestManager mManager;
     
        public SystemTestService(Context context) {
            super(context);
            this.mContext = context;
        }
     
        public void systemReady(IAppOpsService appOps) {
            mAppOps = appOps;
            if (mManager == null) {
                mManager = (SystemTestManager) mContext.getSystemService(Context.SYSTEMTEST_SERVICE);
            }
        }
     
        @Override
        public void onStart() {
            publishBinderService(Context.SYSTEMTEST_SERVICE, new BinderService());
        }
     
     
        private final class BinderService extends ISystemTestService.Stub {
     
            @Override
            public void setTestOne(int flag) throws RemoteException {
                try {
                    Slog.d(TAG, "------setTestOne--------flag:"+String.valueOf(flag));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
               public int setTestTwo() throws RemoteException {
                try {
                    Slog.d(TAG, "------setTestTwo--------");
                   return  66;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
            @Override
               public boolean setTestThree(int flag, String path) throws RemoteException {
                try {
                    Slog.d(TAG, path+"------setTestThree--------flag:"+String.valueOf(flag));
                    return false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
            @Override
               public String setTestFour(boolean flag) throws RemoteException {
                try {
                    Slog.d(TAG, "------setTestFour--------flag:"+String.valueOf(flag));
                    return "abc";
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return  "";
            }
        }
    }

frameworks/base/core/java/android/app/SystemTestManager.java

package android.app;

import android.content.Context;
import android.os.test.ISystemTestService;
import android.util.Log;
import android.util.Slog;
import android.annotation.SystemService;
import android.os.RemoteException;

@SystemService(Context.SYSTEMTEST_SERVICE)
public class SystemTestManager {

    private static final String TAG = "SystemTestManager";
    private ISystemTestService mService;
    private Context context;

    public SystemTestManager(Context ctx, ISystemTestService service) {
        mService = service;
        context = ctx;
    }

    public void setTestOne(int flag) {
        try {
            Slog.d(TAG, "------setTestOne--------flag:" + String.valueOf(flag));
            mService.setTestOne(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int setTestTwo() {
        try {
            Slog.d(TAG, "------setTestTwo--------");
            return mService.setTestTwo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean setTestThree(int flag, String path) {
        try {
            Slog.d(TAG, path + "------setTestThree--------flag:" + String.valueOf(flag));
            return mService.setTestThree(flag, path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String setTestFour(boolean flag) {
        try {
            Slog.d(TAG, "------setTestFour--------flag:" + String.valueOf(flag));
            return mService.setTestFour(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

5、SystemServer.java 中注册该service

修改位置: frameworks/base/services/java/com/android/server/SystemServer.java

import android.os.Message;
import android.os.Parcel;
import android.os.PowerManager;
import com.android.server.SystemTestService ;
import android.os.Process;
import android.os.ServiceManager;
import android.os.StrictMode;

    // TODO: remove all of these references by improving dependency resolution and boot phases
    private PowerManagerService mPowerManagerService;
    private SystemTestService  mSystemTestService; 
    private ActivityManagerService mActivityManagerService;
    private WindowManagerGlobalLock mWindowManagerGlobalLock;
    private WebViewUpdateService mWebViewUpdateService;
        t.traceBegin("StartPowerManager");
        mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
        t.traceEnd();
        //系统服务加入的位置加入以下内容
        t.traceBegin("StartSystemTestManager");
        mSystemTestService = mSystemServiceManager.startService(SystemTestService.class);
        t.traceEnd();

        t.traceBegin("StartThermalManager");
        mSystemServiceManager.startService(ThermalManagerService.class);
            reportWtf("making Power Manager Service ready", e);
        }
        t.traceEnd();
        //系统服务加入的位置加入以下内容
        t.traceBegin("MakeSystemTestManagerServiceReady");
        try {
            mSystemTestService.systemReady(mActivityManagerService.getAppOpsService());
        } catch (Throwable e) {
                reportWtf("making SystemTest Manager Service ready", e);
        }
        t.traceEnd();

        // Permission policy service
        t.traceBegin("StartPermissionPolicyService");

6、SystemServiceRegistry的static{}, 并在其中注册该service

修改位置:frameworks/base/core/java/android/app/SystemServiceRegistry.java

package android.app;

import android.os.test.ISystemTestService;
import android.accounts.AccountManager;
import android.accounts.IAccountManager;
import android.annotation.NonNull;
                return AccessibilityManager.getInstance(ctx);
            }});

            registerService(Context.SYSTEMTEST_SERVICE, SystemTestManager.class,
            new CachedServiceFetcher<SystemTestManager>() {
                @Override
                public SystemTestManager createService(ContextImpl ctx)
                        throws ServiceNotFoundException {
                    IBinder b = ServiceManager.getServiceOrThrow(
                            Context.SYSTEMTEST_SERVICE);
                    return new SystemTestManager(
                            ctx.getOuterContext(),ISystemTestService.Stub.asInterface(b));
                }});

        registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,
                new CachedServiceFetcher<CaptioningManager>() {
            @Override

7、自定义服务增加相关的 SELinux 规则

打开selinux要记住这个命令 adb shell setenforce 1 # 1为打开 #0为关闭

Android 11 的 selinux 规则是放在 system/sepolicy 目录下的:

service.te 和 service_contexts 都要加上配置:

位置:

system/sepolicy/prebuilts/api/30.0/public/service.te

system/sepolicy/public/service.te

type aidl_lazy_test_service,    service_manager_type;
type systemtest_service,    app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;  //新增
type apex_service,              service_manager_type;
type audioserver_service,       service_manager_type;
type batteryproperties_service, app_api_service, ephemeral_app_api_service, service_manager_type;

system/sepolicy/prebuilts/api/30.0/private/service_contexts

system/sepolicy/private/service_contexts

android.hardware.vibrator.IVibrator/default                          u:object_r:hal_vibrator_service:s0

accessibility                             u:object_r:accessibility_service:s0
systemtest                             u:object_r:systemtest_service:s0 //新增
account                                   u:object_r:account_service:s0
activity                                  u:object_r:activity_service:s0
activity_task                             u:object_r:activity_task_service:s0

8、以下目录修改以下忽略配置,才能正常编译

system/sepolicy/prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil

system/sepolicy/prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil

system/sepolicy/prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil

system/sepolicy/prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil

system/sepolicy/private/compat/26.0/26.0.ignore.cil

system/sepolicy/private/compat/27.0/27.0.ignore.cil

system/sepolicy/private/compat/28.0/28.0.ignore.cil

system/sepolicy/private/compat/29.0/29.0.ignore.cil

新增服务编译SDK可能报错,需要更新API

可以执行更新API命令(我这项目不行报错),或者make clean后进行(我这执行还是报错)。

最终手动拷贝提升的报错文件内容,把差异内容拷贝进去,或者全复制拷贝,主要是以下两个out目录文件拷贝到current.txt里即可。

--- frameworks/base/api/current.txt   
+++ out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api-stubs-docs_api.txt    

--- frameworks/base/non-updatable-api/current.txt    
+++ out/soong/.intermediates/frameworks/base/api-stubs-docs-non-updatable/android_common/api-stubs-docs-non-updatable_api.txt     

9、验证:

编译完成后,输入adb shell

#   adb shell
#   service list
#   service list|grep systemtest(服务关键字)
返回
 systemtest: [android.os.test.ISystemTestService]

查看服务列表中 是否存在有添加服务

找到编译最新生成的class.jar文件,导入Androidstudio(如何导入自行百度)即可调用。

编译后的class.jar目录\out\target\common\obj\JAVA_LIBRARIES\framework_intermediates

如果未生成 执行下make javac-check-framework 这个命令 就会生成!!!

不同版本位置不一样

Android N/O:  7 和 8
out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
Android P/Q:  9 和 10
out/soong/.intermediates/frameworks/base/framework/android_common/combined/framework.jar
Android R:  11以上
out/soong/.intermediates/frameworks/base/framework-minus-apex/android_common/combined/framework-minus-apex.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值