开发安卓系统,有一些特殊需求,修改系统源码比较普遍,下面说说
需求一:屏蔽按键和触摸功能【物理按键】,但是模拟按键是可以操作【自动化操作】,组合键取消屏蔽
思路:作为系统都知道PhoneWindowManager PWM是最终处理按键的结果,但是在模拟按键处理上,也会屏蔽掉,所以不合适在PWM服务进行处理,按键触摸流程是从kernel 按键值 和系统按键值 是有映射关系的,需要通过映射表进行转换,后面查询了按键流程【后续发表相关文章】 InputReader.cpp processEventsLocked 实时对驱动层上报按键的十六进制进行处理,最后决定在方法进行处理,这样不好影响模拟按键处理。这里有个属性 persist.sys.input.stop 系统属性进行判断是否要屏蔽按键和触摸,这里通过按一下电源键,2s内在按一下音量+键,取消屏蔽按键和触摸。
+long long startWhen;
void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
for (const RawEvent* rawEvent = rawEvents; count;) {
+ int32_t isStopInputDevices;
+ isStopInputDevices = property_get_int32("persist.sys.input.stop", 0);
+ //ALOGD("processEventsLocked:isStopInputDevices=%d startWhen=%lld",isStopInputDevices,startWhen);
+ if(isStopInputDevices == 1){
+ //power key
+ if((rawEvent->code == 0x0074) && (rawEvent->value ==0x00000000)){
+ ALOGD("--POWER up---");
+ startWhen = rawEvent->when;
+ }
+ //volumn up
+ else if(rawEvent->code == 0x0073 && (rawEvent->value ==0x00000001)){
+ ALOGD("--VOLUME_UP down---when=%lld",rawEvent->when - startWhen);
+ if( (rawEvent->when - startWhen) <= 2000000000){
+ property_set("persist.sys.input.stop", "0");
+ }
+ }
+ return;
+ }
int32_t type = rawEvent->type;
size_t batchSize = 1;
if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
需求二 安装普通第三方应用,默认授予权限【比如 摄像头 sd卡权限之类】
思路:安装应用 其实大家都知道PackageManagerService.java是由pms服务进行逻辑的处理的,要找到具体处理授权的方法grantPermissionsLPw
private void grantPermissionsLPw(PackageParser.Package pkg, boolean replace,
String packageOfInterest) {
// IMPORTANT: There are two types of permissions: install and runtime.
// Install time permissions are granted when the app is installed to
// all device users and users added in the future. Runtime permissions
// are granted at runtime explicitly to specific users. Normal and signature
// protected permissions are install time permissions. Dangerous permissions
// are install permissions if the app's target SDK is Lollipop MR1 or older,
// otherwise they are runtime permissions. This function does not manage
// runtime permissions except for the case an app targeting Lollipop MR1
// being upgraded to target a newer SDK, in which case dangerous permissions
// are transformed from install time to runtime ones.
......
Slog.d(TAG, "--- grant permission ----" );
//包名来默认确定授权
if(pkg.packageName.contains("com.tencent.mm") ) {
final int permsSize = pkg.requestedPermissions.size();
for (int i=0; i
需求三,系统时间更新会不准确,有时候是1970时间点
思路:系统时间更新时候偶发是会出现不准确,但是要检查的时候,又是准确的,后面发现是ntp服务器配置返回的值是不可靠的,第一修改ntp 服务地址,第二 对获取时间毫秒进行验证,在进行设置时间【以上个更新时间为基准,进行判断】,这里persist.sys.ntp.time 系统属性是为了保存上个更新的时间点毫秒时间
@@ -46,6 +46,7 @@ import com.android.internal.util.DumpUtils;
import java.io.FileDescriptor;
import java.io.PrintWriter;
+import android.os.SystemProperties;
/**
* Monitors the network time and updates the system time if it is out of sync
@@ -193,6 +194,11 @@ public class NetworkTimeUpdateService extends Binder {
// only update when NTP time is fresh
if (mTime.getCacheAge() < mPollingIntervalMs) {
final long ntp = mTime.currentTimeMillis();
+ long originNtp = SystemProperties.getLong("persist.sys.ntp.time", 0);
+ Log.d(TAG, "originNtp = " + originNtp+" ntp="+ntp);
+ if(originNtp > ntp){
+ return;
+ }
mTryAgainCounter = 0;
// If the clock is more than N seconds off or this is the first time it's been
// fetched since boot, set the current time.
@@ -207,6 +213,8 @@ public class NetworkTimeUpdateService extends Binder {
// Make sure we don't overflow, since it's going to be converted to an int
if (ntp / 1000 < Integer.MAX_VALUE) {
SystemClock.setCurrentTimeMillis(ntp);
+ SystemProperties.set("persist.sys.ntp.time", ntp+"");
+ Log.d(TAG, "----setCurrentTimeMillis-----");
}
} else {
if (DBG) Log.d(TAG, "Ntp time is close enough = " + ntp);
需求四,应用白名单,限制应用安装
思路:根据应用的包名 和 版本号 进行限制安装
参考之前写的博客:https://blog.csdn.net/fmc088/article/details/80949812
需求五,ota升级
思路:第一在有系统的源码基础上,可以使用RecoverySystem.installPackageLegacy 接口进行ota升级,第二;如果第三方应用有root权限,某种意义上来说,echo -e --update_package=path > /cache/recovery/command && reboot recovery
参考博客:https://blog.csdn.net/fmc088/article/details/80400726