Search in sources :

Example 1 with PowerMenuService

use of com.farmerbb.taskbar.service.PowerMenuService in project Taskbar by farmerbb.

the class U method sendAccessibilityAction.

public static void sendAccessibilityAction(Context context, int action, Runnable onComplete) {
    setComponentEnabled(context, PowerMenuService.class, true);
    boolean isAccessibilityServiceEnabled = isAccessibilityServiceEnabled(context);
    if (!isAccessibilityServiceEnabled && hasWriteSecureSettingsPermission(context)) {
        String services = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        String finalServices = services == null ? "" : services;
        String powerMenuService = new ComponentName(context, PowerMenuService.class).flattenToString();
        if (!finalServices.contains(powerMenuService)) {
            try {
                Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, finalServices.isEmpty() ? powerMenuService : finalServices + ":" + powerMenuService);
            } catch (Exception ignored) {
            }
        }
        newHandler().postDelayed(() -> {
            Intent intent = new Intent(ACTION_ACCESSIBILITY_ACTION);
            intent.putExtra(EXTRA_ACTION, action);
            sendBroadcast(context, intent);
            try {
                Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, finalServices);
            } catch (Exception ignored) {
            }
            if (onComplete != null)
                onComplete.run();
        }, 100);
    } else if (isAccessibilityServiceEnabled) {
        Intent intent = new Intent(ACTION_ACCESSIBILITY_ACTION);
        intent.putExtra(EXTRA_ACTION, action);
        sendBroadcast(context, intent);
        if (onComplete != null)
            onComplete.run();
    } else {
        launchApp(context, () -> {
            Intent intent = new Intent(context, DummyActivity.class);
            intent.putExtra("accessibility", true);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            try {
                context.startActivity(intent, getActivityOptionsBundle(context, ApplicationType.APP_PORTRAIT, null));
            } catch (IllegalArgumentException | SecurityException ignored) {
            }
        });
    }
}
Also used : DummyActivity(com.farmerbb.taskbar.activity.DummyActivity) ComponentName(android.content.ComponentName) Intent(android.content.Intent) PowerMenuService(com.farmerbb.taskbar.service.PowerMenuService) ActivityNotFoundException(android.content.ActivityNotFoundException) IOException(java.io.IOException)

Aggregations

ActivityNotFoundException (android.content.ActivityNotFoundException)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 DummyActivity (com.farmerbb.taskbar.activity.DummyActivity)1 PowerMenuService (com.farmerbb.taskbar.service.PowerMenuService)1 IOException (java.io.IOException)1