OSDN Git Service

Implement visual feedback for icons
[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.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.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;
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 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);
68
69         final AppEntry entry = getItem(position);
70         final SharedPreferences pref = U.getSharedPreferences(getContext());
71
72         TextView textView = (TextView) convertView.findViewById(R.id.name);
73         textView.setText(entry.getLabel());
74
75         Intent intent = new Intent();
76         intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
77         ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);
78
79         if(activityInfo != null)
80             textView.setTypeface(null, isTopApp(activityInfo) ? Typeface.BOLD : Typeface.NORMAL);
81
82         switch(pref.getString("theme", "light")) {
83             case "light":
84                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
85                 break;
86             case "dark":
87                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
88                 break;
89         }
90
91         ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
92         imageView.setImageDrawable(entry.getIcon(getContext()));
93
94         LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
95         layout.setOnClickListener(new View.OnClickListener() {
96             @Override
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);
100             }
101         });
102
103         layout.setOnLongClickListener(new View.OnLongClickListener() {
104             @Override
105             public boolean onLongClick(View view) {
106                 int[] location = new int[2];
107                 view.getLocationOnScreen(location);
108                 openContextMenu(entry, location);
109                 return true;
110             }
111         });
112
113         layout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
114             @Override
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);
121                 }
122
123                 return false;
124             }
125         });
126
127         if(pref.getBoolean("visual_feedback", true)) {
128             layout.setOnHoverListener(new View.OnHoverListener() {
129                 @Override
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);
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             layout.setOnTouchListener(new View.OnTouchListener() {
148                 @Override
149                 public boolean onTouch(View v, MotionEvent event) {
150                     v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
151                     return false;
152                 }
153             });
154         }
155
156         return convertView;
157     }
158
159     private boolean isTopApp(ActivityInfo activityInfo) {
160         TopApps topApps = TopApps.getInstance(getContext());
161         return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
162     }
163
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"));
167
168         new Handler().postDelayed(new Runnable() {
169             @Override
170             public void run() {
171                 SharedPreferences pref = U.getSharedPreferences(getContext());
172                 Intent intent = null;
173
174                 switch(pref.getString("theme", "light")) {
175                     case "light":
176                         intent = new Intent(getContext(), ContextMenuActivity.class);
177                         break;
178                     case "dark":
179                         intent = new Intent(getContext(), ContextMenuActivityDark.class);
180                         break;
181                 }
182
183                 if(intent != null) {
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);
192                 }
193
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);
197
198                     getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
199                 } else
200                     getContext().startActivity(intent);
201             }
202         }, shouldDelay() ? 100 : 0);
203     }
204
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();
210     }
211 }