OSDN Git Service

b617b689f5af9598bfa4d01d4e88652135e0f76e
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / activity / MainActivity.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.TargetApi;
19 import android.app.AlertDialog;
20 import android.app.Fragment;
21 import android.app.FragmentTransaction;
22 import android.content.ActivityNotFoundException;
23 import android.content.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.content.pm.PackageInfo;
29 import android.content.pm.PackageManager;
30 import android.content.pm.ShortcutInfo;
31 import android.content.pm.ShortcutManager;
32 import android.graphics.drawable.Icon;
33 import android.net.Uri;
34 import android.os.Build;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.view.View;
38 import android.widget.ImageView;
39
40 import androidx.appcompat.app.ActionBar;
41 import androidx.appcompat.app.AppCompatActivity;
42 import androidx.appcompat.widget.SwitchCompat;
43
44 import com.farmerbb.taskbar.BuildConfig;
45 import com.farmerbb.taskbar.R;
46 import com.farmerbb.taskbar.content.TaskbarIntent;
47 import com.farmerbb.taskbar.fragment.AboutFragment;
48 import com.farmerbb.taskbar.fragment.AdvancedFragment;
49 import com.farmerbb.taskbar.fragment.AppearanceFragment;
50 import com.farmerbb.taskbar.fragment.DesktopModeFragment;
51 import com.farmerbb.taskbar.fragment.FreeformModeFragment;
52 import com.farmerbb.taskbar.fragment.ManageAppDataFragment;
53 import com.farmerbb.taskbar.fragment.SettingsFragment;
54 import com.farmerbb.taskbar.service.DashboardService;
55 import com.farmerbb.taskbar.service.NotificationService;
56 import com.farmerbb.taskbar.service.StartMenuService;
57 import com.farmerbb.taskbar.service.TaskbarService;
58 import com.farmerbb.taskbar.util.FreeformHackHelper;
59 import com.farmerbb.taskbar.util.IconCache;
60 import com.farmerbb.taskbar.util.LauncherHelper;
61 import com.farmerbb.taskbar.util.U;
62
63 import java.io.File;
64 import java.util.Arrays;
65 import java.util.Collections;
66
67 public class MainActivity extends AppCompatActivity {
68
69     private SwitchCompat theSwitch;
70     private ImageView helpButton;
71
72     private BroadcastReceiver switchReceiver = new BroadcastReceiver() {
73         @Override
74         public void onReceive(Context context, Intent intent) {
75             updateSwitch();
76         }
77     };
78
79     private boolean hasCaption = false;
80
81     @Override
82     protected void onCreate(Bundle savedInstanceState) {
83         super.onCreate(savedInstanceState);
84
85         U.registerReceiver(this, switchReceiver, TaskbarIntent.ACTION_UPDATE_SWITCH);
86
87         final SharedPreferences pref = U.getSharedPreferences(this);
88         SharedPreferences.Editor editor = pref.edit();
89
90         if(!U.isLibrary(this)) {
91             switch(pref.getString("theme", "light")) {
92                 case "light":
93                     setTheme(R.style.Taskbar);
94                     break;
95                 case "dark":
96                     setTheme(R.style.Taskbar_Dark);
97                     break;
98             }
99         } else {
100             int theme = getIntent().getIntExtra("theme", -1);
101             if(theme != -1)
102                 setTheme(theme);
103         }
104
105         if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
106             editor.putBoolean("taskbar_active", false);
107
108         // Ensure that components that should be enabled are enabled properly
109         boolean launcherEnabled = (pref.getBoolean("launcher", false) && U.canDrawOverlays(this))
110                 || U.isLauncherPermanentlyEnabled(this);
111
112         boolean desktopModeEnabled = U.isDesktopModeSupported(this)
113                 && pref.getBoolean("desktop_mode", false);
114
115         editor.putBoolean("launcher", launcherEnabled);
116         editor.putBoolean("desktop_mode", desktopModeEnabled);
117         editor.apply();
118
119         if(!U.isLibrary(this)) {
120             ComponentName component = new ComponentName(this, HomeActivity.class);
121             getPackageManager().setComponentEnabledSetting(component,
122                     launcherEnabled && !U.isDelegatingHomeActivity(this)
123                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
124                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
125                     PackageManager.DONT_KILL_APP);
126
127             ComponentName component2 = new ComponentName(this, KeyboardShortcutActivity.class);
128             getPackageManager().setComponentEnabledSetting(component2,
129                     pref.getBoolean("keyboard_shortcut", false)
130                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
131                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
132                     PackageManager.DONT_KILL_APP);
133
134             ComponentName component3 = new ComponentName(this, ShortcutActivity.class);
135             getPackageManager().setComponentEnabledSetting(component3,
136                     U.enableFreeformModeShortcut(this)
137                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
138                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
139                     PackageManager.DONT_KILL_APP);
140
141             ComponentName component4 = new ComponentName(this, StartTaskbarActivity.class);
142             getPackageManager().setComponentEnabledSetting(component4,
143                     PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
144                     PackageManager.DONT_KILL_APP);
145
146             ComponentName component5 = new ComponentName(this, SecondaryHomeActivity.class);
147             getPackageManager().setComponentEnabledSetting(component5,
148                     desktopModeEnabled
149                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
150                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
151                     PackageManager.DONT_KILL_APP);
152
153             ComponentName component6 = new ComponentName(this, HSLActivity.class);
154             getPackageManager().setComponentEnabledSetting(component6,
155                     desktopModeEnabled
156                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
157                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
158                     PackageManager.DONT_KILL_APP);
159
160             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
161                 ComponentName component7 = new ComponentName(this, KeyboardShortcutActivityLockDevice.class);
162                 getPackageManager().setComponentEnabledSetting(component7,
163                         pref.getBoolean("keyboard_shortcut", false)
164                                 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
165                                 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
166                         PackageManager.DONT_KILL_APP);
167             }
168         }
169
170         if(!launcherEnabled) {
171             U.sendBroadcast(this, TaskbarIntent.ACTION_KILL_HOME_ACTIVITY);
172         }
173
174         // Update caption state
175         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && U.isChromeOs(this)) {
176             getWindow().setRestrictedCaptionAreaListener(rect -> hasCaption = true);
177
178             new Handler().postDelayed(() -> pref.edit().putBoolean("has_caption", hasCaption).apply(), 500);
179         }
180
181         if(getPackageName().equals(BuildConfig.PAID_APPLICATION_ID)) {
182             File file = new File(getFilesDir() + File.separator + "imported_successfully");
183             if(freeVersionInstalled() && !file.exists()) {
184                 startActivity(new Intent(this, ImportSettingsActivity.class));
185                 finish();
186             } else
187                 proceedWithAppLaunch(savedInstanceState);
188         } else
189             proceedWithAppLaunch(savedInstanceState);
190     }
191
192     private boolean freeVersionInstalled() {
193         PackageManager pm = getPackageManager();
194         try {
195             PackageInfo pInfo = pm.getPackageInfo(BuildConfig.BASE_APPLICATION_ID, 0);
196             return pInfo.versionCode >= 68
197                     && pm.checkSignatures(BuildConfig.BASE_APPLICATION_ID, getPackageName())
198                     == PackageManager.SIGNATURE_MATCH;
199         } catch (PackageManager.NameNotFoundException e) {
200             return false;
201         }
202     }
203
204     private void proceedWithAppLaunch(Bundle savedInstanceState) {
205         try {
206             setContentView(R.layout.tb_main);
207         } catch (IllegalStateException e) {
208             setTheme(R.style.Theme_AppCompat_Light);
209             setContentView(R.layout.tb_main);
210         }
211
212         ActionBar actionBar = getSupportActionBar();
213         if(actionBar != null && !U.isLibrary(this)) {
214             actionBar.setCustomView(R.layout.tb_switch_layout);
215             actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
216         }
217
218         theSwitch = findViewById(R.id.the_switch);
219         helpButton = findViewById(R.id.help_button);
220
221         if(theSwitch != null) {
222             final SharedPreferences pref = U.getSharedPreferences(this);
223             theSwitch.setChecked(pref.getBoolean("taskbar_active", false));
224
225             theSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
226                 if(b) {
227                     if(U.canDrawOverlays(this)) {
228                         boolean firstRun = pref.getBoolean("first_run", true);
229                         startTaskbarService();
230
231                         if(firstRun)
232                             U.showRecentAppsDialog(this);
233                     } else {
234                         U.showPermissionDialog(this);
235                         compoundButton.setChecked(false);
236                     }
237                 } else
238                     stopTaskbarService();
239             });
240         }
241
242         if(savedInstanceState == null) {
243             U.initPrefs(this);
244
245             File restoreInProgress = new File(getFilesDir(), "restore_in_progress");
246             File restoreSuccessful = new File(getFilesDir(), "restore_successful");
247
248             if(restoreInProgress.exists() || restoreSuccessful.exists()) {
249                 if(restoreInProgress.exists()) {
250                     U.showToastLong(this, R.string.tb_restore_failed);
251                     restoreInProgress.delete();
252                 }
253
254                 if(restoreSuccessful.exists()) {
255                     U.showToastLong(this, R.string.tb_restore_successful);
256                     restoreSuccessful.delete();
257                 }
258
259                 getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new ManageAppDataFragment(), "ManageAppDataFragment").commit();
260             } else if(!getIntent().hasExtra("theme_change"))
261                 getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new AboutFragment(), "AboutFragment").commit();
262             else
263                 getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new AppearanceFragment(), "AppearanceFragment").commit();
264         }
265
266         SharedPreferences pref = U.getSharedPreferences(this);
267         if(!getPackageName().equals(BuildConfig.BASE_APPLICATION_ID) && freeVersionInstalled()) {
268             if(!pref.getBoolean("dont_show_uninstall_dialog", false)) {
269                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
270                 builder.setTitle(R.string.tb_settings_imported_successfully)
271                         .setMessage(R.string.tb_import_dialog_message)
272                         .setPositiveButton(R.string.tb_action_uninstall, (dialog, which) -> {
273                             pref.edit().putBoolean("uninstall_dialog_shown", true).apply();
274
275                             try {
276                                 startActivity(new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + BuildConfig.BASE_APPLICATION_ID)));
277                             } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
278                         });
279
280                 if(pref.getBoolean("uninstall_dialog_shown", false))
281                     builder.setNegativeButton(R.string.tb_action_dont_show_again, (dialogInterface, i) -> pref.edit().putBoolean("dont_show_uninstall_dialog", true).apply());
282
283                 AlertDialog dialog = builder.create();
284                 dialog.show();
285                 dialog.setCancelable(false);
286             }
287
288             if(!pref.getBoolean("uninstall_dialog_shown", false)) {
289                 if(theSwitch != null) theSwitch.setChecked(false);
290
291                 SharedPreferences.Editor editor = pref.edit();
292
293                 String iconPack = pref.getString("icon_pack", BuildConfig.BASE_APPLICATION_ID);
294                 if(iconPack.contains(BuildConfig.BASE_APPLICATION_ID)) {
295                     editor.putString("icon_pack", getPackageName());
296                 } else {
297                     U.refreshPinnedIcons(this);
298                 }
299
300                 editor.putBoolean("first_run", true);
301                 editor.apply();
302             }
303         }
304
305         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
306             ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
307
308             if(shortcutManager.getDynamicShortcuts().size() == 0) {
309                 Intent intent = new Intent(Intent.ACTION_MAIN);
310                 intent.setClass(this, StartTaskbarActivity.class);
311                 intent.putExtra("is_launching_shortcut", true);
312
313                 ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "start_taskbar")
314                         .setShortLabel(getString(R.string.tb_start_taskbar))
315                         .setIcon(Icon.createWithResource(this, R.drawable.tb_shortcut_icon_start))
316                         .setIntent(intent)
317                         .build();
318
319                 if(U.enableFreeformModeShortcut(this)) {
320                     Intent intent2 = new Intent(Intent.ACTION_MAIN);
321                     intent2.setClass(this, ShortcutActivity.class);
322                     intent2.putExtra("is_launching_shortcut", true);
323
324                     ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "freeform_mode")
325                             .setShortLabel(getString(R.string.tb_pref_header_freeform))
326                             .setIcon(Icon.createWithResource(this, R.drawable.tb_shortcut_icon_freeform))
327                             .setIntent(intent2)
328                             .build();
329
330                     shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2));
331                 } else
332                     shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
333             }
334         }
335     }
336
337     @Override
338     protected void onResume() {
339         super.onResume();
340
341         updateSwitch();
342     }
343
344     @Override
345     protected void onDestroy() {
346         U.unregisterReceiver(this, switchReceiver);
347
348         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && U.isChromeOs(this))
349             getWindow().setRestrictedCaptionAreaListener(null);
350
351         super.onDestroy();
352     }
353
354     @TargetApi(Build.VERSION_CODES.N)
355     private void startTaskbarService() {
356         SharedPreferences pref = U.getSharedPreferences(this);
357         SharedPreferences.Editor editor = pref.edit();
358
359         editor.putBoolean("is_hidden", false);
360
361         if(pref.getBoolean("first_run", true)) {
362             editor.putBoolean("first_run", false);
363             editor.putBoolean("collapsed", true);
364         }
365
366         editor.putBoolean("taskbar_active", true);
367         editor.putLong("time_of_service_start", System.currentTimeMillis());
368         editor.apply();
369
370         if(U.hasFreeformSupport(this)
371                 && pref.getBoolean("freeform_hack", false)
372                 && !FreeformHackHelper.getInstance().isFreeformHackActive()) {
373             U.startFreeformHack(this, true);
374         }
375
376         startService(new Intent(this, TaskbarService.class));
377         startService(new Intent(this, StartMenuService.class));
378         startService(new Intent(this, DashboardService.class));
379         startService(new Intent(this, NotificationService.class));
380     }
381
382     private void stopTaskbarService() {
383         SharedPreferences pref = U.getSharedPreferences(this);
384         pref.edit().putBoolean("taskbar_active", false).apply();
385
386         if(!LauncherHelper.getInstance().isOnHomeScreen()) {
387             stopService(new Intent(this, TaskbarService.class));
388             stopService(new Intent(this, StartMenuService.class));
389             stopService(new Intent(this, DashboardService.class));
390
391             IconCache.getInstance(this).clearCache();
392
393             U.sendBroadcast(this, TaskbarIntent.ACTION_START_MENU_DISAPPEARING);
394         }
395
396         stopService(new Intent(this, NotificationService.class));
397     }
398
399     private void updateSwitch() {
400         if(theSwitch != null) {
401             SharedPreferences pref = U.getSharedPreferences(this);
402             theSwitch.setChecked(pref.getBoolean("taskbar_active", false));
403         }
404     }
405
406     @Override
407     public void onBackPressed() {
408         Fragment oldFragment = getFragmentManager().findFragmentById(R.id.fragmentContainer);
409
410         if(oldFragment instanceof AboutFragment)
411             super.onBackPressed();
412         else {
413             Fragment newFragment;
414             String tag;
415
416             if(oldFragment instanceof ManageAppDataFragment) {
417                 newFragment = new AdvancedFragment();
418                 tag = "AdvancedFragment";
419             } else {
420                 newFragment = new AboutFragment();
421                 tag = "AboutFragment";
422             }
423
424             getFragmentManager()
425                     .beginTransaction()
426                     .replace(R.id.fragmentContainer, newFragment, tag)
427                     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)
428                     .commit();
429         }
430     }
431
432     public String getAboutFragmentTitle() {
433         if(!U.isLibrary(this))
434             return getString(R.string.tb_app_name);
435
436         String title = getIntent().getStringExtra("title");
437         return title != null ? title : getString(R.string.tb_settings);
438     }
439
440     public boolean getAboutFragmentBackArrow() {
441         if(!U.isLibrary(this))
442             return false;
443
444         return getIntent().getBooleanExtra("back_arrow", false);
445     }
446
447     public void updateHelpButton(SettingsFragment fragment) {
448         if(fragment instanceof FreeformModeFragment) {
449             helpButton.setVisibility(View.VISIBLE);
450             helpButton.setOnClickListener(v -> {
451                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
452                 builder.setView(View.inflate(MainActivity.this, R.layout.tb_freeform_help_dialog, null))
453                         .setTitle(R.string.tb_freeform_help_dialog_title)
454                         .setPositiveButton(R.string.tb_action_close, null);
455
456                 AlertDialog dialog = builder.create();
457                 dialog.show();
458             });
459         } else if(fragment instanceof DesktopModeFragment) {
460             helpButton.setVisibility(View.VISIBLE);
461             helpButton.setOnClickListener(v -> {
462                 // TODO
463             });
464         } else {
465             helpButton.setVisibility(View.INVISIBLE);
466             helpButton.setOnClickListener(null);
467         }
468     }
469 }