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.IconCache;
45 import com.farmerbb.taskbar.util.LauncherHelper;
46 import com.farmerbb.taskbar.util.U;
48 public class HomeActivity extends Activity {
50 private boolean forceTaskbarStart = false;
51 private AlertDialog dialog;
53 private boolean shouldDelayFreeformHack;
56 private BroadcastReceiver killReceiver = new BroadcastReceiver() {
58 public void onReceive(Context context, Intent intent) {
63 private BroadcastReceiver forceTaskbarStartReceiver = new BroadcastReceiver() {
65 public void onReceive(Context context, Intent intent) {
66 forceTaskbarStart = true;
70 @SuppressLint("RestrictedApi")
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
75 shouldDelayFreeformHack = true;
78 getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
80 View view = new View(this) {
82 protected void onAttachedToWindow() {
83 super.onAttachedToWindow();
85 WallpaperManager wallpaperManager = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
86 wallpaperManager.setWallpaperOffsets(getWindowToken(), 0.5f, 0.5f);
88 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
89 DisplayMetrics metrics = U.getRealDisplayMetrics(HomeActivity.this);
90 wallpaperManager.suggestDesiredDimensions(metrics.widthPixels, metrics.heightPixels);
93 boolean shouldStartFreeformHack = shouldDelayFreeformHack && hits > 0;
94 shouldDelayFreeformHack = false;
96 if(shouldStartFreeformHack)
101 view.setOnClickListener(view1 -> LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU")));
103 view.setOnLongClickListener(view12 -> {
108 view.setOnGenericMotionListener((view13, motionEvent) -> {
109 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
110 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
116 final GestureDetector detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
118 public boolean onSingleTapUp(MotionEvent e) {
123 public void onShowPress(MotionEvent e) {}
126 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
131 public void onLongPress(MotionEvent e) {}
134 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
139 public boolean onDown(MotionEvent e) {
144 detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
146 public boolean onDoubleTap(MotionEvent e) {
147 final SharedPreferences pref = U.getSharedPreferences(HomeActivity.this);
148 if(!pref.getBoolean("dont_show_double_tap_dialog", false)) {
149 if(pref.getBoolean("double_tap_to_sleep", false)) {
150 U.lockDevice(HomeActivity.this);
152 AlertDialog.Builder builder = new AlertDialog.Builder(U.wrapContext(HomeActivity.this));
153 builder.setTitle(R.string.double_tap_to_sleep)
154 .setMessage(R.string.enable_double_tap_to_sleep)
155 .setNegativeButton(pref.getBoolean("double_tap_dialog_shown", false)
156 ? R.string.action_dont_show_again
157 : R.string.action_cancel, (dialog, which) -> pref.edit().putBoolean(pref.getBoolean("double_tap_dialog_shown", false)
158 ? "dont_show_double_tap_dialog"
159 : "double_tap_dialog_shown", true).apply())
160 .setPositiveButton(R.string.action_ok, (dialog, which) -> {
161 pref.edit().putBoolean("double_tap_to_sleep", true).apply();
162 U.lockDevice(HomeActivity.this);
165 AlertDialog dialog = builder.create();
174 public boolean onDoubleTapEvent(MotionEvent e) {
179 public boolean onSingleTapConfirmed(MotionEvent e) {
185 view.setOnTouchListener((v, event) -> {
186 detector.onTouchEvent(event);
190 if(U.isChromeOs(this))
193 setContentView(view);
195 getWindow().setFlags(
196 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
197 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
200 LocalBroadcastManager.getInstance(this).registerReceiver(killReceiver, new IntentFilter("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
201 LocalBroadcastManager.getInstance(this).registerReceiver(forceTaskbarStartReceiver, new IntentFilter("com.farmerbb.taskbar.FORCE_TASKBAR_RESTART"));
203 SharedPreferences pref = U.getSharedPreferences(this);
204 pref.edit().putBoolean("launcher", true).apply();
209 private void setWallpaper() {
210 if(U.shouldCollapse(this, true))
211 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
213 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
216 startActivity(Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), getString(R.string.set_wallpaper)));
217 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
220 @SuppressWarnings("deprecation")
221 @TargetApi(Build.VERSION_CODES.N)
223 protected void onResume() {
226 if(bootToFreeform()) {
227 if(U.launcherIsDefault(this))
230 U.showToastLong(this, R.string.set_as_default_home);
232 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
233 homeIntent.addCategory(Intent.CATEGORY_HOME);
234 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
237 startActivity(homeIntent);
239 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
242 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR"));
247 protected void onStart() {
250 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
252 if(U.canDrawOverlays(this)) {
253 if(!bootToFreeform()) {
254 final LauncherHelper helper = LauncherHelper.getInstance();
255 helper.setOnHomeScreen(true);
257 if(forceTaskbarStart) {
258 forceTaskbarStart = false;
259 new Handler().postDelayed(() -> {
260 helper.setOnHomeScreen(true);
265 } else if(U.launcherIsDefault(this))
268 dialog = U.showPermissionDialog(U.wrapContext(this),
269 () -> dialog = U.showErrorDialog(U.wrapContext(this), "SYSTEM_ALERT_WINDOW"),
273 private boolean bootToFreeform() {
274 SharedPreferences pref = U.getSharedPreferences(this);
275 return U.hasFreeformSupport(this)
276 && pref.getBoolean("freeform_hack", false)
277 && !U.isOverridingFreeformHack(this);
280 private void startTaskbar() {
281 SharedPreferences pref = U.getSharedPreferences(this);
282 if(pref.getBoolean("first_run", true)) {
283 SharedPreferences.Editor editor = pref.edit();
284 editor.putBoolean("first_run", false);
285 editor.putBoolean("collapsed", true);
288 dialog = U.showRecentAppsDialog(U.wrapContext(this),
289 () -> dialog = U.showErrorDialog(U.wrapContext(this), "GET_USAGE_STATS"),
293 // We always start the Taskbar and Start Menu services, even if the app isn't normally running
294 startService(new Intent(this, TaskbarService.class));
295 startService(new Intent(this, StartMenuService.class));
296 startService(new Intent(this, DashboardService.class));
298 if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
299 pref.edit().putBoolean("taskbar_active", false).apply();
301 // Show the Taskbar temporarily, as nothing else will be visible on screen
302 new Handler().postDelayed(() -> LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_SHOW_TASKBAR")), 100);
305 private void startFreeformHack() {
306 if(shouldDelayFreeformHack)
309 U.startFreeformHack(this, false, false);
313 protected void onStop() {
316 SharedPreferences pref = U.getSharedPreferences(this);
317 if(!bootToFreeform()) {
318 LauncherHelper.getInstance().setOnHomeScreen(false);
320 if(U.shouldCollapse(this, true))
321 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TEMP_HIDE_TASKBAR"));
323 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
325 // Stop the Taskbar and Start Menu services if they should normally not be active
326 if(!pref.getBoolean("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
327 stopService(new Intent(this, TaskbarService.class));
328 stopService(new Intent(this, StartMenuService.class));
329 stopService(new Intent(this, DashboardService.class));
331 IconCache.getInstance(this).clearCache();
342 protected void onDestroy() {
345 LocalBroadcastManager.getInstance(this).unregisterReceiver(killReceiver);
346 LocalBroadcastManager.getInstance(this).unregisterReceiver(forceTaskbarStartReceiver);
350 public void onBackPressed() {
351 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
354 private void killHomeActivity() {
355 LauncherHelper.getInstance().setOnHomeScreen(false);
357 // Stop the Taskbar and Start Menu services if they should normally not be active
358 SharedPreferences pref = U.getSharedPreferences(this);
359 if(!pref.getBoolean("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
360 stopService(new Intent(this, TaskbarService.class));
361 stopService(new Intent(this, StartMenuService.class));
362 stopService(new Intent(this, DashboardService.class));
364 IconCache.getInstance(this).clearCache();
366 U.stopFreeformHack(this);