OSDN Git Service

Implement search bar for all devices + context menu fixes
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / adapter / StartMenuAdapter.java
1 /* Copyright 2016 Braden Farmer
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 package com.farmerbb.taskbar.adapter;
17
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.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.v4.content.ContextCompat;
30 import android.support.v4.content.LocalBroadcastManager;
31 import android.view.Display;
32 import android.view.LayoutInflater;
33 import android.view.MotionEvent;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.ArrayAdapter;
37 import android.widget.ImageView;
38 import android.widget.LinearLayout;
39 import android.widget.TextView;
40
41 import com.farmerbb.taskbar.R;
42 import com.farmerbb.taskbar.activity.ContextMenuActivity;
43 import com.farmerbb.taskbar.activity.ContextMenuActivityDark;
44 import com.farmerbb.taskbar.util.AppEntry;
45 import com.farmerbb.taskbar.util.FreeformHackHelper;
46 import com.farmerbb.taskbar.util.TopApps;
47 import com.farmerbb.taskbar.util.U;
48
49 import java.util.List;
50
51 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
52
53     private boolean isGrid = false;
54
55     public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
56         super(context, layout, list);
57         isGrid = layout == R.layout.row_alt;
58     }
59
60     @Override
61     public View getView(int position, View convertView, final ViewGroup parent) {
62         // Check if an existing view is being reused, otherwise inflate the view
63         if(convertView == null)
64             convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row, parent, false);
65
66         final AppEntry entry = getItem(position);
67         final SharedPreferences pref = U.getSharedPreferences(getContext());
68
69         TextView textView = (TextView) convertView.findViewById(R.id.name);
70         textView.setText(entry.getLabel());
71
72         Intent intent = new Intent();
73         intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
74         ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
75
76         if(activityInfo != null)
77             textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
78
79         switch(pref.getString("theme", "light")) {
80             case "light":
81                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
82                 break;
83             case "dark":
84                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
85                 break;
86         }
87
88         ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
89         imageView.setImageDrawable(entry.getIcon(getContext()));
90
91         LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
92         layout.setOnClickListener(new View.OnClickListener() {
93             @Override
94             public void onClick(View view) {
95                 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
96                 U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(), entry.getUserId(getContext()), null, false, false);
97             }
98         });
99
100         layout.setOnLongClickListener(new View.OnLongClickListener() {
101             @Override
102             public boolean onLongClick(View view) {
103                 int[] location = new int[2];
104                 view.getLocationOnScreen(location);
105                 openContextMenu(entry, location);
106                 return true;
107             }
108         });
109
110         layout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
111             @Override
112             public boolean onGenericMotion(View view, MotionEvent motionEvent) {
113                 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
114                         && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
115                     int[] location = new int[2];
116                     view.getLocationOnScreen(location);
117                     openContextMenu(entry, location);
118                 }
119
120                 return false;
121             }
122         });
123
124         return convertView;
125     }
126
127     private boolean isTopApp(ActivityInfo activityInfo) {
128         TopApps topApps = TopApps.getInstance(getContext());
129         return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
130     }
131
132     @SuppressWarnings("deprecation")
133     private void openContextMenu(final AppEntry entry, final int[] location) {
134         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
135
136         new Handler().postDelayed(new Runnable() {
137             @Override
138             public void run() {
139                 SharedPreferences pref = U.getSharedPreferences(getContext());
140                 Intent intent = null;
141
142                 switch(pref.getString("theme", "light")) {
143                     case "light":
144                         intent = new Intent(getContext(), ContextMenuActivity.class);
145                         break;
146                     case "dark":
147                         intent = new Intent(getContext(), ContextMenuActivityDark.class);
148                         break;
149                 }
150
151                 if(intent != null) {
152                     intent.putExtra("package_name", entry.getPackageName());
153                     intent.putExtra("app_name", entry.getLabel());
154                     intent.putExtra("component_name", entry.getComponentName());
155                     intent.putExtra("user_id", entry.getUserId(getContext()));
156                     intent.putExtra("launched_from_start_menu", true);
157                     intent.putExtra("x", location[0]);
158                     intent.putExtra("y", location[1]);
159                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
160                 }
161
162                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
163                     DisplayManager dm = (DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
164                     Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
165
166                     getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
167                 } else
168                     getContext().startActivity(intent);
169             }
170         }, shouldDelay() ? 100 : 0);
171     }
172
173     private boolean shouldDelay() {
174         SharedPreferences pref = U.getSharedPreferences(getContext());
175         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
176                 && pref.getBoolean("freeform_hack", false)
177                 && !FreeformHackHelper.getInstance().isFreeformHackActive();
178     }
179 }