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 c79a3e8..b9663a2 100644 (file)
@@ -15,9 +15,9 @@
 
 package com.farmerbb.taskbar.util;
 
+import android.Manifest;
 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
-import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.app.AlertDialog;
@@ -55,7 +55,6 @@ import android.support.v7.view.ContextThemeWrapper;
 import android.util.DisplayMetrics;
 import android.view.Display;
 import android.view.Surface;
-import android.view.View;
 import android.view.WindowManager;
 import android.widget.Toast;
 
@@ -183,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);
@@ -213,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);
     }
@@ -371,18 +413,20 @@ public class U {
 
         Bundle bundle = getActivityOptionsBundle(context, type, windowSize);
 
-        if(shortcut == null) {
-            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
-            if(userId == userManager.getSerialNumberForUser(Process.myUserHandle())) {
-                try {
-                    startActivity(context, intent, bundle);
-                } catch (ActivityNotFoundException e) {
+        prepareToStartActivity(context, () -> {
+            if(shortcut == null) {
+                UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+                if(userId == userManager.getSerialNumberForUser(Process.myUserHandle())) {
+                    try {
+                        context.startActivity(intent, bundle);
+                    } catch (ActivityNotFoundException e) {
+                        launchAndroidForWork(context, intent.getComponent(), bundle, userId);
+                    } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
+                } else
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
-                } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
             } else
-                launchAndroidForWork(context, intent.getComponent(), bundle, userId);
-        } else
-            launchShortcut(context, shortcut, bundle);
+                launchShortcut(context, shortcut, bundle);
+        });
 
         if(shouldCollapse(context, true))
             LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
@@ -490,24 +534,24 @@ public class U {
         }
     }
 
-    private static void startActivity(Context context, Intent intent, Bundle bundle) {
+    private static void prepareToStartActivity(Context context, Runnable runnable) {
         boolean shouldLaunchTouchAbsorber =
                 !FreeformHackHelper.getInstance().isTouchAbsorberActive()
                         && isOverridingFreeformHack(context)
                         && !isChromeOs(context);
 
         if(!shouldLaunchTouchAbsorber) {
-            context.startActivity(intent, bundle);
+            runnable.run();
             return;
         }
 
         startTouchAbsorberActivity(context);
-        new Handler().postDelayed(() -> context.startActivity(intent, bundle), 100);
+        new Handler().postDelayed(runnable, 100);
     }
 
     public static void startActivityMaximized(Context context, Intent intent) {
         Bundle bundle = launchMode2(context, MAXIMIZED, ApplicationType.CONTEXT_MENU);
-        startActivity(context, intent, bundle);
+        prepareToStartActivity(context, () -> context.startActivity(intent, bundle));
     }
 
     @TargetApi(Build.VERSION_CODES.N)
@@ -881,6 +925,10 @@ public class U {
         return Build.MANUFACTURER.equalsIgnoreCase("Samsung");
     }
 
+    public static boolean isNvidiaDevice() {
+        return Build.MANUFACTURER.equalsIgnoreCase("NVIDIA");
+    }
+
     public static boolean isServiceRunning(Context context, Class<? extends Service> cls) {
         return isServiceRunning(context, cls.getName());
     }
@@ -1197,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);
@@ -1205,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();
         }
 
@@ -1224,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)
@@ -1320,16 +1363,6 @@ public class U {
                 || (!isChromeOs(context) && getCurrentApiVersion() >= 28.0f));
     }
 
-    @SuppressWarnings("unchecked")
-    public static <T extends View> T findViewById(Activity target, int id) {
-        return (T) target.findViewById(id);
-    }
-
-    @SuppressWarnings("unchecked")
-    public static <T extends View> T findViewById(View target, int id) {
-        return (T) target.findViewById(id);
-    }
-
     public static boolean isPlayStoreInstalled(Context context) {
         try {
             context.getPackageManager().getPackageInfo("com.android.vending", 0);
@@ -1349,7 +1382,8 @@ public class U {
     public static boolean hasBrokenSetLaunchBoundsApi() {
         return getCurrentApiVersion() >= 26.0f
                 && getCurrentApiVersion() < 28.0f
-                && !isSamsungDevice();
+                && !isSamsungDevice()
+                && !isNvidiaDevice();
     }
 
     public static String getSecondScreenPackageName(Context context) {