1 /* Copyright 2016 Braden Farmer
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 package com.farmerbb.taskbar.adapter;
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.content.pm.ActivityInfo;
23 import android.graphics.Color;
24 import android.graphics.Rect;
25 import android.graphics.Typeface;
26 import android.os.Build;
27 import android.os.Handler;
28 import android.support.annotation.NonNull;
29 import android.support.v4.content.ContextCompat;
30 import android.support.v4.content.LocalBroadcastManager;
31 import android.support.v4.graphics.ColorUtils;
32 import android.util.DisplayMetrics;
33 import android.view.LayoutInflater;
34 import android.view.MotionEvent;
35 import android.view.PointerIcon;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.ArrayAdapter;
39 import android.widget.ImageView;
40 import android.widget.LinearLayout;
41 import android.widget.TextView;
43 import com.farmerbb.taskbar.R;
44 import com.farmerbb.taskbar.activity.ContextMenuActivity;
45 import com.farmerbb.taskbar.activity.dark.ContextMenuActivityDark;
46 import com.farmerbb.taskbar.util.AppEntry;
47 import com.farmerbb.taskbar.util.ApplicationType;
48 import com.farmerbb.taskbar.util.FreeformHackHelper;
49 import com.farmerbb.taskbar.util.TopApps;
50 import com.farmerbb.taskbar.util.U;
52 import java.util.List;
54 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
56 private boolean isGrid = false;
58 public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
59 super(context, layout, list);
60 isGrid = layout == R.layout.row_alt;
64 public @NonNull View getView(int position, View convertView, final @NonNull ViewGroup parent) {
65 // Check if an existing view is being reused, otherwise inflate the view
66 if(convertView == null)
67 convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row, parent, false);
69 final AppEntry entry = getItem(position);
72 final SharedPreferences pref = U.getSharedPreferences(getContext());
74 TextView textView = convertView.findViewById(R.id.name);
75 textView.setText(entry.getLabel());
77 Intent intent = new Intent();
78 intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
79 ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
81 if(activityInfo != null)
82 textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
84 switch(pref.getString("theme", "light")) {
86 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
89 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
93 ImageView imageView = convertView.findViewById(R.id.icon);
94 imageView.setImageDrawable(entry.getIcon(getContext()));
96 LinearLayout layout = convertView.findViewById(R.id.entry);
97 layout.setOnClickListener(view -> {
98 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
99 U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(), entry.getUserId(getContext()), null, false, false);
102 layout.setOnLongClickListener(view -> {
103 int[] location = new int[2];
104 view.getLocationOnScreen(location);
105 openContextMenu(entry, location);
109 layout.setOnGenericMotionListener((view, motionEvent) -> {
110 int action = motionEvent.getAction();
112 if(action == MotionEvent.ACTION_BUTTON_PRESS
113 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
114 int[] location = new int[2];
115 view.getLocationOnScreen(location);
116 openContextMenu(entry, location);
119 if(action == MotionEvent.ACTION_SCROLL && visualFeedbackEnabled())
120 view.setBackgroundColor(0);
125 if(visualFeedbackEnabled()) {
126 layout.setOnHoverListener((v, event) -> {
127 if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
128 int backgroundTint = pref.getBoolean("transparent_start_menu", false)
129 ? U.getAccentColor(getContext())
130 : U.getBackgroundTint(getContext());
132 //noinspection ResourceAsColor
133 backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
134 v.setBackgroundColor(backgroundTint);
137 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
138 v.setBackgroundColor(0);
140 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
141 v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
147 if(pref.getBoolean("visual_feedback", true)) {
148 layout.setOnTouchListener((v, event) -> {
149 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
157 private boolean isTopApp(ActivityInfo activityInfo) {
158 TopApps topApps = TopApps.getInstance(getContext());
159 return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
162 @SuppressWarnings("deprecation")
163 private void openContextMenu(final AppEntry entry, final int[] location) {
164 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
166 new Handler().postDelayed(() -> {
167 SharedPreferences pref = U.getSharedPreferences(getContext());
168 Intent intent = null;
170 switch(pref.getString("theme", "light")) {
172 intent = new Intent(getContext(), ContextMenuActivity.class);
175 intent = new Intent(getContext(), ContextMenuActivityDark.class);
180 intent.putExtra("package_name", entry.getPackageName());
181 intent.putExtra("app_name", entry.getLabel());
182 intent.putExtra("component_name", entry.getComponentName());
183 intent.putExtra("user_id", entry.getUserId(getContext()));
184 intent.putExtra("launched_from_start_menu", true);
185 intent.putExtra("x", location[0]);
186 intent.putExtra("y", location[1]);
187 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
190 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && FreeformHackHelper.getInstance().isInFreeformWorkspace()) {
191 DisplayMetrics metrics = U.getRealDisplayMetrics(getContext());
193 if(intent != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
194 intent.putExtra("context_menu_fix", true);
196 getContext().startActivity(intent, U.getActivityOptions(ApplicationType.CONTEXT_MENU).setLaunchBounds(new Rect(0, 0, metrics.widthPixels, metrics.heightPixels)).toBundle());
198 getContext().startActivity(intent);
199 }, shouldDelay() ? 100 : 0);
202 private boolean shouldDelay() {
203 SharedPreferences pref = U.getSharedPreferences(getContext());
204 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
205 && pref.getBoolean("freeform_hack", false)
206 && !FreeformHackHelper.getInstance().isFreeformHackActive();
209 private boolean visualFeedbackEnabled() {
210 SharedPreferences pref = U.getSharedPreferences(getContext());
211 return Build.VERSION.SDK_INT < Build.VERSION_CODES.O && pref.getBoolean("visual_feedback", true);