Java Code Examples for android.app.Activity#getBaseContext()

The following examples show how to use android.app.Activity#getBaseContext() . 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: DialogUtils.java    From AndroidRipper with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Checks that the specified DecorView and the Activity DecorView are not equal.
 * 
 * @param activity the activity which DecorView is to be compared
 * @param decorView the DecorView to compare
 * @return true if not equal
 */

private boolean isDialog(Activity activity, View decorView){
	if(decorView == null || !decorView.isShown() || activity == null){
		return false;
	}
	Context viewContext = null;
	if(decorView != null){
		viewContext = decorView.getContext();
	}
	
	if (viewContext instanceof ContextThemeWrapper) {
		ContextThemeWrapper ctw = (ContextThemeWrapper) viewContext;
		viewContext = ctw.getBaseContext();
	}
	Context activityContext = activity;
	Context activityBaseContext = activity.getBaseContext();
	return (activityContext.equals(viewContext) || activityBaseContext.equals(viewContext)) && (decorView != activity.getWindow().getDecorView());
}
 
Example 2
Source File: PluginThemeHelper.java    From PluginLoader with Apache License 2.0 6 votes vote down vote up
/**
 * Used by host for skin
 * 宿主程序使用插件主题
 */
public static void applyPluginTheme(Activity activity, String pluginId, int themeResId) {

	if (!(activity.getBaseContext() instanceof PluginContextTheme)) {
		PluginDescriptor pd = PluginLoader.getPluginDescriptorByPluginId(pluginId);
		if (pd != null) {

			//插件可能尚未初始化,确保使用前已经初始化
			PluginLoader.ensurePluginInited(pd);

			//注入插件上下文和主题
			Context defaultContext = pd.getPluginContext();
			Context pluginContext = PluginLoader.getNewPluginComponentContext(defaultContext, activity.getBaseContext());
			PluginInjector.resetActivityContext(pluginContext, activity, themeResId);
		}
	}

}
 
Example 3
Source File: TaskListFragment.java    From opentasks with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);

    mTwoPaneLayout = activity.getResources().getBoolean(R.bool.has_two_panes);

    mAuthority = AuthorityUtil.taskAuthority(activity);

    mAppContext = activity.getBaseContext();

    // Activities containing this fragment must implement its callbacks.
    if (!(activity instanceof Callbacks))
    {
        throw new IllegalStateException("Activity must implement fragment's callbacks.");
    }

    mCallbacks = (Callbacks) activity;

    // load accounts early
    Sources.loadModelAsync(activity, TaskContract.LOCAL_ACCOUNT_TYPE, this);
}
 
Example 4
Source File: Dialog.java    From PocketMaps with MIT License 6 votes vote down vote up
public static void showAutoSelectMapSelector(Activity activity)
{
  AlertDialog.Builder builder1 = new AlertDialog.Builder(activity);
  builder1.setTitle(R.string.autoselect_map);
  builder1.setCancelable(true);
  final CheckBox cb = new CheckBox(activity.getBaseContext());
  cb.setChecked(Variable.getVariable().getAutoSelectMap());
  cb.setText(R.string.autoselect_map_text);
  builder1.setView(cb);
  OnClickListener listener = new OnClickListener()
  {
    @Override
    public void onClick(DialogInterface dialog, int buttonNr)
    {
      Variable.getVariable().setAutoSelectMap(cb.isChecked());
      Variable.getVariable().saveVariables(Variable.VarType.Base);
    }
  };
  builder1.setPositiveButton(R.string.ok, listener);
  AlertDialog alert11 = builder1.create();
  alert11.show();
}
 
Example 5
Source File: SettingsListFragment.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
    mSources = Sources.getInstance(activity);
    mContext = activity.getBaseContext();
    mAuthority = AuthorityUtil.taskAuthority(activity);
}
 
Example 6
Source File: BlurActionBarDrawerToggle.java    From BlurNavigationDrawer with Apache License 2.0 5 votes vote down vote up
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
                                 int drawerImageRes, int openDrawerContentDescRes,
                                 int closeDrawerContentDescRes) {
    super(activity, drawerLayout, drawerImageRes, openDrawerContentDescRes, closeDrawerContentDescRes);
    this.context = activity.getBaseContext();
    this.mDrawerLayout = drawerLayout;
    init();
}
 
Example 7
Source File: BlurActionBarDrawerToggle.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
                                 int drawerImageRes, int openDrawerContentDescRes,
                                 int closeDrawerContentDescRes) {
    super(activity, drawerLayout, drawerImageRes, openDrawerContentDescRes, closeDrawerContentDescRes);
    this.context = activity.getBaseContext();
    this.mDrawerLayout = drawerLayout;
    init();
}
 
Example 8
Source File: BlurActionBarDrawerToggle.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
                                 int drawerImageRes, int openDrawerContentDescRes,
                                 int closeDrawerContentDescRes) {
    super(activity, drawerLayout, drawerImageRes, openDrawerContentDescRes, closeDrawerContentDescRes);
    this.context = activity.getBaseContext();
    this.mDrawerLayout = drawerLayout;
    init();
}
 
Example 9
Source File: PlaceAutocompleteView.java    From android with Apache License 2.0 5 votes vote down vote up
private Intent constructIntent(Activity activity) {
  Intent intent = new Intent(activity.getBaseContext(), PlaceAutocompleteActivity.class);
  intent.putExtra(EXTRA_FILTER, filter);
  intent.putExtra(EXTRA_BOUNDS, bounds);
  intent.putExtra(EXTRA_TEXT, text);
  return intent;
}
 
Example 10
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void callActivityOnCreate(Activity activity, Bundle bundle) {
    if (RuntimeVariables.androidApplication.getPackageName().equals(activity.getPackageName())) {
        ContextImplHook contextImplHook = new ContextImplHook(activity.getBaseContext(), activity.getClass().getClassLoader());
        if (!(AtlasHacks.ContextThemeWrapper_mBase == null || AtlasHacks.ContextThemeWrapper_mBase.getField() == null)) {
            AtlasHacks.ContextThemeWrapper_mBase.set(activity, contextImplHook);
        }
        AtlasHacks.ContextWrapper_mBase.set(activity, contextImplHook);
        if (activity.getClass().getClassLoader() instanceof BundleClassLoader) {
            try {
                ((BundleClassLoader) activity.getClass().getClassLoader()).getBundle().startBundle();
            } catch (BundleException e) {
                log.error(e.getMessage() + " Caused by: ", e.getNestedException());
            }
        }
        Object property = Framework.getProperty("android.taobao.atlas.welcome", "com.taobao.tao.welcome.Welcome");
        if (TextUtils.isEmpty(property)) {
            property = "com.taobao.tao.welcome.Welcome";
        }
        if (activity.getClass().getName().equals(property)) {
            this.mBase.callActivityOnCreate(activity, null);
            return;
        } else {
            this.mBase.callActivityOnCreate(activity, bundle);
            return;
        }
    }
    this.mBase.callActivityOnCreate(activity, bundle);
}
 
Example 11
Source File: InstrumentationHook.java    From ACDD with MIT License 5 votes vote down vote up
private void ensureResourcesInjected(Activity activity) {
    ContextImplHook contextImplHook = new ContextImplHook(activity.getBaseContext(), activity.getClass()
            .getClassLoader());
    if (OpenAtlasHacks.ContextThemeWrapper_mResources != null) {
        try {
            validateActivityResource(activity);
        } catch (Throwable th) {
        }
        OpenAtlasHacks.ContextThemeWrapper_mResources.set(activity, RuntimeVariables.delegateResources);
    }
    if (!(OpenAtlasHacks.ContextThemeWrapper_mBase == null || OpenAtlasHacks.ContextThemeWrapper_mBase.getField() == null)) {
        OpenAtlasHacks.ContextThemeWrapper_mBase.set(activity, contextImplHook);
    }
    OpenAtlasHacks.ContextWrapper_mBase.set(activity, contextImplHook);
}
 
Example 12
Source File: InstrumentationHook.java    From ACDD with MIT License 5 votes vote down vote up
/**
 * Perform calling of an activity's {@link Activity#onCreate}
 * method.  The default implementation simply calls through to that method.
 *
 * @param activity The activity being created.
 * @param icicle The previously frozen state (or null) to pass through to
 *               onCreate().
 */
@Override
public void callActivityOnCreate(Activity activity, Bundle icicle) {
    if (RuntimeVariables.androidApplication.getPackageName().equals(activity.getPackageName())) {
        ContextImplHook contextImplHook = new ContextImplHook(activity.getBaseContext(), activity.getClass()
                .getClassLoader());
        if (!(OpenAtlasHacks.ContextThemeWrapper_mBase == null || OpenAtlasHacks.ContextThemeWrapper_mBase.getField() == null)) {
            OpenAtlasHacks.ContextThemeWrapper_mBase.set(activity, contextImplHook);
        }
        OpenAtlasHacks.ContextWrapper_mBase.set(activity, contextImplHook);
        if (activity.getClass().getClassLoader() instanceof BundleClassLoader) {
            try {
                ((BundleClassLoader) activity.getClass().getClassLoader()).getBundle().startBundle();
            } catch (BundleException e) {
                log.error(e.getMessage() + " Caused by: ", e.getNestedException());
            }
        }
        String property = Framework.getProperty(OpenAtlasInternalConstant.BOOT_ACTIVITY, OpenAtlasInternalConstant.BOOT_ACTIVITY);
        if (TextUtils.isEmpty(property)) {
            property = OpenAtlasInternalConstant.BOOT_ACTIVITY;
        }
        try {
            ensureResourcesInjected(activity);
            this.mBase.callActivityOnCreate(activity, icicle);
            return;
        } catch (Exception e2) {
            if (!e2.toString().contains("android.content.res.Resources")
                    || e2.toString().contains("OutOfMemoryError")) {
                e2.printStackTrace();
            }
            HandleResourceNotFound(activity, icicle, e2);
            return;
        }
    }
    this.mBase.callActivityOnCreate(activity, icicle);
}
 
Example 13
Source File: PluginUtil.java    From VirtualAPK with Apache License 2.0 5 votes vote down vote up
public static void hookActivityResources(Activity activity, String packageName) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isVivo(activity.getResources())) {
        // for 5.0+ vivo
        return;
    }

    // designed for 5.0 - only, but some bad phones not work, eg:letv
    try {
        Context base = activity.getBaseContext();
        final LoadedPlugin plugin = PluginManager.getInstance(activity).getLoadedPlugin(packageName);
        final Resources resources = plugin.getResources();
        if (resources != null) {
            Reflector.with(base).field("mResources").set(resources);

            // copy theme
            Resources.Theme theme = resources.newTheme();
            theme.setTo(activity.getTheme());
            Reflector reflector = Reflector.with(activity);
            int themeResource = reflector.field("mThemeResource").get();
            theme.applyStyle(themeResource, true);
            reflector.field("mTheme").set(theme);

            reflector.field("mResources").set(resources);
        }
    } catch (Exception e) {
        Log.w(Constants.TAG, e);
    }
}
 
Example 14
Source File: VAInstrumentation.java    From VirtualAPK with Apache License 2.0 5 votes vote down vote up
protected void injectActivity(Activity activity) {
    final Intent intent = activity.getIntent();
    if (PluginUtil.isIntentFromPlugin(intent)) {
        Context base = activity.getBaseContext();
        try {
            LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(intent);
            Reflector.with(base).field("mResources").set(plugin.getResources());
            Reflector reflector = Reflector.with(activity);
            reflector.field("mBase").set(plugin.createPluginContext(activity.getBaseContext()));
            reflector.field("mApplication").set(plugin.getApplication());

            // set screenOrientation
            ActivityInfo activityInfo = plugin.getActivityInfo(PluginUtil.getComponent(intent));
            if (activityInfo.screenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
                activity.setRequestedOrientation(activityInfo.screenOrientation);
            }

            // for native activity
            ComponentName component = PluginUtil.getComponent(intent);
            Intent wrapperIntent = new Intent(intent);
            wrapperIntent.setClassName(component.getPackageName(), component.getClassName());
            activity.setIntent(wrapperIntent);
            
        } catch (Exception e) {
            Log.w(TAG, e);
        }
    }
}
 
Example 15
Source File: ActivityWrapper.java    From Neptune with Apache License 2.0 5 votes vote down vote up
public ActivityWrapper(Activity origin, PluginLoadedApk loadedApk) {
    mPluginContext = new PluginContextWrapper(origin.getBaseContext(), loadedApk);
    mOriginActivity = origin;
    if (mOriginActivity instanceof FragmentActivity) {
        mOriginFragmentActivity = (FragmentActivity) mOriginActivity;
    }
    // 将ActivityWrapper的mBase设置为插件的Context
    attachBaseContext(mPluginContext);
}
 
Example 16
Source File: PluginInstrumentionWrapper.java    From Android-Plugin-Framework with MIT License 4 votes vote down vote up
@Override
public void callActivityOnCreate(Activity activity, Bundle icicle) {
	if (icicle != null && icicle.getParcelable("android:support:fragments") != null) {
		if (ProcessUtil.isPluginProcess()) {
			if (AnnotationProcessor.getPluginContainer(activity.getClass()) != null) {
				// 加了注解的Activity正在自动恢复且页面包含了Fragment。直接清除fragment,
				// 防止如果被恢复的fragment来自插件时,在某些情况下会使用宿主的classloader加载插件fragment
				// 导致classnotfound问题
				icicle.clear();
				icicle = null;
			}
		}
	}

	PluginInjector.injectInstrumetionFor360Safe(activity, this);

	PluginInjector.injectActivityContext(activity);

	Intent intent = activity.getIntent();

	if (intent != null) {
		intent.setExtrasClassLoader(activity.getClassLoader());
	}

	if (icicle != null) {
		icicle.setClassLoader(activity.getClassLoader());
	}

	if (ProcessUtil.isPluginProcess()) {

		installPluginViewFactory(activity);

		if (activity instanceof WaitForLoadingPluginActivity) {
			//NOTHING
		} else {
			AndroidWebkitWebViewFactoryProvider.switchWebViewContext(activity);
		}

		if (activity.isChild()) {
			//修正TabActivity中的Activity的ContextImpl的packageName
			Context base = activity.getBaseContext();
			while(base instanceof ContextWrapper) {
				base = ((ContextWrapper)base).getBaseContext();
			}
			if (HackContextImpl.instanceOf(base)) {
				HackContextImpl impl = new HackContextImpl(base);
				String packageName = FairyGlobal.getHostApplication().getPackageName();
				String packageName1 = activity.getPackageName();
				impl.setBasePackageName(packageName);
				impl.setOpPackageName(packageName);
			}
		}
	}

	try {
		real.callActivityOnCreate(activity, icicle);
	} catch (RuntimeException e) {
		throw new RuntimeException(
				" activity : " + activity.getClassLoader() +
				" pluginContainer : " + AnnotationProcessor.getPluginContainer(activity.getClass()) +
				", process : " + ProcessUtil.isPluginProcess(), e);
	}

	monitor.onActivityCreate(activity);

}
 
Example 17
Source File: PluginInjector.java    From PluginLoader with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static void injectActivityContext(Activity activity) {
	Intent intent = activity.getIntent();
	FragmentContainer fragmentContainer = AnnotationProcessor.getFragmentContainer(activity.getClass());
	// 如果是打开插件中的activity, 或者是打开的用来显示插件fragment的宿主activity
	if (fragmentContainer != null || (intent.getComponent() != null
			&& (intent.getComponent().getClassName().startsWith(PluginStubBinding.STUB_ACTIVITY_PRE)))) {
		// 为了不需要重写插件Activity的attachBaseContext方法为:
		// 我们在activityoncreate之前去完成attachBaseContext的事情

		Context pluginContext = null;
		PluginDescriptor pd = null;

		//是打开的用来显示插件fragment的宿主activity
		if (fragmentContainer != null) {
			// 为了能够在宿主中的Activiy里面展示来自插件的普通Fragment,
			// 我们将宿主程序中用来展示插件普通Fragment的Activity的Context也替换掉

			if (!TextUtils.isEmpty(fragmentContainer.pluginId())) {

				pd = PluginLoader.getPluginDescriptorByPluginId(fragmentContainer.pluginId());
				pluginContext = PluginLoader.getNewPluginComponentContext(pd.getPluginContext(), activity.getBaseContext());

			} else if (!TextUtils.isEmpty(fragmentContainer.fragmentId())) {
				String classId = null;
				try {
					classId = activity.getIntent().getStringExtra(fragmentContainer.fragmentId());
				} catch (Exception e) {
					PaLog.printException("这里的Intent如果包含来自插件的VO对象实例," +
							"会产生ClassNotFound异常", e);
				}
				if (classId != null) {
					@SuppressWarnings("rawtypes")
					Class clazz = PluginLoader.loadPluginFragmentClassById(classId);

					pd = PluginLoader.getPluginDescriptorByClassName(clazz.getName());
					pluginContext = PluginLoader.getNewPluginComponentContext(pd.getPluginContext(), activity.getBaseContext());

				} else {
					return;
				}
			} else {
				PaLog.e("FragmentContainer注解至少配置一个参数:pluginId, fragmentId");
				return;
			}

		} else {

			//是打开插件中的activity
			pd = PluginLoader.getPluginDescriptorByClassName(activity.getClass().getName());
			pluginContext = PluginLoader.getNewPluginContext(activity.getClass());

		}

		PluginActivityInfo pluginActivityInfo = pd.getActivityInfos().get(activity.getClass().getName());
		ActivityInfo activityInfo = (ActivityInfo) RefInvoker.getFieldObject(activity, Activity.class.getName(),
				android_app_Activity_mActivityInfo);
		int pluginAppTheme = getPluginTheme(activityInfo, pluginActivityInfo, pd);

		resetActivityContext(pluginContext, activity, pluginAppTheme);

		resetWindowConfig(pluginContext, pd, activity, activityInfo, pluginActivityInfo);

		activity.setTitle(activity.getClass().getName());

	} else {
		// 如果是打开宿主程序的activity,注入一个无害的Context,用来在宿主程序中startService和sendBroadcast时检查打开的对象是否是插件中的对象
		// 插入Context
		Context mainContext = new PluginBaseContextWrapper(activity.getBaseContext());
		RefInvoker.setFieldObject(activity, ContextWrapper.class.getName(), android_content_ContextWrapper_mBase, null);
		RefInvoker.invokeMethod(activity, ContextThemeWrapper.class.getName(), android_content_ContextThemeWrapper_attachBaseContext,
				new Class[]{Context.class}, new Object[]{mainContext});
	}
}
 
Example 18
Source File: BlurActionBarDrawerToggle.java    From BlurNavigationDrawer with Apache License 2.0 4 votes vote down vote up
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
    super(activity, drawerLayout, openDrawerContentDescRes, closeDrawerContentDescRes);
    this.context = activity.getBaseContext();
    this.mDrawerLayout = drawerLayout;
    init();
}
 
Example 19
Source File: BlurActionBarDrawerToggle.java    From BlurNavigationDrawer with Apache License 2.0 4 votes vote down vote up
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
    super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
    this.context = activity.getBaseContext();
    this.mDrawerLayout = drawerLayout;
    init();
}
 
Example 20
Source File: PluginActivityDelegate.java    From Neptune with Apache License 2.0 4 votes vote down vote up
/**
 * 在插件Activity的onCreate调用前调用该方法
 */
void handleActivityOnCreateBefore(Activity activity, Bundle savedInstanceState) {

    if (PluginDebugLog.isDebug()) {
        PluginDebugLog.runtimeLog(TAG, "activity handleActivityOnCreateBefore() is called(): " + activity.getClass().getName());
    }

    if (mPlugin == null) {
        // 通过插件的Intent再去查找一遍
        String pkgName = IntentUtils.parsePkgNameFromActivity(activity);
        if (!TextUtils.isEmpty(pkgName)) {
            mPlugin = PluginManager.getPluginLoadedApkByPkgName(pkgName);
        }
    }

    ClassLoader cl = mPlugin != null ? mPlugin.getPluginClassLoader() : activity.getClassLoader();
    // 修正Intent的ClassLoader,解决序列化的问题
    Intent intent = activity.getIntent();
    intent.setExtrasClassLoader(cl);
    IntentUtils.resetAction(intent); //恢复Intent的Action

    // 对FragmentActivity做特殊处理,不保留Fragment的恢复数据
    if (savedInstanceState != null) {
        savedInstanceState.setClassLoader(cl);
        savedInstanceState.remove(SUPPORT_FRAGMENTS_TAG);
        savedInstanceState.remove(FRAGMENTS_TAG);
    }
    // 替换Application,不然插件内无法监听LifeCycle
    if (mPlugin != null) {
        ReflectionUtils.on(activity).setNoException("mApplication", mPlugin.getPluginApplication());
    }
    // 再次确保Activity的Base Context已经被替换了
    Context mBase = activity.getBaseContext();
    if (mBase instanceof PluginContextWrapper) {
        PluginDebugLog.runtimeLog(TAG, "activity " + activity.getClass().getName() + " base context already be replaced");
    } else if (mPlugin != null) {
        mBase = new PluginContextWrapper(mBase, mPlugin);
        // 反射替换mBase成员变量
        ReflectionUtils.on(activity, ContextWrapper.class).set("mBase", mBase);
        ReflectionUtils.on(activity, ContextThemeWrapper.class).setNoException("mBase", mBase);
    }
    // 给LayoutInflater设置privateFactory
    LayoutInflaterCompat.setPrivateFactory(activity.getLayoutInflater());
    // 修改Activity的ActivityInfo和主题信息
    PluginActivityControl.changeActivityInfo(activity, activity.getClass().getName(), mPlugin);
}