OSDN Git Service

Taskbar 2.0
[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.graphics.Rect;
24 import android.hardware.display.DisplayManager;
25 import android.os.Build;
26 import android.os.Handler;
27 import android.support.v4.content.ContextCompat;
28 import android.support.v4.content.LocalBroadcastManager;
29 import android.view.Display;
30 import android.view.LayoutInflater;
31 import android.view.MotionEvent;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.ArrayAdapter;
35 import android.widget.ImageView;
36 import android.widget.LinearLayout;
37 import android.widget.TextView;
38
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.activity.ContextMenuActivity;
41 import com.farmerbb.taskbar.activity.ContextMenuActivityDark;
42 import com.farmerbb.taskbar.activity.InvisibleActivityFreeform;
43 import com.farmerbb.taskbar.util.AppEntry;
44 import com.farmerbb.taskbar.util.FreeformHackHelper;
45 import com.farmerbb.taskbar.util.SavedWindowSizes;
46 import com.farmerbb.taskbar.util.U;
47
48 import java.util.List;
49
50 import static android.content.Context.DISPLAY_SERVICE;
51
52 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
53
54     private boolean isGrid = false;
55
56     public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
57         super(context, layout, list);
58         isGrid = layout == R.layout.row_alt;
59     }
60
61     @Override
62     public View getView(int position, View convertView, final ViewGroup parent) {
63         // Check if an existing view is being reused, otherwise inflate the view
64         if(convertView == null)
65             convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row, parent, false);
66
67         final AppEntry entry = getItem(position);
68         final SharedPreferences pref = U.getSharedPreferences(getContext());
69
70         TextView textView = (TextView) convertView.findViewById(R.id.name);
71         textView.setText(entry.getLabel());
72
73         switch(pref.getString("theme", "light")) {
74             case "light":
75                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
76                 break;
77             case "dark":
78                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
79                 break;
80         }
81
82         ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
83         imageView.setImageDrawable(entry.getIcon(getContext()));
84
85         LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
86         layout.setOnClickListener(new View.OnClickListener() {
87             @Override
88             public void onClick(View view) {
89                 boolean shouldDelay = false;
90
91                 SharedPreferences pref = U.getSharedPreferences(getContext());
92                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
93                         && pref.getBoolean("freeform_hack", false)
94                         && !FreeformHackHelper.getInstance().isFreeformHackActive()) {
95                     shouldDelay = true;
96
97                     Intent freeformHackIntent = new Intent(getContext(), InvisibleActivityFreeform.class);
98                     freeformHackIntent.putExtra("check_multiwindow", true);
99                     freeformHackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
100                     getContext().startActivity(freeformHackIntent);
101                 }
102
103                 if(shouldDelay) {
104                     new Handler().postDelayed(new Runnable() {
105                         @Override
106                         public void run() {
107                             launchApp(entry.getPackageName(), entry.getComponentName());
108                         }
109                     }, 100);
110                 } else launchApp(entry.getPackageName(), entry.getComponentName());
111             }
112         });
113
114         layout.setOnLongClickListener(new View.OnLongClickListener() {
115             @Override
116             public boolean onLongClick(View view) {
117                 int[] location = new int[2];
118                 view.getLocationOnScreen(location);
119                 openContextMenu(entry, location);
120                 return true;
121             }
122         });
123
124         layout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
125             @Override
126             public boolean onGenericMotion(View view, MotionEvent motionEvent) {
127                 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
128                         && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
129                     int[] location = new int[2];
130                     view.getLocationOnScreen(location);
131                     openContextMenu(entry, location);
132                 }
133
134                 return false;
135             }
136         });
137
138         return convertView;
139     }
140
141     @SuppressWarnings("deprecation")
142     private void openContextMenu(AppEntry entry, int[] location) {
143         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
144
145         SharedPreferences pref = U.getSharedPreferences(getContext());
146         Intent intent = null;
147
148         switch(pref.getString("theme", "light")) {
149             case "light":
150                 intent = new Intent(getContext(), ContextMenuActivity.class);
151                 break;
152             case "dark":
153                 intent = new Intent(getContext(), ContextMenuActivityDark.class);
154                 break;
155         }
156
157         if(intent != null) {
158             intent.putExtra("package_name", entry.getPackageName());
159             intent.putExtra("app_name", entry.getLabel());
160             intent.putExtra("component_name", entry.getComponentName());
161             intent.putExtra("launched_from_start_menu", true);
162             intent.putExtra("x", location[0]);
163             intent.putExtra("y", location[1]);
164             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
165         }
166
167         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
168             DisplayManager dm = (DisplayManager) getContext().getSystemService(DISPLAY_SERVICE);
169             Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
170
171             getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
172         } else
173             getContext().startActivity(intent);
174     }
175
176     private void launchApp(String packageName, String componentName) {
177         Intent intent = new Intent();
178         intent.setComponent(ComponentName.unflattenFromString(componentName));
179         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
180         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
181
182         SharedPreferences pref = U.getSharedPreferences(getContext());
183         if(!pref.getBoolean("freeform_hack", false))
184             U.launchStandard(getContext(), intent);
185         else switch(SavedWindowSizes.getInstance(getContext()).getWindowSize(getContext(), packageName)) {
186             case "standard":
187                 U.launchStandard(getContext(), intent);
188                 break;
189             case "large":
190                 U.launchLarge(getContext(), intent);
191                 break;
192             case "fullscreen":
193                 U.launchFullscreen(getContext(), intent, true);
194                 break;
195             case "half_left":
196                 U.launchHalfLeft(getContext(), intent, true);
197                 break;
198             case "half_right":
199                 U.launchHalfRight(getContext(), intent, true);
200                 break;
201             case "phone_size":
202                 U.launchPhoneSize(getContext(), intent);
203                 break;
204         }
205
206         if(pref.getBoolean("hide_taskbar", true) && !FreeformHackHelper.getInstance().isInFreeformWorkspace())
207             LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
208         else
209             LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
210     }
211 }