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.util.DisplayMetrics;
34 import android.view.GestureDetector;
35 import android.view.MotionEvent;
36 import android.view.View;
37 import android.view.WindowManager;
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.service.DashboardService;
41 import com.farmerbb.taskbar.service.NotificationService;
42 import com.farmerbb.taskbar.service.StartMenuService;
43 import com.farmerbb.taskbar.service.TaskbarService;
44 import com.farmerbb.taskbar.util.CompatUtils;
45 import com.farmerbb.taskbar.util.IconCache;
46 import com.farmerbb.taskbar.util.LauncherHelper;
47 import com.farmerbb.taskbar.util.U;
49 public class HomeActivity extends Activity {
51 private boolean forceTaskbarStart = false;
52 private AlertDialog dialog;
54 private boolean shouldDelayFreeformHack;
57 private BroadcastReceiver killReceiver = new BroadcastReceiver() {
59 public void onReceive(Context context, Intent intent) {
64 private BroadcastReceiver forceTaskbarStartReceiver = new BroadcastReceiver() {
66 public void onReceive(Context context, Intent intent) {
67 forceTaskbarStart = true;
71 private BroadcastReceiver freeformToggleReceiver = new BroadcastReceiver() {
73 public void onReceive(Context context, Intent intent) {
78 @SuppressLint("RestrictedApi")
80 protected void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
83 shouldDelayFreeformHack = true;
86 WindowManager.LayoutParams params = getWindow().getAttributes();
87 if(CompatUtils.applyDisplayCutoutModeTo(params))
88 getWindow().setAttributes(params);
90 getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
92 View view = new View(this) {
94 protected void onAttachedToWindow() {
95 super.onAttachedToWindow();
97 WallpaperManager wallpaperManager = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
98 wallpaperManager.setWallpaperOffsets(getWindowToken(), 0.5f, 0.5f);
100 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
101 DisplayMetrics metrics = U.getRealDisplayMetrics(HomeActivity.this);
102 wallpaperManager.suggestDesiredDimensions(metrics.widthPixels, metrics.heightPixels);
105 boolean shouldStartFreeformHack = shouldDelayFreeformHack && hits > 0;
106 shouldDelayFreeformHack = false;
108 if(shouldStartFreeformHack)
113 view.setOnClickListener(view1 -> LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU")));
115 view.setOnLongClickListener(view12 -> {
120 view.setOnGenericMotionListener((view13, motionEvent) -> {
121 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
122 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
128 final GestureDetector detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
130 public boolean onSingleTapUp(MotionEvent e) {
135 public void onShowPress(MotionEvent e) {}
138 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
143 public void onLongPress(MotionEvent e) {}
146 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
151 public boolean onDown(MotionEvent e) {
156 detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
158 public boolean onDoubleTap(MotionEvent e) {
159 final SharedPreferences pref = U.getSharedPreferences(HomeActivity.this);
160 if(!pref.getBoolean("dont_show_double_tap_dialog", false)) {
161 if(pref.getBoolean("double_tap_to_sleep", false)) {
162 U.lockDevice(HomeActivity.this);
164 AlertDialog.Builder builder = new AlertDialog.Builder(U.wrapContext(HomeActivity.this));
165 builder.setTitle(R.string.double_tap_to_sleep)
166 .setMessage(R.string.enable_double_tap_to_sleep)
167 .setNegativeButton(pref.getBoolean("double_tap_dialog_shown", false)
168 ? R.string.action_dont_show_again
169 : R.string.action_cancel, (dialog, which) -> pref.edit().putBoolean(pref.getBoolean("double_tap_dialog_shown", false)
170 ? "dont_show_double_tap_dialog"
171 : "double_tap_dialog_shown", true).apply())
172 .setPositiveButton(R.string.action_ok, (dialog, which) -> {
173 pref.edit().putBoolean("double_tap_to_sleep", true).apply();
174 U.lockDevice(HomeActivity.this);
177 AlertDialog dialog = builder.create();
186 public boolean onDoubleTapEvent(MotionEvent e) {
191 public boolean onSingleTapConfirmed(MotionEvent e) {
197 view.setOnTouchListener((v, event) -> {
198 detector.onTouchEvent(event);
202 if(U.isChromeOs(this))
205 setContentView(view);
209 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
210 lbm.registerReceiver(killReceiver, new IntentFilter("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
211 lbm.registerReceiver(forceTaskbarStartReceiver, new IntentFilter("com.farmerbb.taskbar.FORCE_TASKBAR_RESTART"));
212 lbm.registerReceiver(freeformToggleReceiver, new IntentFilter("com.farmerbb.taskbar.UPDATE_FREEFORM_CHECKBOX"));
214 SharedPreferences pref = U.getSharedPreferences(this);
215 pref.edit().putBoolean("launcher", true).apply();
220 private void setWallpaper() {
221 if(U.shouldCollapse(this, true))
222 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
224 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
227 startActivity(Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), getString(R.string.set_wallpaper)));
228 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
231 @SuppressWarnings("deprecation")
232 @TargetApi(Build.VERSION_CODES.N)
234 protected void onResume() {
237 if(bootToFreeform()) {
238 if(U.launcherIsDefault(this))
241 U.showToastLong(this, R.string.set_as_default_home);
243 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
244 homeIntent.addCategory(Intent.CATEGORY_HOME);
245 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
248 startActivity(homeIntent);
250 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
253 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR"));
258 protected void onStart() {
261 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
263 if(U.canDrawOverlays(this)) {
264 if(!bootToFreeform()) {
265 final LauncherHelper helper = LauncherHelper.getInstance();
266 helper.setOnHomeScreen(true);
268 if(forceTaskbarStart) {
269 forceTaskbarStart = false;
270 new Handler().postDelayed(() -> {
271 helper.setOnHomeScreen(true);
276 } else if(U.launcherIsDefault(this))
279 dialog = U.showPermissionDialog(U.wrapContext(this),
280 () -> dialog = U.showErrorDialog(U.wrapContext(this), "SYSTEM_ALERT_WINDOW"),
284 private boolean bootToFreeform() {
285 SharedPreferences pref = U.getSharedPreferences(this);
286 return U.hasFreeformSupport(this)
287 && pref.getBoolean("freeform_hack", false)
288 && !U.isOverridingFreeformHack(this);
291 private void startTaskbar() {
292 SharedPreferences pref = U.getSharedPreferences(this);
293 if(pref.getBoolean("first_run", true)) {
294 SharedPreferences.Editor editor = pref.edit();
295 editor.putBoolean("first_run", false);
296 editor.putBoolean("collapsed", true);
299 dialog = U.showRecentAppsDialog(U.wrapContext(this),
300 () -> dialog = U.showErrorDialog(U.wrapContext(this), "GET_USAGE_STATS"),
304 // We always start the Taskbar and Start Menu services, even if the app isn't normally running
305 startService(new Intent(this, TaskbarService.class));
306 startService(new Intent(this, StartMenuService.class));
307 startService(new Intent(this, DashboardService.class));
309 if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
310 pref.edit().putBoolean("taskbar_active", false).apply();
312 // Show the Taskbar temporarily, as nothing else will be visible on screen
313 new Handler().postDelayed(() -> LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR")), 100);
316 private void startFreeformHack() {
317 if(shouldDelayFreeformHack)
320 U.startFreeformHack(this);
324 protected void onStop() {
327 SharedPreferences pref = U.getSharedPreferences(this);
328 if(!bootToFreeform()) {
329 LauncherHelper.getInstance().setOnHomeScreen(false);
331 if(U.shouldCollapse(this, true))
332 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
334 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
336 // Stop the Taskbar and Start Menu services if they should normally not be active
337 if(!pref.getBoolean("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
338 stopService(new Intent(this, TaskbarService.class));
339 stopService(new Intent(this, StartMenuService.class));
340 stopService(new Intent(this, DashboardService.class));
342 IconCache.getInstance(this).clearCache();
353 protected void onDestroy() {
356 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
357 lbm.unregisterReceiver(killReceiver);
358 lbm.unregisterReceiver(forceTaskbarStartReceiver);
359 lbm.unregisterReceiver(freeformToggleReceiver);
363 public void onBackPressed() {
364 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
367 private void killHomeActivity() {
368 LauncherHelper.getInstance().setOnHomeScreen(false);
370 // Stop the Taskbar and Start Menu services if they should normally not be active
371 SharedPreferences pref = U.getSharedPreferences(this);
372 if(!pref.getBoolean("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
373 stopService(new Intent(this, TaskbarService.class));
374 stopService(new Intent(this, StartMenuService.class));
375 stopService(new Intent(this, DashboardService.class));
377 IconCache.getInstance(this).clearCache();
379 U.stopFreeformHack(this);
385 private void updateWindowFlags() {
386 if(U.isOverridingFreeformHack(this)) {
387 getWindow().setFlags(
388 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
389 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
392 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);