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.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.ContextMenuActivityDark;
47 import com.farmerbb.taskbar.util.AppEntry;
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 View getView(int position, View convertView, final 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);
70 final SharedPreferences pref = U.getSharedPreferences(getContext());
72 TextView textView = (TextView) convertView.findViewById(R.id.name);
73 textView.setText(entry.getLabel());
75 Intent intent = new Intent();
76 intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
77 ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
79 if(activityInfo != null)
80 textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
82 switch(pref.getString("theme", "light")) {
84 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
87 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
91 ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
92 imageView.setImageDrawable(entry.getIcon(getContext()));
94 LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
95 layout.setOnClickListener(new View.OnClickListener() {
97 public void onClick(View 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);
103 layout.setOnLongClickListener(new View.OnLongClickListener() {
105 public boolean onLongClick(View view) {
106 int[] location = new int[2];
107 view.getLocationOnScreen(location);
108 openContextMenu(entry, location);
113 layout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
115 public boolean onGenericMotion(View view, MotionEvent motionEvent) {
116 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
117 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
118 int[] location = new int[2];
119 view.getLocationOnScreen(location);
120 openContextMenu(entry, location);
127 if(pref.getBoolean("visual_feedback", true)) {
128 layout.setOnHoverListener(new View.OnHoverListener() {
130 public boolean onHover(View v, MotionEvent event) {
131 if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
132 int backgroundTint = U.getBackgroundTint(getContext());
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 layout.setOnTouchListener(new View.OnTouchListener() {
149 public boolean onTouch(View v, MotionEvent event) {
150 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
159 private boolean isTopApp(ActivityInfo activityInfo) {
160 TopApps topApps = TopApps.getInstance(getContext());
161 return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
164 @SuppressWarnings("deprecation")
165 private void openContextMenu(final AppEntry entry, final int[] location) {
166 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
168 new Handler().postDelayed(new Runnable() {
171 SharedPreferences pref = U.getSharedPreferences(getContext());
172 Intent intent = null;
174 switch(pref.getString("theme", "light")) {
176 intent = new Intent(getContext(), ContextMenuActivity.class);
179 intent = new Intent(getContext(), ContextMenuActivityDark.class);
184 intent.putExtra("package_name", entry.getPackageName());
185 intent.putExtra("app_name", entry.getLabel());
186 intent.putExtra("component_name", entry.getComponentName());
187 intent.putExtra("user_id", entry.getUserId(getContext()));
188 intent.putExtra("launched_from_start_menu", true);
189 intent.putExtra("x", location[0]);
190 intent.putExtra("y", location[1]);
191 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
194 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
195 DisplayManager dm = (DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
196 Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
198 getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
200 getContext().startActivity(intent);
202 }, shouldDelay() ? 100 : 0);
205 private boolean shouldDelay() {
206 SharedPreferences pref = U.getSharedPreferences(getContext());
207 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
208 && pref.getBoolean("freeform_hack", false)
209 && !FreeformHackHelper.getInstance().isFreeformHackActive();