Java Code Examples for android.content.pm.PackageManager#COMPONENT_ENABLED_STATE_ENABLED

The following examples show how to use android.content.pm.PackageManager#COMPONENT_ENABLED_STATE_ENABLED . 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: ApplicationInfoEx.java    From XPrivacy with GNU General Public License v3.0 6 votes vote down vote up
public boolean isFrozen(Context context) {
	if (mFrozen == null) {
		PackageManager pm = context.getPackageManager();
		boolean enabled = false;
		for (ApplicationInfo appInfo : mMapAppInfo.values())
			try {
				int setting = pm.getApplicationEnabledSetting(appInfo.packageName);
				enabled = (enabled || setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
				enabled = (enabled || setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
				if (enabled)
					break;
			} catch (IllegalArgumentException ignored) {
			}
		mFrozen = !enabled;
	}
	return mFrozen;
}
 
Example 2
Source File: SipService.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
private void applyComponentEnablingState(boolean active) {
    int enableState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    if(active && prefsWrapper.getPreferenceBooleanValue(SipConfigManager.INTEGRATE_TEL_PRIVILEGED) ) {
           // Check whether we should register for stock tel: intents
           // Useful for devices without gsm
           enableState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    }
       PackageManager pm = getPackageManager();
       
       ComponentName cmp = new ComponentName(this, "com.csipsimple.ui.PrivilegedOutgoingCallBroadcaster");
       try {
           if (pm.getComponentEnabledSetting(cmp) != enableState) {
               pm.setComponentEnabledSetting(cmp, enableState, PackageManager.DONT_KILL_APP);
           }
       } catch (IllegalArgumentException e) {
           Log.d(THIS_FILE,
                   "Current manifest has no PrivilegedOutgoingCallBroadcaster -- you can ignore this if voluntary", e);
       }
}
 
Example 3
Source File: PackageUtils.java    From Android-Next with Apache License 2.0 5 votes vote down vote up
public static void setComponentState(Context context, Class<?> clazz, boolean enable) {
    ComponentName componentName = new ComponentName(context, clazz);
    PackageManager pm = context.getPackageManager();
    final int oldState = pm.getComponentEnabledSetting(componentName);
    final int newState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    if (newState != oldState) {
        final int flags = PackageManager.DONT_KILL_APP;
        pm.setComponentEnabledSetting(componentName, newState, flags);
    }
}
 
Example 4
Source File: SettingsFragment.java    From HideMockLocation with MIT License 5 votes vote down vote up
private void changeIconView(boolean showIcon) {
    PackageManager packageManager = getActivity().getPackageManager();
    int state = showIcon ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    String alias_package = Common.PACKAGE_NAME + ".MainAlias";
    ComponentName alias = new ComponentName(getActivity(), alias_package);
    packageManager.setComponentEnabledSetting(alias, state,
            PackageManager.DONT_KILL_APP);
}
 
Example 5
Source File: SettingsPresenter.java    From XposedSmsCode with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void hideOrShowLauncherIcon(boolean hide) {
    PackageManager pm = mContext.getPackageManager();
    ComponentName launcherCN = new ComponentName(mContext, Const.HOME_ACTIVITY_ALIAS);
    int state = hide ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED : PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    if (pm.getComponentEnabledSetting(launcherCN) != state) {
        pm.setComponentEnabledSetting(launcherCN, state, PackageManager.DONT_KILL_APP);
    }
}
 
Example 6
Source File: Util.java    From android-host-monitor with Apache License 2.0 5 votes vote down vote up
/**
 * Enables or disables a {@link BroadcastReceiver}.
 * Note: be aware that enabling or disabling a component with DONT_KILL_APP on API 14 or 15
 * will wipe out any ongoing notifications your app has created.
 * http://stackoverflow.com/questions/5624470/enable-and-disable-a-broadcast-receiver
 * @param context application context
 * @param receiver broadcast receiver class to enable or disable
 * @param enabled new status
 */
public static void setBroadcastReceiverEnabled(Context context,
                                               Class<? extends BroadcastReceiver> receiver,
                                               boolean enabled) {
    int newState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                           : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

    Logger.debug(LOG_TAG, (enabled ? "enabling" : "disabling") + " connectivity receiver");

    context.getPackageManager()
           .setComponentEnabledSetting(new ComponentName(context, receiver),
                                       newState, PackageManager.DONT_KILL_APP);
}
 
Example 7
Source File: ComponentUtil.java    From mobile-messaging-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * Enables or disables component
 *
 * @param context        context object
 * @param enabled        desired state
 * @param componentClass class of the component
 * @throws ConfigurationException if desired component is not registered in manifest
 */
public static void setState(Context context, boolean enabled, Class componentClass) {
    int state = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    ComponentName componentName = new ComponentName(context, componentClass);
    try {
        context.getPackageManager().setComponentEnabledSetting(componentName, state, PackageManager.DONT_KILL_APP);
    } catch (Exception e) {
        throw new ConfigurationException(ConfigurationException.Reason.MISSING_REQUIRED_COMPONENT, componentClass.getCanonicalName());
    }
}
 
Example 8
Source File: Util.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
static boolean isEnabled(PackageInfo info, Context context) {
    int setting;
    try {
        PackageManager pm = context.getPackageManager();
        setting = pm.getApplicationEnabledSetting(info.packageName);
    } catch (IllegalArgumentException ex) {
        setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        Log.w(LOG_TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
        return info.applicationInfo.enabled;
    else
        return (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}
 
Example 9
Source File: EnableComponentsTestRule.java    From custom-tabs-client with Apache License 2.0 5 votes vote down vote up
private static void setComponentEnabled(Class clazz, boolean enabled) {
    Context context = InstrumentationRegistry.getContext();
    PackageManager pm = context.getPackageManager();
    ComponentName name = new ComponentName(context, clazz);

    int newState = enabled
            ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    int flags = PackageManager.DONT_KILL_APP;

    if (pm.getComponentEnabledSetting(name) != newState) {
        pm.setComponentEnabledSetting(name, newState, flags);
    }
}
 
Example 10
Source File: BroadcastReceiverUtils.java    From android-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Enable/Disable Broadcast Receiver
 *
 * @param context the context
 * @param brClass the br class
 * @param enabled the enabled
 */
public static void setStateOfReceiver(Context context, Class<?> brClass, boolean enabled) {
    ComponentName receiverName = new ComponentName(context, brClass.getName());
    PackageManager pm = context.getPackageManager();

    int newstate;
    if (enabled) {
        newstate = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    } else {
        newstate = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    }

    pm.setComponentEnabledSetting(receiverName, newstate, PackageManager.DONT_KILL_APP);
}
 
Example 11
Source File: Util.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isEnabled(PackageInfo info, Context context) {
    int setting;
    try {
        PackageManager pm = context.getPackageManager();
        setting = pm.getApplicationEnabledSetting(info.packageName);
    } catch (IllegalArgumentException ex) {
        setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
        return info.applicationInfo.enabled;
    else
        return (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}
 
Example 12
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static String enabledSettingToString(int state) {
    switch (state) {
        case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
            return "default";
        case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
            return "enabled";
        case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
            return "disabled";
        case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER:
            return "disabled-user";
        case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED:
            return "disabled-until-used";
    }
    return "unknown";
}
 
Example 13
Source File: WifiBroadcastReceiver.java    From WiFiAfterConnect with Apache License 2.0 5 votes vote down vote up
public static void setEnabled (Context context, boolean enable) {
	if (context == null)
		return;
	PackageManager pm = context.getPackageManager();
	if (pm == null)
		return;
	ComponentName component = new ComponentName (context, WifiBroadcastReceiver.class);
	int status = pm.getComponentEnabledSetting(component);
	int statusNew = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
	if (status != statusNew)
		pm.setComponentEnabledSetting(component,  statusNew, PackageManager.DONT_KILL_APP);
}
 
Example 14
Source File: EnableComponentsTestRule.java    From android-browser-helper with Apache License 2.0 5 votes vote down vote up
private static void setComponentEnabled(Class clazz, boolean enabled) {
    Context context = InstrumentationRegistry.getContext();
    PackageManager pm = context.getPackageManager();
    ComponentName name = new ComponentName(context, clazz);

    int newState = enabled
            ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    int flags = PackageManager.DONT_KILL_APP;

    if (pm.getComponentEnabledSetting(name) != newState) {
        pm.setComponentEnabledSetting(name, newState, flags);
    }
}
 
Example 15
Source File: PushControllerUtils.java    From MiPushFramework with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isBootReceiverEnable(Context context) {
    return context.getPackageManager()
            .getComponentEnabledSetting(new ComponentName(context,
                    BootReceiver.class)) ==
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}
 
Example 16
Source File: BootReceiver.java    From Maying with Apache License 2.0 4 votes vote down vote up
public static void setEnabled(Context context, boolean enabled) {
    PackageManager pm = context.getPackageManager();
    ComponentName cn = new ComponentName(context, BootReceiver.class);
    int state = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    pm.setComponentEnabledSetting(cn, state, PackageManager.DONT_KILL_APP);
}
 
Example 17
Source File: Settings.java    From android-app with GNU General Public License v3.0 4 votes vote down vote up
public boolean isHandlingHttpScheme() {
    return context.getPackageManager()
            .getComponentEnabledSetting(getHttpSchemeHandlingComponent())
            == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}
 
Example 18
Source File: Application.java    From aard2-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Void doInBackground(Slob[] slobs) {
    Set<String> hosts = new HashSet<String>();
    for (Slob slob : slobs) {
        try {
            String uriValue = slob.getTags().get("uri");
            Uri uri = Uri.parse(uriValue);
            String host = uri.getHost();
            if (host != null) {
                hosts.add(host.toLowerCase());
            }
        }
        catch (Exception ex) {
            Log.w(TAG, ex);
        }
    }

    long t0 = System.currentTimeMillis();
    String packageName = getPackageName();
    try {
        PackageManager pm = getPackageManager();
        PackageInfo p = pm.getPackageInfo(packageName,
                PackageManager.GET_ACTIVITIES | PackageManager.GET_DISABLED_COMPONENTS);
        Log.d(TAG, "Done getting available activities in " + (System.currentTimeMillis() - t0));
        t0 = System.currentTimeMillis();
        for (ActivityInfo activityInfo : p.activities) {
            if (isCancelled()) break;
            if (activityInfo.targetActivity != null) {
                boolean enabled = hosts.contains(activityInfo.name);
                if (enabled) {
                    Log.d(TAG, "Enabling links handling for " + activityInfo.name);
                }
                int setting = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
                pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(), activityInfo.name),
                        setting, PackageManager.DONT_KILL_APP);
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, e);
    }
    Log.d(TAG, "Done enabling activities in " + (System.currentTimeMillis() - t0));
    return null;
}
 
Example 19
Source File: AppUtils.java    From OpenHub with GNU General Public License v3.0 4 votes vote down vote up
public static boolean checkApplicationEnabledSetting(Context context, String packageName) {
    int state = context.getPackageManager().getApplicationEnabledSetting(packageName);
    return state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT ||
            state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}
 
Example 20
Source File: SkillUtils.java    From Easer with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isServiceEnabled(Context context, Class<? extends Service> serviceClass) {
    PackageManager pm = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, serviceClass);
    return pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}