1 /* Copyright 2016 Braden Farmer
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.farmerbb.taskbar.activity;
18 import android.annotation.SuppressLint;
19 import android.annotation.TargetApi;
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.app.WallpaperManager;
23 import android.content.ActivityNotFoundException;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.SharedPreferences;
29 import android.os.Build;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.support.v4.content.LocalBroadcastManager;
33 import android.view.MotionEvent;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.FrameLayout;
38 import com.farmerbb.taskbar.R;
39 import com.farmerbb.taskbar.service.DashboardService;
40 import com.farmerbb.taskbar.service.NotificationService;
41 import com.farmerbb.taskbar.service.StartMenuService;
42 import com.farmerbb.taskbar.service.TaskbarService;
43 import com.farmerbb.taskbar.ui.DashboardController;
44 import com.farmerbb.taskbar.ui.Host;
45 import com.farmerbb.taskbar.ui.ViewParams;
46 import com.farmerbb.taskbar.ui.StartMenuController;
47 import com.farmerbb.taskbar.ui.TaskbarController;
48 import com.farmerbb.taskbar.util.CompatUtils;
49 import com.farmerbb.taskbar.util.DisplayInfo;
50 import com.farmerbb.taskbar.util.FreeformHackHelper;
51 import com.farmerbb.taskbar.util.IconCache;
52 import com.farmerbb.taskbar.util.LauncherHelper;
53 import com.farmerbb.taskbar.util.U;
55 public class HomeActivityDelegate extends Activity implements Host {
56 private TaskbarController taskbarController;
57 private StartMenuController startMenuController;
58 private DashboardController dashboardController;
60 private FrameLayout layout;
62 private boolean forceTaskbarStart = false;
63 private AlertDialog dialog;
65 private boolean shouldDelayFreeformHack;
68 private BroadcastReceiver killReceiver = new BroadcastReceiver() {
70 public void onReceive(Context context, Intent intent) {
75 private BroadcastReceiver forceTaskbarStartReceiver = new BroadcastReceiver() {
77 public void onReceive(Context context, Intent intent) {
78 forceTaskbarStart = true;
82 private BroadcastReceiver restartReceiver = new BroadcastReceiver() {
84 public void onReceive(Context context, Intent intent) {
85 if(taskbarController != null) taskbarController.onRecreateHost(HomeActivityDelegate.this);
86 if(startMenuController != null) startMenuController.onRecreateHost(HomeActivityDelegate.this);
87 if(dashboardController != null) dashboardController.onRecreateHost(HomeActivityDelegate.this);
91 private BroadcastReceiver freeformToggleReceiver = new BroadcastReceiver() {
93 public void onReceive(Context context, Intent intent) {
98 @SuppressLint("RestrictedApi")
100 protected void onCreate(Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
103 shouldDelayFreeformHack = true;
106 WindowManager.LayoutParams params = getWindow().getAttributes();
107 if(CompatUtils.applyDisplayCutoutModeTo(params))
108 getWindow().setAttributes(params);
110 SharedPreferences pref = U.getSharedPreferences(this);
112 layout = new FrameLayout(this) {
114 protected void onAttachedToWindow() {
115 super.onAttachedToWindow();
117 WallpaperManager wallpaperManager = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
118 wallpaperManager.setWallpaperOffsets(getWindowToken(), 0.5f, 0.5f);
120 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
121 DisplayInfo display = U.getDisplayInfo(HomeActivityDelegate.this);
122 wallpaperManager.suggestDesiredDimensions(display.width, display.height);
125 boolean shouldStartFreeformHack = shouldDelayFreeformHack && hits > 0;
126 shouldDelayFreeformHack = false;
128 if(shouldStartFreeformHack)
133 layout.setOnClickListener(view1 -> LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU")));
135 layout.setOnLongClickListener(view2 -> {
136 if(!pref.getBoolean("freeform_hack", false))
142 layout.setOnGenericMotionListener((view3, motionEvent) -> {
143 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
144 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY
145 && !pref.getBoolean("freeform_hack", false))
151 layout.setFitsSystemWindows(true);
153 if((this instanceof HomeActivity || U.isLauncherPermanentlyEnabled(this))
154 && !U.isChromeOs(this)) {
155 setContentView(layout);
156 pref.edit().putBoolean("launcher", true).apply();
162 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
163 lbm.registerReceiver(killReceiver, new IntentFilter("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
164 lbm.registerReceiver(forceTaskbarStartReceiver, new IntentFilter("com.farmerbb.taskbar.FORCE_TASKBAR_RESTART"));
165 lbm.registerReceiver(restartReceiver, new IntentFilter("com.farmerbb.taskbar.RESTART"));
167 IntentFilter intentFilter = new IntentFilter();
168 intentFilter.addAction("com.farmerbb.taskbar.UPDATE_FREEFORM_CHECKBOX");
169 intentFilter.addAction("com.farmerbb.taskbar.TOUCH_ABSORBER_STATE_CHANGED");
171 lbm.registerReceiver(freeformToggleReceiver, intentFilter);
176 private void setWallpaper() {
177 if(U.shouldCollapse(this, true))
178 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
180 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
183 startActivity(Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), getString(R.string.set_wallpaper)));
184 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
187 @TargetApi(Build.VERSION_CODES.N)
189 protected void onResume() {
192 if(U.canBootToFreeform(this)) {
193 if(U.launcherIsDefault(this))
196 U.showToastLong(this, R.string.set_as_default_home);
198 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
199 homeIntent.addCategory(Intent.CATEGORY_HOME);
200 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
203 startActivity(homeIntent);
205 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
208 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR"));
213 protected void onStart() {
216 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
218 if(U.canDrawOverlays(this, true)) {
219 if(!U.canBootToFreeform(this)) {
220 final LauncherHelper helper = LauncherHelper.getInstance();
221 helper.setOnHomeScreen(true);
223 if(forceTaskbarStart) {
224 forceTaskbarStart = false;
225 new Handler().postDelayed(() -> {
226 helper.setOnHomeScreen(true);
231 } else if(U.launcherIsDefault(this))
234 dialog = U.showPermissionDialog(U.wrapContext(this),
235 () -> dialog = U.showErrorDialog(U.wrapContext(this), "SYSTEM_ALERT_WINDOW"),
239 private void startTaskbar() {
240 SharedPreferences pref = U.getSharedPreferences(this);
241 if(pref.getBoolean("first_run", true)) {
242 SharedPreferences.Editor editor = pref.edit();
243 editor.putBoolean("first_run", false);
244 editor.putBoolean("collapsed", true);
247 dialog = U.showRecentAppsDialog(U.wrapContext(this),
248 () -> dialog = U.showErrorDialog(U.wrapContext(this), "GET_USAGE_STATS"),
252 if(U.isHomeActivityUIHost()) {
253 // Stop any currently running services and switch to using HomeActivityDelegate as UI host
254 stopService(new Intent(this, TaskbarService.class));
255 stopService(new Intent(this, StartMenuService.class));
256 stopService(new Intent(this, DashboardService.class));
258 taskbarController = new TaskbarController(this);
259 startMenuController = new StartMenuController(this);
260 dashboardController = new DashboardController(this);
262 taskbarController.onCreateHost(this);
263 startMenuController.onCreateHost(this);
264 dashboardController.onCreateHost(this);
266 // We always start the Taskbar and Start Menu services, even if the app isn't normally running
267 startService(new Intent(this, TaskbarService.class));
268 startService(new Intent(this, StartMenuService.class));
269 startService(new Intent(this, DashboardService.class));
272 if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
273 pref.edit().putBoolean("taskbar_active", false).apply();
275 // Show the Taskbar temporarily, as nothing else will be visible on screen
276 new Handler().postDelayed(() -> LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR")), 100);
279 private void startFreeformHack() {
280 if(shouldDelayFreeformHack)
283 U.startFreeformHack(this);
287 protected void onStop() {
290 SharedPreferences pref = U.getSharedPreferences(this);
291 if(!U.canBootToFreeform(this)) {
292 LauncherHelper.getInstance().setOnHomeScreen(false);
294 if(U.shouldCollapse(this, true))
295 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
297 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
299 if(U.isHomeActivityUIHost()) {
300 if(taskbarController != null) taskbarController.onDestroyHost(this);
301 if(startMenuController != null) startMenuController.onDestroyHost(this);
302 if(dashboardController != null) dashboardController.onDestroyHost(this);
304 IconCache.getInstance(this).clearCache();
306 // Stop using HomeActivityDelegate as UI host and restart services if needed
307 if(pref.getBoolean("taskbar_active", false) && !pref.getBoolean("is_hidden", false)) {
308 startService(new Intent(this, TaskbarService.class));
309 startService(new Intent(this, StartMenuService.class));
310 startService(new Intent(this, DashboardService.class));
313 // Stop the Taskbar and Start Menu services if they should normally not be active
314 if(!pref.getBoolean("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
315 stopService(new Intent(this, TaskbarService.class));
316 stopService(new Intent(this, StartMenuService.class));
317 stopService(new Intent(this, DashboardService.class));
319 IconCache.getInstance(this).clearCache();
331 protected void onDestroy() {
334 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
335 lbm.unregisterReceiver(killReceiver);
336 lbm.unregisterReceiver(forceTaskbarStartReceiver);
337 lbm.unregisterReceiver(restartReceiver);
338 lbm.unregisterReceiver(freeformToggleReceiver);
342 public void onBackPressed() {
343 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
346 private void killHomeActivity() {
347 LauncherHelper.getInstance().setOnHomeScreen(false);
349 if(U.isHomeActivityUIHost()) {
350 if(taskbarController != null) taskbarController.onDestroyHost(this);
351 if(startMenuController != null) startMenuController.onDestroyHost(this);
352 if(dashboardController != null) dashboardController.onDestroyHost(this);
354 IconCache.getInstance(this).clearCache();
356 U.stopFreeformHack(this);
358 // Stop using HomeActivityDelegate as UI host and restart services if needed
359 SharedPreferences pref = U.getSharedPreferences(this);
360 if(pref.getBoolean("taskbar_active", false) && !pref.getBoolean("is_hidden", false)) {
361 startService(new Intent(this, TaskbarService.class));
362 startService(new Intent(this, StartMenuService.class));
363 startService(new Intent(this, DashboardService.class));
366 // Stop the Taskbar and Start Menu services if they should normally not be active
367 SharedPreferences pref = U.getSharedPreferences(this);
368 if(!pref.getBoolean("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
369 stopService(new Intent(this, TaskbarService.class));
370 stopService(new Intent(this, StartMenuService.class));
371 stopService(new Intent(this, DashboardService.class));
373 IconCache.getInstance(this).clearCache();
375 U.stopFreeformHack(this);
382 private void updateWindowFlags() {
383 int flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
384 if(FreeformHackHelper.getInstance().isTouchAbsorberActive() && U.isOverridingFreeformHack(this))
385 getWindow().setFlags(flags, flags);
387 getWindow().clearFlags(flags);
391 public void addView(View view, ViewParams params) {
392 final FrameLayout.LayoutParams flParams = new FrameLayout.LayoutParams(
397 if(params.gravity > -1)
398 flParams.gravity = params.gravity;
400 view.setLayoutParams(flParams);
401 layout.addView(view);
405 public void removeView(View view) {
406 layout.removeView(view);
410 public void terminate() {