Search in sources :

Example 11 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class PatchMerger method findOriginalBundleFile.

/**
     * be not call in main thread
     */
/**
     * get original bundle
     *
     * @param bundleName
     * @return
     */
public File findOriginalBundleFile(String bundleName, String bundleDirIfNeedCreate, UpdateInfo.Item item) throws IOException {
    if (bundleName.equals(MAIN_DEX)) {
        return new File(RuntimeVariables.androidApplication.getApplicationInfo().sourceDir);
    }
    if (!TextUtils.isEmpty(bundleName)) {
        File oldBundle = null;
        if (TextUtils.isEmpty(item.srcVersion)) {
            oldBundle = Atlas.getInstance().getBundleFile(bundleName) != null ? Atlas.getInstance().getBundleFile(bundleName) : Framework.getInstalledBundle(bundleName, "");
        } else {
            BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
            if (impl != null && !BaselineInfoManager.instance().isDexPatched(bundleName)) {
                String version = impl.getArchive().getCurrentRevision().getVersion();
                if (version != null && version.equals(item.srcVersion)) {
                    oldBundle = impl.getArchive().getArchiveFile();
                } else if (version != null && !version.equals(item.srcVersion) && !AtlasBundleInfoManager.instance().isInternalBundle(bundleName)) {
                    //                            Atlas.getInstance().restoreBundle(new String[]{bundleName});
                    return null;
                } else if (version != null && !version.equals(item.srcVersion)) {
                    throw new IOException("can not find valid src bundle of " + bundleName);
                }
            } else {
                oldBundle = Framework.getInstalledBundle(bundleName, item.srcVersion);
                if (oldBundle == null) {
                    throw new IOException("can not find valid src bundle of " + bundleName);
                }
            }
        }
        if (oldBundle == null) {
            String oldBundleFileName = String.format("lib%s.so", bundleName.replace(".", "_"));
            File libDir = new File(RuntimeVariables.androidApplication.getFilesDir().getParentFile(), "lib");
            oldBundle = new File(libDir, oldBundleFileName);
            if (oldBundle.exists()) {
                return oldBundle;
            } else {
                if (apkZip == null) {
                    apkZip = new ZipFile(RuntimeVariables.androidApplication.getApplicationInfo().sourceDir);
                }
                String entryName = String.format("lib/armeabi/%s", oldBundleFileName);
                if (apkZip.getEntry(entryName) != null) {
                    InputStream inputStream = apkZip.getInputStream(apkZip.getEntry(entryName));
                    oldBundle = new File(bundleDirIfNeedCreate, oldBundleFileName);
                    //                        oldBundle.createNewFile();
                    ApkUtils.copyInputStreamToFile(inputStream, oldBundle);
                    return oldBundle;
                }
            }
        }
        return oldBundle;
    }
    return null;
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile)

Example 12 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class ServiceDetector method prepareServiceBundle.

private static DetectResult prepareServiceBundle(final Intent service, final int retryCount, final DetectResult result) {
    if (service != null) {
        String componentName = findComponent(service);
        if (TextUtils.isEmpty(componentName)) {
            return updateResult(DetectResult.BUNDLE_PREPARED, result);
        }
        String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(componentName);
        if (TextUtils.isEmpty(bundleName)) {
            if (isClassExistInMaindex(componentName)) {
                return updateResult(DetectResult.BUNDLE_PREPARED, result);
            } else {
                return updateResult(DetectResult.BUNDLE_UNPREPARED, result);
            }
        } else {
            BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
            if (impl != null && impl.checkValidate()) {
                return updateResult(DetectResult.BUNDLE_PREPARED, result);
            } else {
                if (retryCount > 0) {
                    final DetectResult newResult = updateResult(DetectResult.BUNDLE_WAIT, result);
                    BundleUtil.checkBundleStateAsync(bundleName, new Runnable() {

                        @Override
                        public void run() {
                            int newRetryCout = retryCount - 1;
                            prepareServiceBundle(service, newRetryCout, newResult);
                        }
                    }, null);
                    return newResult;
                } else {
                    DetectResult newResult = updateResult(DetectResult.BUNDLE_UNPREPARED, result);
                    return newResult;
                }
            }
        }
    }
    return updateResult(DetectResult.BUNDLE_UNPREPARED, result);
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl)

Example 13 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class BundlePackageManager method queryIntenServices.

public static List<ResolveInfo> queryIntenServices(Intent intent, String resolvedType, int flags, int userId) {
    List<Bundle> bundles = Atlas.getInstance().getBundles();
    for (Bundle bundle : bundles) {
        BundleImpl impl = (BundleImpl) bundle;
        if (impl.isUpdated() && impl.getPackageManager() != null) {
            ResolveInfo info = impl.getPackageManager().wrapperActivityIntentIfNeed(intent);
            if (info != null) {
                List<ResolveInfo> rf = new ArrayList<ResolveInfo>(1);
                rf.add(info);
                return rf;
            }
        }
    }
    return null;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) BundleImpl(android.taobao.atlas.framework.BundleImpl) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList)

Example 14 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class BundlePackageManager method wrapperActivityIntent.

private void wrapperActivityIntent(Intent intent, ActivityInfo info) {
    String rawData = intent.getDataString();
    intent.putExtra("atlas_rawData", rawData);
    intent.setData(null);
    intent.putExtra("atlas_rawComponent", info.name);
    intent.putExtra("atlas_activity_location", bundleName);
    BundleImpl bundle = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
    if (!bundle.getArchive().isDexOpted()) {
        bundle.getArchive().optDexFile();
    }
    intent.setClassName(RuntimeVariables.androidApplication.getPackageName(), AtlasFakeActivity.class.getName());
    ActivityTaskMgr.getInstance().handleActivityStack(info.name, intent, intent.getFlags(), info.launchMode);
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) AtlasFakeActivity(android.taobao.atlas.runtime.AtlasFakeActivity)

Example 15 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class BundlePackageManager method parseBundle.

/**
     * 解析bundle Manifest
     * @param context
     * @param bundle
     * @return
     * @throws InvocationTargetException
     */
public static BundlePackageManager parseBundle(Context context, Bundle bundle) throws InvocationTargetException {
    if (android.os.Build.MODEL.contains("H30") || android.os.Build.MODEL.contains("G750")) {
        return null;
    }
    String new_activity_bundle = Atlas.getInstance().getConfig(Atlas.ATLAS_NEW_ACTIVITY_BUNDLE);
    if (!Framework.isDeubgMode()) {
        if (Atlas.getInstance().getConfig(Atlas.ATLAS_NEW_ACTIVITY_SUPPORT).equals("0") || TextUtils.isEmpty(new_activity_bundle)) {
            return null;
        }
        if (!TextUtils.isEmpty(new_activity_bundle) && !new_activity_bundle.contains(bundle.getLocation())) {
            return null;
        }
    }
    String apkPath = ((BundleImpl) bundle).getArchive().getArchiveFile().getAbsolutePath();
    if (apkPath == null || apkPath.length() == 0 || context == null) {
        return null;
    }
    // 解析apk,获取packageName
    Class<?> pkgParserCls = AtlasHacks.PackageParser.getmClass();
    Object packageObj = null;
    Object pkgParser = null;
    if (Build.VERSION.SDK_INT <= 20) {
        pkgParser = AtlasHacks.PackageParser_constructor.getInstance(apkPath);
    } else {
        pkgParser = AtlasHacks.PackageParser_constructor.getInstance();
    }
    try {
        AssetManager assmgr = AssetManager.class.newInstance();
        AtlasHacks.AssetManager_addAssetPath.invoke(assmgr, RuntimeVariables.androidApplication.getApplicationInfo().sourceDir);
        int cookie = (Integer) AtlasHacks.AssetManager_addAssetPath.invoke(assmgr, apkPath);
        Resources res = new Resources(assmgr, RuntimeVariables.delegateResources.getDisplayMetrics(), RuntimeVariables.delegateResources.getConfiguration());
        XmlResourceParser parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
        packageObj = parsePackage(pkgParser, res, parser);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    if (packageObj == null) {
        return null;
    }
    BundlePackageManager packageManager = new BundlePackageManager();
    packageManager.setPackage(packageObj);
    packageManager.bundleName = bundle.getLocation();
    String packageName = (String) AtlasHacks.PackageParser$Package_packageName.get(packageObj);
    packageManager.setPackageName(packageName);
    AtlasHacks.PackageParser$Package_packageName.set(packageObj, RuntimeVariables.androidApplication.getPackageName());
    ApplicationInfo info = AtlasHacks.PackageParser$Package_applicationInfo.get(packageObj);
    info.name = RuntimeVariables.androidApplication.getApplicationInfo().name;
    info.className = RuntimeVariables.androidApplication.getApplicationInfo().className;
    info.taskAffinity = RuntimeVariables.androidApplication.getApplicationInfo().taskAffinity;
    info.permission = RuntimeVariables.androidApplication.getApplicationInfo().permission;
    info.processName = RuntimeVariables.androidApplication.getApplicationInfo().processName;
    info.theme = RuntimeVariables.androidApplication.getApplicationInfo().theme;
    info.flags = RuntimeVariables.androidApplication.getApplicationInfo().flags;
    info.uiOptions = RuntimeVariables.androidApplication.getApplicationInfo().uiOptions;
    info.backupAgentName = RuntimeVariables.androidApplication.getApplicationInfo().backupAgentName;
    info.descriptionRes = RuntimeVariables.androidApplication.getApplicationInfo().descriptionRes;
    info.targetSdkVersion = RuntimeVariables.androidApplication.getApplicationInfo().targetSdkVersion;
    info.compatibleWidthLimitDp = RuntimeVariables.androidApplication.getApplicationInfo().compatibleWidthLimitDp;
    info.uid = RuntimeVariables.androidApplication.getApplicationInfo().uid;
    info.largestWidthLimitDp = RuntimeVariables.androidApplication.getApplicationInfo().largestWidthLimitDp;
    info.enabled = RuntimeVariables.androidApplication.getApplicationInfo().enabled;
    info.requiresSmallestWidthDp = RuntimeVariables.androidApplication.getApplicationInfo().requiresSmallestWidthDp;
    info.packageName = RuntimeVariables.androidApplication.getApplicationInfo().packageName;
    ExternalComponentIntentResolver activityResolver = new ExternalComponentIntentResolver();
    ExternalComponentIntentResolver serviceResolver = new ExternalComponentIntentResolver();
    List<String> components = new ArrayList<String>();
    ArrayList<Object> activityList = (ArrayList<Object>) AtlasHacks.PackageParser$Package_activities.get(packageObj);
    //        ArrayList<Object> serviceList = (ArrayList<Object>)  AtlasHacks.PackageParser$Package_services.get(packageObj);
    //        ArrayList<Object> receiverList = (ArrayList<Object>) AtlasHacks.PackageParser$Package_receivers.get(packageObj);
    Intent testIntent = new Intent();
    testIntent.putExtra("RawQuery", true);
    /**
         * 新增Activity
         */
    if (activityList != null) {
        for (Object activity : activityList) {
            ComponentName componentName = (ComponentName) AtlasHacks.PackageParser$Component_getComponentName.invoke(activity);
            if (componentName != null) {
                if (componentName.getClassName() != null && componentName.getClassName().endsWith("Alias") || componentName.getClassName().endsWith("Alice")) {
                    continue;
                }
                componentName = new ComponentName(RuntimeVariables.androidApplication.getPackageName(), componentName.getClassName());
                components.add(componentName.getClassName());
            }
            testIntent.setComponent(componentName);
            if (RuntimeVariables.androidApplication.getPackageManager().resolveActivity(testIntent, 0) == null && !AtlasBundleInfoManager.instance().getBundleInfo(bundle.getLocation()).getActivities().containsKey(componentName.getClassName())) {
                try {
                    ActivityInfo activityInfo = (ActivityInfo) activity.getClass().getField("info").get(activity);
                    if (activityInfo.targetActivity != null) {
                        activityInfo.taskAffinity = RuntimeVariables.androidApplication.getApplicationInfo().taskAffinity;
                    }
                    activityInfo.packageName = RuntimeVariables.androidApplication.getApplicationInfo().packageName;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                activityResolver.addComponent(activity);
            }
        }
        if (activityResolver != null && activityResolver.mComponents.size() > 0) {
            packageManager.setNewActivityResolver(activityResolver);
            AtlasMonitor.getInstance().trace(AtlasMonitor.NEW_ACTIVITY_BUNDLE, bundle.getLocation(), AtlasMonitor.NEW_ACTIVITY_BUNDLE, FileUtils.getDataAvailableSpace());
        }
    }
    /**
         * 新增Service
         */
    //        if (serviceList != null) {
    //            for (Object service : serviceList) {
    //                ComponentName componentName = (ComponentName) AtlasHacks.PackageParser$Component_getComponentName.invoke(service);
    //                if(componentName!=null) {
    //                    components.add(componentName.getClassName());
    //                }
    //                testIntent.setComponent(componentName);
    //                if(RuntimeVariables.androidApplication.getPackageManager().resolveService(testIntent, 0)==null) {
    //                    try {
    //                        ServiceInfo serviceInfo = (ServiceInfo) service.getClass().getField("info").get(service);
    //                        serviceInfo.packageName = RuntimeVariables.androidApplication.getApplicationInfo().packageName;
    //                    } catch (Exception e) {
    //                        e.printStackTrace();
    //                    }
    //                    serviceResolver.addComponent(service);
    //                }
    //            }
    //            packageManager.setNewServiceResolver(serviceResolver);
    //        }
    packageManager.setComponents(components);
    return packageManager;
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) ActivityInfo(android.content.pm.ActivityInfo) AssetManager(android.content.res.AssetManager) XmlResourceParser(android.content.res.XmlResourceParser) ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) Intent(android.content.Intent) InvocationTargetException(java.lang.reflect.InvocationTargetException) ComponentName(android.content.ComponentName) Resources(android.content.res.Resources)

Aggregations

BundleImpl (android.taobao.atlas.framework.BundleImpl)21 ResolveInfo (android.content.pm.ResolveInfo)5 Bundle (org.osgi.framework.Bundle)5 ArrayList (java.util.ArrayList)4 ComponentName (android.content.ComponentName)3 Resources (android.content.res.Resources)3 BundleClassLoader (android.taobao.atlas.framework.BundleClassLoader)3 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Intent (android.content.Intent)2 Activity (android.app.Activity)1 Application (android.app.Application)1 BroadcastReceiver (android.content.BroadcastReceiver)1 ComponentCallbacks (android.content.ComponentCallbacks)1 ActivityInfo (android.content.pm.ActivityInfo)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 ServiceInfo (android.content.pm.ServiceInfo)1 AssetManager (android.content.res.AssetManager)1 Configuration (android.content.res.Configuration)1 XmlResourceParser (android.content.res.XmlResourceParser)1