Java Code Examples for android.content.pm.PackageManager#setComponentEnabledSetting()

The following examples show how to use android.content.pm.PackageManager#setComponentEnabledSetting() . 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: Preferences.java    From XInstaller with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    String preferenceKey = preference.getKey();
    if (Common.PREF_ENABLE_APP_ICON.equals(preferenceKey)) {
        PackageManager packageManager = context.getPackageManager();
        int state = (Boolean) newValue ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        String settings = Common.PACKAGE_NAME + ".Settings";
        ComponentName alias = new ComponentName(context, settings);
        packageManager.setComponentEnabledSetting(alias, state,
                PackageManager.DONT_KILL_APP);
        return true;
    }
    else if (Common.PREF_ENABLE_EXPERT_MODE.equals(preferenceKey)
            || Common.PREF_APP_LOCALE.equals(preferenceKey)) {
        recreateApp();
        return true;
    }
    return true;
}
 
Example 2
Source File: OpenAtlas.java    From ACDD with MIT License 6 votes vote down vote up
public void enableComponent(String componentName) {
    PackageLite packageLite = DelegateComponent.getPackage(componentName);
    if (packageLite != null && packageLite.disableComponents != null) {
        for (String disableComponent : packageLite.disableComponents) {
            PackageManager packageManager = RuntimeVariables.androidApplication
                    .getPackageManager();
            ComponentName componentName2 = new ComponentName(
                    RuntimeVariables.androidApplication.getPackageName(),
                    disableComponent);
            try {
                packageManager.setComponentEnabledSetting(componentName2, 1,
                        1);
                log.debug("enableComponent: "
                        + componentName2.getClassName());
            } catch (Exception e) {
                log.error("enableComponent error: "
                        + componentName2.getClassName() + e.getMessage());
            }
        }
    }
}
 
Example 3
Source File: GetAwayNotificationListenerService.java    From timecat with Apache License 2.0 6 votes vote down vote up
public static boolean enableNotificationListenerComponent(Context paramContext, boolean paramBoolean) {
    try {
        if (Build.VERSION.SDK_INT < 18) {
            return false;
        }
        if (paramContext != null) {
            PackageManager localPackageManager = paramContext.getPackageManager();
            ComponentName componentName = new ComponentName(paramContext.getPackageName(), GetAwayNotificationListenerService.class.getName());
            if (paramBoolean) {
                localPackageManager.setComponentEnabledSetting(componentName, 1, 1);
            } else {
                localPackageManager.setComponentEnabledSetting(componentName, 2, 1);
            }
        }
        return true;
    } catch (Throwable e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 4
Source File: MainActivity.java    From island with Apache License 2.0 6 votes vote down vote up
private void onCreateInProfile() {
	final DevicePolicies policies = new DevicePolicies(this);
	if (! policies.invoke(DevicePolicyManager::isAdminActive)) {
		Analytics.$().event("inactive_device_admin").send();
		startActivity(new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
				.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, DeviceAdmins.getComponentName(this))
				.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.dialog_reactivate_message)));
		return;
	}
	final PackageManager pm = getPackageManager();
	final List<ResolveInfo> resolve = pm.queryBroadcastReceivers(new Intent(Intent.ACTION_USER_INITIALIZE).setPackage(Modules.MODULE_ENGINE), 0);
	if (! resolve.isEmpty()) {
		Log.w(TAG, "Manual provisioning is pending, resume it now.");
		Analytics.$().event("profile_post_provision_pending").send();
		final ActivityInfo receiver = resolve.get(0).activityInfo;
		sendBroadcast(new Intent().setComponent(new ComponentName(receiver.packageName, receiver.name)));
	} else {    // Receiver disabled but launcher entrance is left enabled. The best bet is just disabling the launcher entrance. No provisioning attempt any more.
		Log.w(TAG, "Manual provisioning is finished, but launcher activity is still left enabled. Disable it now.");
		Analytics.$().event("profile_post_provision_activity_leftover").send();
		pm.setComponentEnabledSetting(new ComponentName(this, getClass()), COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP);
	}
}
 
Example 5
Source File: public_func.java    From telegram-sms with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static void start_service(Context context, Boolean battery_switch, Boolean chat_command_switch) {
    Intent battery_service = new Intent(context, battery_service.class);
    Intent chat_long_polling_service = new Intent(context, chat_command_service.class);
    if (is_notify_listener(context)) {
        Log.d("start_service", "start_service: ");
        ComponentName thisComponent = new ComponentName(context, notification_listener_service.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (battery_switch) {
            context.startForegroundService(battery_service);
        }
        if (chat_command_switch) {
            context.startForegroundService(chat_long_polling_service);
        }
    } else {//Service activation after O
        if (battery_switch) {
            context.startService(battery_service);
        }
        if (chat_command_switch) {
            context.startService(chat_long_polling_service);
        }
    }
}
 
Example 6
Source File: FakeLauncherActivity.java    From BaldPhone with Apache License 2.0 5 votes vote down vote up
public static void resetPreferredLauncherAndOpenChooser(Context context) {
    final PackageManager packageManager = context.getPackageManager();
    final ComponentName componentName = new ComponentName(context, FakeLauncherActivity.class);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    context.startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}
 
Example 7
Source File: SkillUtils.java    From Easer with GNU General Public License v3.0 5 votes vote down vote up
public static void reenableComponent(Context context, Class cls) {
    PackageManager pm = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, cls);

    pm.setComponentEnabledSetting(componentName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

    pm.setComponentEnabledSetting(componentName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
 
Example 8
Source File: LeakCanaryInternals.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
public static void setEnabledBlocking(Context appContext, Class<?> componentClass,
                                      boolean enabled) {
  ComponentName component = new ComponentName(appContext, componentClass);
  PackageManager packageManager = appContext.getPackageManager();
  int newState = enabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED;
  // Blocks on IPC.
  packageManager.setComponentEnabledSetting(component, newState, DONT_KILL_APP);
}
 
Example 9
Source File: MainActivity.java    From xpay with Apache License 2.0 5 votes vote down vote up
/** 退出調用
 * 功能 Disable掉 NotificationService 直接退出App
 * */
private void disableNotificationService(){
    // 先disable 服务
    PackageManager localPackageManager = getPackageManager();
    localPackageManager.setComponentEnabledSetting(new ComponentName(this, NotificationMonitorService.class),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);// 最后一个参数 DONT_KILL_APP或者0。 0说明杀死包含该组件的app
    //
}
 
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: LLandActivity.java    From heads-up with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onDestroy() {
    super.onDestroy();
    if (isFirstRun) {
        PackageManager packageManager = getPackageManager();
        final ComponentName componentName = new ComponentName(getApplicationContext(),
                "codes.simen.l50notifications.ui.LLandActivitySys");
        packageManager.setComponentEnabledSetting(componentName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
    }
}
 
Example 12
Source File: VisibilityManager.java    From droid-stealth with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method makes the class visible
 *
 * @param context the context to use for this operation
 * @param toShow  the class to show
 */
public static void showClass(Context context, Class<?> toShow, int flag) {
	PackageManager pm = context.getPackageManager();
	ComponentName homeName = new ComponentName(context, toShow);
	if (pm != null) {
		// make sure activity can be called
		pm.setComponentEnabledSetting(
				homeName,
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
				flag);
	}
}
 
Example 13
Source File: MainActivity.java    From XposedManyMoney with GNU General Public License v2.0 5 votes vote down vote up
public void hideLauncherIcon(boolean isHide) {
    PackageManager packageManager = this.getPackageManager();
    int hide = isHide ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
            : PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    packageManager.setComponentEnabledSetting(getAliasComponentName(),
            hide, PackageManager.DONT_KILL_APP);
}
 
Example 14
Source File: AlarmReceiver.java    From LibreAlarm with GNU General Public License v3.0 5 votes vote down vote up
public static void start(Context context) {
    ComponentName receiver = new ComponentName(context, AlarmReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    post(context);
}
 
Example 15
Source File: MainActivity.java    From fuckView with GNU Affero General Public License v3.0 5 votes vote down vote up
private void changeIcon(String activityPath) {
    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(getComponentName(),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    pm.setComponentEnabledSetting(new ComponentName(this, activityPath),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

}
 
Example 16
Source File: ActivityUtils.java    From openlauncher with Apache License 2.0 5 votes vote down vote up
public ActivityUtils setLauncherActivityEnabled(Class activityClass, boolean enable) {
    Context context = _context.getApplicationContext();
    PackageManager pkg = context.getPackageManager();
    ComponentName component = new ComponentName(context, activityClass);
    pkg.setComponentEnabledSetting(component, enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    return this;
}
 
Example 17
Source File: VisibilityManager.java    From droid-stealth with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method hides the given class
 *
 * @param context the context to use for this operation
 * @param toHide  the class to hide
 */
public static void hideClass(Context context, Class<?> toHide, int flag) {
	PackageManager pm = context.getPackageManager();
	ComponentName homeName = new ComponentName(context, toHide);
	if (pm != null
			&& pm.getComponentEnabledSetting(homeName) == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
		pm.setComponentEnabledSetting(homeName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, flag);
	}
}
 
Example 18
Source File: PropUtil.java    From LocationReportEnabler with GNU General Public License v3.0 5 votes vote down vote up
public static void hideOrShowLauncher(Context context, boolean isHide) {
    PackageManager p = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, SettingActivity.class);
    if (isHide) {
        if (p.getComponentEnabledSetting(componentName) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
            p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
            Log.d("PropUtil", "Hide the icon.");
        }
    } else {
        if (p.getComponentEnabledSetting(componentName) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
            p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
            Log.d("PropUtil", "Show the icon.");
        }
    }
}
 
Example 19
Source File: NotificationListenerService.java    From QuickLyric with GNU General Public License v3.0 4 votes vote down vote up
private void disableNotificationListenerService() {
    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(getApplicationContext(), NotificationListenerService.class),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
 
Example 20
Source File: ReceiverUtils.java    From JobSchedulerCompat with Apache License 2.0 4 votes vote down vote up
public static <T extends BroadcastReceiver> void enable(Context context, Class<T> receiverClass) {
    ComponentName receiver = new ComponentName(context, receiverClass);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP);
}