Search in sources :

Example 66 with Application

use of android.app.Application in project atlas by alibaba.

the class BundleLifecycleHandler method started.

private void started(Bundle bundle) {
    BundleImpl b = (BundleImpl) bundle;
    if (b.getClassLoader() == null || !((BundleClassLoader) b.getClassLoader()).validateClasses()) {
        return;
    }
    long time = System.currentTimeMillis();
    // load application from AndroidManifest.xml
    //        PackageLite packageLite = DelegateComponent.getPackage(b.getLocation());
    BundleListing.BundleInfo info = AtlasBundleInfoManager.instance().getBundleInfo(b.getLocation());
    if (info != null) {
        String appClassName = info.getApplicationName();
        if (StringUtils.isNotEmpty(appClassName)) {
            try {
                Log.e("BundleLifeCycle", "start " + appClassName);
                Application app = newApplication(appClassName, b.getClassLoader());
                app.onCreate();
                Log.e("BundleLifeCycle", "start finish" + appClassName);
                ((BundleImpl) bundle).setActive();
            } catch (ApplicationInitException e) {
                if (b.getArchive() != null && b.getArchive().isDexOpted()) {
                    throw new RuntimeException(e);
                }
                AtlasMonitor.getInstance().trace(AtlasMonitor.BUNDLE_APPLICATION_FAIL, bundle.getLocation(), e.getMessage(), FileUtils.getAvailableDisk());
                Log.e("BundleLifeCycle", "started application crash | " + (Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId()));
            }
        } else {
            ((BundleImpl) bundle).setActive();
            Log.e("BundleLifeCycle", "started with no application");
        }
    }
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) BundleClassLoader(android.taobao.atlas.framework.BundleClassLoader) BundleListing(android.taobao.atlas.bundleInfo.BundleListing) Application(android.app.Application)

Example 67 with Application

use of android.app.Application in project atlas by alibaba.

the class FrameworkLifecycleHandler method starting.

private void starting() {
    if (RuntimeVariables.safeMode) {
        return;
    }
    long time = System.currentTimeMillis();
    android.os.Bundle metaData = null;
    try {
        ApplicationInfo applicationInfo = RuntimeVariables.androidApplication.getPackageManager().getApplicationInfo(RuntimeVariables.androidApplication.getPackageName(), PackageManager.GET_META_DATA);
        metaData = applicationInfo.metaData;
    } catch (NameNotFoundException e1) {
        e1.printStackTrace();
    }
    if (metaData != null) {
        String strApps = metaData.getString("application");
        if (StringUtils.isNotEmpty(strApps)) {
            String[] appClassNames = StringUtils.split(strApps, ",");
            if (appClassNames == null || appClassNames.length == 0) {
                appClassNames = new String[] { strApps };
            }
            for (String appClassName : appClassNames) {
                try {
                    Application app = BundleLifecycleHandler.newApplication(appClassName, Framework.getSystemClassLoader());
                    app.onCreate();
                //                        DelegateComponent.apkApplications.put("system:" + appClassName, app);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    final long timediff = System.currentTimeMillis() - time;
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) Application(android.app.Application) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) BundleException(org.osgi.framework.BundleException)

Example 68 with Application

use of android.app.Application in project robolectric by robolectric.

the class ShadowLooperTest method soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler.

@Test
public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception {
    Looper mainLooper = Looper.getMainLooper();
    Scheduler scheduler = shadowOf(mainLooper).getScheduler();
    shadowOf(mainLooper).quit = true;
    assertThat(RuntimeEnvironment.application.getMainLooper()).isSameAs(mainLooper);
    Scheduler s = new Scheduler();
    RuntimeEnvironment.setMasterScheduler(s);
    ShadowLooper.resetThreadLoopers();
    Application application = new Application();
    ReflectionHelpers.callInstanceMethod(application, "attach", ReflectionHelpers.ClassParameter.from(Context.class, RuntimeEnvironment.application.getBaseContext()));
    assertThat(Looper.getMainLooper()).as("Looper.getMainLooper()").isSameAs(mainLooper);
    assertThat(application.getMainLooper()).as("app.getMainLooper()").isSameAs(mainLooper);
    assertThat(shadowOf(mainLooper).getScheduler()).as("scheduler").isNotSameAs(scheduler).isSameAs(s);
    assertThat(shadowOf(mainLooper).hasQuit()).as("quit").isFalse();
}
Also used : Context(android.content.Context) Looper(android.os.Looper) Scheduler(org.robolectric.util.Scheduler) Application(android.app.Application) Test(org.junit.Test)

Example 69 with Application

use of android.app.Application in project platform_frameworks_base by android.

the class WebViewFactory method getWebViewContextAndSetProvider.

private static Context getWebViewContextAndSetProvider() {
    Application initialApplication = AppGlobals.getInitialApplication();
    try {
        WebViewProviderResponse response = null;
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewUpdateService.waitForAndGetProvider()");
        try {
            response = getUpdateService().waitForAndGetProvider();
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        if (response.status != LIBLOAD_SUCCESS && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) {
            throw new MissingWebViewPackageException("Failed to load WebView provider: " + getWebViewPreparationErrorReason(response.status));
        }
        // Register to be killed before fetching package info - so that we will be
        // killed if the package info goes out-of-date.
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "ActivityManager.addPackageDependency()");
        try {
            ActivityManagerNative.getDefault().addPackageDependency(response.packageInfo.packageName);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        // Fetch package info and verify it against the chosen package
        PackageInfo newPackageInfo = null;
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()");
        try {
            newPackageInfo = initialApplication.getPackageManager().getPackageInfo(response.packageInfo.packageName, PackageManager.GET_SHARED_LIBRARY_FILES | PackageManager.MATCH_DEBUG_TRIAGED_MISSING | // installed for the current user
            PackageManager.MATCH_UNINSTALLED_PACKAGES | // Fetch signatures for verification
            PackageManager.GET_SIGNATURES | // Get meta-data for meta data flag verification
            PackageManager.GET_META_DATA);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        // Validate the newly fetched package info, throws MissingWebViewPackageException on
        // failure
        verifyPackageInfo(response.packageInfo, newPackageInfo);
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "initialApplication.createApplicationContext");
        try {
            // Construct an app context to load the Java code into the current app.
            Context webViewContext = initialApplication.createApplicationContext(newPackageInfo.applicationInfo, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
            sPackageInfo = newPackageInfo;
            return webViewContext;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
    } catch (RemoteException | PackageManager.NameNotFoundException e) {
        throw new MissingWebViewPackageException("Failed to load WebView provider: " + e);
    }
}
Also used : Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) RemoteException(android.os.RemoteException) Application(android.app.Application)

Example 70 with Application

use of android.app.Application in project platform_frameworks_base by android.

the class WebViewFactory method getProviderClass.

private static Class<WebViewFactoryProvider> getProviderClass() {
    Context webViewContext = null;
    Application initialApplication = AppGlobals.getInitialApplication();
    try {
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getWebViewContextAndSetProvider()");
        try {
            webViewContext = getWebViewContextAndSetProvider();
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
        Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " + sPackageInfo.versionName + " (code " + sPackageInfo.versionCode + ")");
        Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()");
        try {
            initialApplication.getAssets().addAssetPathAsSharedLibrary(webViewContext.getApplicationInfo().sourceDir);
            ClassLoader clazzLoader = webViewContext.getClassLoader();
            Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()");
            loadNativeLibrary(clazzLoader);
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
            Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "Class.forName()");
            try {
                return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY, true, clazzLoader);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
            }
        } catch (ClassNotFoundException e) {
            Log.e(LOGTAG, "error loading provider", e);
            throw new AndroidRuntimeException(e);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
        }
    } catch (MissingWebViewPackageException e) {
        // original exception.
        try {
            return (Class<WebViewFactoryProvider>) Class.forName(NULL_WEBVIEW_FACTORY);
        } catch (ClassNotFoundException e2) {
        // Ignore.
        }
        Log.e(LOGTAG, "Chromium WebView package does not exist", e);
        throw new AndroidRuntimeException(e);
    }
}
Also used : Context(android.content.Context) AndroidRuntimeException(android.util.AndroidRuntimeException) Application(android.app.Application)

Aggregations

Application (android.app.Application)125 Test (org.junit.Test)27 Context (android.content.Context)24 Activity (android.app.Activity)23 ApplicationInfo (android.content.pm.ApplicationInfo)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 File (java.io.File)10 MvpPresenter (com.hannesdorfmann.mosby3.mvp.MvpPresenter)8 Before (org.junit.Before)8 ShadowApplication (org.robolectric.shadows.ShadowApplication)8 MvpView (com.hannesdorfmann.mosby3.mvp.MvpView)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 PrintWriter (java.io.PrintWriter)6 Date (java.util.Date)6 SuppressLint (android.annotation.SuppressLint)5 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 RemoteException (android.os.RemoteException)5 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 AndroidRuntimeException (android.util.AndroidRuntimeException)5