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.hardware.display.DisplayManager;
27 import android.os.Build;
28 import android.os.Handler;
29 import android.support.annotation.NonNull;
30 import android.support.v4.content.ContextCompat;
31 import android.support.v4.content.LocalBroadcastManager;
32 import android.support.v4.graphics.ColorUtils;
33 import android.view.Display;
34 import android.view.LayoutInflater;
35 import android.view.MotionEvent;
36 import android.view.PointerIcon;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.widget.ArrayAdapter;
40 import android.widget.ImageView;
41 import android.widget.LinearLayout;
42 import android.widget.TextView;
44 import com.farmerbb.taskbar.R;
45 import com.farmerbb.taskbar.activity.ContextMenuActivity;
46 import com.farmerbb.taskbar.activity.dark.ContextMenuActivityDark;
47 import com.farmerbb.taskbar.util.AppEntry;
48 import com.farmerbb.taskbar.util.ApplicationType;
49 import com.farmerbb.taskbar.util.FreeformHackHelper;
50 import com.farmerbb.taskbar.util.TopApps;
51 import com.farmerbb.taskbar.util.U;
53 import java.util.List;
55 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
57 private boolean isGrid = false;
59 public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
60 super(context, layout, list);
61 isGrid = layout == R.layout.row_alt;
65 public @NonNull View getView(int position, View convertView, final @NonNull ViewGroup parent) {
66 // Check if an existing view is being reused, otherwise inflate the view
67 if(convertView == null)
68 convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row, parent, false);
70 final AppEntry entry = getItem(position);
73 final SharedPreferences pref = U.getSharedPreferences(getContext());
75 TextView textView = (TextView) convertView.findViewById(R.id.name);
76 textView.setText(entry.getLabel());
78 Intent intent = new Intent();
79 intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
80 ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
82 if(activityInfo != null)
83 textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
85 switch(pref.getString("theme", "light")) {
87 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
90 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
94 ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
95 imageView.setImageDrawable(entry.getIcon(getContext()));
97 LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
98 layout.setOnClickListener(view -> {
99 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
100 U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(), entry.getUserId(getContext()), null, false, false);
103 layout.setOnLongClickListener(view -> {
104 int[] location = new int[2];
105 view.getLocationOnScreen(location);
106 openContextMenu(entry, location);
110 layout.setOnGenericMotionListener((view, motionEvent) -> {
111 int action = motionEvent.getAction();
113 if(action == MotionEvent.ACTION_BUTTON_PRESS
114 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
115 int[] location = new int[2];
116 view.getLocationOnScreen(location);
117 openContextMenu(entry, location);
120 if(action == MotionEvent.ACTION_SCROLL && pref.getBoolean("visual_feedback", true))
121 view.setBackgroundColor(0);
126 if(pref.getBoolean("visual_feedback", true)) {
127 layout.setOnHoverListener((v, event) -> {
128 if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
129 int backgroundTint = pref.getBoolean("transparent_start_menu", false)
130 ? U.getAccentColor(getContext())
131 : U.getBackgroundTint(getContext());
133 //noinspection ResourceAsColor
134 backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
135 v.setBackgroundColor(backgroundTint);
138 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
139 v.setBackgroundColor(0);
141 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
142 v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
147 layout.setOnTouchListener((v, event) -> {
148 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
156 private boolean isTopApp(ActivityInfo activityInfo) {
157 TopApps topApps = TopApps.getInstance(getContext());
158 return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
161 @SuppressWarnings("deprecation")
162 private void openContextMenu(final AppEntry entry, final int[] location) {
163 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
165 new Handler().postDelayed(() -> {
166 SharedPreferences pref = U.getSharedPreferences(getContext());
167 Intent intent = null;
169 switch(pref.getString("theme", "light")) {
171 intent = new Intent(getContext(), ContextMenuActivity.class);
174 intent = new Intent(getContext(), ContextMenuActivityDark.class);
179 intent.putExtra("package_name", entry.getPackageName());
180 intent.putExtra("app_name", entry.getLabel());
181 intent.putExtra("component_name", entry.getComponentName());
182 intent.putExtra("user_id", entry.getUserId(getContext()));
183 intent.putExtra("launched_from_start_menu", true);
184 intent.putExtra("x", location[0]);
185 intent.putExtra("y", location[1]);
186 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
189 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && FreeformHackHelper.getInstance().isInFreeformWorkspace()) {
190 DisplayManager dm = (DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
191 Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
193 if(intent != null && U.isOPreview())
194 intent.putExtra("context_menu_fix", true);
196 getContext().startActivity(intent, U.getActivityOptions(ApplicationType.CONTEXT_MENU).setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).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();