OSDN Git Service

Merge pull request #90 from utzcoz/master
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / ui / TaskbarController.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.ui;
17
18 import android.accessibilityservice.AccessibilityService;
19 import android.annotation.SuppressLint;
20 import android.annotation.TargetApi;
21 import android.app.ActivityManager;
22 import android.app.AlarmManager;
23 import android.app.usage.UsageEvents;
24 import android.app.usage.UsageStats;
25 import android.app.usage.UsageStatsManager;
26 import android.bluetooth.BluetoothAdapter;
27 import android.content.ActivityNotFoundException;
28 import android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.content.SharedPreferences;
33 import android.content.pm.LauncherActivityInfo;
34 import android.content.pm.LauncherApps;
35 import android.content.pm.PackageManager;
36 import android.content.pm.ResolveInfo;
37 import android.content.res.Resources;
38 import android.graphics.Bitmap;
39 import android.graphics.BitmapFactory;
40 import android.graphics.Color;
41 import android.graphics.Point;
42 import android.graphics.Typeface;
43 import android.graphics.drawable.BitmapDrawable;
44 import android.graphics.drawable.Drawable;
45 import android.net.ConnectivityManager;
46 import android.net.NetworkInfo;
47 import android.net.wifi.WifiInfo;
48 import android.net.wifi.WifiManager;
49 import android.os.BatteryManager;
50 import android.os.Build;
51 import android.os.Bundle;
52 import android.os.Handler;
53 import android.os.PowerManager;
54 import android.os.Process;
55 import android.os.SystemClock;
56 import android.os.UserHandle;
57 import android.os.UserManager;
58 import android.provider.Settings;
59 import android.speech.RecognizerIntent;
60 import androidx.core.content.ContextCompat;
61 import androidx.core.graphics.ColorUtils;
62 import android.telephony.PhoneStateListener;
63 import android.telephony.SignalStrength;
64 import android.telephony.TelephonyManager;
65 import android.text.format.DateFormat;
66 import android.view.Display;
67 import android.view.Gravity;
68 import android.view.LayoutInflater;
69 import android.view.MotionEvent;
70 import android.view.PointerIcon;
71 import android.view.View;
72 import android.view.ViewGroup;
73 import android.view.WindowManager;
74 import android.view.inputmethod.InputMethodManager;
75 import android.widget.Button;
76 import android.widget.FrameLayout;
77 import android.widget.ImageView;
78
79 import java.io.File;
80 import java.lang.reflect.Field;
81 import java.util.ArrayList;
82 import java.util.Collections;
83 import java.util.Date;
84 import java.util.List;
85
86 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
87 import android.widget.LinearLayout;
88 import android.widget.Space;
89 import android.widget.TextView;
90
91 import com.farmerbb.taskbar.BuildConfig;
92 import com.farmerbb.taskbar.activity.HomeActivityDelegate;
93 import com.farmerbb.taskbar.activity.MainActivity;
94 import com.farmerbb.taskbar.R;
95 import com.farmerbb.taskbar.activity.HomeActivity;
96 import com.farmerbb.taskbar.activity.InvisibleActivityFreeform;
97 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
98 import com.farmerbb.taskbar.content.TaskbarIntent;
99 import com.farmerbb.taskbar.util.AppEntry;
100 import com.farmerbb.taskbar.util.DisplayInfo;
101 import com.farmerbb.taskbar.util.FreeformHackHelper;
102 import com.farmerbb.taskbar.util.IconCache;
103 import com.farmerbb.taskbar.util.LauncherHelper;
104 import com.farmerbb.taskbar.util.PinnedBlockedApps;
105 import com.farmerbb.taskbar.util.MenuHelper;
106 import com.farmerbb.taskbar.util.U;
107
108 public class TaskbarController implements UIController {
109
110     private Context context;
111     private LinearLayout layout;
112     private ImageView startButton;
113     private LinearLayout taskbar;
114     private FrameLayout scrollView;
115     private Button button;
116     private Space space;
117     private FrameLayout dashboardButton;
118     private LinearLayout navbarButtons;
119     private LinearLayout sysTrayLayout;
120     private FrameLayout sysTrayParentLayout;
121     private TextView time;
122
123     private Handler handler;
124     private Handler handler2;
125     private Thread thread;
126     private Thread thread2;
127
128     private boolean isShowingRecents = true;
129     private boolean shouldRefreshRecents = true;
130     private boolean taskbarShownTemporarily = false;
131     private boolean taskbarHiddenTemporarily = false;
132     private boolean isRefreshingRecents = false;
133     private boolean isFirstStart = true;
134
135     private boolean startThread2 = false;
136     private boolean stopThread2 = false;
137
138     private int refreshInterval = -1;
139     private long searchInterval = -1;
140     private String sortOrder = "false";
141     private boolean runningAppsOnly = false;
142
143     private int layoutId = R.layout.tb_taskbar_left;
144     private int currentTaskbarPosition = 0;
145     private boolean showHideAutomagically = false;
146     private boolean positionIsVertical = false;
147     private boolean dashboardEnabled = false;
148     private boolean navbarButtonsEnabled = false;
149     private boolean sysTrayEnabled = false;
150
151     private List<String> currentTaskbarIds = new ArrayList<>();
152     private int numOfPinnedApps = -1;
153
154     private int cellStrength = -1;
155
156     private View.OnClickListener ocl = view -> {
157         Intent intent = new Intent(TaskbarIntent.ACTION_TOGGLE_START_MENU);
158         LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
159     };
160
161     private BroadcastReceiver showReceiver = new BroadcastReceiver() {
162         @Override
163         public void onReceive(Context context, Intent intent) {
164             showTaskbar(true);
165         }
166     };
167
168     private BroadcastReceiver hideReceiver = new BroadcastReceiver() {
169         @Override
170         public void onReceive(Context context, Intent intent) {
171             hideTaskbar(true);
172         }
173     };
174
175     private BroadcastReceiver tempShowReceiver = new BroadcastReceiver() {
176         @Override
177         public void onReceive(Context context, Intent intent) {
178             tempShowTaskbar();
179         }
180     };
181
182     private BroadcastReceiver tempHideReceiver = new BroadcastReceiver() {
183         @Override
184         public void onReceive(Context context, Intent intent) {
185             tempHideTaskbar(false);
186         }
187     };
188
189     private BroadcastReceiver startMenuAppearReceiver = new BroadcastReceiver() {
190         @Override
191         public void onReceive(Context context, Intent intent) {
192             if(startButton.getVisibility() == View.GONE
193                     && (!LauncherHelper.getInstance().isOnHomeScreen() || FreeformHackHelper.getInstance().isInFreeformWorkspace()))
194                 layout.setVisibility(View.GONE);
195         }
196     };
197
198     private BroadcastReceiver startMenuDisappearReceiver = new BroadcastReceiver() {
199         @Override
200         public void onReceive(Context context, Intent intent) {
201             if(startButton.getVisibility() == View.GONE)
202                 layout.setVisibility(View.VISIBLE);
203         }
204     };
205
206     @TargetApi(Build.VERSION_CODES.M)
207     private PhoneStateListener listener = new PhoneStateListener() {
208         @Override
209         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
210             try {
211                 cellStrength = signalStrength.getLevel();
212             } catch (SecurityException e) {
213                 cellStrength = -1;
214             }
215         }
216     };
217
218     public TaskbarController(Context context) {
219         this.context = context;
220     }
221
222     @TargetApi(Build.VERSION_CODES.M)
223     @Override
224     public void onCreateHost(UIHost host) {
225         SharedPreferences pref = U.getSharedPreferences(context);
226         if(pref.getBoolean("taskbar_active", false) || LauncherHelper.getInstance().isOnHomeScreen()) {
227             if(U.canDrawOverlays(context))
228                 drawTaskbar(host);
229             else {
230                 pref.edit().putBoolean("taskbar_active", false).apply();
231
232                 host.terminate();
233             }
234         } else host.terminate();
235     }
236
237     @SuppressLint("RtlHardcoded")
238     private void drawTaskbar(UIHost host) {
239         IconCache.getInstance(context).clearCache();
240
241         // Initialize layout params
242         WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
243         U.setCachedRotation(windowManager.getDefaultDisplay().getRotation());
244
245         final ViewParams params = new ViewParams(
246                 WindowManager.LayoutParams.WRAP_CONTENT,
247                 WindowManager.LayoutParams.WRAP_CONTENT,
248                 -1,
249                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
250         );
251
252         // Determine where to show the taskbar on screen
253         switch(U.getTaskbarPosition(context)) {
254             case "bottom_left":
255                 layoutId = R.layout.tb_taskbar_left;
256                 params.gravity = Gravity.BOTTOM | Gravity.LEFT;
257                 positionIsVertical = false;
258                 break;
259             case "bottom_vertical_left":
260                 layoutId = R.layout.tb_taskbar_vertical;
261                 params.gravity = Gravity.BOTTOM | Gravity.LEFT;
262                 positionIsVertical = true;
263                 break;
264             case "bottom_right":
265                 layoutId = R.layout.tb_taskbar_right;
266                 params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
267                 positionIsVertical = false;
268                 break;
269             case "bottom_vertical_right":
270                 layoutId = R.layout.tb_taskbar_vertical;
271                 params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
272                 positionIsVertical = true;
273                 break;
274             case "top_left":
275                 layoutId = R.layout.tb_taskbar_left;
276                 params.gravity = Gravity.TOP | Gravity.LEFT;
277                 positionIsVertical = false;
278                 break;
279             case "top_vertical_left":
280                 layoutId = R.layout.tb_taskbar_top_vertical;
281                 params.gravity = Gravity.TOP | Gravity.LEFT;
282                 positionIsVertical = true;
283                 break;
284             case "top_right":
285                 layoutId = R.layout.tb_taskbar_right;
286                 params.gravity = Gravity.TOP | Gravity.RIGHT;
287                 positionIsVertical = false;
288                 break;
289             case "top_vertical_right":
290                 layoutId = R.layout.tb_taskbar_top_vertical;
291                 params.gravity = Gravity.TOP | Gravity.RIGHT;
292                 positionIsVertical = true;
293                 break;
294         }
295
296         // Initialize views
297         SharedPreferences pref = U.getSharedPreferences(context);
298         boolean altButtonConfig = pref.getBoolean("alt_button_config", false);
299
300         layout = (LinearLayout) LayoutInflater.from(U.wrapContext(context)).inflate(layoutId, null);
301         taskbar = layout.findViewById(R.id.taskbar);
302         scrollView = layout.findViewById(R.id.taskbar_scrollview);
303
304         int backgroundTint = U.getBackgroundTint(context);
305         int accentColor = U.getAccentColor(context);
306
307         if(altButtonConfig) {
308             space = layout.findViewById(R.id.space_alt);
309             layout.findViewById(R.id.space).setVisibility(View.GONE);
310         } else {
311             space = layout.findViewById(R.id.space);
312             layout.findViewById(R.id.space_alt).setVisibility(View.GONE);
313         }
314
315         space.setOnClickListener(v -> toggleTaskbar(true));
316
317         startButton = layout.findViewById(R.id.start_button);
318         int padding = 0;
319
320         switch(pref.getString("start_button_image", U.getDefaultStartButtonImage(context))) {
321             case "default":
322                 startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
323                 padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
324                 break;
325             case "app_logo":
326                 Drawable drawable;
327
328                 if(U.isBlissOs(context)) {
329                     drawable = ContextCompat.getDrawable(context, R.drawable.tb_bliss);
330                     drawable.setTint(accentColor);
331                 } else {
332                     LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
333                     LauncherActivityInfo info = launcherApps.getActivityList(context.getPackageName(), Process.myUserHandle()).get(0);
334                     drawable = IconCache.getInstance(context).getIcon(context, context.getPackageManager(), info);
335                 }
336
337                 startButton.setImageDrawable(drawable);
338                 padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding_alt);
339                 break;
340             case "custom":
341                 File file = new File(context.getFilesDir() + "/tb_images", "custom_image");
342                 if(file.exists()) {
343                     Handler handler = new Handler();
344                     new Thread(() -> {
345                         Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
346                         handler.post(() -> {
347                             if(bitmap != null) {
348                                 BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
349                                 bitmapDrawable.setFilterBitmap(bitmap.getWidth() * bitmap.getHeight() > 2000);
350                                 startButton.setImageDrawable(bitmapDrawable);
351                             } else {
352                                 U.showToastLong(context, R.string.tb_error_reading_custom_start_image);
353                                 startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
354                             }
355                         });
356                     }).start();
357                 } else
358                     startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
359
360                 padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
361                 break;
362         }
363
364         startButton.setPadding(padding, padding, padding, padding);
365         startButton.setOnClickListener(ocl);
366         startButton.setOnLongClickListener(view -> {
367             openContextMenu();
368             return true;
369         });
370
371         startButton.setOnGenericMotionListener((view, motionEvent) -> {
372             if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
373                     && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
374                 openContextMenu();
375
376             return false;
377         });
378
379         refreshInterval = (int) (Float.parseFloat(pref.getString("refresh_frequency", "1")) * 1000);
380         if(refreshInterval == 0)
381             refreshInterval = 100;
382
383         sortOrder = pref.getString("sort_order", "false");
384         runningAppsOnly = pref.getString("recents_amount", "past_day").equals("running_apps_only");
385
386         switch(pref.getString("recents_amount", "past_day")) {
387             case "past_day":
388                 searchInterval = System.currentTimeMillis() - AlarmManager.INTERVAL_DAY;
389                 break;
390             case "app_start":
391                 long appStartTime = pref.getLong("time_of_service_start", System.currentTimeMillis());
392                 long deviceStartTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
393
394                 searchInterval = deviceStartTime > appStartTime ? deviceStartTime : appStartTime;
395                 break;
396             case "show_all":
397                 searchInterval = 0;
398                 break;
399         }
400
401         LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
402         lbm.sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_START_MENU));
403         lbm.sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_HOME_SCREEN_MARGINS"));
404
405         if(altButtonConfig) {
406             button = layout.findViewById(R.id.hide_taskbar_button_alt);
407             layout.findViewById(R.id.hide_taskbar_button).setVisibility(View.GONE);
408         } else {
409             button = layout.findViewById(R.id.hide_taskbar_button);
410             layout.findViewById(R.id.hide_taskbar_button_alt).setVisibility(View.GONE);
411         }
412
413         try {
414             button.setTypeface(Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf"));
415         } catch (RuntimeException e) { /* Gracefully fail */ }
416
417         updateButton(false);
418         button.setOnClickListener(v -> toggleTaskbar(true));
419
420         LinearLayout buttonLayout = layout.findViewById(altButtonConfig
421                 ? R.id.hide_taskbar_button_layout_alt
422                 : R.id.hide_taskbar_button_layout);
423         if(buttonLayout != null) buttonLayout.setOnClickListener(v -> toggleTaskbar(true));
424
425         LinearLayout buttonLayoutToHide = layout.findViewById(altButtonConfig
426                 ? R.id.hide_taskbar_button_layout
427                 : R.id.hide_taskbar_button_layout_alt);
428         if(buttonLayoutToHide != null) buttonLayoutToHide.setVisibility(View.GONE);
429
430         dashboardButton = layout.findViewById(R.id.dashboard_button);
431         navbarButtons = layout.findViewById(R.id.navbar_buttons);
432
433         dashboardEnabled = pref.getBoolean("dashboard", context.getResources().getBoolean(R.bool.tb_def_dashboard));
434         if(dashboardEnabled) {
435             layout.findViewById(R.id.square1).setBackgroundColor(accentColor);
436             layout.findViewById(R.id.square2).setBackgroundColor(accentColor);
437             layout.findViewById(R.id.square3).setBackgroundColor(accentColor);
438             layout.findViewById(R.id.square4).setBackgroundColor(accentColor);
439             layout.findViewById(R.id.square5).setBackgroundColor(accentColor);
440             layout.findViewById(R.id.square6).setBackgroundColor(accentColor);
441
442             dashboardButton.setOnClickListener(v -> LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.TOGGLE_DASHBOARD")));
443         } else
444             dashboardButton.setVisibility(View.GONE);
445
446         if(pref.getBoolean("button_back", false)) {
447             navbarButtonsEnabled = true;
448
449             ImageView backButton = layout.findViewById(R.id.button_back);
450             backButton.setColorFilter(accentColor);
451             backButton.setVisibility(View.VISIBLE);
452             backButton.setOnClickListener(v -> {
453                 U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_BACK);
454                 if(U.shouldCollapse(context, false))
455                     hideTaskbar(true);
456             });
457
458             backButton.setOnLongClickListener(v -> {
459                 InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
460                 imm.showInputMethodPicker();
461
462                 if(U.shouldCollapse(context, false))
463                     hideTaskbar(true);
464
465                 return true;
466             });
467
468             backButton.setOnGenericMotionListener((view13, motionEvent) -> {
469                 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
470                         && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
471                     InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
472                     imm.showInputMethodPicker();
473
474                     if(U.shouldCollapse(context, false))
475                         hideTaskbar(true);
476                 }
477                 return true;
478             });
479         }
480
481         if(pref.getBoolean("button_home", false)) {
482             navbarButtonsEnabled = true;
483
484             ImageView homeButton = layout.findViewById(R.id.button_home);
485             homeButton.setColorFilter(accentColor);
486             homeButton.setVisibility(View.VISIBLE);
487             homeButton.setOnClickListener(v -> {
488                 U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_HOME);
489                 if(U.shouldCollapse(context, false))
490                     hideTaskbar(true);
491             });
492
493             homeButton.setOnLongClickListener(v -> {
494                 Intent voiceSearchIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
495                 voiceSearchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
496
497                 try {
498                     context.startActivity(voiceSearchIntent);
499                 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
500
501                 if(U.shouldCollapse(context, false))
502                     hideTaskbar(true);
503
504                 return true;
505             });
506
507             homeButton.setOnGenericMotionListener((view13, motionEvent) -> {
508                 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
509                         && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
510                     Intent voiceSearchIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
511                     voiceSearchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
512
513                     try {
514                         context.startActivity(voiceSearchIntent);
515                     } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
516
517                     if(U.shouldCollapse(context, false))
518                         hideTaskbar(true);
519                 }
520                 return true;
521             });
522         }
523
524         if(pref.getBoolean("button_recents", false)) {
525             navbarButtonsEnabled = true;
526
527             ImageView recentsButton = layout.findViewById(R.id.button_recents);
528             recentsButton.setColorFilter(accentColor);
529             recentsButton.setVisibility(View.VISIBLE);
530             recentsButton.setOnClickListener(v -> {
531                 U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_RECENTS);
532                 if(U.shouldCollapse(context, false))
533                     hideTaskbar(true);
534             });
535
536             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
537                 recentsButton.setOnLongClickListener(v -> {
538                     U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN);
539                     if(U.shouldCollapse(context, false))
540                         hideTaskbar(true);
541
542                     return true;
543                 });
544
545                 recentsButton.setOnGenericMotionListener((view13, motionEvent) -> {
546                     if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
547                             && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
548                         U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN);
549                         if(U.shouldCollapse(context, false))
550                             hideTaskbar(true);
551                     }
552                     return true;
553                 });
554             }
555         }
556
557         if(!navbarButtonsEnabled)
558             navbarButtons.setVisibility(View.GONE);
559
560         sysTrayEnabled = U.isSystemTrayEnabled(context);
561
562         if(sysTrayEnabled) {
563             sysTrayLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.tb_system_tray, null);
564
565             FrameLayout.LayoutParams sysTrayParams = new FrameLayout.LayoutParams(
566                     FrameLayout.LayoutParams.WRAP_CONTENT,
567                     context.getResources().getDimensionPixelSize(R.dimen.tb_icon_size)
568             );
569
570             if(layoutId == R.layout.tb_taskbar_right) {
571                 time = sysTrayLayout.findViewById(R.id.time_left);
572                 sysTrayParams.gravity = Gravity.START;
573             } else {
574                 time = sysTrayLayout.findViewById(R.id.time_right);
575                 sysTrayParams.gravity = Gravity.END;
576             }
577
578             time.setVisibility(View.VISIBLE);
579             sysTrayLayout.setLayoutParams(sysTrayParams);
580
581             if(!U.isLibrary(context)) {
582                 sysTrayLayout.setOnClickListener(v -> {
583                     U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS);
584                     if(U.shouldCollapse(context, false))
585                         hideTaskbar(true);
586                 });
587
588                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
589                     sysTrayLayout.setOnLongClickListener(v -> {
590                         U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS);
591                         if(U.shouldCollapse(context, false))
592                             hideTaskbar(true);
593
594                         return true;
595                     });
596
597                     sysTrayLayout.setOnGenericMotionListener((view, motionEvent) -> {
598                         if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
599                                 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
600                             U.sendAccessibilityAction(context, AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS);
601                             if (U.shouldCollapse(context, false))
602                                 hideTaskbar(true);
603                         }
604                         return true;
605                     });
606                 }
607             }
608
609             sysTrayParentLayout = layout.findViewById(R.id.add_systray_here);
610             sysTrayParentLayout.setVisibility(View.VISIBLE);
611             sysTrayParentLayout.addView(sysTrayLayout);
612
613             TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
614             manager.listen(listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
615         }
616
617         layout.setBackgroundColor(backgroundTint);
618         layout.findViewById(R.id.divider).setBackgroundColor(accentColor);
619         button.setTextColor(accentColor);
620
621         if(isFirstStart && FreeformHackHelper.getInstance().isInFreeformWorkspace())
622             showTaskbar(false);
623         else if(!pref.getBoolean("collapsed", false) && pref.getBoolean("taskbar_active", false))
624             toggleTaskbar(false);
625
626         if(pref.getBoolean("auto_hide_navbar", false))
627             U.showHideNavigationBar(context, false);
628
629         if(FreeformHackHelper.getInstance().isTouchAbsorberActive()) {
630             lbm.sendBroadcast(new Intent("com.farmerbb.taskbar.FINISH_FREEFORM_ACTIVITY"));
631
632             new Handler().postDelayed(() -> U.startTouchAbsorberActivity(context), 500);
633         }
634
635         lbm.unregisterReceiver(showReceiver);
636         lbm.unregisterReceiver(hideReceiver);
637         lbm.unregisterReceiver(tempShowReceiver);
638         lbm.unregisterReceiver(tempHideReceiver);
639         lbm.unregisterReceiver(startMenuAppearReceiver);
640         lbm.unregisterReceiver(startMenuDisappearReceiver);
641
642         lbm.registerReceiver(showReceiver, new IntentFilter("com.farmerbb.taskbar.SHOW_TASKBAR"));
643         lbm.registerReceiver(hideReceiver, new IntentFilter(TaskbarIntent.ACTION_HIDE_TASKBAR));
644         lbm.registerReceiver(tempShowReceiver, new IntentFilter("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR"));
645         lbm.registerReceiver(tempHideReceiver, new IntentFilter("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
646         lbm.registerReceiver(
647                 startMenuAppearReceiver,
648                 new IntentFilter(TaskbarIntent.ACTION_START_MENU_APPEARING)
649         );
650         lbm.registerReceiver(
651                 startMenuDisappearReceiver,
652                 new IntentFilter(TaskbarIntent.ACTION_START_MENU_DISAPPEARING)
653         );
654
655         startRefreshingRecents();
656
657         host.addView(layout, params);
658
659         isFirstStart = false;
660     }
661
662     private void startRefreshingRecents() {
663         if(thread != null) thread.interrupt();
664         stopThread2 = true;
665
666         SharedPreferences pref = U.getSharedPreferences(context);
667         showHideAutomagically = pref.getBoolean("hide_when_keyboard_shown", false);
668
669         currentTaskbarIds.clear();
670
671         handler = new Handler();
672         thread = new Thread(() -> {
673             updateSystemTray();
674             updateRecentApps(true);
675
676             if(!isRefreshingRecents) {
677                 isRefreshingRecents = true;
678
679                 while(shouldRefreshRecents) {
680                     SystemClock.sleep(refreshInterval);
681                     updateSystemTray();
682                     updateRecentApps(false);
683
684                     if(showHideAutomagically && !positionIsVertical && !MenuHelper.getInstance().isStartMenuOpen())
685                         handler.post(() -> {
686                             if(layout != null) {
687                                 int[] location = new int[2];
688                                 layout.getLocationOnScreen(location);
689
690                                 if(location[1] != 0) {
691                                     if(location[1] > currentTaskbarPosition) {
692                                         currentTaskbarPosition = location[1];
693                                     } else if(location[1] < currentTaskbarPosition) {
694                                         if(currentTaskbarPosition - location[1] == getNavBarSize())
695                                             currentTaskbarPosition = location[1];
696                                         else if(!startThread2) {
697                                             startThread2 = true;
698                                             tempHideTaskbar(true);
699                                         }
700                                     }
701                                 }
702                             }
703                         });
704                 }
705
706                 isRefreshingRecents = false;
707             }
708         });
709
710         thread.start();
711     }
712
713     @SuppressWarnings("Convert2streamapi")
714     @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
715     private void updateRecentApps(final boolean firstRefresh) {
716         if(isScreenOff()) return;
717
718         SharedPreferences pref = U.getSharedPreferences(context);
719         final PackageManager pm = context.getPackageManager();
720         final List<AppEntry> entries = new ArrayList<>();
721         List<LauncherActivityInfo> launcherAppCache = new ArrayList<>();
722         int maxNumOfEntries = U.getMaxNumOfEntries(context);
723         int realNumOfPinnedApps = 0;
724         boolean fullLength = pref.getBoolean("full_length", context.getResources().getBoolean(R.bool.tb_def_full_length));
725
726         PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
727         List<AppEntry> pinnedApps = pba.getPinnedApps();
728         List<AppEntry> blockedApps = pba.getBlockedApps();
729         List<String> applicationIdsToRemove = new ArrayList<>();
730
731         // Filter out anything on the pinned/blocked apps lists
732         if(pinnedApps.size() > 0) {
733             UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
734             LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
735
736             for(AppEntry entry : pinnedApps) {
737                 boolean packageEnabled = launcherApps.isPackageEnabled(entry.getPackageName(),
738                         userManager.getUserForSerialNumber(entry.getUserId(context)));
739
740                 if(packageEnabled)
741                     entries.add(entry);
742                 else
743                     realNumOfPinnedApps--;
744
745                 applicationIdsToRemove.add(entry.getPackageName());
746             }
747
748             realNumOfPinnedApps = realNumOfPinnedApps + pinnedApps.size();
749         }
750
751         if(blockedApps.size() > 0) {
752             for(AppEntry entry : blockedApps) {
753                 applicationIdsToRemove.add(entry.getPackageName());
754             }
755         }
756
757         // Get list of all recently used apps
758         List<AppEntry> usageStatsList = realNumOfPinnedApps < maxNumOfEntries ? getAppEntries() : new ArrayList<>();
759         if(usageStatsList.size() > 0 || realNumOfPinnedApps > 0 || fullLength) {
760             if(realNumOfPinnedApps < maxNumOfEntries) {
761                 List<AppEntry> usageStatsList2 = new ArrayList<>();
762                 List<AppEntry> usageStatsList3 = new ArrayList<>();
763                 List<AppEntry> usageStatsList4 = new ArrayList<>();
764                 List<AppEntry> usageStatsList5 = new ArrayList<>();
765                 List<AppEntry> usageStatsList6;
766
767                 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
768                 homeIntent.addCategory(Intent.CATEGORY_HOME);
769                 ResolveInfo defaultLauncher = pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
770
771                 // Filter out apps without a launcher intent
772                 // Also filter out the current launcher, and Taskbar itself
773                 for(AppEntry packageInfo : usageStatsList) {
774                     if(hasLauncherIntent(packageInfo.getPackageName())
775                             && !packageInfo.getPackageName().contains(BuildConfig.BASE_APPLICATION_ID)
776                             && !packageInfo.getPackageName().equals(defaultLauncher.activityInfo.packageName))
777                         usageStatsList2.add(packageInfo);
778                 }
779
780                 // Filter out apps that don't fall within our current search interval
781                 for(AppEntry stats : usageStatsList2) {
782                     if(stats.getLastTimeUsed() > searchInterval || runningAppsOnly)
783                         usageStatsList3.add(stats);
784                 }
785
786                 // Sort apps by either most recently used, or most time used
787                 if(!runningAppsOnly && sortOrder.contains("most_used")) {
788                     Collections.sort(usageStatsList3, (us1, us2) -> Long.compare(us2.getTotalTimeInForeground(), us1.getTotalTimeInForeground()));
789                 } else {
790                     Collections.sort(usageStatsList3, (us1, us2) -> Long.compare(us2.getLastTimeUsed(), us1.getLastTimeUsed()));
791                 }
792
793                 // Filter out any duplicate entries
794                 List<String> applicationIds = new ArrayList<>();
795                 for(AppEntry stats : usageStatsList3) {
796                     if(!applicationIds.contains(stats.getPackageName())) {
797                         usageStatsList4.add(stats);
798                         applicationIds.add(stats.getPackageName());
799                     }
800                 }
801
802                 // Filter out the currently running foreground app, if requested by the user
803                 if(pref.getBoolean("hide_foreground", false)) {
804                     UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
805                     UsageEvents events = mUsageStatsManager.queryEvents(searchInterval, System.currentTimeMillis());
806                     UsageEvents.Event eventCache = new UsageEvents.Event();
807                     String currentForegroundApp = null;
808
809                     while(events.hasNextEvent()) {
810                         events.getNextEvent(eventCache);
811
812                         if(eventCache.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
813                             if(!(eventCache.getPackageName().contains(BuildConfig.BASE_APPLICATION_ID)
814                                     && !eventCache.getClassName().equals(MainActivity.class.getCanonicalName())
815                                     && !eventCache.getClassName().equals(HomeActivity.class.getCanonicalName())
816                                     && !eventCache.getClassName().equals(HomeActivityDelegate.class.getCanonicalName())
817                                     && !eventCache.getClassName().equals(SecondaryHomeActivity.class.getCanonicalName())
818                                     && !eventCache.getClassName().equals(InvisibleActivityFreeform.class.getCanonicalName())))
819                                 currentForegroundApp = eventCache.getPackageName();
820                         }
821                     }
822
823                     if(!applicationIdsToRemove.contains(currentForegroundApp))
824                         applicationIdsToRemove.add(currentForegroundApp);
825                 }
826
827                 for(AppEntry stats : usageStatsList4) {
828                     if(!applicationIdsToRemove.contains(stats.getPackageName())) {
829                         usageStatsList5.add(stats);
830                     }
831                 }
832
833                 // Truncate list to a maximum length
834                 if(usageStatsList5.size() > maxNumOfEntries)
835                     usageStatsList6 = usageStatsList5.subList(0, maxNumOfEntries);
836                 else
837                     usageStatsList6 = usageStatsList5;
838
839                 // Determine if we need to reverse the order
840                 boolean needToReverseOrder;
841                 switch(U.getTaskbarPosition(context)) {
842                     case "bottom_right":
843                     case "top_right":
844                         needToReverseOrder = sortOrder.contains("false");
845                         break;
846                     default:
847                         needToReverseOrder = sortOrder.contains("true");
848                         break;
849                 }
850
851                 if(needToReverseOrder) {
852                     Collections.reverse(usageStatsList6);
853                 }
854
855                 // Generate the AppEntries for the recent apps list
856                 int number = usageStatsList6.size() == maxNumOfEntries
857                         ? usageStatsList6.size() - realNumOfPinnedApps
858                         : usageStatsList6.size();
859
860                 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
861                 LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
862
863                 final List<UserHandle> userHandles = userManager.getUserProfiles();
864
865                 for(int i = 0; i < number; i++) {
866                     for(UserHandle handle : userHandles) {
867                         String packageName = usageStatsList6.get(i).getPackageName();
868                         long lastTimeUsed = usageStatsList6.get(i).getLastTimeUsed();
869                         List<LauncherActivityInfo> list = launcherApps.getActivityList(packageName, handle);
870                         if(!list.isEmpty()) {
871                             // Google App workaround
872                             if(!packageName.equals("com.google.android.googlequicksearchbox"))
873                                 launcherAppCache.add(list.get(0));
874                             else {
875                                 boolean added = false;
876                                 for(LauncherActivityInfo info : list) {
877                                     if(info.getName().equals("com.google.android.googlequicksearchbox.SearchActivity")) {
878                                         launcherAppCache.add(info);
879                                         added = true;
880                                     }
881                                 }
882
883                                 if(!added) launcherAppCache.add(list.get(0));
884                             }
885
886                             AppEntry newEntry = new AppEntry(
887                                     packageName,
888                                     null,
889                                     null,
890                                     null,
891                                     false
892                             );
893
894                             newEntry.setUserId(userManager.getSerialNumberForUser(handle));
895                             newEntry.setLastTimeUsed(lastTimeUsed);
896                             entries.add(newEntry);
897
898                             break;
899                         }
900                     }
901                 }
902             }
903
904             while(entries.size() > maxNumOfEntries) {
905                 try {
906                     entries.remove(entries.size() - 1);
907                     launcherAppCache.remove(launcherAppCache.size() - 1);
908                 } catch (ArrayIndexOutOfBoundsException e) { /* Gracefully fail */ }
909             }
910
911             // Determine if we need to reverse the order again
912             if(U.getTaskbarPosition(context).contains("vertical")) {
913                 Collections.reverse(entries);
914                 Collections.reverse(launcherAppCache);
915             }
916
917             // Now that we've generated the list of apps,
918             // we need to determine if we need to redraw the Taskbar or not
919             boolean shouldRedrawTaskbar = firstRefresh;
920
921             List<String> finalApplicationIds = new ArrayList<>();
922             for(AppEntry entry : entries) {
923                 finalApplicationIds.add(entry.getPackageName());
924             }
925
926             if(finalApplicationIds.size() != currentTaskbarIds.size()
927                     || numOfPinnedApps != realNumOfPinnedApps)
928                 shouldRedrawTaskbar = true;
929             else {
930                 for(int i = 0; i < finalApplicationIds.size(); i++) {
931                     if(!finalApplicationIds.get(i).equals(currentTaskbarIds.get(i))) {
932                         shouldRedrawTaskbar = true;
933                         break;
934                     }
935                 }
936             }
937
938             if(shouldRedrawTaskbar) {
939                 currentTaskbarIds = finalApplicationIds;
940                 numOfPinnedApps = realNumOfPinnedApps;
941
942                 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
943
944                 int launcherAppCachePos = -1;
945                 for(int i = 0; i < entries.size(); i++) {
946                     if(entries.get(i).getComponentName() == null) {
947                         launcherAppCachePos++;
948                         LauncherActivityInfo appInfo = launcherAppCache.get(launcherAppCachePos);
949                         String packageName = entries.get(i).getPackageName();
950                         long lastTimeUsed = entries.get(i).getLastTimeUsed();
951
952                         entries.remove(i);
953
954                         AppEntry newEntry = new AppEntry(
955                                 packageName,
956                                 appInfo.getComponentName().flattenToString(),
957                                 appInfo.getLabel().toString(),
958                                 IconCache.getInstance(context).getIcon(context, pm, appInfo),
959                                 false);
960
961                         newEntry.setUserId(userManager.getSerialNumberForUser(appInfo.getUser()));
962                         newEntry.setLastTimeUsed(lastTimeUsed);
963                         entries.add(i, newEntry);
964                     }
965                 }
966
967                 final int numOfEntries = Math.min(entries.size(), maxNumOfEntries);
968
969                 handler.post(() -> {
970                     if(numOfEntries > 0 || fullLength) {
971                         ViewGroup.LayoutParams params = scrollView.getLayoutParams();
972                         DisplayInfo display = U.getDisplayInfo(context, true);
973                         int recentsSize = context.getResources().getDimensionPixelSize(R.dimen.tb_icon_size) * numOfEntries;
974                         float maxRecentsSize = fullLength ? Float.MAX_VALUE : recentsSize;
975
976                         if(U.getTaskbarPosition(context).contains("vertical")) {
977                             int maxScreenSize = Math.max(0, display.height
978                                     - U.getStatusBarHeight(context)
979                                     - U.getBaseTaskbarSize(context));
980
981                             params.height = (int) Math.min(maxRecentsSize, maxScreenSize)
982                                     + context.getResources().getDimensionPixelSize(R.dimen.tb_divider_size);
983
984                             if(fullLength) {
985                                 try {
986                                     Space whitespaceTop = layout.findViewById(R.id.whitespace_top);
987                                     Space whitespaceBottom = layout.findViewById(R.id.whitespace_bottom);
988                                     int height = maxScreenSize - recentsSize;
989
990                                     if(pref.getBoolean("centered_icons", false)) {
991                                         ViewGroup.LayoutParams topParams = whitespaceTop.getLayoutParams();
992                                         topParams.height = height / 2;
993                                         whitespaceTop.setLayoutParams(topParams);
994
995                                         ViewGroup.LayoutParams bottomParams = whitespaceBottom.getLayoutParams();
996                                         bottomParams.height = height / 2;
997                                         whitespaceBottom.setLayoutParams(bottomParams);
998                                     } else if(U.getTaskbarPosition(context).contains("bottom")) {
999                                         ViewGroup.LayoutParams topParams = whitespaceTop.getLayoutParams();
1000                                         topParams.height = height;
1001                                         whitespaceTop.setLayoutParams(topParams);
1002                                     } else {
1003                                         ViewGroup.LayoutParams bottomParams = whitespaceBottom.getLayoutParams();
1004                                         bottomParams.height = height;
1005                                         whitespaceBottom.setLayoutParams(bottomParams);
1006                                     }
1007                                 } catch (NullPointerException e) { /* Gracefully fail */ }
1008                             }
1009                         } else {
1010                             int maxScreenSize = Math.max(0, display.width - U.getBaseTaskbarSize(context));
1011
1012                             params.width = (int) Math.min(maxRecentsSize, maxScreenSize)
1013                                     + context.getResources().getDimensionPixelSize(R.dimen.tb_divider_size);
1014
1015                             if(fullLength) {
1016                                 try {
1017                                     Space whitespaceLeft = layout.findViewById(R.id.whitespace_left);
1018                                     Space whitespaceRight = layout.findViewById(R.id.whitespace_right);
1019                                     int width = maxScreenSize - recentsSize;
1020
1021                                     if(pref.getBoolean("centered_icons", false)) {
1022                                         ViewGroup.LayoutParams leftParams = whitespaceLeft.getLayoutParams();
1023                                         leftParams.width = width / 2;
1024                                         whitespaceLeft.setLayoutParams(leftParams);
1025
1026                                         ViewGroup.LayoutParams rightParams = whitespaceRight.getLayoutParams();
1027                                         rightParams.width = width / 2;
1028                                         whitespaceRight.setLayoutParams(rightParams);
1029                                     } else if(U.getTaskbarPosition(context).contains("right")) {
1030                                         ViewGroup.LayoutParams leftParams = whitespaceLeft.getLayoutParams();
1031                                         leftParams.width = width;
1032                                         whitespaceLeft.setLayoutParams(leftParams);
1033                                     } else {
1034                                         ViewGroup.LayoutParams rightParams = whitespaceRight.getLayoutParams();
1035                                         rightParams.width = width;
1036                                         whitespaceRight.setLayoutParams(rightParams);
1037                                     }
1038                                 } catch (NullPointerException e) { /* Gracefully fail */ }
1039                             }
1040                         }
1041
1042                         scrollView.setLayoutParams(params);
1043
1044                         taskbar.removeAllViews();
1045                         for(int i = 0; i < entries.size(); i++) {
1046                             taskbar.addView(getView(entries, i));
1047                         }
1048
1049                         if(runningAppsOnly)
1050                             updateRunningAppIndicators(pinnedApps, usageStatsList, entries);
1051
1052                         isShowingRecents = true;
1053                         if(shouldRefreshRecents && scrollView.getVisibility() != View.VISIBLE) {
1054                             if(firstRefresh)
1055                                 scrollView.setVisibility(View.INVISIBLE);
1056                             else
1057                                 scrollView.setVisibility(View.VISIBLE);
1058                         }
1059
1060                         if(firstRefresh && scrollView.getVisibility() != View.VISIBLE)
1061                             new Handler().post(() -> {
1062                                 switch(U.getTaskbarPosition(context)) {
1063                                     case "bottom_left":
1064                                     case "bottom_right":
1065                                     case "top_left":
1066                                     case "top_right":
1067                                         if(sortOrder.contains("false"))
1068                                             scrollView.scrollTo(0, 0);
1069                                         else if(sortOrder.contains("true"))
1070                                             scrollView.scrollTo(taskbar.getWidth(), taskbar.getHeight());
1071                                         break;
1072                                     case "bottom_vertical_left":
1073                                     case "bottom_vertical_right":
1074                                     case "top_vertical_left":
1075                                     case "top_vertical_right":
1076                                         if(sortOrder.contains("false"))
1077                                             scrollView.scrollTo(taskbar.getWidth(), taskbar.getHeight());
1078                                         else if(sortOrder.contains("true"))
1079                                             scrollView.scrollTo(0, 0);
1080                                         break;
1081                                 }
1082
1083                                 if(shouldRefreshRecents) {
1084                                     scrollView.setVisibility(View.VISIBLE);
1085                                 }
1086                             });
1087                     } else {
1088                         isShowingRecents = false;
1089                         scrollView.setVisibility(View.GONE);
1090                     }
1091                 });
1092             } else if(runningAppsOnly)
1093                 handler.post(() -> updateRunningAppIndicators(pinnedApps, usageStatsList, entries));
1094         } else if(firstRefresh || currentTaskbarIds.size() > 0) {
1095             currentTaskbarIds.clear();
1096             handler.post(() -> {
1097                 isShowingRecents = false;
1098                 scrollView.setVisibility(View.GONE);
1099             });
1100         }
1101     }
1102
1103     private void updateRunningAppIndicators(List<AppEntry> pinnedApps, List<AppEntry> usageStatsList, List<AppEntry> entries) {
1104         if(taskbar.getChildCount() != entries.size())
1105             return;
1106
1107         List<String> pinnedPackageList = new ArrayList<>();
1108         List<String> runningPackageList = new ArrayList<>();
1109
1110         for(AppEntry entry : pinnedApps)
1111             pinnedPackageList.add(entry.getPackageName());
1112
1113         for(AppEntry entry : usageStatsList)
1114             runningPackageList.add(entry.getPackageName());
1115
1116         for(int i = 0; i < taskbar.getChildCount(); i++) {
1117             View convertView = taskbar.getChildAt(i);
1118             String packageName = entries.get(i).getPackageName();
1119
1120             ImageView runningAppIndicator = convertView.findViewById(R.id.running_app_indicator);
1121             if(pinnedPackageList.contains(packageName) && !runningPackageList.contains(packageName))
1122                 runningAppIndicator.setVisibility(View.GONE);
1123             else {
1124                 runningAppIndicator.setVisibility(View.VISIBLE);
1125                 runningAppIndicator.setColorFilter(U.getAccentColor(context));
1126             }
1127         }
1128     }
1129
1130     private void toggleTaskbar(boolean userInitiated) {
1131         if(userInitiated && Build.BRAND.equalsIgnoreCase("essential")) {
1132             SharedPreferences pref = U.getSharedPreferences(context);
1133             if(!pref.getBoolean("grip_rejection_toast_shown", false)) {
1134                 U.showToastLong(context, R.string.tb_essential_phone_grip_rejection);
1135                 pref.edit().putBoolean("grip_rejection_toast_shown", true).apply();
1136             }
1137         }
1138
1139         if(startButton.getVisibility() == View.GONE)
1140             showTaskbar(true);
1141         else
1142             hideTaskbar(true);
1143     }
1144
1145     private void showTaskbar(boolean clearVariables) {
1146         if(clearVariables) {
1147             taskbarShownTemporarily = false;
1148             taskbarHiddenTemporarily = false;
1149         }
1150
1151         if(startButton.getVisibility() == View.GONE) {
1152             startButton.setVisibility(View.VISIBLE);
1153             space.setVisibility(View.VISIBLE);
1154
1155             if(dashboardEnabled)
1156                 dashboardButton.setVisibility(View.VISIBLE);
1157
1158             if(navbarButtonsEnabled)
1159                 navbarButtons.setVisibility(View.VISIBLE);
1160
1161             if(isShowingRecents && scrollView.getVisibility() == View.GONE)
1162                 scrollView.setVisibility(View.INVISIBLE);
1163
1164             if(sysTrayEnabled)
1165                 sysTrayParentLayout.setVisibility(View.VISIBLE);
1166
1167             shouldRefreshRecents = true;
1168             startRefreshingRecents();
1169
1170             SharedPreferences pref = U.getSharedPreferences(context);
1171             pref.edit().putBoolean("collapsed", true).apply();
1172
1173             updateButton(false);
1174
1175             new Handler().post(() ->
1176                     LocalBroadcastManager
1177                             .getInstance(context)
1178                             .sendBroadcast(new Intent(TaskbarIntent.ACTION_SHOW_START_MENU_SPACE))
1179             );
1180         }
1181     }
1182
1183     private void hideTaskbar(boolean clearVariables) {
1184         if(clearVariables) {
1185             taskbarShownTemporarily = false;
1186             taskbarHiddenTemporarily = false;
1187         }
1188
1189         if(startButton.getVisibility() == View.VISIBLE) {
1190             startButton.setVisibility(View.GONE);
1191             space.setVisibility(View.GONE);
1192
1193             if(dashboardEnabled)
1194                 dashboardButton.setVisibility(View.GONE);
1195
1196             if(navbarButtonsEnabled)
1197                 navbarButtons.setVisibility(View.GONE);
1198
1199             if(isShowingRecents)
1200                 scrollView.setVisibility(View.GONE);
1201
1202             if(sysTrayEnabled)
1203                 sysTrayParentLayout.setVisibility(View.GONE);
1204
1205             shouldRefreshRecents = false;
1206             if(thread != null) thread.interrupt();
1207
1208             SharedPreferences pref = U.getSharedPreferences(context);
1209             pref.edit().putBoolean("collapsed", false).apply();
1210
1211             updateButton(true);
1212
1213             if(clearVariables) {
1214                 LocalBroadcastManager
1215                         .getInstance(context)
1216                         .sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_START_MENU));
1217                 LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_DASHBOARD"));
1218             }
1219
1220             new Handler().post(() ->
1221                     LocalBroadcastManager
1222                             .getInstance(context)
1223                             .sendBroadcast(new Intent(TaskbarIntent.ACTION_HIDE_START_MENU_SPACE))
1224             );
1225         }
1226     }
1227
1228     private void tempShowTaskbar() {
1229         if(!taskbarHiddenTemporarily) {
1230             SharedPreferences pref = U.getSharedPreferences(context);
1231             if(!pref.getBoolean("collapsed", false)) taskbarShownTemporarily = true;
1232         }
1233
1234         showTaskbar(false);
1235
1236         if(taskbarHiddenTemporarily)
1237             taskbarHiddenTemporarily = false;
1238     }
1239
1240     private void tempHideTaskbar(boolean monitorPositionChanges) {
1241         if(!taskbarShownTemporarily) {
1242             SharedPreferences pref = U.getSharedPreferences(context);
1243             if(pref.getBoolean("collapsed", false)) taskbarHiddenTemporarily = true;
1244         }
1245
1246         hideTaskbar(false);
1247
1248         if(taskbarShownTemporarily)
1249             taskbarShownTemporarily = false;
1250
1251         if(monitorPositionChanges && showHideAutomagically && !positionIsVertical) {
1252             if(thread2 != null) thread2.interrupt();
1253
1254             handler2 = new Handler();
1255             thread2 = new Thread(() -> {
1256                 stopThread2 = false;
1257
1258                 while(!stopThread2) {
1259                     SystemClock.sleep(refreshInterval);
1260
1261                     handler2.post(() -> stopThread2 = checkPositionChange());
1262                 }
1263
1264                 startThread2 = false;
1265             });
1266
1267             thread2.start();
1268         }
1269     }
1270
1271     private boolean checkPositionChange() {
1272         if(!isScreenOff() && layout != null) {
1273             int[] location = new int[2];
1274             layout.getLocationOnScreen(location);
1275
1276             if(location[1] == 0) {
1277                 return true;
1278             } else {
1279                 if(location[1] > currentTaskbarPosition) {
1280                     currentTaskbarPosition = location[1];
1281                     if(taskbarHiddenTemporarily) {
1282                         tempShowTaskbar();
1283                         return true;
1284                     }
1285                 } else if(location[1] == currentTaskbarPosition && taskbarHiddenTemporarily) {
1286                     tempShowTaskbar();
1287                     return true;
1288                 } else if(location[1] < currentTaskbarPosition
1289                         && currentTaskbarPosition - location[1] == getNavBarSize()) {
1290                     currentTaskbarPosition = location[1];
1291                 }
1292             }
1293         }
1294
1295         return false;
1296     }
1297
1298     private int getNavBarSize() {
1299         Point size = new Point();
1300         Point realSize = new Point();
1301
1302         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
1303         Display display = wm.getDefaultDisplay();
1304         display.getSize(size);
1305         display.getRealSize(realSize);
1306
1307         return realSize.y - size.y;
1308     }
1309
1310     @Override
1311     public void onDestroyHost(UIHost host) {
1312         shouldRefreshRecents = false;
1313
1314         if(layout != null)
1315             try {
1316                 host.removeView(layout);
1317             } catch (IllegalArgumentException e) { /* Gracefully fail */ }
1318
1319         SharedPreferences pref = U.getSharedPreferences(context);
1320         if(pref.getBoolean("skip_auto_hide_navbar", false)) {
1321             pref.edit().remove("skip_auto_hide_navbar").apply();
1322         } else if(pref.getBoolean("auto_hide_navbar", false))
1323             U.showHideNavigationBar(context, true);
1324
1325         LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
1326
1327         lbm.unregisterReceiver(showReceiver);
1328         lbm.unregisterReceiver(hideReceiver);
1329         lbm.unregisterReceiver(tempShowReceiver);
1330         lbm.unregisterReceiver(tempHideReceiver);
1331         lbm.unregisterReceiver(startMenuAppearReceiver);
1332         lbm.unregisterReceiver(startMenuDisappearReceiver);
1333
1334         if(sysTrayEnabled) {
1335             TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
1336             manager.listen(listener, PhoneStateListener.LISTEN_NONE);
1337         }
1338
1339         isFirstStart = true;
1340     }
1341
1342     private void openContextMenu() {
1343         SharedPreferences pref = U.getSharedPreferences(context);
1344
1345         Bundle args = new Bundle();
1346         args.putBoolean("dont_show_quit",
1347                 LauncherHelper.getInstance().isOnHomeScreen()
1348                         && !pref.getBoolean("taskbar_active", false));
1349         args.putBoolean("is_start_button", true);
1350
1351         U.startContextMenuActivity(context, args);
1352     }
1353
1354     private void updateButton(boolean isCollapsed) {
1355         SharedPreferences pref = U.getSharedPreferences(context);
1356         boolean hide = pref.getBoolean("invisible_button", false);
1357
1358         if(button != null) button.setText(context.getString(isCollapsed ? R.string.tb_right_arrow : R.string.tb_left_arrow));
1359         if(layout != null) layout.setAlpha(isCollapsed && hide ? 0 : 1);
1360     }
1361
1362     @TargetApi(Build.VERSION_CODES.M)
1363     @Override
1364     public void onRecreateHost(UIHost host) {
1365         if(layout != null) {
1366             try {
1367                 host.removeView(layout);
1368             } catch (IllegalArgumentException e) { /* Gracefully fail */ }
1369
1370             currentTaskbarPosition = 0;
1371
1372             if(U.canDrawOverlays(context))
1373                 drawTaskbar(host);
1374             else {
1375                 SharedPreferences pref = U.getSharedPreferences(context);
1376                 pref.edit().putBoolean("taskbar_active", false).apply();
1377
1378                 host.terminate();
1379             }
1380         }
1381     }
1382
1383     private View getView(List<AppEntry> list, int position) {
1384         View convertView = View.inflate(context, R.layout.tb_icon, null);
1385
1386         final AppEntry entry = list.get(position);
1387         final SharedPreferences pref = U.getSharedPreferences(context);
1388
1389         ImageView imageView = convertView.findViewById(R.id.icon);
1390         ImageView imageView2 = convertView.findViewById(R.id.shortcut_icon);
1391         imageView.setImageDrawable(entry.getIcon(context));
1392         imageView2.setBackgroundColor(U.getAccentColor(context));
1393
1394         String taskbarPosition = U.getTaskbarPosition(context);
1395         if(pref.getBoolean("shortcut_icon", true)) {
1396             boolean shouldShowShortcutIcon;
1397             if(taskbarPosition.contains("vertical"))
1398                 shouldShowShortcutIcon = position >= list.size() - numOfPinnedApps;
1399             else
1400                 shouldShowShortcutIcon = position < numOfPinnedApps;
1401
1402             if(shouldShowShortcutIcon) imageView2.setVisibility(View.VISIBLE);
1403         }
1404
1405         if(taskbarPosition.equals("bottom_right") || taskbarPosition.equals("top_right")) {
1406             imageView.setRotationY(180);
1407             imageView2.setRotationY(180);
1408         }
1409
1410         FrameLayout layout = convertView.findViewById(R.id.entry);
1411         layout.setOnClickListener(view -> U.launchApp(
1412                 context,
1413                 entry,
1414                 null,
1415                 true,
1416                 false,
1417                 view
1418         ));
1419
1420         layout.setOnLongClickListener(view -> {
1421             int[] location = new int[2];
1422             view.getLocationOnScreen(location);
1423             openContextMenu(entry, location);
1424             return true;
1425         });
1426
1427         layout.setOnGenericMotionListener((view, motionEvent) -> {
1428             int action = motionEvent.getAction();
1429
1430             if(action == MotionEvent.ACTION_BUTTON_PRESS
1431                     && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
1432                 int[] location = new int[2];
1433                 view.getLocationOnScreen(location);
1434                 openContextMenu(entry, location);
1435             }
1436
1437             if(action == MotionEvent.ACTION_SCROLL && pref.getBoolean("visual_feedback", true))
1438                 view.setBackgroundColor(0);
1439
1440             return false;
1441         });
1442
1443         if(pref.getBoolean("visual_feedback", true)) {
1444             layout.setOnHoverListener((v, event) -> {
1445                 if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
1446                     int accentColor = U.getAccentColor(context);
1447                     accentColor = ColorUtils.setAlphaComponent(accentColor, Color.alpha(accentColor) / 2);
1448                     v.setBackgroundColor(accentColor);
1449                 }
1450
1451                 if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
1452                     v.setBackgroundColor(0);
1453
1454                 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
1455                     v.setPointerIcon(PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT));
1456
1457                 return false;
1458             });
1459
1460             layout.setOnTouchListener((v, event) -> {
1461                 v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
1462                 return false;
1463             });
1464         }
1465
1466         return convertView;
1467     }
1468
1469     private void openContextMenu(AppEntry entry, int[] location) {
1470         Bundle args = new Bundle();
1471         args.putSerializable("app_entry", entry);
1472         args.putInt("x", location[0]);
1473         args.putInt("y", location[1]);
1474
1475         U.startContextMenuActivity(context, args);
1476     }
1477
1478     private List<AppEntry> getAppEntries() {
1479         SharedPreferences pref = U.getSharedPreferences(context);
1480         if(runningAppsOnly)
1481             return getAppEntriesUsingActivityManager(Integer.parseInt(pref.getString("max_num_of_recents", "10")));
1482         else
1483             return getAppEntriesUsingUsageStats();
1484     }
1485
1486     @SuppressWarnings("deprecation")
1487     @TargetApi(Build.VERSION_CODES.M)
1488     private List<AppEntry> getAppEntriesUsingActivityManager(int maxNum) {
1489         ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
1490         List<ActivityManager.RecentTaskInfo> usageStatsList = mActivityManager.getRecentTasks(maxNum, 0);
1491         List<AppEntry> entries = new ArrayList<>();
1492
1493         for(int i = 0; i < usageStatsList.size(); i++) {
1494             ActivityManager.RecentTaskInfo recentTaskInfo = usageStatsList.get(i);
1495             if(recentTaskInfo.id != -1) {
1496                 String packageName = recentTaskInfo.baseActivity.getPackageName();
1497                 AppEntry newEntry = new AppEntry(
1498                         packageName,
1499                         null,
1500                         null,
1501                         null,
1502                         false
1503                 );
1504
1505                 try {
1506                     Field field = ActivityManager.RecentTaskInfo.class.getField("firstActiveTime");
1507                     newEntry.setLastTimeUsed(field.getLong(recentTaskInfo));
1508                 } catch (Exception e) {
1509                     newEntry.setLastTimeUsed(i);
1510                 }
1511
1512                 entries.add(newEntry);
1513             }
1514         }
1515
1516         return entries;
1517     }
1518
1519     @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
1520     private List<AppEntry> getAppEntriesUsingUsageStats() {
1521         UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
1522         List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, searchInterval, System.currentTimeMillis());
1523         List<AppEntry> entries = new ArrayList<>();
1524
1525         for(UsageStats usageStats : usageStatsList) {
1526             AppEntry newEntry = new AppEntry(
1527                     usageStats.getPackageName(),
1528                     null,
1529                     null,
1530                     null,
1531                     false
1532             );
1533
1534             newEntry.setTotalTimeInForeground(usageStats.getTotalTimeInForeground());
1535             newEntry.setLastTimeUsed(usageStats.getLastTimeUsed());
1536             entries.add(newEntry);
1537         }
1538
1539         return entries;
1540     }
1541
1542     private boolean hasLauncherIntent(String packageName) {
1543         Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
1544         intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
1545         intentToResolve.setPackage(packageName);
1546
1547         List<ResolveInfo> ris = context.getPackageManager().queryIntentActivities(intentToResolve, 0);
1548         return ris != null && ris.size() > 0;
1549     }
1550
1551     private boolean isScreenOff() {
1552         if(U.isChromeOs(context))
1553             return false;
1554
1555         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
1556         return !pm.isInteractive();
1557     }
1558
1559     private void updateSystemTray() {
1560         if(!sysTrayEnabled || isScreenOff()) return;
1561
1562         handler.post(() -> {
1563             ImageView battery = sysTrayLayout.findViewById(R.id.battery);
1564             battery.setImageDrawable(getBatteryDrawable());
1565
1566             ImageView wifi = sysTrayLayout.findViewById(R.id.wifi);
1567             wifi.setImageDrawable(getWifiDrawable());
1568
1569             ImageView bluetooth = sysTrayLayout.findViewById(R.id.bluetooth);
1570             bluetooth.setImageDrawable(getBluetoothDrawable());
1571
1572             ImageView cellular = sysTrayLayout.findViewById(R.id.cellular);
1573             cellular.setImageDrawable(getCellularDrawable());
1574
1575             time.setText(context.getString(R.string.tb_systray_clock,
1576                     DateFormat.getTimeFormat(context).format(new Date()),
1577                     DateFormat.getDateFormat(context).format(new Date())));
1578             time.setTextColor(U.getAccentColor(context));
1579         });
1580     }
1581
1582     @TargetApi(Build.VERSION_CODES.M)
1583     private Drawable getBatteryDrawable() {
1584         BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
1585         int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
1586
1587         if(batLevel == Integer.MIN_VALUE)
1588             return null;
1589
1590         IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
1591         Intent batteryStatus = context.registerReceiver(null, ifilter);
1592
1593         int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
1594         boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
1595                 status == BatteryManager.BATTERY_STATUS_FULL;
1596
1597         String batDrawable;
1598         if(batLevel < 10 && !isCharging)
1599             batDrawable = "alert";
1600         else if(batLevel < 25)
1601             batDrawable = "20";
1602         else if(batLevel < 40)
1603             batDrawable = "30";
1604         else if(batLevel < 55)
1605             batDrawable = "50";
1606         else if(batLevel < 70)
1607             batDrawable = "60";
1608         else if(batLevel < 85)
1609             batDrawable = "80";
1610         else if(batLevel < 95)
1611             batDrawable = "90";
1612         else
1613             batDrawable = "full";
1614
1615         String charging;
1616         if(isCharging)
1617             charging = "charging_";
1618         else
1619             charging = "";
1620
1621         String batRes = "tb_battery_" + charging + batDrawable;
1622         int id = getResourceIdFor(batRes);
1623
1624         return getDrawableForSysTray(id);
1625     }
1626
1627     @TargetApi(Build.VERSION_CODES.M)
1628     private Drawable getWifiDrawable() {
1629         ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1630
1631         NetworkInfo ethernet = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
1632         if(ethernet != null && ethernet.isConnected())
1633             return getDrawableForSysTray(R.drawable.tb_settings_ethernet);
1634
1635         NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
1636         if(wifi == null || !wifi.isConnected())
1637             return null;
1638
1639         WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
1640         int numberOfLevels = 5;
1641
1642         WifiInfo wifiInfo = wifiManager.getConnectionInfo();
1643         int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
1644
1645         String wifiRes = "tb_signal_wifi_" + level + "_bar";
1646         int id = getResourceIdFor(wifiRes);
1647
1648         return getDrawableForSysTray(id);
1649     }
1650
1651     private Drawable getBluetoothDrawable() {
1652         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
1653         if(adapter != null && adapter.isEnabled())
1654             return getDrawableForSysTray(R.drawable.tb_bluetooth);
1655
1656         return null;
1657     }
1658
1659     @TargetApi(Build.VERSION_CODES.M)
1660     private Drawable getCellularDrawable() {
1661         if(Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0)
1662             return getDrawableForSysTray(R.drawable.tb_airplanemode_active);
1663
1664         if(cellStrength == -1)
1665             return null;
1666
1667         String cellRes = "tb_signal_cellular_" + cellStrength + "_bar";
1668         int id = getResourceIdFor(cellRes);
1669
1670         return getDrawableForSysTray(id);
1671     }
1672
1673     private Drawable getDrawableForSysTray(int id) {
1674         Drawable drawable = null;
1675         try {
1676             drawable = ContextCompat.getDrawable(context, id);
1677         } catch (Resources.NotFoundException e) { /* Gracefully fail */ }
1678
1679         if(drawable == null) return null;
1680
1681         drawable.setTint(U.getAccentColor(context));
1682         return drawable;
1683     }
1684
1685     private int getResourceIdFor(String name) {
1686         String packageName = context.getResources().getResourcePackageName(R.drawable.tb_dummy);
1687         return context.getResources().getIdentifier(name, "drawable", packageName);
1688     }
1689 }