-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathApp.java
More file actions
278 lines (253 loc) · 11.4 KB
/
App.java
File metadata and controls
278 lines (253 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* This file is part of LSPosed.
*
* LSPosed is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LSPosed is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
package org.lsposed.manager;
import android.app.ActivityManager;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.provider.MediaStore;
import android.provider.Settings;
import android.system.Os;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;
import org.lsposed.hiddenapibypass.HiddenApiBypass;
import org.lsposed.manager.adapters.AppHelper;
import org.lsposed.manager.receivers.LSPManagerServiceHolder;
import org.lsposed.manager.repo.RepoLoader;
import org.lsposed.manager.util.CloudflareDNS;
import org.lsposed.manager.util.ModuleUtil;
import org.lsposed.manager.util.ThemeUtil;
import org.lsposed.manager.util.UpdateUtil;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import rikka.core.os.FileUtils;
import rikka.material.app.LocaleDelegate;
public class App extends Application {
public static final int PER_USER_RANGE = 100000;
public static final FutureTask<String> HTML_TEMPLATE = new FutureTask<>(() -> readWebviewHTML("template.html"));
public static final FutureTask<String> HTML_TEMPLATE_DARK = new FutureTask<>(() -> readWebviewHTML("template_dark.html"));
private static String readWebviewHTML(String name) {
try {
var input = App.getInstance().getAssets().open("webview/" + name);
var result = new ByteArrayOutputStream(1024);
FileUtils.copy(input, result);
return result.toString(StandardCharsets.UTF_8.name());
} catch (IOException e) {
Log.e(App.TAG, "read webview HTML", e);
return "<html dir\"@dir@\"><body>@body@</body></html>";
}
}
static {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
HiddenApiBypass.addHiddenApiExemptions("");
}
Looper.myQueue().addIdleHandler(() -> {
if (App.getInstance() == null || App.getExecutorService() == null) return true;
App.getExecutorService().submit(() -> {
var list = AppHelper.getAppList(false);
var pm = App.getInstance().getPackageManager();
list.parallelStream().forEach(i -> AppHelper.getAppLabel(i, pm));
ModuleUtil.getInstance();
RepoLoader.getInstance();
});
App.getExecutorService().submit(HTML_TEMPLATE);
App.getExecutorService().submit(HTML_TEMPLATE_DARK);
return false;
});
}
public static final String TAG = "LSPosedManager";
private static final String ACTION_USER_ADDED = "android.intent.action.USER_ADDED";
private static final String ACTION_USER_REMOVED = "android.intent.action.USER_REMOVED";
private static final String ACTION_USER_INFO_CHANGED = "android.intent.action.USER_INFO_CHANGED";
private static final String EXTRA_REMOVED_FOR_ALL_USERS = "android.intent.extra.REMOVED_FOR_ALL_USERS";
private static App instance = null;
private static OkHttpClient okHttpClient;
private static Cache okHttpCache;
private SharedPreferences pref;
private static final ExecutorService executorService = Executors.newCachedThreadPool();
private static final Handler MainHandler = new Handler(Looper.getMainLooper());
public static App getInstance() {
return instance;
}
public static SharedPreferences getPreferences() {
return instance.pref;
}
public static ExecutorService getExecutorService() {
return executorService;
}
public static final boolean isParasitic = !Process.isApplicationUid(Process.myUid());
public static Handler getMainHandler() {
return MainHandler;
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
var map = new HashMap<String, String>(1);
map.put("isParasitic", String.valueOf(isParasitic));
var am = getSystemService(ActivityManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
map.clear();
var reasons = am.getHistoricalProcessExitReasons(null, 0, 1);
if (reasons.size() == 1) {
map.put("description", reasons.get(0).getDescription());
map.put("importance", String.valueOf(reasons.get(0).getImportance()));
map.put("process", reasons.get(0).getProcessName());
map.put("reason", String.valueOf(reasons.get(0).getReason()));
map.put("status", String.valueOf(reasons.get(0).getStatus()));
}
}
}
private void setCrashReport() {
var handler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
var time = OffsetDateTime.now();
var dir = new File(getCacheDir(), "crash");
//noinspection ResultOfMethodCallIgnored
dir.mkdir();
var file = new File(dir, time.toEpochSecond() + ".log");
try (var pw = new PrintWriter(file)) {
pw.println(BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")");
pw.println(time);
pw.println("pid: " + Os.getpid() + " uid: " + Os.getuid());
throwable.printStackTrace(pw);
} catch (IOException ignored) {
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
var table = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
var values = new ContentValues();
values.put(MediaStore.Downloads.DISPLAY_NAME, "LSPosed_crash_report" + time.toEpochSecond() + ".zip");
values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOCUMENTS);
var cr = getContentResolver();
var uri = cr.insert(table, values);
if (uri == null) return;
try (var zipFd = cr.openFileDescriptor(uri, "wt")) {
LSPManagerServiceHolder.getService().getLogs(zipFd);
} catch (Exception ignored) {
cr.delete(uri, null, null);
}
}
if (handler != null) {
handler.uncaughtException(thread, throwable);
}
});
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
setCrashReport();
pref = PreferenceManager.getDefaultSharedPreferences(this);
if (!pref.contains("doh")) {
var name = "private_dns_mode";
if ("hostname".equals(Settings.Global.getString(getContentResolver(), name))) {
pref.edit().putBoolean("doh", false).apply();
} else {
pref.edit().putBoolean("doh", true).apply();
}
}
AppCompatDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
LocaleDelegate.setDefaultLocale(getLocale());
var res = getResources();
var config = res.getConfiguration();
config.setLocale(LocaleDelegate.getDefaultLocale());
//noinspection deprecation
res.updateConfiguration(config, res.getDisplayMetrics());
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("org.lsposed.manager.NOTIFICATION");
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent inIntent) {
var intent = (Intent) inIntent.getParcelableExtra(Intent.EXTRA_INTENT);
Log.d(TAG, "onReceive: " + intent);
switch (intent.getAction()) {
case Intent.ACTION_PACKAGE_ADDED, Intent.ACTION_PACKAGE_CHANGED, Intent.ACTION_PACKAGE_FULLY_REMOVED, Intent.ACTION_UID_REMOVED -> {
var userId = intent.getIntExtra(Intent.EXTRA_USER, 0);
var packageName = intent.getStringExtra("android.intent.extra.PACKAGES");
var packageRemovedForAllUsers = intent.getBooleanExtra(EXTRA_REMOVED_FOR_ALL_USERS, false);
var isXposedModule = intent.getBooleanExtra("isXposedModule", false);
if (packageName != null) {
if (isXposedModule)
ModuleUtil.getInstance().reloadSingleModule(packageName, userId, packageRemovedForAllUsers);
else
App.getExecutorService().submit(() -> AppHelper.getAppList(true));
}
}
case ACTION_USER_ADDED, ACTION_USER_REMOVED, ACTION_USER_INFO_CHANGED -> App.getExecutorService().submit(() -> ModuleUtil.getInstance().reloadInstalledModules());
}
}
}, intentFilter, Context.RECEIVER_NOT_EXPORTED);
UpdateUtil.loadRemoteVersion();
}
@NonNull
public static OkHttpClient getOkHttpClient() {
if (okHttpClient != null) return okHttpClient;
var builder = new OkHttpClient.Builder()
.cache(getOkHttpCache())
.dns(new CloudflareDNS());
if (BuildConfig.DEBUG) {
var log = new HttpLoggingInterceptor();
log.setLevel(HttpLoggingInterceptor.Level.HEADERS);
builder.addInterceptor(log);
}
okHttpClient = builder.build();
return okHttpClient;
}
@NonNull
public static Cache getOkHttpCache() {
if (okHttpCache != null) return okHttpCache;
long size50MiB = 50 * 1024 * 1024;
okHttpCache = new Cache(new File(instance.getCacheDir(), "http_cache"), size50MiB);
return okHttpCache;
}
public static Locale getLocale(String tag) {
if (TextUtils.isEmpty(tag) || "SYSTEM".equals(tag)) {
return LocaleDelegate.getSystemLocale();
}
return Locale.forLanguageTag(tag);
}
public static Locale getLocale() {
String tag = getPreferences().getString("language", null);
return getLocale(tag);
}
}