OSDN Git Service

Partially re-enable visual feedback on Android O
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / adapter / StartMenuAdapter.java
index 7861768..8bbf795 100644 (file)
 
 package com.farmerbb.taskbar.adapter;
 
-import android.app.ActivityOptions;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.pm.ActivityInfo;
+import android.graphics.Color;
 import android.graphics.Rect;
-import android.hardware.display.DisplayManager;
+import android.graphics.Typeface;
 import android.os.Build;
 import android.os.Handler;
+import android.support.annotation.NonNull;
 import android.support.v4.content.ContextCompat;
 import android.support.v4.content.LocalBroadcastManager;
-import android.view.Display;
+import android.support.v4.graphics.ColorUtils;
+import android.util.DisplayMetrics;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
+import android.view.PointerIcon;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
@@ -38,17 +42,15 @@ import android.widget.TextView;
 
 import com.farmerbb.taskbar.R;
 import com.farmerbb.taskbar.activity.ContextMenuActivity;
-import com.farmerbb.taskbar.activity.ContextMenuActivityDark;
-import com.farmerbb.taskbar.activity.InvisibleActivityFreeform;
+import com.farmerbb.taskbar.activity.dark.ContextMenuActivityDark;
 import com.farmerbb.taskbar.util.AppEntry;
+import com.farmerbb.taskbar.util.ApplicationType;
 import com.farmerbb.taskbar.util.FreeformHackHelper;
-import com.farmerbb.taskbar.util.SavedWindowSizes;
+import com.farmerbb.taskbar.util.TopApps;
 import com.farmerbb.taskbar.util.U;
 
 import java.util.List;
 
-import static android.content.Context.DISPLAY_SERVICE;
-
 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
 
     private boolean isGrid = false;
@@ -59,17 +61,26 @@ public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
     }
 
     @Override
-    public View getView(int position, View convertView, final ViewGroup parent) {
+    public @NonNull View getView(int position, View convertView, final @NonNull ViewGroup parent) {
         // Check if an existing view is being reused, otherwise inflate the view
         if(convertView == null)
             convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row, parent, false);
 
         final AppEntry entry = getItem(position);
+        assert entry != null;
+
         final SharedPreferences pref = U.getSharedPreferences(getContext());
 
         TextView textView = (TextView) convertView.findViewById(R.id.name);
         textView.setText(entry.getLabel());
 
+        Intent intent = new Intent();
+        intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
+        ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
+
+        if(activityInfo != null)
+            textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
+
         switch(pref.getString("theme", "light")) {
             case "light":
                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
@@ -83,129 +94,120 @@ public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
         imageView.setImageDrawable(entry.getIcon(getContext()));
 
         LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
-        layout.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                boolean shouldDelay = false;
-
-                SharedPreferences pref = U.getSharedPreferences(getContext());
-                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
-                        && pref.getBoolean("freeform_hack", false)
-                        && !FreeformHackHelper.getInstance().isFreeformHackActive()) {
-                    shouldDelay = true;
-
-                    Intent freeformHackIntent = new Intent(getContext(), InvisibleActivityFreeform.class);
-                    freeformHackIntent.putExtra("check_multiwindow", true);
-                    freeformHackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                    getContext().startActivity(freeformHackIntent);
-                }
+        layout.setOnClickListener(view -> {
+            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
+            U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(), entry.getUserId(getContext()), null, false, false);
+        });
 
-                if(shouldDelay) {
-                    new Handler().postDelayed(new Runnable() {
-                        @Override
-                        public void run() {
-                            launchApp(entry.getPackageName(), entry.getComponentName());
-                        }
-                    }, 100);
-                } else launchApp(entry.getPackageName(), entry.getComponentName());
-            }
+        layout.setOnLongClickListener(view -> {
+            int[] location = new int[2];
+            view.getLocationOnScreen(location);
+            openContextMenu(entry, location);
+            return true;
         });
 
-        layout.setOnLongClickListener(new View.OnLongClickListener() {
-            @Override
-            public boolean onLongClick(View view) {
+        layout.setOnGenericMotionListener((view, motionEvent) -> {
+            int action = motionEvent.getAction();
+
+            if(action == MotionEvent.ACTION_BUTTON_PRESS
+                    && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
                 int[] location = new int[2];
                 view.getLocationOnScreen(location);
                 openContextMenu(entry, location);
-                return true;
             }
+
+            if(action == MotionEvent.ACTION_SCROLL && visualFeedbackEnabled())
+                view.setBackgroundColor(0);
+
+            return false;
         });
 
-        layout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
-            @Override
-            public boolean onGenericMotion(View view, MotionEvent motionEvent) {
-                if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
-                        && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
-                    int[] location = new int[2];
-                    view.getLocationOnScreen(location);
-                    openContextMenu(entry, location);
+        if(visualFeedbackEnabled()) {
+            layout.setOnHoverListener((v, event) -> {
+                if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
+                    int backgroundTint = pref.getBoolean("transparent_start_menu", false)
+                            ? U.getAccentColor(getContext())
+                            : U.getBackgroundTint(getContext());
+
+                    //noinspection ResourceAsColor
+                    backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
+                    v.setBackgroundColor(backgroundTint);
                 }
 
+                if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
+                    v.setBackgroundColor(0);
+
+                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+                    v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
+
                 return false;
-            }
-        });
+            });
+        }
+
+        if(pref.getBoolean("visual_feedback", true)) {
+            layout.setOnTouchListener((v, event) -> {
+                v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
+                return false;
+            });
+        }
 
         return convertView;
     }
 
+    private boolean isTopApp(ActivityInfo activityInfo) {
+        TopApps topApps = TopApps.getInstance(getContext());
+        return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
+    }
+
     @SuppressWarnings("deprecation")
-    private void openContextMenu(AppEntry entry, int[] location) {
+    private void openContextMenu(final AppEntry entry, final int[] location) {
         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
 
-        SharedPreferences pref = U.getSharedPreferences(getContext());
-        Intent intent = null;
+        new Handler().postDelayed(() -> {
+            SharedPreferences pref = U.getSharedPreferences(getContext());
+            Intent intent = null;
+
+            switch(pref.getString("theme", "light")) {
+                case "light":
+                    intent = new Intent(getContext(), ContextMenuActivity.class);
+                    break;
+                case "dark":
+                    intent = new Intent(getContext(), ContextMenuActivityDark.class);
+                    break;
+            }
 
-        switch(pref.getString("theme", "light")) {
-            case "light":
-                intent = new Intent(getContext(), ContextMenuActivity.class);
-                break;
-            case "dark":
-                intent = new Intent(getContext(), ContextMenuActivityDark.class);
-                break;
-        }
+            if(intent != null) {
+                intent.putExtra("package_name", entry.getPackageName());
+                intent.putExtra("app_name", entry.getLabel());
+                intent.putExtra("component_name", entry.getComponentName());
+                intent.putExtra("user_id", entry.getUserId(getContext()));
+                intent.putExtra("launched_from_start_menu", true);
+                intent.putExtra("x", location[0]);
+                intent.putExtra("y", location[1]);
+                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            }
 
-        if(intent != null) {
-            intent.putExtra("package_name", entry.getPackageName());
-            intent.putExtra("app_name", entry.getLabel());
-            intent.putExtra("component_name", entry.getComponentName());
-            intent.putExtra("launched_from_start_menu", true);
-            intent.putExtra("x", location[0]);
-            intent.putExtra("y", location[1]);
-            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        }
+            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && FreeformHackHelper.getInstance().isInFreeformWorkspace()) {
+                DisplayMetrics metrics = U.getRealDisplayMetrics(getContext());
 
-        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
-            DisplayManager dm = (DisplayManager) getContext().getSystemService(DISPLAY_SERVICE);
-            Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
+                if(intent != null && U.isOPreview())
+                    intent.putExtra("context_menu_fix", true);
 
-            getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
-        } else
-            getContext().startActivity(intent);
+                getContext().startActivity(intent, U.getActivityOptions(ApplicationType.CONTEXT_MENU).setLaunchBounds(new Rect(0, 0, metrics.widthPixels, metrics.heightPixels)).toBundle());
+            } else
+                getContext().startActivity(intent);
+        }, shouldDelay() ? 100 : 0);
     }
 
-    private void launchApp(String packageName, String componentName) {
-        Intent intent = new Intent();
-        intent.setComponent(ComponentName.unflattenFromString(componentName));
-        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
-
+    private boolean shouldDelay() {
         SharedPreferences pref = U.getSharedPreferences(getContext());
-        if(!pref.getBoolean("freeform_hack", false))
-            U.launchStandard(getContext(), intent);
-        else switch(SavedWindowSizes.getInstance(getContext()).getWindowSize(getContext(), packageName)) {
-            case "standard":
-                U.launchStandard(getContext(), intent);
-                break;
-            case "large":
-                U.launchLarge(getContext(), intent);
-                break;
-            case "fullscreen":
-                U.launchFullscreen(getContext(), intent, true);
-                break;
-            case "half_left":
-                U.launchHalfLeft(getContext(), intent, true);
-                break;
-            case "half_right":
-                U.launchHalfRight(getContext(), intent, true);
-                break;
-            case "phone_size":
-                U.launchPhoneSize(getContext(), intent);
-                break;
-        }
+        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
+                && pref.getBoolean("freeform_hack", false)
+                && !FreeformHackHelper.getInstance().isFreeformHackActive();
+    }
 
-        if(pref.getBoolean("hide_taskbar", true) && !FreeformHackHelper.getInstance().isInFreeformWorkspace())
-            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
-        else
-            LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
+    private boolean visualFeedbackEnabled() {
+        SharedPreferences pref = U.getSharedPreferences(getContext());
+        return !U.isOPreview() && pref.getBoolean("visual_feedback", true);
     }
 }