OSDN Git Service

Taskbar 3.2
[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.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;
43
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;
52
53 import java.util.List;
54
55 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
56
57     private boolean isGrid = false;
58
59     public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
60         super(context, layout, list);
61         isGrid = layout == R.layout.row_alt;
62     }
63
64     @Override
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);
69
70         final AppEntry entry = getItem(position);
71         assert entry != null;
72
73         final SharedPreferences pref = U.getSharedPreferences(getContext());
74
75         TextView textView = (TextView) convertView.findViewById(R.id.name);
76         textView.setText(entry.getLabel());
77
78         Intent intent = new Intent();
79         intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
80         ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
81
82         if(activityInfo != null)
83             textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
84
85         switch(pref.getString("theme", "light")) {
86             case "light":
87                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
88                 break;
89             case "dark":
90                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
91                 break;
92         }
93
94         ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
95         imageView.setImageDrawable(entry.getIcon(getContext()));
96
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);
101         });
102
103         layout.setOnLongClickListener(view -> {
104             int[] location = new int[2];
105             view.getLocationOnScreen(location);
106             openContextMenu(entry, location);
107             return true;
108         });
109
110         layout.setOnGenericMotionListener((view, motionEvent) -> {
111             int action = motionEvent.getAction();
112
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);
118             }
119
120             if(action == MotionEvent.ACTION_SCROLL && pref.getBoolean("visual_feedback", true))
121                 view.setBackgroundColor(0);
122
123             return false;
124         });
125
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());
132
133                     //noinspection ResourceAsColor
134                     backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
135                     v.setBackgroundColor(backgroundTint);
136                 }
137
138                 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
139                     v.setBackgroundColor(0);
140
141                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
142                     v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
143
144                 return false;
145             });
146
147             layout.setOnTouchListener((v, event) -> {
148                 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
149                 return false;
150             });
151         }
152
153         return convertView;
154     }
155
156     private boolean isTopApp(ActivityInfo activityInfo) {
157         TopApps topApps = TopApps.getInstance(getContext());
158         return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
159     }
160
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"));
164
165         new Handler().postDelayed(() -> {
166             SharedPreferences pref = U.getSharedPreferences(getContext());
167             Intent intent = null;
168
169             switch(pref.getString("theme", "light")) {
170                 case "light":
171                     intent = new Intent(getContext(), ContextMenuActivity.class);
172                     break;
173                 case "dark":
174                     intent = new Intent(getContext(), ContextMenuActivityDark.class);
175                     break;
176             }
177
178             if(intent != null) {
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);
187             }
188
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);
192
193                 if(intent != null && U.isOPreview())
194                     intent.putExtra("context_menu_fix", true);
195
196                 getContext().startActivity(intent, U.getActivityOptions(ApplicationType.CONTEXT_MENU).setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
197             } else
198                 getContext().startActivity(intent);
199         }, shouldDelay() ? 100 : 0);
200     }
201
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();
207     }
208 }