下面列出了android.content.Context#createPackageContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get the Context through the package we are hooking
*
* @param param An MethodHookParam contains the thisObject on which we can call methods
* @return The context of our own package
*/
public static Context getContext(MethodHookParam param) {
Context ret = null;
try {
// Get the current activity
Context wifiSettingsContext = ((Activity) callMethod(param.thisObject, "getActivity"))
.getApplicationContext();
// Create our own Context from the WifiSettings Context
ret = wifiSettingsContext.createPackageContext
("com.whd.wifikeyview", Context.CONTEXT_IGNORE_SECURITY);
// Thrown if the package could not be found
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return ret;
}
public static boolean checkPro(Context currentContext) {
if (currentContext == null)
return false;
Context newProContext;
try {
newProContext = currentContext.createPackageContext("eu.thedarken.trust.pro", 0);
} catch (NameNotFoundException e) {
return false;
}
if (newProContext != null) {
if (currentContext.getPackageManager().checkSignatures(currentContext.getPackageName(), newProContext.getPackageName()) == PackageManager.SIGNATURE_MATCH) {
return true;
}
}
return false;
}
public void init(final Context context) {
if (context == null) {
return;
}
mPreferences = context.getSharedPreferences(CommonParams.CARLIFE_NORMAL_PREFERENCES,
Activity.MODE_PRIVATE);
mEditor = mPreferences.edit();
Context carlifeContext = null;
try {
carlifeContext = context.createPackageContext(context.getPackageName(),
Context.CONTEXT_IGNORE_SECURITY);
mJarPreferences = carlifeContext.getSharedPreferences(
CommonParams.CONNECT_STATUS_SHARED_PREFERENCES, Context.MODE_WORLD_WRITEABLE
| Context.MODE_WORLD_READABLE | Context.MODE_MULTI_PROCESS);
mJarEditor = mJarPreferences.edit();
} catch (Exception e) {
LogUtil.e(TAG, "init jar sp fail");
e.printStackTrace();
}
}
public XmlModel(Context context, AuthenticatorDescription authenticator) throws ModelInflaterException
{
super(context, authenticator.type);
mPackageName = authenticator.packageName;
mPackageManager = context.getPackageManager();
try
{
mModelContext = context.createPackageContext(authenticator.packageName, 0);
AccountManager am = AccountManager.get(context);
mAccountLabel = mModelContext.getString(authenticator.labelId);
}
catch (NameNotFoundException e)
{
throw new ModelInflaterException("No model definition found for package " + mPackageName);
}
}
private static Resources getManagedProvisioningPackageResources(final Context context) {
try {
final Resources self_resources = context.getResources();
final Context target_context = context.createPackageContext(getManagedProvisioningPackageName(context), 0);
final Resources target_resources = target_context.getResources();
return new Resources(target_resources.getAssets(), target_resources.getDisplayMetrics(), target_resources.getConfiguration()) {
@NonNull @Override public String[] getStringArray(final int id) throws NotFoundException {
final String entry_name = self_resources.getResourceEntryName(id);
final int target_res_id = target_resources.getIdentifier(entry_name, "array", target_context.getPackageName());
if (target_res_id == 0) return new String[0]; // Return empty array instead of throwing NotFoundException.
return target_resources.getStringArray(target_res_id);
}
};
} catch (final NameNotFoundException e) {
return context.getResources(); // Fall-back to self, with default resource values.
}
}
private View getView(Context context) {
try {
Context moduleContext = context.createPackageContext(BuildConfig.APPLICATION_ID, Context.CONTEXT_IGNORE_SECURITY);
return View.inflate(moduleContext, R.layout.dialog_color_choose, null);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
private static void loadApplicationResources(Context context, Map<String, String> iconPackResources, String packageName) {
Field[] drawableItems = null;
try {
Context appContext = context.createPackageContext(packageName,
Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
drawableItems = Class.forName(packageName + ".R$drawable",
true, appContext.getClassLoader()).getFields();
} catch (Exception e) {
return;
}
for (Field f : drawableItems) {
String name = f.getName();
String icon = name.toLowerCase();
name = name.replaceAll("_", ".");
iconPackResources.put(name, icon);
int activityIndex = name.lastIndexOf(".");
if (activityIndex <= 0 || activityIndex == name.length() - 1) {
continue;
}
String iconPackage = name.substring(0, activityIndex);
if (TextUtils.isEmpty(iconPackage)) {
continue;
}
iconPackResources.put(iconPackage, icon);
String iconActivity = name.substring(activityIndex + 1);
if (TextUtils.isEmpty(iconActivity)) {
continue;
}
iconPackResources.put(iconPackage + "." + iconActivity, icon);
}
}
public static ApplicationContextWrapper gmsContextWithAttachedApplicationContext(Context applicationContext) {
try {
Context context = applicationContext.createPackageContext(Constants.GMS_PACKAGE_NAME, CONTEXT_INCLUDE_CODE & CONTEXT_IGNORE_SECURITY);
return new ApplicationContextWrapper(context, applicationContext);
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
* @param context context参数
* @param modulePackageName 当前模块包名
* @return return apk file
*/
private File findApkFile(Context context,String modulePackageName){
if (context==null){
return null;
}
try {
Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
String apkPath = moudleContext.getPackageCodePath();
return new File(apkPath);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
private static String getString(int resId, Context context) throws PackageManager.NameNotFoundException {
try {
Context appContext = context.createPackageContext("ml.qingsu.fuckview", Context.CONTEXT_IGNORE_SECURITY);
return appContext.getResources().getString(resId);
} catch (NullPointerException e) {
return null;
}
}
/**
* 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
*
* @param context context参数
* @param modulePackageName 当前模块包名
* @return return apk file
*/
private File findApkFile(Context context, String modulePackageName) {
if (context == null) {
return null;
}
try {
Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
String apkPath = moudleContext.getPackageCodePath();
return new File(apkPath);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static synchronized Context getGbContext(Context context) throws Throwable {
if (mGbContext == null) {
mGbContext = context.createPackageContext(GravityBox.PACKAGE_NAME,
Context.CONTEXT_IGNORE_SECURITY);
}
return mGbContext;
}
private Context createPackageContext(String packageName) {
try {
Context hostContext = VirtualCore.get().getContext();
return hostContext.createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}
private Context getAppContext(Context base, String packageName) {
try {
return base.createPackageContext(packageName,
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
private void init(Context context) {
if (notification_panel_width == 0) {
Context systemUi = null;
try {
systemUi = context.createPackageContext("com.android.systemui", Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
}
if (Build.VERSION.SDK_INT <= 19) {
notification_side_padding = 0;
} else {
notification_side_padding = getDimem(context, systemUi, "notification_side_padding",
R.dimen.notification_side_padding);
}
notification_panel_width = getDimem(context, systemUi, "notification_panel_width",
R.dimen.notification_panel_width);
if (notification_panel_width <= 0) {
notification_panel_width = context.getResources().getDisplayMetrics().widthPixels;
}
notification_min_height = getDimem(context, systemUi, "notification_min_height",
R.dimen.notification_min_height);
// getDimem(context, systemUi, "notification_row_min_height", 0);
// if (notification_min_height == 0) {
// notification_min_height =
// }
notification_max_height = getDimem(context, systemUi, "notification_max_height",
R.dimen.notification_max_height);
notification_mid_height = getDimem(context, systemUi, "notification_mid_height",
R.dimen.notification_mid_height);
notification_padding = getDimem(context, systemUi, "notification_padding", R.dimen.notification_padding);
// notification_collapse_second_card_padding
}
}
private void init(Context context) {
if (init) return;
init = true;
if (notification_panel_width == 0) {
Context systemUi = null;
try {
systemUi = context.createPackageContext(NotificationCompat.SYSTEM_UI_PKG, Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
}
if (Build.VERSION.SDK_INT <= 19) {
notification_side_padding = 0;
} else {
notification_side_padding = getDimem(context, systemUi, "notification_side_padding",
R.dimen.notification_side_padding);
}
notification_panel_width = getDimem(context, systemUi, "notification_panel_width",
R.dimen.notification_panel_width);
if (notification_panel_width <= 0) {
notification_panel_width = context.getResources().getDisplayMetrics().widthPixels;
}
notification_min_height = getDimem(context, systemUi, "notification_min_height",
R.dimen.notification_min_height);
// getDimem(context, systemUi, "notification_row_min_height", 0);
// if (notification_min_height == 0) {
// notification_min_height =
// }
notification_max_height = getDimem(context, systemUi, "notification_max_height",
R.dimen.notification_max_height);
notification_mid_height = getDimem(context, systemUi, "notification_mid_height",
R.dimen.notification_mid_height);
notification_padding = getDimem(context, systemUi, "notification_padding", R.dimen.notification_padding);
// notification_collapse_second_card_padding
}
}
@Nullable
public static Context createContext(@NonNull Context context, @NonNull OpenNotification n) {
try {
return context.createPackageContext(n.getPackageName(), Context.CONTEXT_RESTRICTED);
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Failed to create notification\'s context");
return null;
}
}
public static void init(Context context) {
if (sInstance == null) {
try {
sInstance = new AppContext(context.createPackageContext(context.getPackageName(),
Context.CONTEXT_INCLUDE_CODE));
} catch (NameNotFoundException e) {
throw new RuntimeException(e);
}
}
}
public static Context getRemoteContext(Context context) {
try {
return context.createPackageContext("com.google.android.gms", 3);
} catch (NameNotFoundException localNameNotFoundException) {
}
return null;
}
private CallerResult callClass(ClassCaller caller){
CallerResult result = new CallerResult();
try {
ActivityThread activityThread = ActivityThread.currentActivityThread();
Context context = activityThread.getSystemContext();
Context packageContext = null;
//create or from cache get context
WeakReference<Context> contextWeakReference = sLocalContext.get(caller.getPackageName());
if (contextWeakReference != null && contextWeakReference.get() != null) {
packageContext = contextWeakReference.get();
}
if (packageContext == null) {
packageContext = context.createPackageContext(caller.getPackageName(), Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
sLocalContext.put(caller.getPackageName(), new WeakReference<Context>(packageContext));
}
//load class
Class<?> aClass = sClassCache.get(caller.getClassName());
Constructor<?> aConstructor = sConstructorCache.get(caller.getClassName());
if (aClass == null || aConstructor == null) {
aClass = Class.forName(caller.getClassName(), false, packageContext.getClassLoader());
Class<?> processer=Class.forName(ClassCallerProcessor.class.getName(),false,packageContext.getClassLoader());
if (processer.isAssignableFrom(aClass)) {
sClassCache.put(caller.getClassName(), aClass);
sConstructorCache.put(caller.getClassName(),aClass.getConstructor(Context.class,Context.class,byte[].class));
}else {
throw new ClassCastException("class "+aClass.getName()+" need extends ClassCallerProcessor !");
}
}
//if found class,invoke proxyInvoke method
if (aClass != null) {
Object o = null;
if(aConstructor != null){
o = aConstructor.newInstance(packageContext,context,ParcelableUtil.marshall(LifecycleAgent.serverRunInfo));
}
Object[] params = caller.getParams();
if(params != null){
for (Object param : params) {
if(param instanceof Bundle){
((Bundle) param).setClassLoader(packageContext.getClassLoader());
}
}
}
FLog.log("------new object "+o+" params "+Arrays.toString(params)+" "+aClass);
Object ret = MethodUtils.invokeExactMethod(o, "proxyInvoke", params,new Class[]{Bundle.class});
if (ret != null && ret instanceof Bundle) {
writeResult(result, ret);
} else {
writeResult(result, Bundle.EMPTY);
}
} else {
throw new ClassNotFoundException("not found class " + caller.getClassName() + " in package: " + caller.getPackageName());
}
} catch (Throwable e) {
e.printStackTrace();
FLog.log(e);
result.setThrowable(e);
}
return result;
}