OSDN Git Service

Taskbar 3.0.4
[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.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;
44
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;
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 = U.getBackgroundTint(getContext());
130                     backgroundTint = ColorUtils.setAlphaComponent(backgroundTint, Color.alpha(backgroundTint) / 2);
131                     v.setBackgroundColor(backgroundTint);
132                 }
133
134                 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
135                     v.setBackgroundColor(0);
136
137                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
138                     v.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
139
140                 return false;
141             });
142
143             layout.setOnTouchListener((v, event) -> {
144                 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
145                 return false;
146             });
147         }
148
149         return convertView;
150     }
151
152     private boolean isTopApp(ActivityInfo activityInfo) {
153         TopApps topApps = TopApps.getInstance(getContext());
154         return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
155     }
156
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"));
160
161         new Handler().postDelayed(() -> {
162             SharedPreferences pref = U.getSharedPreferences(getContext());
163             Intent intent = null;
164
165             switch(pref.getString("theme", "light")) {
166                 case "light":
167                     intent = new Intent(getContext(), ContextMenuActivity.class);
168                     break;
169                 case "dark":
170                     intent = new Intent(getContext(), ContextMenuActivityDark.class);
171                     break;
172             }
173
174             if(intent != null) {
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);
183             }
184
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);
188
189                 getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
190             } else
191                 getContext().startActivity(intent);
192         }, shouldDelay() ? 100 : 0);
193     }
194
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();
200     }
201 }