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 0f33074..febae19 100644 (file)
@@ -37,7 +37,6 @@ 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;
@@ -187,7 +186,8 @@ public class U {
         cancelToast();
 
         ToastInterface toast;
-        if(BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID))
+        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);
@@ -248,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
@@ -300,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) {
@@ -365,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
@@ -375,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,
@@ -397,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
@@ -407,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);
 
@@ -417,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);
 
@@ -458,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
@@ -468,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(
@@ -490,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
@@ -527,16 +524,15 @@ 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) {
@@ -687,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;
 
@@ -896,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) {
@@ -923,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) {
@@ -1060,4 +1056,24 @@ public class U {
                     .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;
+    }
 }