OSDN Git Service

0bab3c6c16db419b41fb5cdf45829fefa3ddb1e1
[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.FragmentTransaction;
21 import android.content.ActivityNotFoundException;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
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.support.design.widget.Snackbar;
38 import android.support.v4.content.LocalBroadcastManager;
39 import android.support.v7.app.ActionBar;
40 import android.support.v7.app.AppCompatActivity;
41 import android.support.v7.widget.SwitchCompat;
42
43 import com.farmerbb.taskbar.BuildConfig;
44 import com.farmerbb.taskbar.R;
45 import com.farmerbb.taskbar.fragment.AboutFragment;
46 import com.farmerbb.taskbar.fragment.AppearanceFragment;
47 import com.farmerbb.taskbar.service.DashboardService;
48 import com.farmerbb.taskbar.service.NotificationService;
49 import com.farmerbb.taskbar.service.StartMenuService;
50 import com.farmerbb.taskbar.service.TaskbarService;
51 import com.farmerbb.taskbar.util.FreeformHackHelper;
52 import com.farmerbb.taskbar.util.IconCache;
53 import com.farmerbb.taskbar.util.LauncherHelper;
54 import com.farmerbb.taskbar.util.U;
55
56 import java.io.File;
57 import java.util.Arrays;
58 import java.util.Collections;
59
60 public class MainActivity extends AppCompatActivity {
61
62     private SwitchCompat theSwitch;
63
64     private BroadcastReceiver switchReceiver = new BroadcastReceiver() {
65         @Override
66         public void onReceive(Context context, Intent intent) {
67             updateSwitch();
68         }
69     };
70
71     private boolean hasCaption = false;
72
73     @Override
74     protected void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76
77         LocalBroadcastManager.getInstance(this).registerReceiver(switchReceiver, new IntentFilter("com.farmerbb.taskbar.UPDATE_SWITCH"));
78
79         final SharedPreferences pref = U.getSharedPreferences(this);
80         SharedPreferences.Editor editor = pref.edit();
81
82         switch(pref.getString("theme", "light")) {
83             case "light":
84                 setTheme(R.style.AppTheme);
85                 break;
86             case "dark":
87                 setTheme(R.style.AppTheme_Dark);
88                 break;
89         }
90
91         if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
92             editor.putBoolean("taskbar_active", false);
93
94         // Ensure that components that should be enabled are enabled properly
95         boolean launcherEnabled = (pref.getBoolean("launcher", false) && U.canDrawOverlays(this))
96                 || U.isLauncherPermanentlyEnabled(this);
97
98         editor.putBoolean("launcher", launcherEnabled);
99         editor.apply();
100
101         ComponentName component = new ComponentName(this, HomeActivity.class);
102         getPackageManager().setComponentEnabledSetting(component,
103                 launcherEnabled && !U.isDelegatingHomeActivity(this)
104                         ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
105                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
106                 PackageManager.DONT_KILL_APP);
107
108         ComponentName component2 = new ComponentName(this, KeyboardShortcutActivity.class);
109         getPackageManager().setComponentEnabledSetting(component2,
110                 pref.getBoolean("keyboard_shortcut", false)
111                         ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
112                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
113                 PackageManager.DONT_KILL_APP);
114
115         ComponentName component3 = new ComponentName(this, ShortcutActivity.class);
116         getPackageManager().setComponentEnabledSetting(component3,
117                 U.enableFreeformModeShortcut(this)
118                         ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
119                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
120                 PackageManager.DONT_KILL_APP);
121
122         ComponentName component4 = new ComponentName(this, StartTaskbarActivity.class);
123         getPackageManager().setComponentEnabledSetting(component4,
124                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
125                 PackageManager.DONT_KILL_APP);
126
127         if(!launcherEnabled)
128             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
129
130         // Update caption state
131         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && U.isChromeOs(this)) {
132             getWindow().setRestrictedCaptionAreaListener(rect -> hasCaption = true);
133
134             new Handler().postDelayed(() -> pref.edit().putBoolean("has_caption", hasCaption).apply(), 500);
135         }
136
137         if(BuildConfig.APPLICATION_ID.equals(BuildConfig.PAID_APPLICATION_ID)) {
138             File file = new File(getFilesDir() + File.separator + "imported_successfully");
139             if(freeVersionInstalled() && !file.exists()) {
140                 startActivity(new Intent(this, ImportSettingsActivity.class));
141                 finish();
142             } else
143                 proceedWithAppLaunch(savedInstanceState);
144         } else
145             proceedWithAppLaunch(savedInstanceState);
146     }
147
148     private boolean freeVersionInstalled() {
149         PackageManager pm = getPackageManager();
150         try {
151             PackageInfo pInfo = pm.getPackageInfo(BuildConfig.BASE_APPLICATION_ID, 0);
152             return pInfo.versionCode >= 68
153                     && pm.checkSignatures(BuildConfig.BASE_APPLICATION_ID, BuildConfig.APPLICATION_ID)
154                     == PackageManager.SIGNATURE_MATCH;
155         } catch (PackageManager.NameNotFoundException e) {
156             return false;
157         }
158     }
159
160     private void proceedWithAppLaunch(Bundle savedInstanceState) {
161         setContentView(R.layout.main);
162
163         ActionBar actionBar = getSupportActionBar();
164         if(actionBar != null) {
165             actionBar.setCustomView(R.layout.switch_layout);
166             actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
167         }
168
169         theSwitch = findViewById(R.id.the_switch);
170         if(theSwitch != null) {
171             final SharedPreferences pref = U.getSharedPreferences(this);
172             theSwitch.setChecked(pref.getBoolean("taskbar_active", false));
173
174             theSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
175                 if(b) {
176                     if(U.canDrawOverlays(this)) {
177                         boolean firstRun = pref.getBoolean("first_run", true);
178                         startTaskbarService();
179
180                         if(firstRun)
181                             U.showRecentAppsDialog(this);
182                     } else {
183                         U.showPermissionDialog(this);
184                         compoundButton.setChecked(false);
185                     }
186                 } else
187                     stopTaskbarService();
188             });
189         }
190
191         if(savedInstanceState == null) {
192             U.initPrefs(this);
193
194             if(!getIntent().hasExtra("theme_change"))
195                 getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new AboutFragment(), "AboutFragment").commit();
196             else
197                 getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new AppearanceFragment(), "AppearanceFragment").commit();
198         }
199
200         SharedPreferences pref = U.getSharedPreferences(this);
201         if(!BuildConfig.APPLICATION_ID.equals(BuildConfig.BASE_APPLICATION_ID) && freeVersionInstalled()) {
202             if(!pref.getBoolean("dont_show_uninstall_dialog", false)) {
203                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
204                 builder.setTitle(R.string.settings_imported_successfully)
205                         .setMessage(R.string.import_dialog_message)
206                         .setPositiveButton(R.string.action_uninstall, (dialog, which) -> {
207                             pref.edit().putBoolean("uninstall_dialog_shown", true).apply();
208
209                             try {
210                                 startActivity(new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + BuildConfig.BASE_APPLICATION_ID)));
211                             } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
212                         });
213
214                 if(pref.getBoolean("uninstall_dialog_shown", false))
215                     builder.setNegativeButton(R.string.action_dont_show_again, (dialogInterface, i) -> pref.edit().putBoolean("dont_show_uninstall_dialog", true).apply());
216
217                 AlertDialog dialog = builder.create();
218                 dialog.show();
219                 dialog.setCancelable(false);
220             }
221
222             if(!pref.getBoolean("uninstall_dialog_shown", false)) {
223                 if(theSwitch != null) theSwitch.setChecked(false);
224
225                 SharedPreferences.Editor editor = pref.edit();
226
227                 String iconPack = pref.getString("icon_pack", BuildConfig.BASE_APPLICATION_ID);
228                 if(iconPack.contains(BuildConfig.BASE_APPLICATION_ID)) {
229                     editor.putString("icon_pack", BuildConfig.APPLICATION_ID);
230                 } else {
231                     U.refreshPinnedIcons(this);
232                 }
233
234                 editor.putBoolean("first_run", true);
235                 editor.apply();
236             }
237         }
238
239         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
240             ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
241
242             if(shortcutManager.getDynamicShortcuts().size() == 0) {
243                 Intent intent = new Intent(Intent.ACTION_MAIN);
244                 intent.setClass(this, StartTaskbarActivity.class);
245                 intent.putExtra("is_launching_shortcut", true);
246
247                 ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "start_taskbar")
248                         .setShortLabel(getString(R.string.start_taskbar))
249                         .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_start))
250                         .setIntent(intent)
251                         .build();
252
253                 if(U.enableFreeformModeShortcut(this)) {
254                     Intent intent2 = new Intent(Intent.ACTION_MAIN);
255                     intent2.setClass(this, ShortcutActivity.class);
256                     intent2.putExtra("is_launching_shortcut", true);
257
258                     ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "freeform_mode")
259                             .setShortLabel(getString(R.string.pref_header_freeform))
260                             .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon_freeform))
261                             .setIntent(intent2)
262                             .build();
263
264                     shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2));
265                 } else
266                     shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
267             }
268         }
269
270         if(pref.getBoolean("show_freeform_disabled_message", false)) {
271             Snackbar snackbar = Snackbar.make(
272                     findViewById(R.id.main_activity_layout),
273                     R.string.rip_freeform_snackbar,
274                     Snackbar.LENGTH_INDEFINITE
275             );
276
277             snackbar.setAction(R.string.action_details, v -> {
278                 snackbar.dismiss();
279
280                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
281                 builder.setTitle(R.string.pref_header_freeform)
282                         .setMessage(R.string.rip_freeform_dialog)
283                         .setPositiveButton(R.string.action_close, (dialog, which) ->
284                                 pref.edit().putBoolean("show_freeform_disabled_message", false).apply()
285                         );
286
287                 AlertDialog dialog = builder.create();
288                 dialog.show();
289                 dialog.setCancelable(false);
290             });
291
292             snackbar.show();
293         }
294     }
295
296     @Override
297     protected void onResume() {
298         super.onResume();
299
300         updateSwitch();
301     }
302
303     @Override
304     protected void onDestroy() {
305         LocalBroadcastManager.getInstance(this).unregisterReceiver(switchReceiver);
306
307         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && U.isChromeOs(this))
308             getWindow().setRestrictedCaptionAreaListener(null);
309
310         super.onDestroy();
311     }
312
313     @SuppressWarnings("deprecation")
314     @TargetApi(Build.VERSION_CODES.N)
315     private void startTaskbarService() {
316         SharedPreferences pref = U.getSharedPreferences(this);
317         SharedPreferences.Editor editor = pref.edit();
318
319         editor.putBoolean("is_hidden", false);
320
321         if(pref.getBoolean("first_run", true)) {
322             editor.putBoolean("first_run", false);
323             editor.putBoolean("collapsed", true);
324         }
325
326         editor.putBoolean("taskbar_active", true);
327         editor.putLong("time_of_service_start", System.currentTimeMillis());
328         editor.apply();
329
330         if(U.hasFreeformSupport(this)
331                 && pref.getBoolean("freeform_hack", false)
332                 && !FreeformHackHelper.getInstance().isFreeformHackActive()) {
333             U.startFreeformHack(this, true);
334         }
335
336         startService(new Intent(this, TaskbarService.class));
337         startService(new Intent(this, StartMenuService.class));
338         startService(new Intent(this, DashboardService.class));
339         startService(new Intent(this, NotificationService.class));
340     }
341
342     private void stopTaskbarService() {
343         SharedPreferences pref = U.getSharedPreferences(this);
344         pref.edit().putBoolean("taskbar_active", false).apply();
345
346         if(!LauncherHelper.getInstance().isOnHomeScreen()) {
347             stopService(new Intent(this, TaskbarService.class));
348             stopService(new Intent(this, StartMenuService.class));
349             stopService(new Intent(this, DashboardService.class));
350
351             IconCache.getInstance(this).clearCache();
352
353             LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.START_MENU_DISAPPEARING"));
354         }
355
356         stopService(new Intent(this, NotificationService.class));
357     }
358
359     private void updateSwitch() {
360         if(theSwitch != null) {
361             SharedPreferences pref = U.getSharedPreferences(this);
362             theSwitch.setChecked(pref.getBoolean("taskbar_active", false));
363         }
364     }
365
366     @Override
367     public void onBackPressed() {
368         if(getFragmentManager().findFragmentById(R.id.fragmentContainer) instanceof AboutFragment)
369             super.onBackPressed();
370         else
371             getFragmentManager()
372                     .beginTransaction()
373                     .replace(R.id.fragmentContainer, new AboutFragment(), "AboutFragment")
374                     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)
375                     .commit();
376     }
377 }