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 d0d61af..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;
@@ -84,17 +83,12 @@ public class U {
     public static final int HIDDEN = 0;
     public static final int TOP_APPS = 1;
 
-    public static final String BASE_APPLICATION_ID = "com.farmerbb.taskbar";
-    public static final String PAID_APPLICATION_ID = "com.farmerbb.taskbar.paid";
-    private static final String SUPPORT_APPLICATION_ID = "com.farmerbb.taskbar.support";
-    public static final String ANDROIDX86_APPLICATION_ID = "com.farmerbb.taskbar.androidx86";
-
     // From android.app.ActivityManager.StackId
     private static final int FULLSCREEN_WORKSPACE_STACK_ID = 1;
     private static final int FREEFORM_WORKSPACE_STACK_ID = 2;
 
     public static SharedPreferences getSharedPreferences(Context context) {
-        if(pref == null) pref = context.getSharedPreferences(context.getPackageName() + "_preferences", Context.MODE_PRIVATE);
+        if(pref == null) pref = context.getSharedPreferences(BuildConfig.APPLICATION_ID + "_preferences", Context.MODE_PRIVATE);
         return pref;
     }
 
@@ -106,7 +100,7 @@ public class U {
                 .setPositiveButton(R.string.action_grant_permission, (dialog, which) -> {
                     try {
                         context.startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
-                                Uri.parse("package:" + context.getPackageName())));
+                                Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
                     } catch (ActivityNotFoundException e) {
                         showErrorDialog(context, "SYSTEM_ALERT_WINDOW");
                     }
@@ -122,7 +116,7 @@ public class U {
     public static void showErrorDialog(final Context context, String appopCmd) {
         AlertDialog.Builder builder = new AlertDialog.Builder(context);
         builder.setTitle(R.string.error_dialog_title)
-                .setMessage(context.getString(R.string.error_dialog_message, context.getPackageName(), appopCmd))
+                .setMessage(context.getString(R.string.error_dialog_message, BuildConfig.APPLICATION_ID, appopCmd))
                 .setPositiveButton(R.string.action_ok, null);
 
         AlertDialog dialog = builder.create();
@@ -192,11 +186,11 @@ public class U {
         cancelToast();
 
         ToastInterface toast;
-        try {
-            toast = new ToastCompatImpl(context.getApplicationContext(), message, length);
-        } catch (Exception e) {
-            toast = new ToastFrameworkImpl(context.getApplicationContext(), message, length);
-        }
+        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();
 
@@ -254,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
@@ -306,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) {
@@ -371,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
@@ -381,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,
@@ -403,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
@@ -413,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);
 
@@ -423,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);
 
@@ -464,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
@@ -474,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(
@@ -496,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
@@ -533,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=" + context.getPackageName();
-            } catch (PackageManager.NameNotFoundException e) {
-                url = "https://f-droid.org/repository/browse/?fdid=" + 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) {
@@ -571,7 +563,7 @@ public class U {
         homeIntent.addCategory(Intent.CATEGORY_HOME);
         ResolveInfo defaultLauncher = context.getPackageManager().resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
 
-        return defaultLauncher.activityInfo.packageName.equals(context.getPackageName());
+        return defaultLauncher.activityInfo.packageName.equals(BuildConfig.APPLICATION_ID);
     }
 
     public static void setCachedRotation(int cachedRotation) {
@@ -691,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;
 
@@ -900,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) {
@@ -919,7 +912,7 @@ public class U {
 
     public static boolean isSystemApp(Context context) {
         try {
-            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
+            ApplicationInfo info = context.getPackageManager().getApplicationInfo(BuildConfig.APPLICATION_ID, 0);
             int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
             return (info.flags & mask) != 0;
         } catch (PackageManager.NameNotFoundException e) {
@@ -927,17 +920,16 @@ 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) {
         PackageManager pm = context.getPackageManager();
         try {
-            pm.getPackageInfo(SUPPORT_APPLICATION_ID, 0);
-            return pm.checkSignatures(SUPPORT_APPLICATION_ID, context.getPackageName()) == PackageManager.SIGNATURE_MATCH
-                    && context.getPackageName().equals(BASE_APPLICATION_ID)
+            pm.getPackageInfo(BuildConfig.SUPPORT_APPLICATION_ID, 0);
+            return pm.checkSignatures(BuildConfig.SUPPORT_APPLICATION_ID, BuildConfig.APPLICATION_ID) == PackageManager.SIGNATURE_MATCH
+                    && BuildConfig.APPLICATION_ID.equals(BuildConfig.BASE_APPLICATION_ID)
                     && isSystemApp(context);
         } catch (PackageManager.NameNotFoundException e) {
             return false;
@@ -1035,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()
@@ -1048,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;
     }
 }