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.app.ActivityOptions;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.ActivityInfo;
24 import android.graphics.Color;
25 import android.graphics.Rect;
26 import android.graphics.Typeface;
27 import android.hardware.display.DisplayManager;
28 import android.os.Build;
29 import android.os.Handler;
30 import android.support.annotation.NonNull;
31 import android.support.v4.content.ContextCompat;
32 import android.support.v4.content.LocalBroadcastManager;
33 import android.support.v4.graphics.ColorUtils;
34 import android.view.Display;
35 import android.view.LayoutInflater;
36 import android.view.MotionEvent;
37 import android.view.PointerIcon;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.ArrayAdapter;
41 import android.widget.ImageView;
42 import android.widget.LinearLayout;
43 import android.widget.TextView;
45 import com.farmerbb.taskbar.R;
46 import com.farmerbb.taskbar.activity.ContextMenuActivity;
47 import com.farmerbb.taskbar.activity.dark.ContextMenuActivityDark;
48 import com.farmerbb.taskbar.util.AppEntry;
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 = U.getBackgroundTint(getContext());
130 backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
131 v.setBackgroundColor(backgroundTint);
134 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
135 v.setBackgroundColor(0);
137 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
138 v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
143 layout.setOnTouchListener((v, event) -> {
144 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
152 private boolean isTopApp(ActivityInfo activityInfo) {
153 TopApps topApps = TopApps.getInstance(getContext());
154 return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
157 @SuppressWarnings("deprecation")
158 private void openContextMenu(final AppEntry entry, final int[] location) {
159 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
161 new Handler().postDelayed(() -> {
162 SharedPreferences pref = U.getSharedPreferences(getContext());
163 Intent intent = null;
165 switch(pref.getString("theme", "light")) {
167 intent = new Intent(getContext(), ContextMenuActivity.class);
170 intent = new Intent(getContext(), ContextMenuActivityDark.class);
175 intent.putExtra("package_name", entry.getPackageName());
176 intent.putExtra("app_name", entry.getLabel());
177 intent.putExtra("component_name", entry.getComponentName());
178 intent.putExtra("user_id", entry.getUserId(getContext()));
179 intent.putExtra("launched_from_start_menu", true);
180 intent.putExtra("x", location[0]);
181 intent.putExtra("y", location[1]);
182 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
185 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
186 DisplayManager dm = (DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
187 Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
189 getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
191 getContext().startActivity(intent);
192 }, shouldDelay() ? 100 : 0);
195 private boolean shouldDelay() {
196 SharedPreferences pref = U.getSharedPreferences(getContext());
197 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
198 && pref.getBoolean("freeform_hack", false)
199 && !FreeformHackHelper.getInstance().isFreeformHackActive();