/** * Install the condom protection for current process if it is not the default process. * * <p>This method must be called in {@link Application#onCreate()} to eliminate potential leakage. */ public static void installExceptDefaultProcess(final Application app) { installExceptDefaultProcess(app, new CondomOptions()); }
/** * Install the condom protection for current process if its process name matches. This method should be called in {@link Application#onCreate()}. * * @param process_names list of processes where Condom process should NOT be installed, in the form exactly as defined * by <code>"android:process"</code> attribute of components in <code>AndroidManifest.xml</code>. * <b>BEWARE: Default process must be explicitly listed here if it is expected to be excluded.</b> */ public static void installExcept(final Application app, final CondomOptions options, final String... process_names) { if (process_names.length == 0) throw new IllegalArgumentException("At lease one process name must be specified"); validateCondomOptions(options); final String current_process_name = getProcessName(app); if (current_process_name == null) return; for (final String process_name : process_names) if (! current_process_name.equals(getFullProcessName(app, process_name))) { install(app, current_process_name, options); return; } if ((app.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) validateProcessNames(app, process_names); }
private static void install(final Application app, final String process_name_or_tag, final CondomOptions options) { final int pos_colon = process_name_or_tag.indexOf(':'); final String tag = pos_colon > 0 ? process_name_or_tag.substring(pos_colon + 1) : process_name_or_tag; FULL_TAG = "Condom:" + tag; TAG = CondomCore.asLogTag(FULL_TAG); final CondomCore condom = new CondomCore(app, options, TAG); try { installCondomProcessActivityManager(condom); installCondomProcessPackageManager(condom); Log.d(TAG, "Global condom is installed in current process"); } catch (final Exception e) { condom.logConcern(TAG_INCOMPATIBILITY, e.getMessage()); Log.e(TAG, "Error installing global condom in current process", e); } }
/** * Install the condom protection for current process if it is not the default process. * * <p>This method must be called in {@link Application#onCreate()} to eliminate potential leakage. */ public static void installExceptDefaultProcess(final Application app, final CondomOptions options) { validateCondomOptions(options); final String current_process_name = getProcessName(app); if (current_process_name == null) return; final String default_process_name = app.getApplicationInfo().processName; if (! current_process_name.equals(default_process_name)) install(app, current_process_name, options); }
private static void doValidateProcessNames(final Application app, final String[] process_names) { try { final PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), GET_ACTIVITIES | GET_SERVICES | GET_RECEIVERS | GET_PROVIDERS); final Set<String> defined_process_names = new HashSet<>(); if (info.activities != null) for (final ActivityInfo activity : info.activities) defined_process_names.add(activity.processName); if (info.services != null) for (final ServiceInfo service : info.services) defined_process_names.add(service.processName); if (info.receivers != null) for (final ActivityInfo receiver : info.receivers) defined_process_names.add(receiver.processName); if (info.providers != null) for (final ProviderInfo provider : info.providers) defined_process_names.add(provider.processName); for (final String process_name : process_names) if (! defined_process_names.contains(getFullProcessName(app, process_name))) throw new IllegalArgumentException("Process name \"" + process_name + "\" is not used by any component in AndroidManifest.xml"); } catch (final PackageManager.NameNotFoundException ignored) {} // Should never happen }
/** * Install the condom protection for current process. This method should generally never be called in the main process of app. * * <p>It is suggested to use {@link android.content.ContentProvider ContentProvider} with "android:process", to install it in specified process. */ public static void installInCurrentProcess(final Application app, final String tag, final CondomOptions options) { install(app, tag, options); }
@BeforeClass public static void checkInstallation() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException { try { CondomProcess.installExcept(((Application) InstrumentationRegistry.getTargetContext().getApplicationContext()), new CondomOptions().addKit(new NullDeviceIdKit()), ""); fail("CondomKit is incompatible with CondomProcess"); } catch (final IllegalArgumentException ignored) {} // Install in default process intentionally, since test cases cannot run in secondary process. CondomProcess.installExcept(((Application) InstrumentationRegistry.getTargetContext().getApplicationContext()), new CondomOptions(), ""); // Check IActivityManager proxy @SuppressLint("PrivateApi") final Object am_proxy = Class.forName("android.app.ActivityManagerNative").getMethod("getDefault").invoke(null); assertTrue(Proxy.isProxyClass(am_proxy.getClass())); sCondomProcessActivityManager = (CondomProcess.CondomProcessActivityManager) Proxy.getInvocationHandler(am_proxy); assertEquals(CondomProcess.CondomProcessActivityManager.class, sCondomProcessActivityManager.getClass()); // Check IPackageManager proxy final PackageManager pm = context().getPackageManager(); assertEquals("android.app.ApplicationPackageManager", pm.getClass().getName()); final Field ApplicationPackageManager_mPm = pm.getClass().getDeclaredField("mPM"); ApplicationPackageManager_mPm.setAccessible(true); final Object pm_proxy = ApplicationPackageManager_mPm.get(pm); assertTrue(Proxy.isProxyClass(pm_proxy.getClass())); sCondomProcessPackageManager = (CondomProcess.CondomProcessPackageManager) Proxy.getInvocationHandler(pm_proxy); assertEquals(CondomProcess.CondomProcessPackageManager.class, sCondomProcessPackageManager.getClass()); }
/** * Install the condom protection for current process if it is not the default process. * * <p>This method must be called in {@link Application#onCreate()} to eliminate potential leakage. */ public static void installExceptDefaultProcess(final Application app, final CondomOptions options) { validateCondomOptions(options); final String current_process_name = getProcessName(app); if (current_process_name == null) return; final String default_process_name = app.getApplicationInfo().processName; if (! current_process_name.equals(default_process_name)) install(app, current_process_name, options); }
private static void doValidateProcessNames(final Application app, final String[] process_names) { try { final PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), GET_ACTIVITIES | GET_SERVICES | GET_RECEIVERS | GET_PROVIDERS); final Set<String> defined_process_names = new HashSet<>(); if (info.activities != null) for (final ActivityInfo activity : info.activities) defined_process_names.add(activity.processName); if (info.services != null) for (final ServiceInfo service : info.services) defined_process_names.add(service.processName); if (info.receivers != null) for (final ActivityInfo receiver : info.receivers) defined_process_names.add(receiver.processName); if (info.providers != null) for (final ProviderInfo provider : info.providers) defined_process_names.add(provider.processName); for (final String process_name : process_names) if (! defined_process_names.contains(getFullProcessName(app, process_name))) throw new IllegalArgumentException("Process name \"" + process_name + "\" is not used by any component in AndroidManifest.xml"); } catch (final PackageManager.NameNotFoundException ignored) {} // Should never happen }
/** * Install the condom protection for current process if its process name matches. This method should be called in {@link Application#onCreate()}. * * @param process_names list of processes where Condom process should NOT be installed, in the form exactly as defined * by <code>"android:process"</code> attribute of components in <code>AndroidManifest.xml</code>. * <b>BEWARE: Default process must be explicitly listed here if it is expected to be excluded.</b> */ public static void installExcept(final Application app, final CondomOptions options, final String... process_names) { if (process_names.length == 0) throw new IllegalArgumentException("At lease one process name must be specified"); validateCondomOptions(options); final String current_process_name = getProcessName(app); if (current_process_name == null) return; for (final String process_name : process_names) if (! current_process_name.equals(getFullProcessName(app, process_name))) { install(app, current_process_name, options); return; } if ((app.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) validateProcessNames(app, process_names); }
private static void install(final Application app, final String current_process_name, final CondomOptions options) { final int pos_colon = current_process_name.indexOf(':'); final String tag = pos_colon > 0 ? current_process_name.substring(pos_colon) : current_process_name; FULL_TAG = "Condom:" + tag; TAG = CondomCore.asLogTag(FULL_TAG); final CondomCore condom = new CondomCore(app, options); try { installCondomProcessActivityManager(condom); installCondomProcessPackageManager(condom); Log.d(TAG, "Global condom is installed in current process"); } catch (final Exception e) { condom.logConcern(TAG_INCOMPATIBILITY, e.getMessage()); Log.e(TAG, "Error installing global condom in current process", e); } }
private static void installCondomProcess(final Context context, final CondomOptions options) { CondomProcess.installExceptDefaultProcess((Application) context.getApplicationContext(), options); }
/** * Install the condom protection for current process if it is not the default process. * * <p>This method must be called in {@link Application#onCreate()} to eliminate potential leakage. */ public static void installExceptDefaultProcess(final Application app) { installExceptDefaultProcess(app, new CondomOptions()); }