Java Code Examples for android.app.Application#getResources()

The following examples show how to use android.app.Application#getResources() . 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: Atlas.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * Init the framework
 * 
 * @param application
 * @return
 * @throws Exception
 */
public void init(Application application,boolean reset) throws AssertionArrayException, Exception {
    if(application==null){
        throw new RuntimeException("application is null,atlas init failed!");
    }
    ApplicationInfo app_info = application.getApplicationInfo();
    sAPKSource = app_info.sourceDir;
    RuntimeVariables.androidApplication = application;
    RuntimeVariables.delegateResources  = application.getResources();
    Framework.containerVersion = RuntimeVariables.sInstalledVersionName;
    ClassLoader cl = Atlas.class.getClassLoader();
    Framework.systemClassLoader = cl;
    // defineAndVerify
    String packageName = application.getPackageName();

    RuntimeVariables.delegateClassLoader = Atlas.class.getClassLoader();

    frameworkLifecycleHandler = new FrameworkLifecycleHandler();
    Framework.frameworkListeners.add(frameworkLifecycleHandler);

}
 
Example 2
Source File: PluginCreator.java    From PluginLoader with Apache License 2.0 6 votes vote down vote up
/**
 * 未使用
 */
/* package */static Resources createPluginResourceFor5(Application application, String absolutePluginApkPath) {
	try {
		AssetManager assetMgr = AssetManager.class.newInstance();
		Method addAssetPaths = AssetManager.class.getDeclaredMethod("addAssetPaths", String[].class);

		String[] assetPaths = new String[2];

		// 不可更改顺序否则不能兼容4.x
		assetPaths[0] = absolutePluginApkPath;
		assetPaths[1] = application.getApplicationInfo().sourceDir;

		addAssetPaths.invoke(assetMgr, new Object[] { assetPaths });

		Resources mainRes = application.getResources();
		Resources pluginRes = new PluginResourceWrapper(assetMgr, mainRes.getDisplayMetrics(),
				mainRes.getConfiguration());

		PaLog.d("create Plugin Resource from: ", assetPaths[0], assetPaths[1]);

		return pluginRes;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 3
Source File: AndroidHack.java    From AtlasForAndroid with MIT License 6 votes vote down vote up
public static Object createNewLoadedApk(Application application, Object obj) {
    try {
        Method declaredMethod;
        ApplicationInfo applicationInfo = application.getPackageManager().getApplicationInfo(application.getPackageName(), 1152);
        application.getPackageManager();
        Resources resources = application.getResources();
        if (resources instanceof DelegateResources) {
            declaredMethod = resources.getClass().getSuperclass().getDeclaredMethod("getCompatibilityInfo", new Class[0]);
        } else {
            declaredMethod = resources.getClass().getDeclaredMethod("getCompatibilityInfo", new Class[0]);
        }
        declaredMethod.setAccessible(true);
        Class cls = Class.forName("android.content.res.CompatibilityInfo");
        Object invoke = declaredMethod.invoke(application.getResources(), new Object[0]);
        Method declaredMethod2 = AtlasHacks.ActivityThread.getmClass().getDeclaredMethod("getPackageInfoNoCheck", new Class[]{ApplicationInfo.class, cls});
        declaredMethod2.setAccessible(true);
        invoke = declaredMethod2.invoke(obj, new Object[]{applicationInfo, invoke});
        _mLoadedApk = invoke;
        return invoke;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: Atlas.java    From AtlasForAndroid with MIT License 6 votes vote down vote up
public void init(Application application, Properties properties) throws AssertionArrayException, Exception {
    String packageName = application.getPackageName();
    AtlasHacks.defineAndVerify();
    ClassLoader classLoader = Atlas.class.getClassLoader();
    ClassLoader delegateClassLoader = new DelegateClassLoader(classLoader);
    Framework.systemClassLoader = classLoader;
    RuntimeVariables.delegateClassLoader = delegateClassLoader;
    RuntimeVariables.delegateResources = application.getResources();
    RuntimeVariables.androidApplication = application;
    AndroidHack.injectClassLoader(packageName, delegateClassLoader);
    AndroidHack.injectInstrumentationHook(new InstrumentationHook(AndroidHack.getInstrumentation(), application.getBaseContext()));
    injectApplication(application, packageName);
    this.bundleLifecycleHandler = new BundleLifecycleHandler();
    Framework.syncBundleListeners.add(this.bundleLifecycleHandler);
    this.frameworkLifecycleHandler = new FrameworkLifecycleHandler();
    Framework.frameworkListeners.add(this.frameworkLifecycleHandler);
    AndroidHack.hackH();
    Framework.initialize(properties);
}
 
Example 5
Source File: GodEyeInitContentProvider.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreate() {
    Application application = (Application) Objects.requireNonNull(this.getContext()).getApplicationContext();
    Resources resources = application.getResources();
    GodEye.instance().internalInit(application);
    if (!resources.getBoolean(R.bool.android_god_eye_manual_install)) {
        GodEye.instance().install(GodEyeConfig.fromAssets(resources.getString(R.string.android_god_eye_install_assets_path)), resources.getBoolean(R.bool.android_god_eye_need_notification));
        GodEyeHelper.startMonitor(resources.getInteger(R.integer.android_god_eye_monitor_port));
    }
    return false;
}
 
Example 6
Source File: ExternalPrerenderHandler.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Provides an estimate of the contents size.
 *
 * The estimate is likely to be incorrect. This is not a problem, as the aim
 * is to avoid getting a different layout and resources than needed at
 * render time.
 * @param application The application to use for getting resources.
 * @param convertToDp Whether the value should be converted to dp from pixels.
 * @return The estimated prerender size in pixels or dp.
 */
public static Rect estimateContentSize(Application application, boolean convertToDp) {
    // The size is estimated as:
    // X = screenSizeX
    // Y = screenSizeY - top bar - bottom bar - custom tabs bar
    // The bounds rectangle includes the bottom bar and the custom tabs bar as well.
    Rect screenBounds = new Rect();
    Point screenSize = new Point();
    WindowManager wm = (WindowManager) application.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getSize(screenSize);
    Resources resources = application.getResources();
    int statusBarId = resources.getIdentifier("status_bar_height", "dimen", "android");
    try {
        screenSize.y -= resources.getDimensionPixelSize(statusBarId);
    } catch (Resources.NotFoundException e) {
        // Nothing, this is just a best effort estimate.
    }
    screenBounds.set(0,
            resources.getDimensionPixelSize(R.dimen.custom_tabs_control_container_height),
            screenSize.x, screenSize.y);

    if (convertToDp) {
        float density = resources.getDisplayMetrics().density;
        screenBounds.top = (int) Math.ceil(screenBounds.top / density);
        screenBounds.left = (int) Math.ceil(screenBounds.left / density);
        screenBounds.right = (int) Math.ceil(screenBounds.right / density);
        screenBounds.bottom = (int) Math.ceil(screenBounds.bottom / density);
    }
    return screenBounds;
}
 
Example 7
Source File: PluginCreator.java    From PluginLoader with Apache License 2.0 5 votes vote down vote up
/**
 * 根据插件apk文件,创建插件资源文件,同时绑定宿主程序的资源,这样就可以在插件中使用宿主程序的资源。
 * 
 * @param application
 *            宿主程序的Application
 * @param absolutePluginApkPath
 *            插件apk文件路径
 * @return
 */
public static Resources createPluginResource(Application application, String absolutePluginApkPath,
		boolean isStandalone) {
	try {
		// 如果是第三方编译的独立插件的话,插件的资源id默认是0x7f开头。
		// 但是宿主程序的资源id必须使用默认值0x7f(否则在5.x系统上主题会由问题)
		// 插件运行时可能会通过getActivityInfo等
		// 会拿到到PluginStubActivity的ActivityInfo以及ApplicationInfo
		// 这两个info里面有部分资源id是在宿主程序的Manifest中配置的,比如logo和icon
		// 所有如果在独立插件中尝试通过Context获取上述这些资源会导致异常
		String[] assetPaths = buildAssetPath(isStandalone, application.getApplicationInfo().sourceDir,
				absolutePluginApkPath);
		AssetManager assetMgr = AssetManager.class.newInstance();
		RefInvoker.invokeMethod(assetMgr, AssetManager.class.getName(), "addAssetPaths",
				new Class[] { String[].class }, new Object[] { assetPaths });
		// Method addAssetPaths =
		// AssetManager.class.getDeclaredMethod("addAssetPaths",
		// String[].class);
		// addAssetPaths.invoke(assetMgr, new Object[] { assetPaths });

		Resources mainRes = application.getResources();
		Resources pluginRes = new PluginResourceWrapper(assetMgr, mainRes.getDisplayMetrics(),
				mainRes.getConfiguration());

		return pluginRes;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 8
Source File: AndroidHack.java    From ACDD with MIT License 5 votes vote down vote up
public static Object createNewLoadedApk(Application application, Object obj) {
    try {
        Method declaredMethod;
        ApplicationInfo applicationInfo = application.getPackageManager()
                .getApplicationInfo(application.getPackageName(), 1152);
        application.getPackageManager();
        Resources resources = application.getResources();
        if (resources instanceof DelegateResources) {
            declaredMethod = resources
                    .getClass()
                    .getSuperclass()
                    .getDeclaredMethod("getCompatibilityInfo");
        } else {
            declaredMethod = resources.getClass().getDeclaredMethod(
                    "getCompatibilityInfo");
        }
        declaredMethod.setAccessible(true);
        Class cls = Class.forName("android.content.res.CompatibilityInfo");
        Object invoke = declaredMethod.invoke(application.getResources()
        );
        Method declaredMethod2 = OpenAtlasHacks.ActivityThread.getmClass()
                .getDeclaredMethod("getPackageInfoNoCheck",
                        ApplicationInfo.class, cls);
        declaredMethod2.setAccessible(true);
        invoke = declaredMethod2.invoke(obj, applicationInfo, invoke);
        _mLoadedApk = invoke;
        return invoke;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: OpenAtlas.java    From ACDD with MIT License 5 votes vote down vote up
/**
 *@since 1.0.0
 * **/
private Resources initResources(Application application) throws Exception {
    Resources resources = application.getResources();
    if (resources != null) {
        return resources;
    }
    log.error(" !!! Failed to get init resources.");
    return application.getPackageManager().getResourcesForApplication(application.getApplicationInfo());
}
 
Example 10
Source File: ExternalPrerenderHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Provides an estimate of the contents size.
 *
 * The estimate is likely to be incorrect. This is not a problem, as the aim
 * is to avoid getting a different layout and resources than needed at
 * render time.
 * @param application The application to use for getting resources.
 * @param convertToDp Whether the value should be converted to dp from pixels.
 * @return The estimated prerender size in pixels or dp.
 */
public static Rect estimateContentSize(Application application, boolean convertToDp) {
    // The size is estimated as:
    // X = screenSizeX
    // Y = screenSizeY - top bar - bottom bar - custom tabs bar
    // The bounds rectangle includes the bottom bar and the custom tabs bar as well.
    Rect screenBounds = new Rect();
    Point screenSize = new Point();
    WindowManager wm = (WindowManager) application.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getSize(screenSize);
    Resources resources = application.getResources();
    int statusBarId = resources.getIdentifier("status_bar_height", "dimen", "android");
    try {
        screenSize.y -= resources.getDimensionPixelSize(statusBarId);
    } catch (Resources.NotFoundException e) {
        // Nothing, this is just a best effort estimate.
    }
    screenBounds.set(0,
            resources.getDimensionPixelSize(R.dimen.custom_tabs_control_container_height),
            screenSize.x, screenSize.y);

    if (convertToDp) {
        float density = resources.getDisplayMetrics().density;
        screenBounds.top = (int) Math.ceil(screenBounds.top / density);
        screenBounds.left = (int) Math.ceil(screenBounds.left / density);
        screenBounds.right = (int) Math.ceil(screenBounds.right / density);
        screenBounds.bottom = (int) Math.ceil(screenBounds.bottom / density);
    }
    return screenBounds;
}
 
Example 11
Source File: ApplicationModule.java    From eternity with Apache License 2.0 4 votes vote down vote up
@Provides
static StringResolver resolver(Application application) {
  return new StringResolver(application.getResources());
}
 
Example 12
Source File: AndroidModule.java    From fogger with Apache License 2.0 4 votes vote down vote up
@Provides
Resources provideResources(Application application) {
    return application.getResources();
}