OSDN Git Service

84d83d8a7755973282194490d38ac9716018e0d3
[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.os.Build;
27 import android.os.Handler;
28 import android.support.annotation.NonNull;
29 import android.support.v4.content.ContextCompat;
30 import android.support.v4.content.LocalBroadcastManager;
31 import android.support.v4.graphics.ColorUtils;
32 import android.util.DisplayMetrics;
33 import android.view.LayoutInflater;
34 import android.view.MotionEvent;
35 import android.view.PointerIcon;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.ArrayAdapter;
39 import android.widget.ImageView;
40 import android.widget.LinearLayout;
41 import android.widget.TextView;
42
43 import com.farmerbb.taskbar.R;
44 import com.farmerbb.taskbar.activity.ContextMenuActivity;
45 import com.farmerbb.taskbar.activity.dark.ContextMenuActivityDark;
46 import com.farmerbb.taskbar.util.AppEntry;
47 import com.farmerbb.taskbar.util.ApplicationType;
48 import com.farmerbb.taskbar.util.FreeformHackHelper;
49 import com.farmerbb.taskbar.util.TopApps;
50 import com.farmerbb.taskbar.util.U;
51
52 import java.util.List;
53
54 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
55
56     private boolean isGrid = false;
57
58     public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
59         super(context, layout, list);
60         isGrid = layout == R.layout.row_alt;
61     }
62
63     @Override
64     public @NonNull View getView(int position, View convertView, final @NonNull 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);
68
69         final AppEntry entry = getItem(position);
70         assert entry != null;
71
72         final SharedPreferences pref = U.getSharedPreferences(getContext());
73
74         TextView textView = convertView.findViewById(R.id.name);
75         textView.setText(entry.getLabel());
76
77         Intent intent = new Intent();
78         intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
79         ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
80
81         if(activityInfo != null)
82             textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
83
84         switch(pref.getString("theme", "light")) {
85             case "light":
86                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
87                 break;
88             case "dark":
89                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
90                 break;
91         }
92
93         ImageView imageView = convertView.findViewById(R.id.icon);
94         imageView.setImageDrawable(entry.getIcon(getContext()));
95
96         LinearLayout layout = convertView.findViewById(R.id.entry);
97         layout.setOnClickListener(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);
100         });
101
102         layout.setOnLongClickListener(view -> {
103             int[] location = new int[2];
104             view.getLocationOnScreen(location);
105             openContextMenu(entry, location);
106             return true;
107         });
108
109         layout.setOnGenericMotionListener((view, motionEvent) -> {
110             int action = motionEvent.getAction();
111
112             if(action == MotionEvent.ACTION_BUTTON_PRESS
113                     && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
114                 int[] location = new int[2];
115                 view.getLocationOnScreen(location);
116                 openContextMenu(entry, location);
117             }
118
119             if(action == MotionEvent.ACTION_SCROLL && visualFeedbackEnabled())
120                 view.setBackgroundColor(0);
121
122             return false;
123         });
124
125         if(visualFeedbackEnabled()) {
126             layout.setOnHoverListener((v, event) -> {
127                 if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
128                     int backgroundTint = pref.getBoolean("transparent_start_menu", false)
129                             ? U.getAccentColor(getContext())
130                             : U.getBackgroundTint(getContext());
131
132                     //noinspection ResourceAsColor
133                     backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
134                     v.setBackgroundColor(backgroundTint);
135                 }
136
137                 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
138                     v.setBackgroundColor(0);
139
140                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
141                     v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
142
143                 return false;
144             });
145         }
146
147         if(pref.getBoolean("visual_feedback", true)) {
148             layout.setOnTouchListener((v, event) -> {
149                 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
150                 return false;
151             });
152         }
153
154         return convertView;
155     }
156
157     private boolean isTopApp(ActivityInfo activityInfo) {
158         TopApps topApps = TopApps.getInstance(getContext());
159         return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
160     }
161
162     @SuppressWarnings("deprecation")
163     private void openContextMenu(final AppEntry entry, final int[] location) {
164         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
165
166         new Handler().postDelayed(() -> {
167             SharedPreferences pref = U.getSharedPreferences(getContext());
168             Intent intent = null;
169
170             switch(pref.getString("theme", "light")) {
171                 case "light":
172                     intent = new Intent(getContext(), ContextMenuActivity.class);
173                     break;
174                 case "dark":
175                     intent = new Intent(getContext(), ContextMenuActivityDark.class);
176                     break;
177             }
178
179             if(intent != null) {
180                 intent.putExtra("package_name", entry.getPackageName());
181                 intent.putExtra("app_name", entry.getLabel());
182                 intent.putExtra("component_name", entry.getComponentName());
183                 intent.putExtra("user_id", entry.getUserId(getContext()));
184                 intent.putExtra("launched_from_start_menu", true);
185                 intent.putExtra("x", location[0]);
186                 intent.putExtra("y", location[1]);
187                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
188             }
189
190             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && FreeformHackHelper.getInstance().isInFreeformWorkspace()) {
191                 DisplayMetrics metrics = U.getRealDisplayMetrics(getContext());
192
193                 if(intent != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
194                     intent.putExtra("context_menu_fix", true);
195
196                 getContext().startActivity(intent, U.getActivityOptions(ApplicationType.CONTEXT_MENU).setLaunchBounds(new Rect(0, 0, metrics.widthPixels, metrics.heightPixels)).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
209     private boolean visualFeedbackEnabled() {
210         SharedPreferences pref = U.getSharedPreferences(getContext());
211         return Build.VERSION.SDK_INT < Build.VERSION_CODES.O && pref.getBoolean("visual_feedback", true);
212     }
213 }