OSDN Git Service

Replace U.isOPreview() with BUILD.VERSION checks
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / adapter / StartMenuAdapter.java
index 8393bd0..84d83d8 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.graphics.Typeface;
-import android.hardware.display.DisplayManager;
 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;
@@ -39,15 +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.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.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;
@@ -58,25 +61,25 @@ 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 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) {
-            TopApps topApps = TopApps.getInstance(getContext());
-            textView.setTypeface(null, topApps.isTopApp(activityInfo.name) ? Typeface.BOLD : Typeface.NORMAL);
-        }
+        if(activityInfo != null)
+            textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
 
         switch(pref.getString("theme", "light")) {
             case "light":
@@ -87,77 +90,124 @@ public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
                 break;
         }
 
-        ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
+        ImageView imageView = convertView.findViewById(R.id.icon);
         imageView.setImageDrawable(entry.getIcon(getContext()));
 
-        LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
-        layout.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
-                U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(), false, true, false);
-            }
+        LinearLayout layout = convertView.findViewById(R.id.entry);
+        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);
         });
 
-        layout.setOnLongClickListener(new View.OnLongClickListener() {
-            @Override
-            public boolean onLongClick(View view) {
+        layout.setOnLongClickListener(view -> {
+            int[] location = new int[2];
+            view.getLocationOnScreen(location);
+            openContextMenu(entry, location);
+            return true;
+        });
+
+        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 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+                    intent.putExtra("context_menu_fix", true);
+
+                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);
+    }
 
-            getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
-        } else
-            getContext().startActivity(intent);
+    private boolean shouldDelay() {
+        SharedPreferences pref = U.getSharedPreferences(getContext());
+        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
+                && pref.getBoolean("freeform_hack", false)
+                && !FreeformHackHelper.getInstance().isFreeformHackActive();
+    }
+
+    private boolean visualFeedbackEnabled() {
+        SharedPreferences pref = U.getSharedPreferences(getContext());
+        return Build.VERSION.SDK_INT < Build.VERSION_CODES.O && pref.getBoolean("visual_feedback", true);
     }
 }