OSDN Git Service

Use framework toasts on Android 7.1+ instead of 8.0+
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / util / U.java
index 9e11be6..febae19 100644 (file)
@@ -34,9 +34,9 @@ import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ShortcutInfo;
 import android.content.res.Configuration;
+import android.graphics.Color;
 import android.graphics.Rect;
 import android.graphics.drawable.BitmapDrawable;
-import android.hardware.display.DisplayManager;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
@@ -69,8 +69,6 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
 
-import moe.banana.support.ToastCompat;
-
 public class U {
 
     private U() {}
@@ -187,14 +185,20 @@ public class U {
     public static void showToast(Context context, String message, int length) {
         cancelToast();
 
-        ToastCompat toast = ToastCompat.makeText(context.getApplicationContext(), message, length);
+        ToastInterface toast;
+        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1
+                || BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID))
+            toast = new ToastFrameworkImpl(context, message, length);
+        else
+            toast = new ToastCompatImpl(context, message, length);
+
         toast.show();
 
         ToastHelper.getInstance().setLastToast(toast);
     }
 
-    public static void cancelToast() {
-        ToastCompat toast = ToastHelper.getInstance().getLastToast();
+    private static void cancelToast() {
+        ToastInterface toast = ToastHelper.getInstance().getLastToast();
         if(toast != null) toast.cancel();
     }
 
@@ -244,7 +248,8 @@ public class U {
         SharedPreferences pref = getSharedPreferences(context);
         FreeformHackHelper helper = FreeformHackHelper.getInstance();
 
-        boolean specialLaunch = isOPreview() && FreeformHackHelper.getInstance().isInFreeformWorkspace()
+        boolean specialLaunch = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
+                && FreeformHackHelper.getInstance().isInFreeformWorkspace()
                 && MenuHelper.getInstance().isContextMenuOpen();
 
         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
@@ -296,8 +301,7 @@ public class U {
         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 
         if(FreeformHackHelper.getInstance().isInFreeformWorkspace()
-                && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1
-                && !isOPreview())
+                && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1)
             intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
 
         if(launchedFromTaskbar) {
@@ -361,7 +365,7 @@ public class U {
                     context.startActivity(intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
-                } catch (IllegalArgumentException e) { /* Gracefully fail */ }
+                } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
             } else
                 launchAndroidForWork(context, intent.getComponent(), bundle, userId);
         } else
@@ -371,13 +375,12 @@ public class U {
     @SuppressWarnings("deprecation")
     @TargetApi(Build.VERSION_CODES.N)
     private static void launchMode1(Context context, Intent intent, long userId, ShortcutInfo shortcut, ApplicationType type) {
-        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
-        Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
+        DisplayMetrics metrics = getRealDisplayMetrics(context);
 
-        int width1 = display.getWidth() / 8;
-        int width2 = display.getWidth() - width1;
-        int height1 = display.getHeight() / 8;
-        int height2 = display.getHeight() - height1;
+        int width1 = metrics.widthPixels / 8;
+        int width2 = metrics.widthPixels - width1;
+        int height1 = metrics.heightPixels / 8;
+        int height2 = metrics.heightPixels - height1;
 
         Bundle bundle = getActivityOptions(type).setLaunchBounds(new Rect(
                 width1,
@@ -393,7 +396,7 @@ public class U {
                     context.startActivity(intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
-                } catch (IllegalArgumentException e) { /* Gracefully fail */ }
+                } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
             } else
                 launchAndroidForWork(context, intent.getComponent(), bundle, userId);
         } else
@@ -403,9 +406,8 @@ public class U {
     @SuppressWarnings("deprecation")
     @TargetApi(Build.VERSION_CODES.N)
     private static void launchMode2(Context context, Intent intent, int launchType, long userId, ShortcutInfo shortcut, ApplicationType type) {
-        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
-        Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
-
+        DisplayMetrics metrics = getRealDisplayMetrics(context);
+        
         int statusBarHeight = getStatusBarHeight(context);
         String position = getTaskbarPosition(context);
 
@@ -413,20 +415,20 @@ public class U {
         boolean isLandscape = context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
 
         int left = launchType == RIGHT && isLandscape
-                ? display.getWidth() / 2
+                ? metrics.widthPixels / 2
                 : 0;
 
         int top = launchType == RIGHT && isPortrait
-                ? display.getHeight() / 2
+                ? metrics.heightPixels / 2
                 : statusBarHeight;
 
         int right = launchType == LEFT && isLandscape
-                ? display.getWidth() / 2
-                : display.getWidth();
+                ? metrics.widthPixels / 2
+                : metrics.widthPixels;
 
         int bottom = launchType == LEFT && isPortrait
-                ? display.getHeight() / 2
-                : display.getHeight();
+                ? metrics.heightPixels / 2
+                : metrics.heightPixels;
 
         int iconSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size);
 
@@ -454,7 +456,7 @@ public class U {
                     context.startActivity(intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
-                } catch (IllegalArgumentException e) { /* Gracefully fail */ }
+                } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
             } else
                 launchAndroidForWork(context, intent.getComponent(), bundle, userId);
         } else
@@ -464,12 +466,11 @@ public class U {
     @SuppressWarnings("deprecation")
     @TargetApi(Build.VERSION_CODES.N)
     private static void launchMode3(Context context, Intent intent, long userId, ShortcutInfo shortcut, ApplicationType type) {
-        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
-        Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
+        DisplayMetrics metrics = getRealDisplayMetrics(context);
 
-        int width1 = display.getWidth() / 2;
+        int width1 = metrics.widthPixels / 2;
         int width2 = context.getResources().getDimensionPixelSize(R.dimen.phone_size_width) / 2;
-        int height1 = display.getHeight() / 2;
+        int height1 = metrics.heightPixels / 2;
         int height2 = context.getResources().getDimensionPixelSize(R.dimen.phone_size_height) / 2;
 
         Bundle bundle = getActivityOptions(type).setLaunchBounds(new Rect(
@@ -486,7 +487,7 @@ public class U {
                     context.startActivity(intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
-                } catch (IllegalArgumentException e) { /* Gracefully fail */ }
+                } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
             } else
                 launchAndroidForWork(context, intent.getComponent(), bundle, userId);
         } else
@@ -523,37 +524,38 @@ public class U {
     @SuppressWarnings("deprecation")
     @TargetApi(Build.VERSION_CODES.N)
     public static void launchAppLowerRight(Context context, Intent intent) {
-        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
-        Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
+        DisplayMetrics metrics = getRealDisplayMetrics(context);
         try {
             context.startActivity(intent, getActivityOptions(ApplicationType.FREEFORM_HACK).setLaunchBounds(new Rect(
-                    display.getWidth(),
-                    display.getHeight(),
-                    display.getWidth() + 1,
-                    display.getHeight() + 1
+                    metrics.widthPixels,
+                    metrics.heightPixels,
+                    metrics.widthPixels + 1,
+                    metrics.heightPixels + 1
             )).toBundle());
-        } catch (IllegalArgumentException e) { /* Gracefully fail */ }
+        } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
     }
 
     public static void checkForUpdates(Context context) {
-        if(!BuildConfig.DEBUG) {
-            String url;
-            try {
-                context.getPackageManager().getPackageInfo("com.android.vending", 0);
-                url = "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID;
-            } catch (PackageManager.NameNotFoundException e) {
-                url = "https://f-droid.org/repository/browse/?fdid=" + BuildConfig.BASE_APPLICATION_ID;
-            }
+        if(!BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID)) {
+            if(!BuildConfig.DEBUG) {
+                String url;
+                try {
+                    context.getPackageManager().getPackageInfo("com.android.vending", 0);
+                    url = "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID;
+                } catch (PackageManager.NameNotFoundException e) {
+                    url = "https://f-droid.org/repository/browse/?fdid=" + BuildConfig.BASE_APPLICATION_ID;
+                }
 
-            Intent intent = new Intent(Intent.ACTION_VIEW);
-            intent.setData(Uri.parse(url));
-            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                Intent intent = new Intent(Intent.ACTION_VIEW);
+                intent.setData(Uri.parse(url));
+                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 
-            try {
-                context.startActivity(intent);
-            } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
-        } else
-            showToast(context, R.string.debug_build);
+                try {
+                    context.startActivity(intent);
+                } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
+            } else
+                showToast(context, R.string.debug_build);
+        }
     }
 
     public static boolean launcherIsDefault(Context context) {
@@ -681,7 +683,7 @@ public class U {
 
     private static int getMaxNumOfColumns(Context context) {
         SharedPreferences pref = getSharedPreferences(context);
-        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
+        DisplayMetrics metrics = getRealDisplayMetrics(context);
         float baseTaskbarSize = getBaseTaskbarSizeFloat(context) / metrics.density;
         int numOfColumns = 0;
 
@@ -814,7 +816,7 @@ public class U {
     }
 
     public static boolean hasPartialFreeformSupport() {
-         return Build.MANUFACTURER.equalsIgnoreCase("Samsung") || isOPreview();
+         return Build.MANUFACTURER.equalsIgnoreCase("Samsung");
     }
 
     public static boolean isServiceRunning(Context context, Class<? extends Service> cls) {
@@ -839,7 +841,7 @@ public class U {
             SharedPreferences.Editor editor = pref.edit();
 
             if(!pref.getBoolean("show_background", true))
-                editor.putInt("background_tint", 0).apply();
+                editor.putInt("background_tint", Color.TRANSPARENT).apply();
 
             editor.remove("show_background");
             editor.apply();
@@ -890,7 +892,8 @@ public class U {
                 stackId = FREEFORM_WORKSPACE_STACK_ID;
                 break;
             case CONTEXT_MENU:
-                if(isOPreview()) stackId = FULLSCREEN_WORKSPACE_STACK_ID;
+                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+                    stackId = FULLSCREEN_WORKSPACE_STACK_ID;
         }
 
         if(stackId != null) {
@@ -917,9 +920,8 @@ public class U {
         }
     }
 
-    @TargetApi(Build.VERSION_CODES.M)
-    public static boolean isOPreview() {
-        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) || (Build.VERSION.RELEASE.equals("O") && Build.VERSION.PREVIEW_SDK_INT > 0);
+    public static boolean isChromeOs(Context context) {
+        return context.getPackageManager().hasSystemFeature("org.chromium.arc");
     }
 
     public static boolean hasSupportLibrary(Context context) {
@@ -1025,6 +1027,7 @@ public class U {
             pref.edit().putString("start_menu_layout", "grid").apply();
         }
 
+        // Enable freeform hack automatically on supported devices
         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             if(!pref.getBoolean("freeform_hack_override", false)) {
                 pref.edit()
@@ -1038,5 +1041,39 @@ public class U {
                 LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.FINISH_FREEFORM_ACTIVITY"));
             }
         }
+
+        // Customizations for Android-x86 devices (non-Bliss)
+        if(BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID)
+                && isSystemApp(context)
+                && !pref.getBoolean("android_x86_prefs", false)) {
+            pref.edit()
+                    .putString("recents_amount", "running_apps_only")
+                    .putString("refresh_frequency", "0")
+                    .putString("max_num_of_recents", "2147483647")
+                    .putBoolean("full_length", true)
+                    .putBoolean("dashboard", true)
+                    .putBoolean("android_x86_prefs", true)
+                    .apply();
+        }
+    }
+
+    public static DisplayMetrics getRealDisplayMetrics(Context context) {
+        DisplayMetrics metrics = new DisplayMetrics();
+        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
+        Display disp = wm.getDefaultDisplay();
+
+        SharedPreferences pref = getSharedPreferences(context);
+        if(isChromeOs(context) && !pref.getBoolean("chrome_os_context_menu_fix", false))
+            disp.getRealMetrics(metrics);
+        else
+            disp.getMetrics(metrics);
+
+        return metrics;
+    }
+
+    public static int getOverlayType() {
+        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
+                ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
+                : WindowManager.LayoutParams.TYPE_PHONE;
     }
 }