OSDN Git Service

Default to phone-sized windows on Bliss OS and Android-x86
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / util / U.java
index c1f6e71..b9663a2 100644 (file)
@@ -15,6 +15,7 @@
 
 package com.farmerbb.taskbar.util;
 
+import android.Manifest;
 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
 import android.app.ActivityManager;
@@ -181,14 +182,52 @@ public class U {
     }
 
     public static void sendAccessibilityAction(Context context, int action) {
+        sendAccessibilityAction(context, action, null);
+    }
+
+    public static void sendAccessibilityAction(Context context, int action, Runnable onComplete) {
         ComponentName component = new ComponentName(context, PowerMenuService.class);
         context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                 PackageManager.DONT_KILL_APP);
 
-        if(isAccessibilityServiceEnabled(context)) {
+        boolean isAccessibilityServiceEnabled = isAccessibilityServiceEnabled(context);
+
+        if(!isAccessibilityServiceEnabled
+                && hasWriteSecureSettingsPermission(context)) {
+            String notificationServices = Settings.Secure.getString(context.getContentResolver(),
+                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
+
+            String powerMenuService = new ComponentName(context, PowerMenuService.class).flattenToString();
+
+            if(!notificationServices.contains(powerMenuService)) {
+                try {
+                    Settings.Secure.putString(context.getContentResolver(),
+                            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
+                            notificationServices.isEmpty()
+                                    ? powerMenuService
+                                    : notificationServices + ":" + powerMenuService);
+                } catch (Exception e) { /* Gracefully fail */ }
+            }
+
+            new Handler().postDelayed(() -> {
+                Intent intent = new Intent("com.farmerbb.taskbar.ACCESSIBILITY_ACTION");
+                intent.putExtra("action", action);
+                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
+
+                try {
+                    Settings.Secure.putString(context.getContentResolver(),
+                            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
+                            notificationServices);
+                } catch (Exception e) { /* Gracefully fail */ }
+
+                if(onComplete != null) onComplete.run();
+            }, 100);
+        } else if(isAccessibilityServiceEnabled) {
             Intent intent = new Intent("com.farmerbb.taskbar.ACCESSIBILITY_ACTION");
             intent.putExtra("action", action);
             LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
+
+            if(onComplete != null) onComplete.run();
         } else {
             launchApp(context, () -> {
                 Intent intent = new Intent(context, DummyActivity.class);
@@ -211,6 +250,11 @@ public class U {
                 || accessibilityServices.contains(component.flattenToShortString()));
     }
 
+    public static boolean hasWriteSecureSettingsPermission(Context context) {
+        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
+                && context.checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
+    }
+
     public static void showToast(Context context, int message) {
         showToast(context, context.getString(message), Toast.LENGTH_SHORT);
     }
@@ -1201,6 +1245,7 @@ public class U {
             editor.putString("refresh_frequency", "0");
             editor.putString("max_num_of_recents", "2147483647");
             editor.putString("sort_order", "true");
+            editor.putString("window_size", "phone_size");
             editor.putBoolean("full_length", true);
             editor.putBoolean("dashboard", true);
             editor.putBoolean("app_drawer_icon", true);
@@ -1209,13 +1254,6 @@ public class U {
             editor.putBoolean("button_recents", true);
             editor.putBoolean("auto_hide_navbar", true);
             editor.putBoolean("bliss_os_prefs", true);
-
-            try {
-                Settings.Secure.putString(context.getContentResolver(),
-                        Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
-                        new ComponentName(context, PowerMenuService.class).flattenToString());
-            } catch (Exception e) { /* Gracefully fail */ }
-
             editor.apply();
         }
 
@@ -1228,6 +1266,7 @@ public class U {
                     .putString("refresh_frequency", "0")
                     .putString("max_num_of_recents", "2147483647")
                     .putString("sort_order", "true")
+                    .putString("window_size", "phone_size")
                     .putBoolean("full_length", true)
                     .putBoolean("dashboard", true)
                     .putBoolean("android_x86_prefs", true)