@Override
 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) throws RemoteException {
   waitForReadyInner();
   try {
     enforcePluginFileExists();
     List<PermissionGroupInfo> list = new ArrayList<PermissionGroupInfo>();
     if (shouldNotBlockOtherInfo()) {
       for (PluginPackageParser pluginPackageParser : mPluginCache.values()) {
         List<PermissionGroupInfo> permissionGroupInfos =
             pluginPackageParser.getPermissionGroups();
         for (PermissionGroupInfo permissionGroupInfo : permissionGroupInfos) {
           if (!list.contains(permissionGroupInfo)) {
             list.add(permissionGroupInfo);
           }
         }
       }
     } else {
       List<String> pkgs = mActivityManagerService.getPackageNamesByPid(Binder.getCallingPid());
       for (PluginPackageParser pluginPackageParser : mPluginCache.values()) {
         List<PermissionGroupInfo> permissionGroupInfos =
             pluginPackageParser.getPermissionGroups();
         for (PermissionGroupInfo permissionGroupInfo : permissionGroupInfos) {
           if (!list.contains(permissionGroupInfo)
               && pkgs.contains(permissionGroupInfo.packageName)) {
             list.add(permissionGroupInfo);
           }
         }
       }
     }
     return list;
   } catch (Exception e) {
     handleException(e);
   }
   return null;
 }
 @Override
 public List<ResolveInfo> queryIntentContentProviders(
     Intent intent, String resolvedType, int flags) throws RemoteException {
   waitForReadyInner();
   try {
     enforcePluginFileExists();
     if (shouldNotBlockOtherInfo()) {
       return IntentMatcher.resolveProviderIntent(
           mContext, mPluginCache, intent, resolvedType, flags);
     } else {
       List<String> pkgs = mActivityManagerService.getPackageNamesByPid(Binder.getCallingPid());
       List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
       for (String pkg : pkgs) {
         intent.setPackage(pkg);
         List<ResolveInfo> list =
             IntentMatcher.resolveProviderIntent(
                 mContext, mPluginCache, intent, resolvedType, flags);
         infos.addAll(list);
       }
       if (infos != null && infos.size() > 0) {
         return infos;
       }
     }
   } catch (Exception e) {
     handleException(e);
   }
   return null;
 }
 @Override
 public ProviderInfo resolveContentProvider(String name, int flags) throws RemoteException {
   waitForReadyInner();
   try {
     enforcePluginFileExists();
     if (shouldNotBlockOtherInfo()) {
       for (PluginPackageParser pluginPackageParser : mPluginCache.values()) {
         List<ProviderInfo> providerInfos = pluginPackageParser.getProviders();
         for (ProviderInfo providerInfo : providerInfos) {
           if (TextUtils.equals(providerInfo.authority, name)) {
             return providerInfo;
           }
         }
       }
     } else {
       List<String> pkgs = mActivityManagerService.getPackageNamesByPid(Binder.getCallingPid());
       for (PluginPackageParser pluginPackageParser : mPluginCache.values()) {
         List<ProviderInfo> providerInfos = pluginPackageParser.getProviders();
         for (ProviderInfo providerInfo : providerInfos) {
           if (TextUtils.equals(providerInfo.authority, name)
               && pkgs.contains(providerInfo.packageName)) {
             return providerInfo;
           }
         }
       }
     }
   } catch (Exception e) {
     handleException(e);
   }
   return null;
 }
 private boolean pkgInPid(int pid, String pkg) {
   List<String> pkgs = mActivityManagerService.getPackageNamesByPid(pid);
   if (pkgs != null && pkgs.size() > 0) {
     return pkgs.contains(pkg);
   } else {
     return true;
   }
 }
  private void loadAllPlugin(Context context) {
    long b = System.currentTimeMillis();
    ArrayList<File> apkfiles = new ArrayList<File>();
    File baseDir = new File(PluginDirHelper.getBaseDir(context));
    File[] dirs = baseDir.listFiles();
    for (File dir : dirs) {
      if (dir.isDirectory()) {
        File file = new File(dir, "apk/base-1.apk");
        if (file.exists()) {
          apkfiles.add(file);
        }
      }
    }

    Log.i(TAG, "Search apk cost %s ms", (System.currentTimeMillis() - b));
    b = System.currentTimeMillis();

    if (apkfiles != null && apkfiles.size() > 0) {
      for (File pluginFile : apkfiles) {
        long b1 = System.currentTimeMillis();
        try {
          PluginPackageParser pluginPackageParser = new PluginPackageParser(mContext, pluginFile);
          Signature[] signatures = readSignatures(pluginPackageParser.getPackageName());
          if (signatures == null || signatures.length <= 0) {
            pluginPackageParser.collectCertificates(0);
            PackageInfo info = pluginPackageParser.getPackageInfo(PackageManager.GET_SIGNATURES);
            saveSignatures(info);
          } else {
            mSignatureCache.put(pluginPackageParser.getPackageName(), signatures);
            pluginPackageParser.writeSignature(signatures);
          }
          if (!mPluginCache.containsKey(pluginPackageParser.getPackageName())) {
            mPluginCache.put(pluginPackageParser.getPackageName(), pluginPackageParser);
          }
        } catch (Throwable e) {
          Log.e(TAG, "parse a apk file error %s", e, pluginFile.getPath());
        } finally {
          Log.i(
              TAG,
              "Parse %s apk cost %s ms",
              pluginFile.getPath(),
              (System.currentTimeMillis() - b1));
        }
      }
    }

    Log.i(TAG, "Parse all apk cost %s ms", (System.currentTimeMillis() - b));
    b = System.currentTimeMillis();

    try {
      mActivityManagerService.onCreate(IPluginManagerImpl.this);
    } catch (Exception e) {
      Log.e(TAG, "mActivityManagerService.onCreate", e);
    }

    Log.i(TAG, "ActivityManagerService.onCreate %s ms", (System.currentTimeMillis() - b));
  }
 @Override
 public List<String> getPackageNameByPid(int pid) throws RemoteException {
   List<String> packageNameByProcessName = mActivityManagerService.getPackageNamesByPid(pid);
   if (packageNameByProcessName != null) {
     return new ArrayList<String>(packageNameByProcessName);
   } else {
     return null;
   }
 }
 @Override
 public void reportMyProcessName(
     String stubProcessName, String targetProcessName, String targetPkg) throws RemoteException {
   mActivityManagerService.onReportMyProcessName(
       Binder.getCallingPid(),
       Binder.getCallingUid(),
       stubProcessName,
       targetProcessName,
       targetPkg);
 }
  @Override
  public int deletePackage(String packageName, int flags) throws RemoteException {
    try {
      if (mPluginCache.containsKey(packageName)) {
        forceStopPackage(packageName);

        PluginPackageParser parser;
        synchronized (mPluginCache) {
          parser = mPluginCache.remove(packageName);
        }
        Utils.deleteDir(PluginDirHelper.makePluginBaseDir(mContext, packageName));
        mActivityManagerService.onPkgDeleted(mPluginCache, parser, packageName);
        mSignatureCache.remove(packageName);
        sendUninstalledBroadcast(packageName);
        return PackageManagerCompat.DELETE_SUCCEEDED;
      }
    } catch (Exception e) {
      handleException(e);
    }
    return PackageManagerCompat.DELETE_FAILED_INTERNAL_ERROR;
  }
 @Override
 public List<ApplicationInfo> getInstalledApplications(int flags) throws RemoteException {
   waitForReadyInner();
   try {
     enforcePluginFileExists();
     List<ApplicationInfo> infos = new ArrayList<ApplicationInfo>(mPluginCache.size());
     if (shouldNotBlockOtherInfo()) {
       for (PluginPackageParser pluginPackageParser : mPluginCache.values()) {
         infos.add(pluginPackageParser.getApplicationInfo(flags));
       }
     } else {
       List<String> pkgs = mActivityManagerService.getPackageNamesByPid(Binder.getCallingPid());
       for (PluginPackageParser pluginPackageParser : mPluginCache.values()) {
         if (pkgs.contains(pluginPackageParser.getPackageName())) {
           infos.add(pluginPackageParser.getApplicationInfo(flags));
         }
       }
     }
     return infos;
   } catch (Exception e) {
     handleException(e);
   }
   return null;
 }
 public void onDestroy() {
   mActivityManagerService.onDestory();
 }
 @Override
 public ActivityInfo selectStubActivityInfo(ActivityInfo pluginInfo) throws RemoteException {
   return mActivityManagerService.selectStubActivityInfo(
       Binder.getCallingPid(), Binder.getCallingUid(), pluginInfo);
 }
 @Override
 public void onServiceDestory(ServiceInfo stubInfo, ServiceInfo targetInfo)
     throws RemoteException {
   mActivityManagerService.onServiceDestory(
       Binder.getCallingPid(), Binder.getCallingUid(), stubInfo, targetInfo);
 }
 @Override
 public boolean unregisterApplicationCallback(IApplicationCallback callback)
     throws RemoteException {
   return mActivityManagerService.unregisterApplicationCallback(
       Binder.getCallingPid(), Binder.getCallingUid(), callback);
 }
 @Override
 public String getProcessNameByPid(int pid) throws RemoteException {
   return mActivityManagerService.getProcessNameByPid(pid);
 }
 @Override
 public ProviderInfo selectStubProviderInfo(String name) throws RemoteException {
   ProviderInfo targetInfo = resolveContentProvider(name, 0);
   return mActivityManagerService.selectStubProviderInfo(
       Binder.getCallingPid(), Binder.getCallingUid(), targetInfo);
 }
 @Override
 public ServiceInfo getTargetServiceInfo(ServiceInfo targetInfo) throws RemoteException {
   return mActivityManagerService.getTargetServiceInfo(
       Binder.getCallingPid(), Binder.getCallingUid(), targetInfo);
 }
  @Override
  public int installPackage(String filepath, int flags) throws RemoteException {
    // install plugin
    String apkfile = null;
    try {
      PackageManager pm = mContext.getPackageManager();
      PackageInfo info = pm.getPackageArchiveInfo(filepath, 0);
      if (info == null) {
        return PackageManagerCompat.INSTALL_FAILED_INVALID_APK;
      }

      apkfile = PluginDirHelper.getPluginApkFile(mContext, info.packageName);

      if ((flags & PackageManagerCompat.INSTALL_REPLACE_EXISTING) != 0) {
        forceStopPackage(info.packageName);
        if (mPluginCache.containsKey(info.packageName)) {
          deleteApplicationCacheFiles(info.packageName, null);
        }
        new File(apkfile).delete();
        Utils.copyFile(filepath, apkfile);
        PluginPackageParser parser = new PluginPackageParser(mContext, new File(apkfile));
        parser.collectCertificates(0);
        PackageInfo pkgInfo =
            parser.getPackageInfo(PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES);
        if (pkgInfo != null
            && pkgInfo.requestedPermissions != null
            && pkgInfo.requestedPermissions.length > 0) {
          for (String requestedPermission : pkgInfo.requestedPermissions) {
            boolean b = false;
            try {
              b = pm.getPermissionInfo(requestedPermission, 0) != null;
            } catch (NameNotFoundException e) {
            }
            if (!mHostRequestedPermission.contains(requestedPermission) && b) {
              Log.e(TAG, "No Permission %s", requestedPermission);
              new File(apkfile).delete();
              return PluginManager.INSTALL_FAILED_NO_REQUESTEDPERMISSION;
            }
          }
        }
        saveSignatures(pkgInfo);
        //                if (pkgInfo.reqFeatures != null && pkgInfo.reqFeatures.length > 0) {
        //                    for (FeatureInfo reqFeature : pkgInfo.reqFeatures) {
        //                        Log.e(TAG, "reqFeature name=%s,flags=%s,glesVersion=%s",
        // reqFeature.name, reqFeature.flags, reqFeature.getGlEsVersion());
        //                    }
        //                }
        copyNativeLibs(mContext, apkfile, parser.getApplicationInfo(0));
        dexOpt(mContext, apkfile, parser);
        mPluginCache.put(parser.getPackageName(), parser);
        mActivityManagerService.onPkgInstalled(mPluginCache, parser, parser.getPackageName());
        sendInstalledBroadcast(info.packageName);
        return PackageManagerCompat.INSTALL_SUCCEEDED;
      } else {
        if (mPluginCache.containsKey(info.packageName)) {
          return PackageManagerCompat.INSTALL_FAILED_ALREADY_EXISTS;
        } else {
          forceStopPackage(info.packageName);
          new File(apkfile).delete();
          Utils.copyFile(filepath, apkfile);
          PluginPackageParser parser = new PluginPackageParser(mContext, new File(apkfile));
          parser.collectCertificates(0);
          PackageInfo pkgInfo =
              parser.getPackageInfo(PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES);
          if (pkgInfo != null
              && pkgInfo.requestedPermissions != null
              && pkgInfo.requestedPermissions.length > 0) {
            for (String requestedPermission : pkgInfo.requestedPermissions) {
              boolean b = false;
              try {
                b = pm.getPermissionInfo(requestedPermission, 0) != null;
              } catch (NameNotFoundException e) {
              }
              if (!mHostRequestedPermission.contains(requestedPermission) && b) {
                Log.e(TAG, "No Permission %s", requestedPermission);
                new File(apkfile).delete();
                return PluginManager.INSTALL_FAILED_NO_REQUESTEDPERMISSION;
              }
            }
          }
          saveSignatures(pkgInfo);
          //                    if (pkgInfo.reqFeatures != null && pkgInfo.reqFeatures.length > 0) {
          //                        for (FeatureInfo reqFeature : pkgInfo.reqFeatures) {
          //                            Log.e(TAG, "reqFeature name=%s,flags=%s,glesVersion=%s",
          // reqFeature.name, reqFeature.flags, reqFeature.getGlEsVersion());
          //                        }
          //                    }

          copyNativeLibs(mContext, apkfile, parser.getApplicationInfo(0));
          dexOpt(mContext, apkfile, parser);
          mPluginCache.put(parser.getPackageName(), parser);
          mActivityManagerService.onPkgInstalled(mPluginCache, parser, parser.getPackageName());
          sendInstalledBroadcast(info.packageName);
          return PackageManagerCompat.INSTALL_SUCCEEDED;
        }
      }
    } catch (Exception e) {
      if (apkfile != null) {
        new File(apkfile).delete();
      }
      handleException(e);
      return PackageManagerCompat.INSTALL_FAILED_INTERNAL_ERROR;
    }
  }
 @Override
 public void onProviderCreated(ProviderInfo stubInfo, ProviderInfo targetInfo)
     throws RemoteException {
   mActivityManagerService.onProviderCreated(
       Binder.getCallingPid(), Binder.getCallingUid(), stubInfo, targetInfo);
 }