OSDN Git Service

Cleanup InvisibleActivityFreeform
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / activity / InvisibleActivityFreeform.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.activity;
17
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.IntentFilter;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.provider.Settings;
30 import android.support.v4.content.LocalBroadcastManager;
31 import android.view.WindowManager;
32
33 import com.farmerbb.taskbar.activity.dark.InvisibleActivityAltDark;
34 import com.farmerbb.taskbar.service.DashboardService;
35 import com.farmerbb.taskbar.service.NotificationService;
36 import com.farmerbb.taskbar.service.StartMenuService;
37 import com.farmerbb.taskbar.service.TaskbarService;
38 import com.farmerbb.taskbar.util.FreeformHackHelper;
39 import com.farmerbb.taskbar.util.IconCache;
40 import com.farmerbb.taskbar.util.LauncherHelper;
41 import com.farmerbb.taskbar.util.U;
42
43 public class InvisibleActivityFreeform extends Activity {
44
45     boolean showTaskbar = false;
46     boolean doNotHide = false;
47     boolean proceedWithOnCreate = true;
48     boolean finish = false;
49     boolean bootToFreeform = false;
50     boolean initialLaunch = true;
51
52     private BroadcastReceiver appearingReceiver = new BroadcastReceiver() {
53         @Override
54         public void onReceive(Context context, Intent intent) {
55             doNotHide = true;
56         }
57     };
58
59     private BroadcastReceiver disappearingReceiver = new BroadcastReceiver() {
60         @Override
61         public void onReceive(Context context, Intent intent) {
62             doNotHide = false;
63         }
64     };
65
66     private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
67         @Override
68         public void onReceive(Context context, Intent intent) {
69             reallyFinish();
70         }
71     };
72
73     @SuppressLint("HardwareIds")
74     @TargetApi(Build.VERSION_CODES.N)
75     @Override
76     protected void onCreate(Bundle savedInstanceState) {
77         super.onCreate(savedInstanceState);
78
79         FreeformHackHelper helper = FreeformHackHelper.getInstance();
80         SharedPreferences pref = U.getSharedPreferences(this);
81
82         if(helper.isFreeformHackActive()) {
83             proceedWithOnCreate = false;
84             super.finish();
85         }
86
87         if(getIntent().hasExtra("check_multiwindow")) {
88             showTaskbar = false;
89
90             if(!isInMultiWindowMode()) {
91                 proceedWithOnCreate = false;
92                 super.finish();
93             }
94         }
95
96         if(U.isChromeOs(this)) {
97             helper.setFreeformHackActive(true);
98             helper.setInFreeformWorkspace(true);
99             
100             proceedWithOnCreate = false;
101             super.finish();
102         }
103
104         if(proceedWithOnCreate) {
105             // Detect outside touches, and pass them through to the underlying activity
106             getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
107             getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
108
109             IntentFilter appearingReceiverFilter = new IntentFilter();
110             appearingReceiverFilter.addAction("com.farmerbb.taskbar.START_MENU_APPEARING");
111             appearingReceiverFilter.addAction("com.farmerbb.taskbar.CONTEXT_MENU_APPEARING");
112             appearingReceiverFilter.addAction("com.farmerbb.taskbar.DASHBOARD_APPEARING");
113
114             IntentFilter disappearingReceiverFilter = new IntentFilter();
115             disappearingReceiverFilter.addAction("com.farmerbb.taskbar.START_MENU_DISAPPEARING");
116             disappearingReceiverFilter.addAction("com.farmerbb.taskbar.CONTEXT_MENU_DISAPPEARING");
117             disappearingReceiverFilter.addAction("com.farmerbb.taskbar.DASHBOARD_DISAPPEARING");
118
119             LocalBroadcastManager.getInstance(this).registerReceiver(appearingReceiver, appearingReceiverFilter);
120             LocalBroadcastManager.getInstance(this).registerReceiver(disappearingReceiver, disappearingReceiverFilter);
121             LocalBroadcastManager.getInstance(this).registerReceiver(finishReceiver, new IntentFilter("com.farmerbb.taskbar.FINISH_FREEFORM_ACTIVITY"));
122
123             helper.setFreeformHackActive(true);
124
125             // Show power button warning on CyanogenMod / LineageOS devices
126             if(getPackageManager().hasSystemFeature("com.cyanogenmod.android")
127                     && !pref.getString("power_button_warning", "null").equals(Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID))) {
128                 new Handler().postDelayed(() -> {
129                     if(helper.isInFreeformWorkspace()) {
130                         Intent intent = null;
131
132                         switch(pref.getString("theme", "light")) {
133                             case "light":
134                                 intent = new Intent(InvisibleActivityFreeform.this, InvisibleActivityAlt.class);
135                                 break;
136                             case "dark":
137                                 intent = new Intent(InvisibleActivityFreeform.this, InvisibleActivityAltDark.class);
138                                 break;
139                         }
140
141                         if(intent != null) {
142                             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
143                             intent.putExtra("power_button_warning", true);
144                         }
145
146                         U.launchAppMaximized(getApplicationContext(), intent);
147                     }
148                 }, 100);
149             }
150
151             showTaskbar = true;
152         }
153     }
154
155     @TargetApi(Build.VERSION_CODES.N)
156     @Override
157     protected void onResume() {
158         super.onResume();
159
160         // Show the taskbar when activity is resumed (no other freeform windows are active)
161         if(showTaskbar)
162             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.SHOW_TASKBAR"));
163
164         if(!isInMultiWindowMode() && !initialLaunch)
165             reallyFinish();
166
167         initialLaunch = false;
168     }
169
170     @Override
171     protected void onDestroy() {
172         super.onDestroy();
173
174         if(proceedWithOnCreate) {
175             LocalBroadcastManager.getInstance(this).unregisterReceiver(appearingReceiver);
176             LocalBroadcastManager.getInstance(this).unregisterReceiver(disappearingReceiver);
177             LocalBroadcastManager.getInstance(this).unregisterReceiver(finishReceiver);
178
179             if(!finish) {
180                 FreeformHackHelper helper = FreeformHackHelper.getInstance();
181                 helper.setFreeformHackActive(false);
182                 helper.setInFreeformWorkspace(false);
183
184                 finish = true;
185             }
186         }
187     }
188
189     @Override
190     protected void onStart() {
191         super.onStart();
192
193         FreeformHackHelper.getInstance().setInFreeformWorkspace(true);
194
195         if(U.launcherIsDefault(this)) {
196             LauncherHelper.getInstance().setOnHomeScreen(true);
197             bootToFreeform = true;
198
199             // We always start the Taskbar and Start Menu services, even if the app isn't normally running
200             startService(new Intent(this, TaskbarService.class));
201             startService(new Intent(this, StartMenuService.class));
202             startService(new Intent(this, DashboardService.class));
203
204             SharedPreferences pref = U.getSharedPreferences(this);
205             if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
206                 pref.edit().putBoolean("taskbar_active", false).apply();
207
208             // Show the taskbar when activity is started
209             new Handler().postDelayed(() -> {
210                 if(showTaskbar)
211                     LocalBroadcastManager.getInstance(InvisibleActivityFreeform.this).sendBroadcast(new Intent("com.farmerbb.taskbar.SHOW_TASKBAR"));
212             }, 100);
213         }
214
215         // Show the taskbar when activity is started
216         if(showTaskbar)
217             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.SHOW_TASKBAR"));
218     }
219
220     @Override
221     protected void onPause() {
222         super.onPause();
223
224         possiblyHideTaskbar();
225     }
226
227     @Override
228     protected void onStop() {
229         super.onStop();
230
231         if(!finish) FreeformHackHelper.getInstance().setInFreeformWorkspace(false);
232
233         possiblyHideTaskbar();
234
235         if(bootToFreeform && !finish) {
236             LauncherHelper.getInstance().setOnHomeScreen(false);
237             bootToFreeform = false;
238
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("taskbar_active", false) || pref.getBoolean("is_hidden", false)) {
242                 stopService(new Intent(this, TaskbarService.class));
243                 stopService(new Intent(this, StartMenuService.class));
244                 stopService(new Intent(this, DashboardService.class));
245
246                 IconCache.getInstance(this).clearCache();
247             }
248         }
249     }
250
251     // We don't want this activity to finish under normal circumstances
252     @Override
253     public void finish() {}
254
255     private void possiblyHideTaskbar() {
256         new Handler().postDelayed(() -> {
257             if(!doNotHide) {
258                 if(U.shouldCollapse(this, false) && !LauncherHelper.getInstance().isOnHomeScreen())
259                     LocalBroadcastManager.getInstance(InvisibleActivityFreeform.this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
260                 else
261                     LocalBroadcastManager.getInstance(InvisibleActivityFreeform.this).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
262             }
263         }, 100);
264     }
265
266     private void reallyFinish() {
267         super.finish();
268         overridePendingTransition(0, 0);
269
270         if(!finish) {
271             FreeformHackHelper helper = FreeformHackHelper.getInstance();
272             helper.setFreeformHackActive(false);
273             helper.setInFreeformWorkspace(false);
274
275             finish = true;
276         }
277     }
278 }