Exemple #1
0
  public List<ApkInfo> findapks(Context context, String pathname) {
    results = new ArrayList<String>();
    find(pathname, ".apk");

    List<ApkInfo> apks = new ArrayList<ApkInfo>();
    for (String apk : results) {
      ApkInfo apkinfo = new ApkInfo();
      PackageManager pm = context.getPackageManager();
      PackageInfo pi = pm.getPackageArchiveInfo(apk, PackageManager.GET_ACTIVITIES);
      if (pi == null) continue;
      ApplicationInfo ai = pi.applicationInfo;
      ai.sourceDir = apk;
      ai.publicSourceDir = apk;

      String apksize = pi.versionName;
      String apkname = ai.loadLabel(pm).toString();
      Drawable apkicon = ai.loadIcon(pm);

      apkinfo.setAppname(apkname);
      apkinfo.setIcon(apkicon);
      apkinfo.setSize(apksize);
      apkinfo.setPath(apk);

      apks.add(apkinfo);
    }

    Collections.sort(apks, comparator);
    return apks;
  }
    @Override
    public void onDownloadFinished(Context context, DownloadsUtil.DownloadInfo info) {
      File localFile = new File(info.localFilename);
      if (!localFile.isFile()) return;

      if (moduleVersion.md5sum != null && !moduleVersion.md5sum.isEmpty()) {
        try {
          String actualMd5Sum = HashUtil.md5(localFile);
          if (!moduleVersion.md5sum.equals(actualMd5Sum)) {
            Toast.makeText(
                    context,
                    context.getString(
                        R.string.download_md5sum_incorrect, actualMd5Sum, moduleVersion.md5sum),
                    Toast.LENGTH_LONG)
                .show();
            DownloadsUtil.removeById(context, info.id);
            return;
          }
        } catch (Exception e) {
          Toast.makeText(
                  context,
                  context.getString(R.string.download_could_not_read_file, e.getMessage()),
                  Toast.LENGTH_LONG)
              .show();
          DownloadsUtil.removeById(context, info.id);
          return;
        }
      }

      PackageManager pm = context.getPackageManager();
      PackageInfo packageInfo = pm.getPackageArchiveInfo(info.localFilename, 0);

      if (packageInfo == null) {
        Toast.makeText(context, R.string.download_no_valid_apk, Toast.LENGTH_LONG).show();
        DownloadsUtil.removeById(context, info.id);
        return;
      }

      if (!packageInfo.packageName.equals(moduleVersion.module.packageName)) {
        Toast.makeText(
                context,
                context.getString(
                    R.string.download_incorrect_package_name,
                    packageInfo.packageName,
                    moduleVersion.module.packageName),
                Toast.LENGTH_LONG)
            .show();
        DownloadsUtil.removeById(context, info.id);
        return;
      }

      Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
      installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      installIntent.setDataAndType(Uri.fromFile(localFile), DownloadsUtil.MIME_TYPE_APK);
      // installIntent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
      // installIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
      installIntent.putExtra(
          Intent.EXTRA_INSTALLER_PACKAGE_NAME, context.getApplicationInfo().packageName);
      context.startActivity(installIntent);
    }
 public static String getLabel(PluginDescriptor pd) {
   PackageManager pm = PluginLoader.getApplication().getPackageManager();
   PackageInfo info =
       pm.getPackageArchiveInfo(pd.getInstalledPath(), PackageManager.GET_ACTIVITIES);
   if (info != null) {
     ApplicationInfo appInfo = info.applicationInfo;
     appInfo.sourceDir = pd.getInstalledPath();
     appInfo.publicSourceDir = pd.getInstalledPath();
     String label = null;
     try {
       if (!isMainResId(appInfo.labelRes)) {
         label = pm.getApplicationLabel(appInfo).toString();
       }
     } catch (Resources.NotFoundException e) {
     }
     if (label == null || label.equals(pd.getPackageName())) {
       // 可能设置的lable是来自宿主的资源
       if (pd.getDescription() != null) {
         int id = ResourceUtil.getResourceId(pd.getDescription());
         if (id != 0) {
           // 再宿主中查一次
           try {
             label = PluginLoader.getApplication().getResources().getString(id);
           } catch (Resources.NotFoundException e) {
           }
         }
       }
     }
     if (label != null) {
       return label;
     }
   }
   return pd.getDescription();
 }
  public static Drawable getApkIcon(String path, Context mContext) {
    Drawable icon = null;
    PackageManager pm = mContext.getPackageManager();
    PackageInfo pinfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
    if (pinfo != null) {
      pinfo.applicationInfo.publicSourceDir = path;
      icon = pm.getApplicationIcon(pinfo.applicationInfo);
    }

    return icon;
  }
Exemple #5
0
 /**
  * @Title: getApkInfoByPath @Description: 由路径得到app信息
  *
  * @param context
  * @param absPath
  * @return
  * @throws
  */
 public static ApplicationInfo getApkInfoByPath(Context context, String absPath) {
   ApplicationInfo appInfo = null;
   PackageManager pm = context.getPackageManager();
   PackageInfo pkgInfo = pm.getPackageArchiveInfo(absPath, PackageManager.GET_ACTIVITIES);
   if (pkgInfo != null) {
     appInfo = pkgInfo.applicationInfo;
     /* 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */
     appInfo.sourceDir = absPath;
     appInfo.publicSourceDir = absPath;
   }
   return appInfo;
 }
Exemple #6
0
 /**
  * 获取到apk的包名
  *
  * @param context
  * @param appPath
  * @return
  */
 public static String getPageNameByApk(Context context, String appPath) {
   if (appPath != null) {
     PackageManager pm = context.getPackageManager();
     PackageInfo info = pm.getPackageArchiveInfo(appPath, PackageManager.GET_ACTIVITIES);
     ApplicationInfo appInfo = info.applicationInfo;
     String packageName = appInfo.packageName;
     // String version=info.versionName;
     // Drawable icon = pm.getApplicationIcon(appInfo);
     return packageName;
   } else {
     return null;
   }
 }
 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
 public static Drawable getLogo(PluginDescriptor pd) {
   PackageManager pm = PluginLoader.getApplication().getPackageManager();
   PackageInfo info =
       pm.getPackageArchiveInfo(pd.getInstalledPath(), PackageManager.GET_ACTIVITIES);
   if (info != null) {
     ApplicationInfo appInfo = info.applicationInfo;
     appInfo.sourceDir = pd.getInstalledPath();
     appInfo.publicSourceDir = pd.getInstalledPath();
     Drawable logo = pm.getApplicationLogo(appInfo);
     return logo;
   }
   return null;
 }
 /*
  * 采用了新的办法获取APK图标,之前的失败是因为android中存在的一个BUG,通过
  * appInfo.publicSourceDir = apkPath;来修正这个问题,详情参见:
  * http://code.google.com/p/android/issues/detail?id=9151
  */
 public static Drawable getApkIcon(Context context, String apkPath) {
   PackageManager pm = context.getPackageManager();
   PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
   if (info != null) {
     ApplicationInfo appInfo = info.applicationInfo;
     appInfo.sourceDir = apkPath;
     appInfo.publicSourceDir = apkPath;
     try {
       return appInfo.loadIcon(pm);
     } catch (OutOfMemoryError e) {
       Log.e(LOG_TAG, e.toString());
     }
   }
   return null;
 }
  public static int installSilence(Context context, String filePath, boolean isUpdate) {
    File file = new File(filePath);

    if (!file.isFile() || !file.exists()) {
      return 0;
    }
    Uri packageURI = Uri.fromFile(file);
    int installFlags = 0;
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
      try {
        PackageInfo pi =
            pm.getPackageInfo(info.packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
        if (pi != null) {
          if (isUpdate) {
            installFlags |= ACA.PackageManager.INSTALL_REPLACE_EXISTING;
          } else {
            return ACA.PackageManager.INSTALL_SUCCEEDED;
          }
        }
      } catch (NameNotFoundException e) {
        // the package did't installed, go ahead
        e.printStackTrace();
      }
      PackageInstallObserver obs = new PackageInstallObserver();
      ACA.PackageManager.installPackage(pm, packageURI, obs, installFlags, info.packageName);
      synchronized (obs) {
        while (!obs.finished) {
          try {
            obs.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        ;
        return obs.result;
      }
    }
    return 0;
  }
  private Bitmap getAppDrawable(String path) throws OutOfMemoryError {
    Bitmap bitsat;
    try {
      PackageManager pm = mContext.getPackageManager();
      PackageInfo pi = pm.getPackageArchiveInfo(path, 0);
      // // the secret are these two lines....
      pi.applicationInfo.sourceDir = path;
      pi.applicationInfo.publicSourceDir = path;
      // //
      Drawable d = pi.applicationInfo.loadIcon(pm);

      Bitmap d1 = null;
      d1 = ((BitmapDrawable) d).getBitmap();
      bitsat = d1;
    } catch (Exception e) {
      Drawable apk = ContextCompat.getDrawable(mContext, R.drawable.ic_doc_apk_grid);
      Bitmap apk1 = ((BitmapDrawable) apk).getBitmap();
      bitsat = apk1;
    }
    return bitsat;
  }
  @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;
    }
  }
Exemple #12
0
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == FILE_PICKER) {
      if (data != null) {

        Uri uri = data.getData();
        File apkFile = new File(uri.getPath());
        final String PackageDir = apkFile.getAbsolutePath();

        Ln.d(PackageDir);

        final String PackageName;
        final String PackageId;

        if (FilenameUtils.isExtension(PackageDir, "apk")) {
          PackageManager pm = getPackageManager();
          PackageInfo info = pm.getPackageArchiveInfo(PackageDir, PackageManager.GET_ACTIVITIES);
          if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;

            if (Build.VERSION.SDK_INT >= 8) {
              appInfo.sourceDir = PackageDir;
              appInfo.publicSourceDir = PackageDir;
            }
            PackageName = info.applicationInfo.loadLabel(getPackageManager()).toString();
            PackageId = info.packageName;

          } else {
            PackageName = "";
            PackageId = "";
          }

          if (!prefs.getBoolean("hide_decompiler_select", false)) {
            final CharSequence[] items = {"CFR 0.102", "JaDX 0.6.1"};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Pick a decompiler");
            builder.setItems(
                items,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int item) {
                    Intent i = new Intent(getApplicationContext(), AppProcessActivity.class);
                    i.putExtra("package_id", PackageId);
                    i.putExtra("package_label", PackageName);
                    i.putExtra("package_file_path", PackageDir);
                    i.putExtra("decompiler", (item == 1 ? "jadx" : "cfr"));
                    startActivity(i);
                    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                  }
                });
            AlertDialog alert = builder.create();
            alert.show();
          } else {
            Intent i = new Intent(getApplicationContext(), AppProcessActivity.class);
            i.putExtra("package_id", PackageId);
            i.putExtra("package_label", PackageName);
            i.putExtra("package_file_path", PackageDir);
            i.putExtra("decompiler", prefs.getString("decompiler", "cfr"));
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
          }
        }
      }
    }
  }
 /**
  * 获取未安装的APK信息
  *
  * @param context
  * @param archiveFilePath APK文件的路径�?如:/sdcard/download/XX.apk
  */
 public static PackageInfo getApkInfo(Context context, String archiveFilePath) {
   PackageManager pm = context.getPackageManager();
   PackageInfo apkInfo = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_META_DATA);
   return apkInfo;
 }
Exemple #14
0
 public static PackageInfo getPkgInfoByPath(Context context, String absPath) {
   PackageInfo pkgInfo = null;
   PackageManager pm = context.getPackageManager();
   pkgInfo = pm.getPackageArchiveInfo(absPath, PackageManager.GET_ACTIVITIES);
   return pkgInfo;
 }