OSDN Git Service

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