OSDN Git Service

Moar fixes for animation and window size
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / activity / DummyActivity.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.app.AlertDialog;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.ActivityNotFoundException;
24 import android.content.ComponentName;
25 import android.content.Intent;
26 import android.content.SharedPreferences;
27 import android.net.Uri;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.UserManager;
32 import android.provider.Settings;
33 import android.view.View;
34
35 import com.farmerbb.taskbar.R;
36 import com.farmerbb.taskbar.receiver.LockDeviceReceiver;
37 import com.farmerbb.taskbar.util.ApplicationType;
38 import com.farmerbb.taskbar.util.FreeformHackHelper;
39 import com.farmerbb.taskbar.util.U;
40
41 public class DummyActivity extends Activity {
42
43     boolean shouldFinish = false;
44
45     @Override
46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         setContentView(new View(this));
49     }
50
51     @SuppressLint("RestrictedApi")
52     @TargetApi(Build.VERSION_CODES.N)
53     @Override
54     protected void onResume() {
55         super.onResume();
56         if(shouldFinish)
57             finish();
58         else {
59             shouldFinish = true;
60
61             if(getIntent().hasExtra("uninstall")) {
62                 UserManager userManager = (UserManager) getSystemService(USER_SERVICE);
63
64                 Intent intent = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + getIntent().getStringExtra("uninstall")));
65                 intent.putExtra(Intent.EXTRA_USER, userManager.getUserForSerialNumber(getIntent().getLongExtra("user_id", 0)));
66
67                 try {
68                     startActivity(intent);
69                 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
70             } else if(getIntent().hasExtra("device_admin")) {
71                 AlertDialog.Builder builder = new AlertDialog.Builder(U.wrapContext(this));
72                 builder.setTitle(R.string.permission_dialog_title)
73                         .setMessage(R.string.device_admin_disclosure)
74                         .setNegativeButton(R.string.action_cancel, (dialog, which) -> new Handler().post(this::finish))
75                         .setPositiveButton(R.string.action_activate, (dialog, which) -> {
76                             Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
77                             intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, LockDeviceReceiver.class));
78                             intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.device_admin_description));
79
80                             SharedPreferences pref = U.getSharedPreferences(this);
81                             if(pref.getBoolean("disable_animations", false))
82                                 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
83
84                             U.launchApp(this, () -> {
85                                 try {
86                                     startActivity(intent, U.getActivityOptionsBundle(this, ApplicationType.APPLICATION));
87                                 } catch (ActivityNotFoundException e) {
88                                     U.showToast(this, R.string.lock_device_not_supported);
89
90                                     finish();
91                                 }
92                             });
93                         });
94
95                 AlertDialog dialog = builder.create();
96                 dialog.show();
97                 dialog.setCancelable(false);
98             } else if(getIntent().hasExtra("accessibility")) {
99                 AlertDialog.Builder builder = new AlertDialog.Builder(U.wrapContext(this));
100                 builder.setTitle(R.string.permission_dialog_title)
101                         .setMessage(R.string.enable_accessibility)
102                         .setNegativeButton(R.string.action_cancel, (dialog, which) -> new Handler().post(this::finish))
103                         .setPositiveButton(R.string.action_activate, (dialog, which) -> {
104                             Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
105
106                             SharedPreferences pref = U.getSharedPreferences(this);
107                             if(pref.getBoolean("disable_animations", false))
108                                 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
109
110                             U.launchApp(this, () -> {
111                                 try {
112                                     startActivity(intent, U.getActivityOptionsBundle(this, ApplicationType.APPLICATION));
113                                     U.showToastLong(this, R.string.usage_stats_message);
114                                 } catch (ActivityNotFoundException e) {
115                                     U.showToast(this, R.string.lock_device_not_supported);
116
117                                     finish();
118                                 }
119                             });
120                         });
121
122                 AlertDialog dialog = builder.create();
123                 dialog.show();
124                 dialog.setCancelable(false);
125             } else if(getIntent().hasExtra("start_freeform_hack")) {
126                 SharedPreferences pref = U.getSharedPreferences(this);
127                 if(U.hasFreeformSupport(this)
128                         && pref.getBoolean("freeform_hack", false)
129                         && isInMultiWindowMode()
130                         && !FreeformHackHelper.getInstance().isFreeformHackActive()) {
131                     U.startFreeformHack(this);
132                 }
133
134                 finish();
135             } else if(getIntent().hasExtra("show_permission_dialog"))
136                 U.showPermissionDialog(U.wrapContext(this), null, this::finish);
137             else if(getIntent().hasExtra("show_recent_apps_dialog"))
138                 U.showRecentAppsDialog(U.wrapContext(this), null, this::finish);
139             else
140                 finish();
141         }
142     }
143 }