Android T don't abort background activity starts
log:
2024-08-20 15:45:12.457 581-1128 ActivityTaskManager system_process I START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=acr.browser.lightning cmp=acr.browser.lightning/.MainActivity} from uid 10056
2024-08-20 15:45:12.460 581-1128 ActivityTaskManager system_process W Background activity start [callingPackage: com.xx.xxxapp; callingUid: 10056; appSwitchState: 2; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10056; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; allowBackgroundActivityStart: false; intent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=acr.browser.lightning cmp=acr.browser.lightning/.MainActivity }; callerApp: ProcessRecord{4974058 1519:com.portworld.bootstartapp/u0a56}; inVisibleTask: false]
2024-08-20 15:45:12.491 581-1128 ActivityTaskManager system_process E Abort background activity starts from 10056
看日志是系统不让我们从后台启动app,跟踪源码,开始分析:
./frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
函数流程:
executeRequest->startActivityUnchecked->startActivityInner->isAllowedToStart
private int executeRequest(Request request) {
...
boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,
requestCode, callingPid, callingUid, callingPackage, callingFeatureId,
request.ignoreTargetSecurity, inTask != null, callerApp, resultRecord,
resultRootTask);
abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
callingPid, resolvedType, aInfo.applicationInfo);
abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,
callingPackage);
// Merge the two options bundles, while realCallerOptions takes precedence.
ActivityOptions checkedOptions = options != null
? options.getOptions(intent, aInfo, callerApp, mSupervisor) : null;
@BalCode int balCode = BAL_ALLOW_DEFAULT;
if (!abort) {
try {
Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER,
"shouldAbortBackgroundActivityStart");
BackgroundActivityStartController balController =
mController.getBackgroundActivityLaunchController();
//key
balCode =
balController.checkBackgroundActivityStart(
callingUid,
callingPid,
callingPackage,
realCallingUid,
realCallingPid,
callerApp,
request.originatingPendingIntent,
request.allowBackgroundActivityStart,
intent,
checkedOptions);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
}
}
...
mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
request.voiceInteractor, startFlags, true /* doResume */, checkedOptions,
inTask, inTaskFragment, balCode, intentGrants);
...
}
//这个类是用来管理那些app可以后台启动的,checkBackgroundActivityStart里面有很多判断,符合要求的返回定义的BAL常量
/frameworks/base/services/core/java/com/android/server/wm/BackgroundActivityStartController.java
/**
* @return A code denoting which BAL rule allows an activity to be started,
* or {@link BAL_BLOCK} if the launch should be blocked
*/
@BalCode
int checkBackgroundActivityStart(...){
...
// IME should always be allowed to start activity, like IME settings.
final WindowState imeWindow =
mService.mRootWindowContainer.getCurrentInputMethodWindow();
if (imeWindow != null && callingAppId == imeWindow.mOwnerUid) {
return logStartAllowedAndReturnCode(BAL_ALLOW_ALLOWLISTED_COMPONENT,
/*background*/ false, callingUid, realCallingUid, intent,
"Active ime");
}
}
//add text
if(callingPackage.equals("com.xx.xx")){
Slog.v(TAG, "Background activity start for xxx ,ignored");
return BAL_ALLOW_DEFAULT;
}
//add text
...
return BAL_BLOCK;
}