栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 11 添加系统开机启动的Service方案

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 11 添加系统开机启动的Service方案

近日,在搞一套开机启动的Service,虽然在之前低版本弄过,以为直接照搬过来就可以了,结果还出了一堆问题,比如framework里边@NonNull检测、selinux新规则等等,废了本尊些许时日才完工~

且听我一一道来~~~~

首先,在framework相关目录添加Service和aidl文件;

在frameworks/base/core/java/android/app/目录创建genie目录,新建如下两个文件:

GeniePadManager.java:

package android.app.genie;

import android.content.Context;

import android.os.RemoteException;

import android.util.Slog;

import android.annotation.NonNull;

public class GeniePadManager {

Context mContext;

IGeniePadManager mService;

static final String TAG = "GeniePadManager";

public GeniePadManager(@NonNull Context context, @NonNull IGeniePadManager service) {

mContext = context;

mService = service;

if (mService != null)

Slog.d(TAG, "====== mService not null");

else

Slog.d(TAG, "====== mService not null");

}

public @NonNull String getPadBrightness() {

if (mService != null) {

try {

//Slog.d(TAG, " Will return getPadBrightness");

return mService.getPadBrightness();

} catch (RemoteException e) {

Slog.d(TAG, "RemoteException " + e);

return null;

}

}

return null;

}

public @NonNull String getTpVersion() {

if (mService != null) {

try {

Slog.d(TAG, "====== Will return getTpVersion");

return mService.getTpVersion();

} catch (RemoteException e) {

Slog.d(TAG, "RemoteException " + e);

return null;

}

}

return null;

}

}

IGeniePadManager.aidl:

package android.app.genie;

interface IGeniePadManager {

String getPadBrightness();

String getTpVersion();

}

第二步:执行一次make update-api,会遇到类似下边的报错日志:

1 new API lint issues were found. See tools/metalava/API-LINT.md for how to handle these.

这是Android的lint检查,之前的版本没有明显语法错误是不报错的,android11规范了这种语法,会报错,

假设报错提示为:

Missing nullability on method csdn() return [MissingNullability]

则需要在方法之前添加@NonNull关键字,这是解决这个问题的方案之一;还有一个更简单的方案,就是直接忽略掉这个目录的lint检查:

修改framework/base目录下的Android.bp

diff --git a/Android.bp b/Android.bp

index 14a2bff8a..fd2c2dcb9 100644

--- a/Android.bp

+++ b/Android.bp

@@ -1219,7 +1219,8 @@ metalava_framework_docs_args = "--manifest $(location core/res/AndroidManifest.x

"--api-lint-ignore-prefix android.icu. " +

"--api-lint-ignore-prefix java. " +

"--api-lint-ignore-prefix junit. " +

- "--api-lint-ignore-prefix org. "

+ "--api-lint-ignore-prefix org. " +

+ "--api-lint-ignore-prefix android.app.genie "

build = [

"StubLibraries.bp",

第三步:解决掉编译错误,顺利的话可以make update-api成功:

api文件会自动生成如下的改动:

diff --git a/api/current.txt b/api/current.txt

index 0d8a2c56c..fee9c416e 100644

--- a/api/current.txt

+++ b/api/current.txt

@@ -7616,6 +7616,37 @@ package android.app.blob {

}

+package android.app.genie {

+

+ public class GeniePadManager {

+ ctor public GeniePadManager(@NonNull android.content.Context, @NonNull android.app.genie.IGeniePadManager);

+ method @NonNull public String getPadBrightness();

+ method @NonNull public String getTpVersion();

+ }

+

+ public interface IGeniePadManager extends android.os.IInterface {

+ method public String getPadBrightness() throws android.os.RemoteException;

+ method public String getTpVersion() throws android.os.RemoteException;

+ }

+

+ public static class IGeniePadManager.Default implements android.app.genie.IGeniePadManager {

+ ctor public IGeniePadManager.Default();

+ method public android.os.IBinder asBinder();

+ method public String getPadBrightness() throws android.os.RemoteException;

+ method public String getTpVersion() throws android.os.RemoteException;

+ }

+

+ public abstract static class IGeniePadManager.Stub extends android.os.Binder implements android.app.genie.IGeniePadManager {

+ ctor public IGeniePadManager.Stub();

+ method public android.os.IBinder asBinder();

+ method public static android.app.genie.IGeniePadManager asInterface(android.os.IBinder);

+ method public static android.app.genie.IGeniePadManager getDefaultImpl();

+ method public boolean onTransact(int, android.os.Parcel, android.os.Parcel, int) throws android.os.RemoteException;

+ method public static boolean setDefaultImpl(android.app.genie.IGeniePadManager);

+ }

+

+}

+

package android.app.job {

public class JobInfo implements android.os.Parcelable {

@@ -10193,6 +10224,7 @@ package android.content {

field public static final String EUICC_SERVICE = "euicc";

field public static final String FILE_INTEGRITY_SERVICE = "file_integrity";

field public static final String FINGERPRINT_SERVICE = "fingerprint";

+ field public static final String GENIEPAD_SERVICE = "geniepad";

field public static final String HARDWARE_PROPERTIES_SERVICE = "hardware_properties";

field public static final String INPUT_METHOD_SERVICE = "input_method";

field public static final String INPUT_SERVICE = "input";

--- a/non-updatable-api/current.txt

+++ b/non-updatable-api/current.txt

@@ -7616,6 +7616,37 @@ package android.app.blob {

}

+package android.app.genie {

+

+ public class GeniePadManager {

+ ctor public GeniePadManager(@NonNull android.content.Context, @NonNull android.app.genie.IGeniePadManager);

+ method @NonNull public String getPadBrightness();

+ method @NonNull public String getTpVersion();

+ }

+

+ public interface IGeniePadManager extends android.os.IInterface {

+ method public String getPadBrightness() throws android.os.RemoteException;

+ method public String getTpVersion() throws android.os.RemoteException;

+ }

+

+ public static class IGeniePadManager.Default implements android.app.genie.IGeniePadManager {

+ ctor public IGeniePadManager.Default();

+ method public android.os.IBinder asBinder();

+ method public String getPadBrightness() throws android.os.RemoteException;

+ method public String getTpVersion() throws android.os.RemoteException;

+ }

+

+ public abstract static class IGeniePadManager.Stub extends android.os.Binder implements android.app.genie.IGeniePadManager {

+ ctor public IGeniePadManager.Stub();

+ method public android.os.IBinder asBinder();

+ method public static android.app.genie.IGeniePadManager asInterface(android.os.IBinder);

+ method public static android.app.genie.IGeniePadManager getDefaultImpl();

+ method public boolean onTransact(int, android.os.Parcel, android.os.Parcel, int) throws android.os.RemoteException;

+ method public static boolean setDefaultImpl(android.app.genie.IGeniePadManager);

+ }

+

+}

+

package android.app.job {

public class JobInfo implements android.os.Parcelable {

@@ -10193,6 +10224,7 @@ package android.content {

field public static final String EUICC_SERVICE = "euicc";

field public static final String FILE_INTEGRITY_SERVICE = "file_integrity";

field public static final String FINGERPRINT_SERVICE = "fingerprint";

+ field public static final String GENIEPAD_SERVICE = "geniepad";

field public static final String HARDWARE_PROPERTIES_SERVICE = "hardware_properties";

field public static final String INPUT_METHOD_SERVICE = "input_method";

field public static final String INPUT_SERVICE = "input";

第四步:如上所示是添加完成了接口文件,并且编译完成,这一步开始如何添加至系统开机启动,添加方式如下:

--- a/core/java/android/app/SystemServiceRegistry.java

+++ b/core/java/android/app/SystemServiceRegistry.java

@@ -44,6 +44,8 @@ import android.app.usage.IUsageStatsManager;

import android.app.usage.NetworkStatsManager;

import android.app.usage.StorageStatsManager;

import android.app.usage.UsageStatsManager;

+import android.app.genie.GeniePadManager;

+import android.app.genie.IGeniePadManager;

import android.appwidget.AppWidgetManager;

import android.bluetooth.BluetoothManager;

import android.companion.CompanionDeviceManager;

@@ -1216,6 +1218,18 @@ public final class SystemServiceRegistry {

}

});

+ registerService(Context.GENIEPAD_SERVICE, GeniePadManager.class,

+ new CachedServiceFetcher() {

+ @Override

+ public GeniePadManager createService(ContextImpl ctx)

+ throws ServiceNotFoundException {

+ IBinder b = ServiceManager.getServiceOrThrow(

+ Context.GENIEPAD_SERVICE);

+ IGeniePadManager service = IGeniePadManager.Stub.asInterface(b);

+ return new GeniePadManager(ctx.getOuterContext(), service);

+ }

+ });

+

registerService(Context.SLICE_SERVICE, SliceManager.class,

new CachedServiceFetcher() {

@Override

为service自定义名称:

diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java

index 8472144a9..380e0df29 100644

--- a/core/java/android/content/Context.java

+++ b/core/java/android/content/Context.java

@@ -4188,6 +4188,8 @@ public abstract class Context {

*/

public static final String AUDIO_SERVICE = "audio";

+ public static final String GENIEPAD_SERVICE = "geniepad";

+

/**

* AuthService orchestrates biometric and PIN/pattern/password authentication.

*

--- a/services/java/com/android/server/SystemServer.java

+++ b/services/java/com/android/server/SystemServer.java

@@ -180,6 +180,7 @@ import com.android.server.webkit.WebViewUpdateService;

import com.android.server.wm.ActivityTaskManagerService;

import com.android.server.wm.WindowManagerGlobalLock;

import com.android.server.wm.WindowManagerService;

+import com.android.server.GeniePadManagerService;

import dalvik.system.PathClassLoader;

import dalvik.system.VMRuntime;

@@ -1625,6 +1626,16 @@ public final class SystemServer {

}

t.traceEnd();

+ t.traceBegin("StartGeniePadManagerService");

+ try {

+ Slog.i(TAG, "PadManager Service is create");

+ ServiceManager.addService(Context.GENIEPAD_SERVICE,

+ new GeniePadManagerService(context));

+ } catch (Throwable e) {

+ reportWtf("starting GeniePadManagerService", e);

+ }

+ t.traceEnd();

+

t.traceBegin("StartNotificationManager");

mSystemServiceManager.startService(NotificationManagerService.class);

SystemNotificationChannels.removeDeprecated(context);

第五步:简单描述下如何调用该接口:

mManager = (GeniePadManager) mmContext.getSystemService(Context.GENIEPAD_SERVICE);

mManager.getTpVersion();

最后,编译刷机,验证接口是否可用;

刷机后,发现还是太乐观了,一大块没搞,就是selinux规则配置添加:

修改device/mediatek/sepolicy/bsp目录下te文件,android11针对系统级Service更是添加了严苛规则,需要对servicemanager赋予add和find属性,具体流程及报错不在赘述,直接上最终极改动:

diff --git a/non_plat/file.te b/non_plat/file.te

index 69bfd33..f6de6ac 100644

--- a/non_plat/file.te

+++ b/non_plat/file.te

@@ -96,3 +96,6 @@ type proc_mtk_mdp_debug, fs_type, proc_type;

# Date : 2020/12/22

# Purpose: add permission for /data/vendor/hmp/

type data_vendor_hmp_file, data_file_type, file_type;

+

+# tp

+type tp_file, fs_type, proc_type;

diff --git a/non_plat/file_contexts b/non_plat/file_contexts

index 572925c..69f6a2d 100644

--- a/non_plat/file_contexts

+++ b/non_plat/file_contexts

@@ -215,3 +215,6 @@ data/duraspeed(/.*)? u:object_r:duraspeed_data_file:s0

# Date: 2020/12/29

# Purpose: mtk hmp info file

/data/vendor/hmp(/.*)? u:object_r:data_vendor_hmp_file:s0

+

+# tp

+/proc/android_touch(/.*)? u:object_r:tp_file:s0

diff --git a/non_plat/genfs_contexts b/non_plat/genfs_contexts

index 6ddcf5f..2814908 100644

--- a/non_plat/genfs_contexts

+++ b/non_plat/genfs_contexts

@@ -46,3 +46,7 @@ genfscon sysfs /devices/platform/soc/11170000.sdio/mmc_host/mmc1/mmc1:0001/mmc1:

genfscon sysfs /devices/platform/soc/11170000.sdio/mmc_host/mmc1/mmc1:0001/mmc1:0001:1/net/ap0/mtu u:object_r:sysfs_net:s0

genfscon sysfs /devices/platform/soc/11170000.sdio/mmc_host/mmc1/mmc1:0001/mmc1:0001:1/net/p2p0/mtu u:object_r:sysfs_net:s0

+

+# tp node

+genfscon proc /android_touch/vendor u:object_r:tp_file:s0

+

diff --git a/non_plat/system_app.te b/non_plat/system_app.te

index 818c7c6..c9cc837 100644

--- a/non_plat/system_app.te

+++ b/non_plat/system_app.te

@@ -182,3 +182,8 @@ allow system_app mtk_audiohal_data_file:file create_file_perms;

# Purpose: Allow atmwifimeta apk to use HIDL and access loghidlvendorservice

allow system_app mtk_hal_log_hwservice:hwservice_manager find;

binder_call(system_app, loghidlvendorservice);

+

+# tp

+allow system_app tp_file:dir { search read };

+allow system_app tp_file:file { read write open getattr };

+allow system_app genie_padmanager_service:service_manager { find add };

diff --git a/non_plat/system_server.te b/non_plat/system_server.te

index 3908c9a..23762e5 100644

--- a/non_plat/system_server.te

+++ b/non_plat/system_server.te

@@ -222,3 +222,8 @@ allow system_server proc_mtk_mdp_debug:file getattr;

# Date:2021/04/10

# Operation:allow CachedAppOptimi to search /proc/mtk_mdp_debug

allow system_server proc_mtk_mdp_debug:dir search;

+

+# For system service tp permission

+allow system_server tp_file:dir { search read };

+allow system_server tp_file:file { read write open getattr };

+allow system_server genie_padmanager_service:service_manager { find add };

diff --git a/plat_private/service_contexts b/plat_private/service_contexts

index 34255bf..fcac889 100644

--- a/plat_private/service_contexts

+++ b/plat_private/service_contexts

@@ -33,6 +33,7 @@ capctrl u:object_r:mtk_radio_service:s0

vow_bridge u:object_r:mtk_vowbridge_service:s0

autoboot u:object_r:mtk_autoboot_service:s0

smartratswitch u:object_r:mtk_radio_service:s0

+geniepad u:object_r:genie_padmanager_service:s0

# Other Services

GoogleOtaBinder u:object_r:ota_agent_service:s0

diff --git a/plat_public/service.te b/plat_public/service.te

index 91d97ec..86cd6b3 100644

--- a/plat_public/service.te

+++ b/plat_public/service.te

@@ -40,6 +40,7 @@ type vtservice_hidl_service, service_manager_type;

type mtk_hdmi_service,service_manager_type;

type ppl_agent_service, service_manager_type;

type mtk_gwsd_service, service_manager_type;

+type genie_padmanager_service, service_manager_type;

# Trustonic Services

type tee_service, service_manager_type;

至此,增量编译,上一步示例接口才能拿到数据~

大功告成!Mark~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号