Search in sources :

Example 6 with TinkerRuntimeException

use of com.tencent.tinker.loader.TinkerRuntimeException in project tinker by Tencent.

the class TinkerApplicationHelper method getCurrentVersion.

/**
     * you can use this api to get tinker current version before tinker is installed
     *
     * @return
     */
public static String getCurrentVersion(ApplicationLike applicationLike) {
    if (applicationLike == null || applicationLike.getApplication() == null) {
        throw new TinkerRuntimeException("tinkerApplication is null");
    }
    Intent tinkerResultIntent = applicationLike.getTinkerResultIntent();
    if (tinkerResultIntent == null) {
        return null;
    }
    final String oldVersion = ShareIntentUtil.getStringExtra(tinkerResultIntent, ShareIntentUtil.INTENT_PATCH_OLD_VERSION);
    final String newVersion = ShareIntentUtil.getStringExtra(tinkerResultIntent, ShareIntentUtil.INTENT_PATCH_NEW_VERSION);
    final boolean isMainProcess = ShareTinkerInternals.isInMainProcess(applicationLike.getApplication());
    if (oldVersion != null && newVersion != null) {
        if (isMainProcess) {
            return newVersion;
        } else {
            return oldVersion;
        }
    }
    return null;
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) Intent(android.content.Intent)

Example 7 with TinkerRuntimeException

use of com.tencent.tinker.loader.TinkerRuntimeException in project tinker by Tencent.

the class ResDiffPatchInternal method checkAndExtractResourceLargeFile.

private static boolean checkAndExtractResourceLargeFile(Context context, String apkPath, File directory, File patchFile, ShareResPatchInfo resPatchInfo, int type) {
    long start = System.currentTimeMillis();
    Tinker manager = Tinker.with(context);
    ZipFile apkFile = null;
    ZipFile patchZipFile = null;
    try {
        //recover resources.arsc first
        apkFile = new ZipFile(apkPath);
        ZipEntry arscEntry = apkFile.getEntry(ShareConstants.RES_ARSC);
        File arscFile = new File(directory, ShareConstants.RES_ARSC);
        if (arscEntry == null) {
            TinkerLog.w(TAG, "resources apk entry is null. path:" + ShareConstants.RES_ARSC);
            manager.getPatchReporter().onPatchTypeExtractFail(patchFile, arscFile, ShareConstants.RES_ARSC, type);
            return false;
        }
        //use base resources.arsc crc to identify base.apk
        String baseArscCrc = String.valueOf(arscEntry.getCrc());
        if (!baseArscCrc.equals(resPatchInfo.arscBaseCrc)) {
            TinkerLog.e(TAG, "resources.arsc's crc is not equal, expect crc: %s, got crc: %s", resPatchInfo.arscBaseCrc, baseArscCrc);
            manager.getPatchReporter().onPatchTypeExtractFail(patchFile, arscFile, ShareConstants.RES_ARSC, type);
            return false;
        }
        //resource arsc is not changed, just return true
        if (resPatchInfo.largeModRes.isEmpty()) {
            TinkerLog.i(TAG, "no large modify resources, just return");
            return true;
        }
        for (String name : resPatchInfo.largeModRes) {
            long largeStart = System.currentTimeMillis();
            ShareResPatchInfo.LargeModeInfo largeModeInfo = resPatchInfo.largeModMap.get(name);
            if (largeModeInfo == null) {
                TinkerLog.w(TAG, "resource not found largeModeInfo, type:%s, name: %s", ShareTinkerInternals.getTypeString(type), name);
                manager.getPatchReporter().onPatchPackageCheckFail(patchFile, BasePatchInternal.getMetaCorruptedCode(type));
                return false;
            }
            largeModeInfo.file = new File(directory, name);
            SharePatchFileUtil.ensureFileDirectory(largeModeInfo.file);
            //we do not check the intermediate files' md5 to save time, use check whether it is 32 length
            if (!SharePatchFileUtil.checkIfMd5Valid(largeModeInfo.md5)) {
                TinkerLog.w(TAG, "resource meta file md5 mismatch, type:%s, name: %s, md5: %s", ShareTinkerInternals.getTypeString(type), name, largeModeInfo.md5);
                manager.getPatchReporter().onPatchPackageCheckFail(patchFile, BasePatchInternal.getMetaCorruptedCode(type));
                return false;
            }
            patchZipFile = new ZipFile(patchFile);
            ZipEntry patchEntry = patchZipFile.getEntry(name);
            if (patchEntry == null) {
                TinkerLog.w(TAG, "large mod patch entry is null. path:" + name);
                manager.getPatchReporter().onPatchTypeExtractFail(patchFile, largeModeInfo.file, name, type);
                return false;
            }
            ZipEntry baseEntry = apkFile.getEntry(name);
            if (baseEntry == null) {
                TinkerLog.w(TAG, "resources apk entry is null. path:" + name);
                manager.getPatchReporter().onPatchTypeExtractFail(patchFile, largeModeInfo.file, name, type);
                return false;
            }
            InputStream oldStream = null;
            InputStream newStream = null;
            try {
                oldStream = apkFile.getInputStream(baseEntry);
                newStream = patchZipFile.getInputStream(patchEntry);
                BSPatch.patchFast(oldStream, newStream, largeModeInfo.file);
            } finally {
                SharePatchFileUtil.closeQuietly(oldStream);
                SharePatchFileUtil.closeQuietly(newStream);
            }
            //go go go bsdiff get the
            if (!SharePatchFileUtil.verifyFileMd5(largeModeInfo.file, largeModeInfo.md5)) {
                TinkerLog.w(TAG, "Failed to recover large modify file:%s", largeModeInfo.file.getPath());
                SharePatchFileUtil.safeDeleteFile(largeModeInfo.file);
                manager.getPatchReporter().onPatchTypeExtractFail(patchFile, largeModeInfo.file, name, type);
                return false;
            }
            TinkerLog.w(TAG, "success recover large modify file:%s, file size:%d, use time:%d", largeModeInfo.file.getPath(), largeModeInfo.file.length(), (System.currentTimeMillis() - largeStart));
        }
        TinkerLog.w(TAG, "success recover all large modify use time:%d", (System.currentTimeMillis() - start));
    } catch (Throwable e) {
        //            e.printStackTrace();
        throw new TinkerRuntimeException("patch " + ShareTinkerInternals.getTypeString(type) + " extract failed (" + e.getMessage() + ").", e);
    } finally {
        SharePatchFileUtil.closeZip(apkFile);
        SharePatchFileUtil.closeZip(patchZipFile);
    }
    return true;
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) TinkerZipFile(com.tencent.tinker.commons.ziputil.TinkerZipFile) ZipFile(java.util.zip.ZipFile) ShareResPatchInfo(com.tencent.tinker.loader.shareutil.ShareResPatchInfo) InputStream(java.io.InputStream) TinkerZipEntry(com.tencent.tinker.commons.ziputil.TinkerZipEntry) ZipEntry(java.util.zip.ZipEntry) Tinker(com.tencent.tinker.lib.tinker.Tinker) TinkerZipFile(com.tencent.tinker.commons.ziputil.TinkerZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 8 with TinkerRuntimeException

use of com.tencent.tinker.loader.TinkerRuntimeException in project tinker by Tencent.

the class AbstractResultService method runResultService.

public static void runResultService(Context context, PatchResult result, String resultServiceClass) {
    if (resultServiceClass == null) {
        throw new TinkerRuntimeException("resultServiceClass is null.");
    }
    try {
        Intent intent = new Intent();
        intent.setClassName(context, resultServiceClass);
        intent.putExtra(RESULT_EXTRA, result);
        context.startService(intent);
    } catch (Throwable throwable) {
        TinkerLog.e(TAG, "run result service fail, exception:" + throwable);
    }
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) Intent(android.content.Intent)

Example 9 with TinkerRuntimeException

use of com.tencent.tinker.loader.TinkerRuntimeException in project tinker by Tencent.

the class SharePatchInfo method readAndCheckPropertyWithLock.

public static SharePatchInfo readAndCheckPropertyWithLock(File pathInfoFile, File lockFile) {
    if (pathInfoFile == null || lockFile == null) {
        return null;
    }
    File lockParentFile = lockFile.getParentFile();
    if (!lockParentFile.exists()) {
        lockParentFile.mkdirs();
    }
    SharePatchInfo patchInfo;
    ShareFileLockHelper fileLock = null;
    try {
        fileLock = ShareFileLockHelper.getFileLock(lockFile);
        patchInfo = readAndCheckProperty(pathInfoFile);
    } catch (Exception e) {
        throw new TinkerRuntimeException("readAndCheckPropertyWithLock fail", e);
    } finally {
        try {
            if (fileLock != null) {
                fileLock.close();
            }
        } catch (IOException e) {
            Log.i(TAG, "releaseInfoLock error", e);
        }
    }
    return patchInfo;
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException)

Example 10 with TinkerRuntimeException

use of com.tencent.tinker.loader.TinkerRuntimeException in project tinker by Tencent.

the class ShareSecurityCheck method verifyPatchMetaSignature.

public boolean verifyPatchMetaSignature(File path) {
    if (!SharePatchFileUtil.isLegalFile(path)) {
        return false;
    }
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(path);
        final Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            // no code
            if (jarEntry == null) {
                continue;
            }
            final String name = jarEntry.getName();
            if (name.startsWith("META-INF/")) {
                continue;
            }
            //we will check other files's mad5 written in meta files
            if (!name.endsWith(ShareConstants.META_SUFFIX)) {
                continue;
            }
            metaContentMap.put(name, SharePatchFileUtil.loadDigestes(jarFile, jarEntry));
            Certificate[] certs = jarEntry.getCertificates();
            if (certs == null) {
                return false;
            }
            if (!check(path, certs)) {
                return false;
            }
        }
    } catch (Exception e) {
        throw new TinkerRuntimeException(String.format("ShareSecurityCheck file %s, size %d verifyPatchMetaSignature fail", path.getAbsolutePath(), path.length()), e);
    } finally {
        try {
            if (jarFile != null) {
                jarFile.close();
            }
        } catch (IOException e) {
            Log.e(TAG, path.getAbsolutePath(), e);
        }
    }
    return true;
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException) TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

TinkerRuntimeException (com.tencent.tinker.loader.TinkerRuntimeException)21 File (java.io.File)9 Tinker (com.tencent.tinker.lib.tinker.Tinker)8 Intent (android.content.Intent)6 ZipFile (java.util.zip.ZipFile)5 IOException (java.io.IOException)4 ZipEntry (java.util.zip.ZipEntry)4 ApplicationInfo (android.content.pm.ApplicationInfo)3 InputStream (java.io.InputStream)3 TinkerZipEntry (com.tencent.tinker.commons.ziputil.TinkerZipEntry)2 TinkerZipFile (com.tencent.tinker.commons.ziputil.TinkerZipFile)2 ShareResPatchInfo (com.tencent.tinker.loader.shareutil.ShareResPatchInfo)2 DexFile (dalvik.system.DexFile)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 X509Certificate (java.security.cert.X509Certificate)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 PackageInfo (android.content.pm.PackageInfo)1