android P 禁止应用开机自启动

公司有需求,禁止部分应用开机的时候自启动,实现代码如下:

--- a/frameworks/base/core/res/res/values/config.xml
+++ b/frameworks/base/core/res/res/values/config.xml
@@ -3540,4 +3540,32 @@
        
                "com.onstyle.track"
        
+
+  
+   
+       "com.chinatsp.smartlights:lights_server"
+       "com.chinatsp.dvrcamera"
+       "com.changan.oushangCos1"
+       "com.incall.apps.softmanager"
+       "com.huawei.hiviewlite"
+       "com.huawei.hisight"
+       "com.huawei.dmsdpdevice"
+       "com.huawei.hicar.demoapp"
+       "com.oushang.uploadservice"
+       "com.huawei.nearby"
+       "com.chinatsp.smartlights"
+       "com.chinatsp.media.scan"
+       "com.android.car.messenger"
+       "com.mediatek.engineermode"
+       "com.mediatek.avm"
+       "com.android.managedprovisioning"
+       "com.baidu.map.location"
+       "com.android.printspooler"
+       "com.chinatsp.update"
+       "com.android.se"
+       "com.android.keychain"
+       "android.car.usb.handler"
+       "com.android.onetimeinitializer"
+       "android.car.cluster.loggingrenderer"
+  
 
diff --git a/frameworks/base/core/res/res/values/symbols.xml b/frameworks/base/core/res/res/values/symbols.xml
index b4b7818..6060ea7 100755
--- a/frameworks/base/core/res/res/values/symbols.xml
+++ b/frameworks/base/core/res/res/values/symbols.xml
@@ -3474,4 +3474,6 @@

   
   
+  
+  
 
diff --git a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java b/frameworks/base/services/core/jav
old mode 100644
new mode 100755
index 60315ab..7af349f
--- a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -731,7 +731,7 @@ public class ActivityManagerService extends IActivityManager.Stub
     private boolean mUseFifoUiScheduling = false;

     private static final String SYSUI_COMPONENT_NAME = "com.android.systemui/.SystemUIService";
-
+    private static HashSet mDisallowList;
     BroadcastQueue mFgBroadcastQueue;
     BroadcastQueue mBgBroadcastQueue;
     // Convenient for easy iteration over the queues. Foreground is first
@@ -837,6 +837,59 @@ public class ActivityManagerService extends IActivityManager.Stub
         }
     };

+   private final int getRunningProcessRecordIndex(String packageName) {
+        // Find the application record.
+        if(packageName == null){
+            return -1;
+        }
+        for (int i=mLruProcesses.size()-1; i>=0; i--) {
+            ProcessRecord rec = mLruProcesses.get(i);
+            if (rec.thread != null && packageName.equals(rec.processName)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    private String mCachePkg;
+    final boolean isProcessLocked(ApplicationInfo info, String hostingType,ComponentName hostingName) {
+        if(mDisallowList == null){
+            getDisallowList();
+        }
+       String launchPkg = info.packageName;
+       Slog.i(TAG, "~~~zjy~~~Package name ["+launchPkg+"] isSystemApp: " + info.isSystemApp());
+       boolean isRunningApp = getRunningProcessRecordIndex(launchPkg) != -1;
+          if(isRunningApp){
+               Slog.d(TAG, "~~~zjy Launch: " + launchPkg  + " " + hostingType);
+               return false;
+       }
+       if(launchPkg.equals(mCachePkg)){
+               Slog.d(TAG, "~~~zjy Launch for cache: " + launchPkg  + " " + hostingType);
+               return false;
+               }
+       if ("service".equals(hostingType) || "broadcast".equals(hostingType) || "content provider".equals(hostingType)) {
+
+               if (mDisallowList.contains(launchPkg)) {
+               Slog.d(TAG, "~~~zjy intercept: " + launchPkg + "  " + hostingType + "  " + info.processName);
+               return true;
+               } else {
+               Slog.d(TAG, "~~~zjy allowed: " + launchPkg + "  " + hostingType + "  " + info.processName);
+               return false;
+               }
+
+       }
+       mCachePkg = launchPkg;
+       return false;
+    }
+
+    private void getDisallowList() {
+       String[] disallowList = mContext.getResources().getSystem().getStringArray(com.android.internal.R.array.disallowlist);
+       mDisallowList = new HashSet<>(Arrays.asList(disallowList));
+       Iterator iter = mDisallowList.iterator();
+        while(iter.hasNext()){
+               Slog.d(TAG, "~~~zjy getDisallowList: item:"+(String)iter.next());
+       }
+    }
     public boolean canShowErrorDialogs() {
         return mShowDialogs && !mSleeping && !mShuttingDown
                 && !mKeyguardController.isKeyguardOrAodShowing(DEFAULT_DISPLAY)
@@ -4086,6 +4139,11 @@ public class ActivityManagerService extends IActivityManager.Stub
             ApplicationInfo info, boolean knownToBeDead, int intentFlags,
             String hostingType, ComponentName hostingName, boolean allowWhileBooting,
             boolean isolated, boolean keepIfLarge) {
+        if (isProcessLocked(info, hostingType,hostingName)) {
+               String launchPkg = info.packageName;
+               Slog.i(TAG, "~~~zjy~~~isProcessLocked name ["+launchPkg+"] isSystemApp: " + info.isSystemApp());
+               return null;
+       }
         return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType,
                 hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
                 null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
(END)

你可能感兴趣的:(android系统,禁止自启动)