Java Code Examples for android.content.Context#getPackageCodePath()

The following examples show how to use android.content.Context#getPackageCodePath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InitInjector.java    From XposedHider with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
 *
 * @param context           context参数
 * @param modulePackageName 当前模块包名
 * @return return apk file
 */
private File findApkFile(Context context, String modulePackageName) {
    if (context == null) {
        return null;
    }
    try {
        Context moduleContext = context.createPackageContext(
                modulePackageName,
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        String apkPath = moduleContext.getPackageCodePath();
        return new File(apkPath);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: Utils.java    From SecuritySample with Apache License 2.0 6 votes vote down vote up
private static long getApkFileChecksum(Context context) {
    String apkPath = context.getPackageCodePath();
    Long chksum = null;
    try {
        // Open the file and build a CRC32 checksum.
        FileInputStream fis = new FileInputStream(new File(apkPath));
        CRC32 chk = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(fis, chk);
        byte[] buff = new byte[80];
        while (cis.read(buff) >= 0) ;
        chksum = chk.getValue();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return chksum;
}
 
Example 3
Source File: CommonUtil.java    From Aria with Apache License 2.0 6 votes vote down vote up
/**
 * 获取某包下所有类
 *
 * @param className 过滤的类名
 * @return 类的完整名称
 */
public static List<String> getPkgClassNames(Context context, String className) {
  List<String> classNameList = new ArrayList<>();
  String pPath = context.getPackageCodePath();
  File dir = new File(pPath).getParentFile();
  String[] paths = dir.list();
  if (paths == null) {
    classNameList.addAll(getPkgClassName(pPath, className));
  } else {
    String dPath = dir.getPath();
    for (String path : dir.list()) {
      String fPath = dPath + "/" + path;
      if (!fPath.endsWith(".apk")) {
        continue;
      }
      classNameList.addAll(getPkgClassName(fPath, className));
    }
  }
  return classNameList;
}
 
Example 4
Source File: ObjectHelper.java    From ParcelCheck with Apache License 2.0 6 votes vote down vote up
/**
 * Gets all of the classes within a package
 * @param context the context
 * @param packageName the package name to fetch classes from
 * @return an list of named classes within package
 */
public static ArrayList<String> getClassesOfPackage(Context context, String packageName) {
    ArrayList<String> classes = new ArrayList<>();
    try {
        String packageCodePath = context.getPackageCodePath();
        DexFile df = new DexFile(packageCodePath);
        for (Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
            String className = iter.nextElement();
            if (className.contains(packageName)) {
                classes.add(className.substring(className.lastIndexOf(".") + 1, className.length()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return classes;
}
 
Example 5
Source File: ModelRegistryUtils.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
public void makeReady(Context context) {
    try {
        DexFile dexFile = new DexFile(context.getPackageCodePath());
        for (Enumeration<String> item = dexFile.entries(); item.hasMoreElements(); ) {
            String element = item.nextElement();
            if (element.startsWith(App.class.getPackage().getName())) {
                Class<? extends OModel> clsName = (Class<? extends OModel>) Class.forName(element);
                if (clsName != null && clsName.getSuperclass() != null &&
                        OModel.class.isAssignableFrom(clsName.getSuperclass())) {
                    String modelName = getModelName(context, clsName);
                    if (modelName != null) {
                        this.models.put(modelName, clsName);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: ClassUtils.java    From Cangol-appcore with Apache License 2.0 6 votes vote down vote up
/**
 * 获取dexFile所有类
 *
 * @param context
 * @return
 */
public static List<String> getAllClassNameFromDexFile(Context context, String packageName) {
    final List<String> classList = new ArrayList<>();
    try {
        final DexFile df = new DexFile(context.getPackageCodePath());
        String str;
        for (final Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
            str = iter.nextElement();
            if ((packageName != null && str.startsWith(packageName))
                    || (packageName == null || "".equals(packageName))) {
                classList.add(str);
            }
        }
        df.close();
    } catch (IOException e) {
        Log.e("IOException " + e.getMessage());
    }
    return classList;
}
 
Example 7
Source File: HookLoader.java    From QNotified with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
 *
 * @param context           context参数
 * @param modulePackageName 当前模块包名
 * @return return apk file
 */
@TargetApi(Build.VERSION_CODES.FROYO)
private File findApkFile(Context context, String modulePackageName) {
    if (context == null) {
        return null;
    }
    try {
        Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        String apkPath = moudleContext.getPackageCodePath();
        return new File(apkPath);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 8
Source File: HookLoader.java    From ZhihuXposed with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
 * @param context context参数
 * @param modulePackageName 当前模块包名
 * @return return apk file
 */
private File findApkFile(Context context,String modulePackageName){
    if (context==null){
        return null;
    }
    try {
        Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        String apkPath = moudleContext.getPackageCodePath();
        return new File(apkPath);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 9
Source File: InitInjector.java    From fuckView with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
 *
 * @param context           context参数
 * @param modulePackageName 当前模块包名
 * @return return apk file
 */
private File findApkFile(Context context, String modulePackageName) {
    if (context == null) {
        return null;
    }
    try {
        Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        String apkPath = moudleContext.getPackageCodePath();
        return new File(apkPath);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 10
Source File: Utils.java    From SecuritySample with Apache License 2.0 5 votes vote down vote up
private static byte[] getApkFileDigest(Context context) {
    String apkPath = context.getPackageCodePath();
    try {
        return getDigest(new FileInputStream(apkPath), "SHA-256");
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    return null;
}
 
Example 11
Source File: BackendMapView.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
static synchronized Context loadNativeLib(Context context) {
    try {
        if (nativeLibLoaded) return context;
        ApplicationInfo otherAppInfo = context.getPackageManager().getApplicationInfo(context.getApplicationContext().getPackageName(), 0);

        String primaryCpuAbi = (String) ApplicationInfo.class.getField("primaryCpuAbi").get(otherAppInfo);
        if (primaryCpuAbi != null) {
            String path = "lib/" + primaryCpuAbi + "/libvtm-jni.so";
            File cacheFile = new File(context.getApplicationContext().getCacheDir().getAbsolutePath() + "/.gmscore/" + path);
            cacheFile.getParentFile().mkdirs();
            File apkFile = new File(context.getPackageCodePath());
            if (!cacheFile.exists() || cacheFile.lastModified() < apkFile.lastModified()) {
                ZipFile zipFile = new ZipFile(apkFile);
                ZipEntry entry = zipFile.getEntry(path);
                if (entry != null) {
                    copyInputStream(zipFile.getInputStream(entry), new FileOutputStream(cacheFile));
                } else {
                    Log.d(TAG, "Can't load native library: " + path + " does not exist in " + apkFile);
                }
            }
            Log.d(TAG, "Loading vtm-jni from " + cacheFile.getPath());
            System.load(cacheFile.getAbsolutePath());
            nativeLibLoaded = true;
        }
    } catch (Exception e) {
        Log.w(TAG, e);
    }
    if (!nativeLibLoaded) {
        Log.d(TAG, "Loading native vtm-jni");
        System.loadLibrary("vtm-jni");
        nativeLibLoaded = true;
    }
    return context;
}