Java Code Examples for android.content.Context#createPackageContext()

The following examples show how to use android.content.Context#createPackageContext() . 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: DeleteNonRequiredAppsTask.java    From island with Apache License 2.0 6 votes vote down vote up
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.
    }
}
 
Example 2
Source File: XmlModel.java    From opentasks with Apache License 2.0 6 votes vote down vote up
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);
    }

}
 
Example 3
Source File: WiFiKeyView.java    From WiFiKeyView with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: TrustActivity.java    From trust with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: PreferenceUtil.java    From apollo-DuerOS with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 6
Source File: VClientImpl.java    From container with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 7
Source File: GooglePlayServicesUtil.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
public static Context getRemoteContext(Context context) {
    try {
        return context.createPackageContext("com.google.android.gms", 3);
    } catch (NameNotFoundException localNameNotFoundException) {
    }
    return null;
}
 
Example 8
Source File: AppContext.java    From TigerVideo with Apache License 2.0 5 votes vote down vote up
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);
            }
        }
    }
 
Example 9
Source File: NotificationUtils.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
@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;
    }
}
 
Example 10
Source File: RemoteViewsFixer.java    From container with GNU General Public License v3.0 5 votes vote down vote up
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
	}
}
 
Example 11
Source File: RemoteViewsUtils.java    From container with GNU General Public License v3.0 5 votes vote down vote up
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
	}
}
 
Example 12
Source File: NotificationHandler.java    From container with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: ColorChooseDialog.java    From BiliRoaming with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
}
 
Example 14
Source File: Utils.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
public static synchronized Context getGbContext(Context context) throws Throwable {
    if (mGbContext == null) {
        mGbContext = context.createPackageContext(GravityBox.PACKAGE_NAME,
                Context.CONTEXT_IGNORE_SECURITY);
    }
    return mGbContext;
}
 
Example 15
Source File: InitInjector.java    From fuckView with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 根据包名构建目标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;
}
 
Example 16
Source File: HookLoader.java    From ZhihuXposed with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 根据包名构建目标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;
}
 
Example 17
Source File: ApplicationContextWrapper.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 18
Source File: IconPackHelper.java    From FastAccess with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 19
Source File: RemoteHandler.java    From AppOpsX with MIT License 4 votes vote down vote up
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;
}