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.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.provider.Settings;
28 import android.view.WindowManager;
30 import com.farmerbb.taskbar.service.DashboardService;
31 import com.farmerbb.taskbar.service.NotificationService;
32 import com.farmerbb.taskbar.service.StartMenuService;
33 import com.farmerbb.taskbar.service.TaskbarService;
34 import com.farmerbb.taskbar.helper.FreeformHackHelper;
35 import com.farmerbb.taskbar.util.IconCache;
36 import com.farmerbb.taskbar.helper.LauncherHelper;
37 import com.farmerbb.taskbar.util.U;
39 import static com.farmerbb.taskbar.util.Constants.*;
41 public class InvisibleActivityFreeform extends Activity {
43 boolean showTaskbar = false;
44 boolean doNotHide = false;
45 boolean proceedWithOnCreate = true;
46 boolean finish = false;
47 boolean bootToFreeform = false;
48 boolean initialLaunch = true;
50 private final BroadcastReceiver appearingReceiver = new BroadcastReceiver() {
52 public void onReceive(Context context, Intent intent) {
57 private final BroadcastReceiver disappearingReceiver = new BroadcastReceiver() {
59 public void onReceive(Context context, Intent intent) {
64 private final BroadcastReceiver finishReceiver = new BroadcastReceiver() {
66 public void onReceive(Context context, Intent intent) {
71 @SuppressLint("HardwareIds")
72 @TargetApi(Build.VERSION_CODES.N)
74 protected void onCreate(Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
77 FreeformHackHelper helper = FreeformHackHelper.getInstance();
78 if(helper.isFreeformHackActive()) {
79 proceedWithOnCreate = false;
83 if(getIntent().hasExtra("check_multiwindow")) {
86 if(!isInMultiWindowMode()) {
87 proceedWithOnCreate = false;
92 if(U.isOverridingFreeformHack(this)) {
93 helper.setFreeformHackActive(true);
94 helper.setInFreeformWorkspace(true);
96 proceedWithOnCreate = false;
98 overridePendingTransition(0, 0);
101 if(!proceedWithOnCreate)
104 // Detect outside touches, and pass them through to the underlying activity
105 getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
106 getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
108 U.registerReceiver(this, appearingReceiver,
109 ACTION_START_MENU_APPEARING,
110 ACTION_CONTEXT_MENU_APPEARING,
111 ACTION_DASHBOARD_APPEARING);
113 U.registerReceiver(this, disappearingReceiver,
114 ACTION_START_MENU_DISAPPEARING,
115 ACTION_CONTEXT_MENU_DISAPPEARING,
116 ACTION_DASHBOARD_DISAPPEARING);
118 U.registerReceiver(this, finishReceiver, ACTION_FINISH_FREEFORM_ACTIVITY);
120 helper.setFreeformHackActive(true);
122 // Show power button warning on CyanogenMod / LineageOS devices
123 if(getPackageManager().hasSystemFeature("com.cyanogenmod.android")) {
124 SharedPreferences pref = U.getSharedPreferences(this);
125 if(!pref.getString(PREF_POWER_BUTTON_WARNING, "null").equals(Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID))) {
126 U.newHandler().postDelayed(() -> {
127 if(helper.isInFreeformWorkspace()) {
128 Intent intent = U.getThemedIntent(this, InvisibleActivityAlt.class);
129 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
130 intent.putExtra("power_button_warning", true);
132 U.startActivityMaximized(U.getDisplayContext(this), intent);
141 @TargetApi(Build.VERSION_CODES.N)
143 protected void onResume() {
146 // Show the taskbar when activity is resumed (no other freeform windows are active)
148 U.sendBroadcast(this, ACTION_SHOW_TASKBAR);
151 if(!isInMultiWindowMode() && !initialLaunch) {
155 initialLaunch = false;
159 protected void onDestroy() {
162 if(!proceedWithOnCreate)
165 U.unregisterReceiver(this, appearingReceiver);
166 U.unregisterReceiver(this, disappearingReceiver);
167 U.unregisterReceiver(this, finishReceiver);
173 protected void onStart() {
176 FreeformHackHelper.getInstance().setInFreeformWorkspace(true);
178 if(U.launcherIsDefault(this) && !U.isChromeOs(this)) {
179 LauncherHelper.getInstance().setOnPrimaryHomeScreen(true);
180 bootToFreeform = true;
182 SharedPreferences pref = U.getSharedPreferences(this);
183 if(pref.getBoolean(PREF_FIRST_RUN, true)) {
184 SharedPreferences.Editor editor = pref.edit();
185 editor.putBoolean(PREF_FIRST_RUN, false);
186 editor.putBoolean(PREF_COLLAPSED, true);
189 U.newHandler().postDelayed(() -> {
190 Intent intent = new Intent(this, DummyActivity.class);
191 intent.putExtra(EXTRA_SHOW_RECENT_APPS_DIALOG, true);
192 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
194 startActivity(intent);
198 // We always start the Taskbar and Start Menu services, even if the app isn't normally running
199 startService(new Intent(this, TaskbarService.class));
200 startService(new Intent(this, StartMenuService.class));
201 startService(new Intent(this, DashboardService.class));
203 if(pref.getBoolean(PREF_TASKBAR_ACTIVE, false) && !U.isServiceRunning(this, NotificationService.class))
204 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
206 // Show the taskbar when activity is started
208 U.newHandler().postDelayed(() ->
209 U.sendBroadcast(this, ACTION_SHOW_TASKBAR), 100);
213 // Show the taskbar when activity is started
215 U.sendBroadcast(this, ACTION_SHOW_TASKBAR);
220 protected void onPause() {
223 possiblyHideTaskbar();
227 protected void onStop() {
231 FreeformHackHelper.getInstance().setInFreeformWorkspace(false);
233 possiblyHideTaskbar();
235 if(bootToFreeform && !finish) {
236 LauncherHelper.getInstance().setOnPrimaryHomeScreen(false);
237 bootToFreeform = false;
239 // Stop the Taskbar and Start Menu services if they should normally not be active
240 SharedPreferences pref = U.getSharedPreferences(this);
241 if(!pref.getBoolean(PREF_TASKBAR_ACTIVE, false) || pref.getBoolean(PREF_IS_HIDDEN, false)) {
242 stopService(new Intent(this, TaskbarService.class));
243 stopService(new Intent(this, StartMenuService.class));
244 stopService(new Intent(this, DashboardService.class));
251 // We don't want this activity to finish under normal circumstances
253 public void finish() {}
255 private void possiblyHideTaskbar() {
257 U.newHandler().postDelayed(() -> {
258 if(U.shouldCollapse(this, false)
259 && !LauncherHelper.getInstance().isOnHomeScreen(this)) {
260 U.sendBroadcast(this, ACTION_HIDE_TASKBAR);
262 U.sendBroadcast(this, ACTION_HIDE_START_MENU);
268 private void reallyFinish() {
270 overridePendingTransition(0, 0);
275 private void cleanup() {
277 FreeformHackHelper helper = FreeformHackHelper.getInstance();
278 helper.setFreeformHackActive(false);
279 helper.setInFreeformWorkspace(false);