OSDN Git Service

Implement lots of improvements to freeform mode on Android Pie
authorBraden Farmer <farmerbb@gmail.com>
Thu, 9 Aug 2018 03:25:37 +0000 (21:25 -0600)
committerBraden Farmer <farmerbb@gmail.com>
Thu, 9 Aug 2018 03:25:37 +0000 (21:25 -0600)
app/src/androidx86/AndroidManifest.xml
app/src/main/java/com/farmerbb/taskbar/activity/ContextMenuActivity.java
app/src/main/java/com/farmerbb/taskbar/activity/HomeActivity.java
app/src/main/java/com/farmerbb/taskbar/activity/InvisibleActivityFreeform.java
app/src/main/java/com/farmerbb/taskbar/activity/KeyboardShortcutActivity.java
app/src/main/java/com/farmerbb/taskbar/activity/TouchAbsorberActivity.java [new file with mode: 0644]
app/src/main/java/com/farmerbb/taskbar/service/DashboardService.java
app/src/main/java/com/farmerbb/taskbar/service/StartMenuService.java
app/src/main/java/com/farmerbb/taskbar/util/FreeformHackHelper.java
app/src/main/java/com/farmerbb/taskbar/util/U.java
app/src/playstore/AndroidManifest.xml

index a80eb03..ce006cb 100644 (file)
         <activity
             android:name=".activity.dark.NavigationBarButtonsActivityDark"
             android:theme="@style/AppTheme.Dialog.Preference.Dark"/>
+        <activity
+            android:name=".activity.TouchAbsorberActivity"
+            android:documentLaunchMode="always"
+            android:excludeFromRecents="true"
+            android:launchMode="singleTask"
+            android:theme="@style/AppTheme.Incognito"/>
 
         <service android:name=".service.TaskbarService"/>
         <service android:name=".service.StartMenuService"/>
index df89300..a05d7a1 100644 (file)
@@ -52,6 +52,7 @@ import com.farmerbb.taskbar.util.AppEntry;
 import com.farmerbb.taskbar.util.ApplicationType;
 import com.farmerbb.taskbar.util.FreeformHackHelper;
 import com.farmerbb.taskbar.util.IconCache;
+import com.farmerbb.taskbar.util.LauncherHelper;
 import com.farmerbb.taskbar.util.MenuHelper;
 import com.farmerbb.taskbar.util.PinnedBlockedApps;
 import com.farmerbb.taskbar.util.SavedWindowSizes;
@@ -220,9 +221,10 @@ public class ContextMenuActivity extends PreferenceActivity implements Preferenc
             findPreference("open_taskbar_settings").setOnPreferenceClickListener(this);
             findPreference("start_menu_apps").setOnPreferenceClickListener(this);
 
-            if(U.launcherIsDefault(this)
-                    && FreeformHackHelper.getInstance().isInFreeformWorkspace()
-                    && !U.isOverridingFreeformHack(this)) {
+            if((U.launcherIsDefault(this)
+                    && FreeformHackHelper.getInstance().isInFreeformWorkspace())
+                    || (U.isOverridingFreeformHack(this)
+                    && LauncherHelper.getInstance().isOnHomeScreen())) {
                 addPreferencesFromResource(R.xml.pref_context_menu_change_wallpaper);
                 findPreference("change_wallpaper").setOnPreferenceClickListener(this);
             }
@@ -553,7 +555,7 @@ public class ContextMenuActivity extends PreferenceActivity implements Preferenc
                     intent.putExtra("no_shadow", true);
                     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
 
-                    U.launchAppMaximized(getApplicationContext(), intent);
+                    U.startActivityMaximized(getApplicationContext(), intent);
                 } else {
                     try {
                         startActivity(intent);
@@ -633,7 +635,7 @@ public class ContextMenuActivity extends PreferenceActivity implements Preferenc
             case "change_wallpaper":
                 Intent intent3 = Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), getString(R.string.set_wallpaper));
                 intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                U.launchAppMaximized(getApplicationContext(), intent3);
+                U.startActivityMaximized(getApplicationContext(), intent3);
 
                 showStartMenu = false;
                 shouldHideTaskbar = true;
index fb8350e..4fabb82 100644 (file)
@@ -192,6 +192,11 @@ public class HomeActivity extends Activity {
         else
             setContentView(view);
 
+        getWindow().setFlags(
+                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
+                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
+        );
+
         LocalBroadcastManager.getInstance(this).registerReceiver(killReceiver, new IntentFilter("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
         LocalBroadcastManager.getInstance(this).registerReceiver(forceTaskbarStartReceiver, new IntentFilter("com.farmerbb.taskbar.FORCE_TASKBAR_RESTART"));
 
index bd412f7..4cc9757 100644 (file)
@@ -142,7 +142,7 @@ public class InvisibleActivityFreeform extends Activity {
                                 intent.putExtra("power_button_warning", true);
                             }
 
-                            U.launchAppMaximized(getApplicationContext(), intent);
+                            U.startActivityMaximized(getApplicationContext(), intent);
                         }
                     }, 100);
                 }
index 880e83d..bf59ae0 100644 (file)
@@ -81,7 +81,7 @@ public class KeyboardShortcutActivity extends Activity {
                                 && pref.getBoolean("freeform_hack", false)
                                 && isInMultiWindowMode()) {
                             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
-                            U.launchAppMaximized(getApplicationContext(), intent);
+                            U.startActivityMaximized(getApplicationContext(), intent);
                         } else {
                             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                             startActivity(intent);
diff --git a/app/src/main/java/com/farmerbb/taskbar/activity/TouchAbsorberActivity.java b/app/src/main/java/com/farmerbb/taskbar/activity/TouchAbsorberActivity.java
new file mode 100644 (file)
index 0000000..76c8ec5
--- /dev/null
@@ -0,0 +1,75 @@
+/* Copyright 2017 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.activity;
+
+import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.support.v4.content.LocalBroadcastManager;
+import android.util.DisplayMetrics;
+import android.widget.FrameLayout;
+import android.widget.LinearLayout;
+
+import com.farmerbb.taskbar.R;
+import com.farmerbb.taskbar.util.FreeformHackHelper;
+import com.farmerbb.taskbar.util.U;
+
+public class TouchAbsorberActivity extends Activity {
+
+    private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            finish();
+        }
+    };
+
+    @SuppressLint("HardwareIds")
+    @SuppressWarnings("deprecation")
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.incognito);
+
+        DisplayMetrics metrics = U.getRealDisplayMetrics(this);
+        LinearLayout layout = U.findViewById(this, R.id.incognitoLayout);
+        layout.setLayoutParams(new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels));
+        // layout.setBackgroundColor(0x800000FF);
+
+        LocalBroadcastManager.getInstance(this).registerReceiver(finishReceiver, new IntentFilter("com.farmerbb.taskbar.FINISH_FREEFORM_ACTIVITY"));
+        FreeformHackHelper.getInstance().setTouchAbsorberActive(true);
+    }
+
+    @Override
+    protected void onStop() {
+        super.onStop();
+        finish();
+    }
+
+    @Override
+    protected void onDestroy() {
+        LocalBroadcastManager.getInstance(this).unregisterReceiver(finishReceiver);
+        FreeformHackHelper.getInstance().setTouchAbsorberActive(false);
+
+        super.onDestroy();
+    }
+
+    @Override
+    public void onBackPressed() {}
+}
index cb6582f..6dba1c8 100644 (file)
@@ -351,7 +351,7 @@ public class DashboardService extends Service {
                 if(intent != null && U.hasBrokenSetLaunchBoundsApi())
                     intent.putExtra("context_menu_fix", true);
 
-                U.launchAppMaximized(this, intent);
+                U.startActivityMaximized(this, intent);
             } else
                 startActivity(intent);
 
index 65e6f75..7f5adf2 100644 (file)
@@ -632,9 +632,9 @@ public class StartMenuService extends Service {
 
                 if(inFreeformMode) {
                     if(clazz.equals(InvisibleActivity.class))
-                        U.launchAppLowerRight(this, intent);
+                        U.startActivityLowerRight(this, intent);
                     else if(clazz.equals(InvisibleActivityAlt.class))
-                        U.launchAppMaximized(this, intent);
+                        U.startActivityMaximized(this, intent);
                 } else
                     startActivity(intent);
             }
index 28c5fba..641443f 100644 (file)
@@ -19,6 +19,7 @@ public class FreeformHackHelper {
 
     private boolean freeformHackActive = false;
     private boolean inFreeformWorkspace = false;
+    private boolean touchAbsorberActive = false;
 
     private static FreeformHackHelper theInstance;
 
@@ -45,4 +46,12 @@ public class FreeformHackHelper {
     public void setInFreeformWorkspace(boolean value) {
         inFreeformWorkspace = value;
     }
+
+    public boolean isTouchAbsorberActive() {
+        return touchAbsorberActive;
+    }
+
+    public void setTouchAbsorberActive(boolean value) {
+        touchAbsorberActive = value;
+    }
 }
index a570fce..ad05f94 100644 (file)
@@ -64,6 +64,7 @@ import com.farmerbb.taskbar.activity.DummyActivity;
 import com.farmerbb.taskbar.activity.InvisibleActivityFreeform;
 import com.farmerbb.taskbar.activity.ShortcutActivity;
 import com.farmerbb.taskbar.activity.StartTaskbarActivity;
+import com.farmerbb.taskbar.activity.TouchAbsorberActivity;
 import com.farmerbb.taskbar.receiver.LockDeviceReceiver;
 import com.farmerbb.taskbar.service.DashboardService;
 import com.farmerbb.taskbar.service.NotificationService;
@@ -108,7 +109,7 @@ public class U {
     public static void showPermissionDialog(Context context) {
         showPermissionDialog(context, null, null);
     }
-    
+
     @TargetApi(Build.VERSION_CODES.M)
     public static AlertDialog showPermissionDialog(Context context, Runnable onError, Runnable onFinish) {
         Runnable finalOnFinish = onFinish == null
@@ -157,7 +158,7 @@ public class U {
         AlertDialog dialog = builder.create();
         dialog.show();
         dialog.setCancelable(false);
-        
+
         return dialog;
     }
 
@@ -263,12 +264,12 @@ public class U {
     }
 
     private static void launchApp(final Context context,
-                                 final String packageName,
-                                 final String componentName,
-                                 final long userId, final String windowSize,
-                                 final boolean launchedFromTaskbar,
-                                 final boolean openInNewWindow,
-                                 final ShortcutInfo shortcut) {
+                                  final String packageName,
+                                  final String componentName,
+                                  final long userId, final String windowSize,
+                                  final boolean launchedFromTaskbar,
+                                  final boolean openInNewWindow,
+                                  final ShortcutInfo shortcut) {
         launchApp(context, launchedFromTaskbar, () -> continueLaunchingApp(context, packageName, componentName, userId,
                 windowSize, launchedFromTaskbar, openInNewWindow, shortcut));
     }
@@ -313,7 +314,7 @@ public class U {
         }
 
         if(canDrawOverlays(context))
-            launchAppLowerRight(context, freeformHackIntent);
+            startActivityLowerRight(context, freeformHackIntent);
     }
 
     public static void stopFreeformHack(Context context) {
@@ -398,14 +399,14 @@ public class U {
         else
             LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
     }
-    
+
     private static void launchStandard(Context context, Intent intent, long userId, ShortcutInfo shortcut, ApplicationType type) {
         Bundle bundle = canEnableFreeform() ? getActivityOptions(context, type).toBundle() : null;
         if(shortcut == null) {
             UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
             if(userId == userManager.getSerialNumberForUser(Process.myUserHandle())) {
                 try {
-                    context.startActivity(intent, bundle);
+                    startActivity(context, intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
                 } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
@@ -436,7 +437,7 @@ public class U {
             UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
             if(userId == userManager.getSerialNumberForUser(Process.myUserHandle())) {
                 try {
-                    context.startActivity(intent, bundle);
+                    startActivity(context, intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
                 } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
@@ -450,7 +451,7 @@ public class U {
     @TargetApi(Build.VERSION_CODES.N)
     private static void launchMode2(Context context, Intent intent, int launchType, long userId, ShortcutInfo shortcut, ApplicationType type) {
         DisplayMetrics metrics = getRealDisplayMetrics(context);
-        
+
         int statusBarHeight = getStatusBarHeight(context);
         String position = getTaskbarPosition(context);
 
@@ -487,18 +488,14 @@ public class U {
         } else if(isLandscape || (launchType != RIGHT && isPortrait))
             top = top + iconSize;
 
-        Bundle bundle = getActivityOptions(context, type).setLaunchBounds(new Rect(
-                left,
-                top,
-                right,
-                bottom
-        )).toBundle();
+        Bundle bundle = getActivityOptions(context, type)
+                .setLaunchBounds(new Rect(left, top, right, bottom)).toBundle();
 
         if(shortcut == null) {
             UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
             if(userId == userManager.getSerialNumberForUser(Process.myUserHandle())) {
                 try {
-                    context.startActivity(intent, bundle);
+                    startActivity(context, intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
                 } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
@@ -529,7 +526,7 @@ public class U {
             UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
             if(userId == userManager.getSerialNumberForUser(Process.myUserHandle())) {
                 try {
-                    context.startActivity(intent, bundle);
+                    startActivity(context, intent, bundle);
                 } catch (ActivityNotFoundException e) {
                     launchAndroidForWork(context, intent.getComponent(), bundle, userId);
                 } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
@@ -559,16 +556,30 @@ public class U {
         }
     }
 
-    public static void launchAppMaximized(Context context, Intent intent) {
+    private static void startActivity(Context context, Intent intent, Bundle bundle) {
+        boolean shouldLaunchTouchAbsorber =
+                !FreeformHackHelper.getInstance().isTouchAbsorberActive()
+                        && isOverridingFreeformHack(context)
+                        && !isChromeOs(context);
+
+        if(!shouldLaunchTouchAbsorber) {
+            context.startActivity(intent, bundle);
+            return;
+        }
+
+        startTouchAbsorberActivity(context);
+        new Handler().postDelayed(() -> context.startActivity(intent, bundle), 100);
+    }
+
+    public static void startActivityMaximized(Context context, Intent intent) {
         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
         long userId = userManager.getSerialNumberForUser(Process.myUserHandle());
 
         launchMode2(context, intent, MAXIMIZED, userId, null, ApplicationType.CONTEXT_MENU);
     }
 
-    @SuppressWarnings("deprecation")
     @TargetApi(Build.VERSION_CODES.N)
-    public static void launchAppLowerRight(Context context, Intent intent) {
+    public static void startActivityLowerRight(Context context, Intent intent) {
         DisplayMetrics metrics = getRealDisplayMetrics(context);
         try {
             context.startActivity(intent,
@@ -578,7 +589,39 @@ public class U {
                                     metrics.heightPixels,
                                     metrics.widthPixels + 1,
                                     metrics.heightPixels + 1
-            )).toBundle());
+                            )).toBundle());
+        } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
+    }
+
+    @TargetApi(Build.VERSION_CODES.N)
+    public static void startTouchAbsorberActivity(Context context) {
+        String position = getTaskbarPosition(context);
+        DisplayMetrics metrics = getRealDisplayMetrics(context);
+
+        int left = 0;
+        int top = 0;
+        int right = metrics.widthPixels;
+        int bottom = metrics.heightPixels;
+
+        int iconSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size);
+
+        if(position.contains("vertical_left"))
+            right = iconSize;
+        else if(position.contains("vertical_right"))
+            left = right - iconSize;
+        else if(position.contains("bottom"))
+            top = bottom - iconSize;
+        else
+            bottom = iconSize;
+
+        Intent intent = new Intent(context, TouchAbsorberActivity.class);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
+
+        try {
+            context.startActivity(intent,
+                    getActivityOptions(context, ApplicationType.FREEFORM_HACK)
+                            .setLaunchBounds(new Rect(left, top, right, bottom)).toBundle());
         } catch (IllegalArgumentException | SecurityException e) { /* Gracefully fail */ }
     }
 
@@ -845,7 +888,7 @@ public class U {
 
     public static boolean canEnableFreeform() {
         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
-   }
+    }
 
     @TargetApi(Build.VERSION_CODES.N)
     public static boolean hasFreeformSupport(Context context) {
@@ -857,7 +900,7 @@ public class U {
     }
 
     public static boolean hasPartialFreeformSupport() {
-         return Build.MANUFACTURER.equalsIgnoreCase("Samsung");
+        return Build.MANUFACTURER.equalsIgnoreCase("Samsung");
     }
 
     public static boolean isServiceRunning(Context context, Class<? extends Service> cls) {
@@ -1237,8 +1280,9 @@ public class U {
 
     public static boolean isOverridingFreeformHack(Context context) {
         SharedPreferences pref = getSharedPreferences(context);
-        return (isChromeOs(context) && pref.getBoolean("chrome_os_context_menu_fix", true))
-                || (!isChromeOs(context) && getCurrentApiVersion() >= 28.0f);
+        return pref.getBoolean("freeform_hack", false)
+                && ((isChromeOs(context) && pref.getBoolean("chrome_os_context_menu_fix", true))
+                || (!isChromeOs(context) && getCurrentApiVersion() >= 28.0f));
     }
 
     @SuppressWarnings("unchecked")
@@ -1342,7 +1386,7 @@ public class U {
                     AlertDialog dialog = builder.create();
                     dialog.show();
                     dialog.setCancelable(false);
-                    
+
                     return dialog;
                 }
             }
index 238ab7f..16334a3 100644 (file)
                 <action android:name="com.twofortyfouram.locale.intent.action.EDIT_CONDITION" />
             </intent-filter>
         </activity>
+        <activity
+            android:name=".activity.TouchAbsorberActivity"
+            android:documentLaunchMode="always"
+            android:excludeFromRecents="true"
+            android:launchMode="singleTask"
+            android:theme="@style/AppTheme.Incognito"/>
 
         <service android:name=".service.TaskbarService"/>
         <service android:name=".service.StartMenuService"/>