龙空技术网

Android 后台应用被限制启动Activity的相关代码

睿智的海边风浪 40

前言:

而今你们对“root启动activity”大约比较关怀,同学们都想要剖析一些“root启动activity”的相关文章。那么小编也在网摘上收集了一些有关“root启动activity””的相关资讯,希望朋友们能喜欢,姐妹们快快来了解一下吧!

代码基于android 11 r9

boolean shouldAbortBackgroundActivityStart(int callingUid, int callingPid,            final String callingPackage, int realCallingUid, int realCallingPid,            WindowProcessController callerApp, PendingIntentRecord originatingPendingIntent,            boolean allowBackgroundActivityStart, Intent intent) {        // don't abort for the most important UIDs        final int callingAppId = UserHandle.getAppId(callingUid);        if (callingUid == Process.ROOT_UID || callingAppId == Process.SYSTEM_UID                || callingAppId == Process.NFC_UID) {            if (DEBUG_ACTIVITY_STARTS) {                Slog.d(TAG, "Activity start allowed for important callingUid (" + callingUid + ")");            }            return false;        }        // don't abort if the callingUid has a visible window or is a persistent system process        final int callingUidProcState = mService.getUidState(callingUid);		    // *zhy:* mService.mWindowManager ==> WindowManagerService mWindowManager;		   // *zhy:* mRoot ==> RootWindowContainer mRoot;	   	 // 如果 callingUid 有任何当前对用户可见的非 toast 窗口,则返回 {true}。 		  // 也忽略 {android.view.WindowManager.LayoutParams#TYPE_APPLICATION_STARTING},因为这些窗口不属于应用程序。        final boolean callingUidHasAnyVisibleWindow =                mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(callingUid);        final boolean isCallingUidForeground = callingUidHasAnyVisibleWindow                || callingUidProcState == ActivityManager.PROCESS_STATE_TOP                || callingUidProcState == ActivityManager.PROCESS_STATE_BOUND_TOP;        final boolean isCallingUidPersistentSystemProcess =                callingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;        if (callingUidHasAnyVisibleWindow || isCallingUidPersistentSystemProcess) {            if (DEBUG_ACTIVITY_STARTS) {                Slog.d(TAG, "Activity start allowed: callingUidHasAnyVisibleWindow = " + callingUid                        + ", isCallingUidPersistentSystemProcess = "                        + isCallingUidPersistentSystemProcess);            }            return false;        }        // take realCallingUid into consideration        final int realCallingUidProcState = (callingUid == realCallingUid)                ? callingUidProcState                : mService.getUidState(realCallingUid);        final boolean realCallingUidHasAnyVisibleWindow = (callingUid == realCallingUid)                ? callingUidHasAnyVisibleWindow                : mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(realCallingUid);        final boolean isRealCallingUidForeground = (callingUid == realCallingUid)                ? isCallingUidForeground                : realCallingUidHasAnyVisibleWindow                        || realCallingUidProcState == ActivityManager.PROCESS_STATE_TOP;        final int realCallingAppId = UserHandle.getAppId(realCallingUid);        final boolean isRealCallingUidPersistentSystemProcess = (callingUid == realCallingUid)                ? isCallingUidPersistentSystemProcess                : (realCallingAppId == Process.SYSTEM_UID)                        || realCallingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;        if (realCallingUid != callingUid) {            // don't abort if the realCallingUid has a visible window            if (realCallingUidHasAnyVisibleWindow) {                if (DEBUG_ACTIVITY_STARTS) {                    Slog.d(TAG, "Activity start allowed: realCallingUid (" + realCallingUid                            + ") has visible (non-toast) window");                }                return false;            }            // if the realCallingUid is a persistent system process, abort if the IntentSender            // wasn't whitelisted to start an activity            if (isRealCallingUidPersistentSystemProcess && allowBackgroundActivityStart) {                if (DEBUG_ACTIVITY_STARTS) {                    Slog.d(TAG, "Activity start allowed: realCallingUid (" + realCallingUid                            + ") is persistent system process AND intent sender whitelisted "                            + "(allowBackgroundActivityStart = true)");                }                return false;            }            // don't abort if the realCallingUid is an associated companion app			// *zhy:* isAssociatedCompanionApp 方法的作用是判断指定应用是否与关联的伴侣应用相关联。			// 它通过检查应用包名和特定的标识来确定应用之间的关联性,			// 并根据具体实现方式返回相应的判断结果。            if (mService.isAssociatedCompanionApp(UserHandle.getUserId(realCallingUid),                    realCallingUid)) {                if (DEBUG_ACTIVITY_STARTS) {                    Slog.d(TAG, "Activity start allowed: realCallingUid (" + realCallingUid                            + ") is companion app");                }                return false;            }        }        // don't abort if the callingUid has START_ACTIVITIES_FROM_BACKGROUND permission        // 如果 callingUid 具有 START_ACTIVITIES_FROM_BACKGROUND 权限,则不要中止        if (mService.checkPermission(START_ACTIVITIES_FROM_BACKGROUND, callingPid, callingUid)                == PERMISSION_GRANTED) {            if (DEBUG_ACTIVITY_STARTS) {                Slog.d(TAG,                        "Background activity start allowed: START_ACTIVITIES_FROM_BACKGROUND "                                + "permission granted for uid "                                + callingUid);            }            return false;        }        // don't abort if the caller has the same uid as the recents component        // 如果调用者与 recents 组件具有相同的 uid,则不要中止        if (mSupervisor.mRecentTasks.isCallerRecents(callingUid)) {            if (DEBUG_ACTIVITY_STARTS) {                Slog.d(TAG, "Background activity start allowed: callingUid (" + callingUid                        + ") is recents");            }            return false;        }        // don't abort if the callingUid is the device owner        // 如果 callingUid 是设备所有者,不要中止        if (mService.isDeviceOwner(callingUid)) {            if (DEBUG_ACTIVITY_STARTS) {                Slog.d(TAG, "Background activity start allowed: callingUid (" + callingUid                        + ") is device owner");            }            return false;        }        // don't abort if the callingUid has companion device        final int callingUserId = UserHandle.getUserId(callingUid);        if (mService.isAssociatedCompanionApp(callingUserId, callingUid)) {            if (DEBUG_ACTIVITY_STARTS) {                Slog.d(TAG, "Background activity start allowed: callingUid (" + callingUid                        + ") is companion app");            }            return false;        }        // If we don't have callerApp at this point, no caller was provided to startActivity().        // That's the case for PendingIntent-based starts, since the creator's process might not be        // up and alive. If that's the case, we retrieve the WindowProcessController for the send()        // caller, so that we can make the decision based on its foreground/whitelisted state.        int callerAppUid = callingUid;        if (callerApp == null) {            callerApp = mService.getProcessController(realCallingPid, realCallingUid);            callerAppUid = realCallingUid;        }        // don't abort if the callerApp or other processes of that uid are whitelisted in any way        // 如果该 uid 的 callerApp 或其他进程以任何方式被列入白名单,则不要中止        if (callerApp != null) {            // first check the original calling process            if (callerApp.areBackgroundActivityStartsAllowed()) {                if (DEBUG_ACTIVITY_STARTS) {                    Slog.d(TAG, "Background activity start allowed: callerApp process (pid = "                            + callerApp.getPid() + ", uid = " + callerAppUid + ") is whitelisted");                }                return false;            }            // only if that one wasn't whitelisted, check the other ones            final ArraySet<WindowProcessController> uidProcesses =                    mService.mProcessMap.getProcesses(callerAppUid);            if (uidProcesses != null) {                for (int i = uidProcesses.size() - 1; i >= 0; i--) {                    final WindowProcessController proc = uidProcesses.valueAt(i);                    if (proc != callerApp && proc.areBackgroundActivityStartsAllowed()) {                        if (DEBUG_ACTIVITY_STARTS) {                            Slog.d(TAG,                                    "Background activity start allowed: process " + proc.getPid()                                            + " from uid " + callerAppUid + " is whitelisted");                        }                        return false;                    }                }            }        }        // don't abort if the callingUid has SYSTEM_ALERT_WINDOW permission        // 如果 callingUid 具有 SYSTEM_ALERT_WINDOW 权限,则不会中止        if (mService.hasSystemAlertWindowPermission(callingUid, callingPid, callingPackage)) {            Slog.w(TAG, "Background activity start for " + callingPackage                    + " allowed because SYSTEM_ALERT_WINDOW permission is granted.");            return false;        }        if (mService.isActivityStartsLoggingEnabled()) {            mSupervisor.getActivityMetricsLogger().logAbortedBgActivityStart(intent, callerApp,                    callingUid, callingPackage, callingUidProcState, callingUidHasAnyVisibleWindow,                    realCallingUid, realCallingUidProcState, realCallingUidHasAnyVisibleWindow,                    (originatingPendingIntent != null));        }        return true;    }
START_ACTIVITIES_FROM_BACKGROUND 权限:
    <!-- @SystemApi @hide Allows an application to start activities from background -->    <permission android:name="android.permission.START_ACTIVITIES_FROM_BACKGROUND"        android:protectionLevel="signature|privileged|vendorPrivileged|oem|verifier|role" />

标签: #root启动activity