OSDN Git Service

Cache display windowing mode since it's not consistent on Android 12
[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.SharedPreferences;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.provider.Settings;
28 import android.view.WindowManager;
29
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;
38
39 import static com.farmerbb.taskbar.util.Constants.*;
40
41 public class InvisibleActivityFreeform extends Activity {
42
43     boolean showTaskbar = false;
44     boolean doNotHide = false;
45     boolean proceedWithOnCreate = true;
46     boolean finish = false;
47     boolean bootToFreeform = false;
48     boolean initialLaunch = true;
49
50     private final BroadcastReceiver appearingReceiver = new BroadcastReceiver() {
51         @Override
52         public void onReceive(Context context, Intent intent) {
53             doNotHide = true;
54         }
55     };
56
57     private final BroadcastReceiver disappearingReceiver = new BroadcastReceiver() {
58         @Override
59         public void onReceive(Context context, Intent intent) {
60             doNotHide = false;
61         }
62     };
63
64     private final BroadcastReceiver finishReceiver = new BroadcastReceiver() {
65         @Override
66         public void onReceive(Context context, Intent intent) {
67             reallyFinish();
68         }
69     };
70
71     @SuppressLint("HardwareIds")
72     @TargetApi(Build.VERSION_CODES.N)
73     @Override
74     protected void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76
77         FreeformHackHelper helper = FreeformHackHelper.getInstance();
78         if(helper.isFreeformHackActive()) {
79             proceedWithOnCreate = false;
80             super.finish();
81         }
82
83         if(getIntent().hasExtra("check_multiwindow")) {
84             showTaskbar = false;
85
86             if(!isInMultiWindowMode()) {
87                 proceedWithOnCreate = false;
88                 super.finish();
89             }
90         }
91
92         if(U.isOverridingFreeformHack(this)) {
93             helper.setFreeformHackActive(true);
94             helper.setInFreeformWorkspace(true);
95
96             proceedWithOnCreate = false;
97             super.finish();
98             overridePendingTransition(0, 0);
99         }
100
101         if(!proceedWithOnCreate)
102             return;
103
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);
107
108         U.registerReceiver(this, appearingReceiver,
109                 ACTION_START_MENU_APPEARING,
110                 ACTION_CONTEXT_MENU_APPEARING,
111                 ACTION_DASHBOARD_APPEARING);
112
113         U.registerReceiver(this, disappearingReceiver,
114                 ACTION_START_MENU_DISAPPEARING,
115                 ACTION_CONTEXT_MENU_DISAPPEARING,
116                 ACTION_DASHBOARD_DISAPPEARING);
117
118         U.registerReceiver(this, finishReceiver, ACTION_FINISH_FREEFORM_ACTIVITY);
119
120         helper.setFreeformHackActive(true);
121
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);
131
132                         U.startActivityMaximized(U.getDisplayContext(this), intent);
133                     }
134                 }, 100);
135             }
136         }
137
138         showTaskbar = true;
139     }
140
141     @TargetApi(Build.VERSION_CODES.N)
142     @Override
143     protected void onResume() {
144         super.onResume();
145
146         // Show the taskbar when activity is resumed (no other freeform windows are active)
147         if(showTaskbar) {
148             U.sendBroadcast(this, ACTION_SHOW_TASKBAR);
149         }
150
151         if(!isInMultiWindowMode() && !initialLaunch) {
152             reallyFinish();
153         }
154
155         initialLaunch = false;
156     }
157
158     @Override
159     protected void onDestroy() {
160         super.onDestroy();
161
162         if(!proceedWithOnCreate)
163             return;
164
165         U.unregisterReceiver(this, appearingReceiver);
166         U.unregisterReceiver(this, disappearingReceiver);
167         U.unregisterReceiver(this, finishReceiver);
168
169         cleanup();
170     }
171
172     @Override
173     protected void onStart() {
174         super.onStart();
175
176         FreeformHackHelper.getInstance().setInFreeformWorkspace(true);
177
178         if(U.launcherIsDefault(this) && !U.isChromeOs(this)) {
179             LauncherHelper.getInstance().setOnPrimaryHomeScreen(true);
180             bootToFreeform = true;
181
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);
187                 editor.apply();
188
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);
193
194                     startActivity(intent);
195                 }, 250);
196             }
197
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));
202
203             if(pref.getBoolean(PREF_TASKBAR_ACTIVE, false) && !U.isServiceRunning(this, NotificationService.class))
204                 pref.edit().putBoolean(PREF_TASKBAR_ACTIVE, false).apply();
205
206             // Show the taskbar when activity is started
207             if(showTaskbar) {
208                 U.newHandler().postDelayed(() ->
209                         U.sendBroadcast(this, ACTION_SHOW_TASKBAR), 100);
210             }
211         }
212
213         // Show the taskbar when activity is started
214         if(showTaskbar) {
215             U.sendBroadcast(this, ACTION_SHOW_TASKBAR);
216         }
217     }
218
219     @Override
220     protected void onPause() {
221         super.onPause();
222
223         possiblyHideTaskbar();
224     }
225
226     @Override
227     protected void onStop() {
228         super.onStop();
229
230         if(!finish)
231             FreeformHackHelper.getInstance().setInFreeformWorkspace(false);
232
233         possiblyHideTaskbar();
234
235         if(bootToFreeform && !finish) {
236             LauncherHelper.getInstance().setOnPrimaryHomeScreen(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(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));
245
246                 U.clearCaches(this);
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         if(!doNotHide) {
257             U.newHandler().postDelayed(() -> {
258                 if(U.shouldCollapse(this, false)
259                         && !LauncherHelper.getInstance().isOnHomeScreen(this)) {
260                     U.sendBroadcast(this, ACTION_HIDE_TASKBAR);
261                 } else {
262                     U.sendBroadcast(this, ACTION_HIDE_START_MENU);
263                 }
264             }, 100);
265         }
266     }
267
268     private void reallyFinish() {
269         super.finish();
270         overridePendingTransition(0, 0);
271
272         cleanup();
273     }
274
275     private void cleanup() {
276         if(!finish) {
277             FreeformHackHelper helper = FreeformHackHelper.getInstance();
278             helper.setFreeformHackActive(false);
279             helper.setInFreeformWorkspace(false);
280
281             finish = true;
282         }
283     }
284 }