@Override public Context getBaseContext() { Context base = super.getBaseContext(); if (applicationLike != null) { return (Context) invokeAppLikeGetBaseContext(applicationLike, base); } return base; }
private void fixWeChatRecovery(Application app) { try { Field field = app.getClassLoader().loadClass("com.tencent.recovery.Recovery").getField("context"); field.setAccessible(true); if (field.get(null) != null) { return; } field.set(null, app.getBaseContext()); } catch (Throwable e) { e.printStackTrace(); } }
@Override public Context getBaseContext() { mCondom.logConcern(TAG, "Application.getBaseContext"); return super.getBaseContext(); }
/** * @deprecated Do not depend on this method to override services as it will be removed in a future * update. The preferered method is use the shadow of the corresponding service. */ @Deprecated public void setSystemService(String key, Object service) { ShadowContextImpl shadowContext = Shadow.extract(realApplication.getBaseContext()); shadowContext.setSystemService(key, service); }
private BackupAgentController<T> attach() { if (attached) { return this; } Context baseContext = RuntimeEnvironment.application.getBaseContext(); ReflectionHelpers.callInstanceMethod(BackupAgent.class, component, "attach", ReflectionHelpers.ClassParameter.from(Context.class, baseContext)); return this; }
/** * Create and register {@link ContentProvider} using {@link ProviderInfo} found from manifest. */ public ContentProviderController<T> create() { Context baseContext = RuntimeEnvironment.application.getBaseContext(); ComponentName componentName = createRelative(baseContext.getPackageName(), contentProvider.getClass().getName()); ProviderInfo providerInfo = null; try { providerInfo = baseContext .getPackageManager() .getProviderInfo(componentName, PackageManager.MATCH_DISABLED_COMPONENTS); } catch (PackageManager.NameNotFoundException e) { Logger.strict("Unable to find provider info for " + componentName, e); } return create(providerInfo); }
private ActivityController<T> attach( @Nullable @WithType("android.app.Activity$NonConfigurationInstances") Object lastNonConfigurationInstances) { if (attached) { return this; } // make sure the component is enabled Context context = RuntimeEnvironment.application.getBaseContext(); context .getPackageManager() .setComponentEnabledSetting( new ComponentName(context.getPackageName(), component.getClass().getName()), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); ShadowActivity shadowActivity = Shadow.extract(component); shadowActivity.callAttach(getIntent(), lastNonConfigurationInstances); attached = true; return this; }
/** * Create and register {@link ContentProvider} using the given {@link ProviderInfo}. * * @param providerInfo the {@link ProviderInfo} to use * @return this {@link ContentProviderController} */ public ContentProviderController<T> create(ProviderInfo providerInfo) { Context baseContext = RuntimeEnvironment.application.getBaseContext(); // make sure the component is enabled ComponentName componentName = createRelative(baseContext.getPackageName(), contentProvider.getClass().getName()); baseContext .getPackageManager() .setComponentEnabledSetting( componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); contentProvider.attachInfo(baseContext, providerInfo); if (providerInfo != null) { ShadowContentResolver.registerProviderInternal(providerInfo.authority, contentProvider); } return this; }
private ServiceController<T> attach() { if (attached) { return this; } // make sure the component is enabled Context context = RuntimeEnvironment.application.getBaseContext(); ComponentName name = new ComponentName(context.getPackageName(), component.getClass().getName()); context .getPackageManager() .setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); ReflectionHelpers.callInstanceMethod(Service.class, component, "attach", from(Context.class, RuntimeEnvironment.application.getBaseContext()), from(ActivityThread.class, null), from(String.class, component.getClass().getSimpleName()), from(IBinder.class, null), from(Application.class, RuntimeEnvironment.application), from(Object.class, null)); attached = true; return this; }
private IntentServiceController<T> attach() { if (attached) { return this; } // make sure the component is enabled Context context = RuntimeEnvironment.application.getBaseContext(); ComponentName name = new ComponentName(context.getPackageName(), component.getClass().getName()); context .getPackageManager() .setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); ReflectionHelpers.callInstanceMethod(Service.class, component, "attach", from(Context.class, RuntimeEnvironment.application.getBaseContext()), from(ActivityThread.class, null), from(String.class, component.getClass().getSimpleName()), from(IBinder.class, null), from(Application.class, RuntimeEnvironment.application), from(Object.class, null)); attached = true; return this; }
@Test public void testApplicationContextAsBaseContext() throws Exception { final Context context = InstrumentationRegistry.getTargetContext(); final Context app_context = context.getApplicationContext(); assertTrue(app_context instanceof Application); final CondomContext condom_context = CondomContext.wrap(app_context, TAG); final Context condom_app_context = condom_context.getApplicationContext(); assertTrue(condom_app_context instanceof Application); assertEquals(condom_context, ((Application) condom_app_context).getBaseContext()); assertTrue(((Application) condom_app_context).getBaseContext() instanceof CondomContext); }
@Test public void testApplicationAsApplicationContextOfBaseContext() throws Exception { final Context context = InstrumentationRegistry.getTargetContext(); final Context app_context = context.getApplicationContext(); assertTrue(app_context instanceof Application); final CondomContext condom_context = CondomContext.wrap(context, TAG); final Context condom_app_context = condom_context.getApplicationContext(); assertTrue(condom_app_context instanceof Application); assertNotSame(app_context, condom_app_context); assertTrue(((Application) condom_app_context).getBaseContext() instanceof CondomContext); }
Object lastNonConfigurationInstances) { Application application = RuntimeEnvironment.application; Context baseContext = application.getBaseContext();
@Test public void testSet24HourMode_12() { ShadowSettings.set24HourTimeFormat(false); assertThat(DateFormat.is24HourFormat(context.getBaseContext())).isFalse(); }
@Test public void testSet24HourMode_24() { ShadowSettings.set24HourTimeFormat(true); assertThat(DateFormat.is24HourFormat(context.getBaseContext())).isTrue(); }
private void handleReceiver(ReceiverData data) { BroadcastReceiver.PendingResult result = data.resultData.build(); try { if (!isBound()) { bindApplication(data.component.getPackageName(), data.processName); } Context context = mInitialApplication.getBaseContext(); Context receiverContext = ContextImpl.getReceiverRestrictedContext.call(context); String className = data.component.getClassName(); BroadcastReceiver receiver = (BroadcastReceiver) context.getClassLoader().loadClass(className).newInstance(); mirror.android.content.BroadcastReceiver.setPendingResult.call(receiver, result); data.intent.setExtrasClassLoader(context.getClassLoader()); if (data.intent.getComponent() == null) { data.intent.setComponent(data.component); } receiver.onReceive(receiverContext, data.intent); if (mirror.android.content.BroadcastReceiver.getPendingResult.call(receiver) != null) { result.finish(); } } catch (Exception e) { // must be this for misjudge of anti-virus!! throw new RuntimeException(String.format("Unable to start receiver: %s ", data.component), e); } VActivityManager.get().broadcastFinish(data.resultData); }
@Test public void getRunningAppProcesses_shouldReturnProcessList() { final ActivityManager activityManager = getActivityManager(); final ActivityManager.RunningAppProcessInfo process1 = buildProcessInfo(new ComponentName("org.robolectric", "Process 1")); final ActivityManager.RunningAppProcessInfo process2 = buildProcessInfo(new ComponentName("org.robolectric", "Process 2")); assertThat(activityManager.getRunningAppProcesses().size()).isEqualTo(1); ActivityManager.RunningAppProcessInfo myInfo = activityManager.getRunningAppProcesses().get(0); assertThat(myInfo.pid).isEqualTo(android.os.Process.myPid()); assertThat(myInfo.uid).isEqualTo(android.os.Process.myUid()); assertThat(myInfo.processName) .isEqualTo( ((Application) ApplicationProvider.getApplicationContext()) .getBaseContext() .getPackageName()); shadowOf(activityManager).setProcesses(Lists.newArrayList(process1, process2)); assertThat(activityManager.getRunningAppProcesses()).containsExactly(process1, process2); }
@Test public void shouldSetBaseContext() throws Exception { MyBackupAgent myBackupAgent = backupAgentController.get(); assertThat(myBackupAgent.getBaseContext()) .isEqualTo(((Application) ApplicationProvider.getApplicationContext()).getBaseContext()); }
@Test public void shouldSetBaseContext() throws Exception { TestContentProvider1 myContentProvider = controller.create().get(); assertThat(myContentProvider.getContext()) .isEqualTo(((Application) ApplicationProvider.getApplicationContext()).getBaseContext()); }
@Test public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception { Looper mainLooper = Looper.getMainLooper(); Scheduler scheduler = shadowOf(mainLooper).getScheduler(); shadowOf(mainLooper).quit = true; assertThat(ApplicationProvider.getApplicationContext().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, ((Application) ApplicationProvider.getApplicationContext()).getBaseContext())); assertThat(Looper.getMainLooper()).named("Looper.getMainLooper()").isSameAs(mainLooper); assertThat(application.getMainLooper()).named("app.getMainLooper()").isSameAs(mainLooper); assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isNotSameAs(scheduler); assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isSameAs(s); assertThat(shadowOf(mainLooper).hasQuit()).named("quit").isFalse(); }