package com.farmerbb.taskbar.adapter;
-import android.app.ActivityOptions;
-import android.content.ActivityNotFoundException;
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;
import com.farmerbb.taskbar.R;
import com.farmerbb.taskbar.activity.ContextMenuActivity;
+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;
}
@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());
- ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
+ 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));
+ break;
+ case "dark":
+ textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
+ break;
+ }
+
+ 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) {
- SharedPreferences pref = U.getSharedPreferences(getContext());
-
- Intent intent = new Intent();
- intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
-
- try {
- getContext().startActivity(intent);
- } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
-
- if(pref.getBoolean("hide_taskbar", false))
- LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
- else
- LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
- }
+ 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) {
- openContextMenu(entry);
- return true;
- }
+ layout.setOnLongClickListener(view -> {
+ int[] location = new int[2];
+ view.getLocationOnScreen(location);
+ openContextMenu(entry, location);
+ return true;
});
- 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)
- openContextMenu(entry);
+ layout.setOnGenericMotionListener((view, motionEvent) -> {
+ int action = motionEvent.getAction();
- return false;
+ if(action == MotionEvent.ACTION_BUTTON_PRESS
+ && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
+ int[] location = new int[2];
+ view.getLocationOnScreen(location);
+ openContextMenu(entry, location);
}
+
+ if(action == MotionEvent.ACTION_SCROLL && visualFeedbackEnabled())
+ view.setBackgroundColor(0);
+
+ return false;
});
+ 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) {
- SharedPreferences pref = U.getSharedPreferences(getContext());
- if(pref.getBoolean("hide_taskbar", false))
- 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 void openContextMenu(final AppEntry entry, final int[] location) {
+ LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
+
+ 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;
+ }
- Intent intent = new Intent(getContext(), ContextMenuActivity.class);
- intent.putExtra("package_name", entry.getPackageName());
- intent.putExtra("app_name", entry.getLabel());
- intent.putExtra("component_name", entry.getComponentName());
- 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("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(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && FreeformHackHelper.getInstance().isInFreeformWorkspace()) {
+ DisplayMetrics metrics = U.getRealDisplayMetrics(getContext());
+
+ 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);
+ }
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- DisplayManager dm = (DisplayManager) getContext().getSystemService(DISPLAY_SERVICE);
- Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
+ 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();
+ }
- getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
- } else
- getContext().startActivity(intent);
+ private boolean visualFeedbackEnabled() {
+ SharedPreferences pref = U.getSharedPreferences(getContext());
+ return Build.VERSION.SDK_INT < Build.VERSION_CODES.O && pref.getBoolean("visual_feedback", true);
}
}