Cast AsyncTask.THREAD_POOL_EXECUTOR to ThreadPoolExecutor

This commit is contained in:
topjohnwu 2019-02-02 02:50:49 -05:00
parent 70eba568af
commit 699debdaca
11 changed files with 38 additions and 47 deletions

View File

@ -3,6 +3,7 @@ package com.topjohnwu.magisk;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.AsyncTask;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import com.topjohnwu.magisk.core.BuildConfig; import com.topjohnwu.magisk.core.BuildConfig;
@ -14,10 +15,13 @@ import com.topjohnwu.net.Networking;
import com.topjohnwu.superuser.ContainerApp; import com.topjohnwu.superuser.ContainerApp;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import java.util.concurrent.ThreadPoolExecutor;
public class App extends ContainerApp { public class App extends ContainerApp {
public static App self; public static App self;
public boolean init = false; public boolean init = false;
public static ThreadPoolExecutor THREAD_POOL;
// Global resources // Global resources
public SharedPreferences prefs; public SharedPreferences prefs;
@ -29,6 +33,7 @@ public class App extends ContainerApp {
Shell.Config.verboseLogging(BuildConfig.DEBUG); Shell.Config.verboseLogging(BuildConfig.DEBUG);
Shell.Config.addInitializers(RootUtils.class); Shell.Config.addInitializers(RootUtils.class);
Shell.Config.setTimeout(2); Shell.Config.setTimeout(2);
THREAD_POOL = (ThreadPoolExecutor) AsyncTask.THREAD_POOL_EXECUTOR;
} }
@Override @Override

View File

@ -1,7 +1,6 @@
package com.topjohnwu.magisk.tasks; package com.topjohnwu.magisk.tasks;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
@ -70,7 +69,7 @@ public abstract class FlashZip {
} }
public void exec() { public void exec() {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { App.THREAD_POOL.execute(() -> {
boolean success = false; boolean success = false;
try { try {
success = flash(); success = flash();

View File

@ -1,7 +1,6 @@
package com.topjohnwu.magisk.tasks; package com.topjohnwu.magisk.tasks;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
@ -308,7 +307,7 @@ public abstract class MagiskInstaller {
protected abstract void onResult(boolean success); protected abstract void onResult(boolean success);
public void exec() { public void exec() {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { App.THREAD_POOL.execute(() -> {
boolean b = operations(); boolean b = operations();
UiThreadHandler.run(() -> onResult(b)); UiThreadHandler.run(() -> onResult(b));
}); });

View File

@ -1,7 +1,6 @@
package com.topjohnwu.magisk.tasks; package com.topjohnwu.magisk.tasks;
import android.database.Cursor; import android.database.Cursor;
import android.os.AsyncTask;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Config;
@ -142,7 +141,7 @@ public class UpdateRepos {
public void exec(boolean force) { public void exec(boolean force) {
Topic.reset(Topic.REPO_LOAD_DONE); Topic.reset(Topic.REPO_LOAD_DONE);
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { App.THREAD_POOL.execute(() -> {
cached = Collections.synchronizedSet(app.repoDB.getRepoIDSet()); cached = Collections.synchronizedSet(app.repoDB.getRepoIDSet());
threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE); threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE);

View File

@ -8,7 +8,6 @@ import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.widget.Toast; import android.widget.Toast;
@ -97,7 +96,7 @@ public class Utils {
public static void loadModules() { public static void loadModules() {
Topic.reset(Topic.MODULE_LOAD_DONE); Topic.reset(Topic.MODULE_LOAD_DONE);
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { App.THREAD_POOL.execute(() -> {
Map<String, Module> moduleMap = new ValueSortedMap<>(); Map<String, Module> moduleMap = new ValueSortedMap<>();
SuFile path = new SuFile(Const.MAGISK_PATH); SuFile path = new SuFile(Const.MAGISK_PATH);
SuFile[] modules = path.listFiles( SuFile[] modules = path.listFiles(

View File

@ -6,7 +6,6 @@ import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import com.topjohnwu.magisk.components.BaseActivity; import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.tasks.CheckUpdates;
import com.topjohnwu.magisk.tasks.UpdateRepos; import com.topjohnwu.magisk.tasks.UpdateRepos;
import com.topjohnwu.magisk.uicomponents.Notifications; import com.topjohnwu.magisk.uicomponents.Notifications;
import com.topjohnwu.magisk.uicomponents.Shortcuts; import com.topjohnwu.magisk.uicomponents.Shortcuts;

View File

@ -151,7 +151,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
} }
public void refresh() { public void refresh() {
AsyncTask.THREAD_POOL_EXECUTOR.execute(this::loadApps); AsyncTask.SERIAL_EXECUTOR.execute(this::loadApps);
} }
static class ViewHolder extends RecyclerView.ViewHolder { static class ViewHolder extends RecyclerView.ViewHolder {

View File

@ -3,7 +3,6 @@ package com.topjohnwu.magisk.components;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.ClassMap; import com.topjohnwu.magisk.ClassMap;
@ -17,7 +16,6 @@ import com.topjohnwu.magisk.uicomponents.Shortcuts;
import com.topjohnwu.magisk.utils.DownloadApp; import com.topjohnwu.magisk.utils.DownloadApp;
import com.topjohnwu.magisk.utils.SuLogger; import com.topjohnwu.magisk.utils.SuLogger;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
public class GeneralReceiver extends BroadcastReceiver { public class GeneralReceiver extends BroadcastReceiver {
@ -61,7 +59,6 @@ public class GeneralReceiver extends BroadcastReceiver {
break; break;
case "boot": case "boot":
default: default:
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
/* Devices with DTBO might want to patch dtbo.img. /* Devices with DTBO might want to patch dtbo.img.
* However, that is not possible if Magisk is installed by * However, that is not possible if Magisk is installed by
* patching boot image with Magisk Manager and flashed via * patching boot image with Magisk Manager and flashed via
@ -69,7 +66,8 @@ public class GeneralReceiver extends BroadcastReceiver {
* Check for dtbo status every boot time, and prompt user * Check for dtbo status every boot time, and prompt user
* to reboot if dtbo wasn't patched and patched by Magisk Manager. * to reboot if dtbo wasn't patched and patched by Magisk Manager.
* */ * */
if (Shell.rootAccess() && ShellUtils.fastCmdResult("mm_patch_dtbo")) Shell.su("mm_patch_dtbo").submit(result -> {
if (result.isSuccess())
Notifications.dtboPatched(); Notifications.dtboPatched();
}); });
break; break;

View File

@ -5,12 +5,10 @@ import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import com.caverock.androidsvg.SVG; import com.caverock.androidsvg.SVG;
import com.caverock.androidsvg.SVGParseException;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
@ -22,6 +20,7 @@ import com.topjohnwu.superuser.ShellUtils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.concurrent.Callable;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -72,17 +71,16 @@ public class MarkDownWindow {
@Override @Override
public void load(@NonNull String url, @NonNull AsyncDrawable asyncDrawable) { public void load(@NonNull String url, @NonNull AsyncDrawable asyncDrawable) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { App.THREAD_POOL.submit((Callable<?>) () -> {
InputStream is = Networking.get(url).execForInputStream().getResult(); InputStream is = Networking.get(url).execForInputStream().getResult();
if (is == null) if (is == null)
return; return null;
ByteArrayStream buf = new ByteArrayStream(); ByteArrayStream buf = new ByteArrayStream();
buf.readFrom(is); buf.readFrom(is);
// First try default drawables // First try default drawables
Drawable drawable = Drawable.createFromStream(buf.getInputStream(), ""); Drawable drawable = Drawable.createFromStream(buf.getInputStream(), "");
if (drawable == null) { if (drawable == null) {
// SVG // SVG
try {
SVG svg = SVG.getFromInputStream(buf.getInputStream()); SVG svg = SVG.getFromInputStream(buf.getInputStream());
int width = Utils.dpInPx((int) svg.getDocumentWidth()); int width = Utils.dpInPx((int) svg.getDocumentWidth());
int height = Utils.dpInPx((int) svg.getDocumentHeight()); int height = Utils.dpInPx((int) svg.getDocumentHeight());
@ -92,12 +90,10 @@ public class MarkDownWindow {
canvas.scale(density, density); canvas.scale(density, density);
svg.renderToCanvas(canvas); svg.renderToCanvas(canvas);
drawable = new BitmapDrawable(App.self.getResources(), bitmap); drawable = new BitmapDrawable(App.self.getResources(), bitmap);
} catch (SVGParseException ignored) {}
} }
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
asyncDrawable.setResult(drawable); asyncDrawable.setResult(drawable);
} return null;
}); });
} }

View File

@ -1,7 +1,5 @@
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.os.AsyncTask;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.BuildConfig; import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Config;
@ -32,7 +30,7 @@ public class DownloadApp {
ProgressNotification progress = new ProgressNotification(name); ProgressNotification progress = new ProgressNotification(name);
listener.progress = progress; listener.progress = progress;
Networking.get(Config.managerLink) Networking.get(Config.managerLink)
.setExecutor(AsyncTask.THREAD_POOL_EXECUTOR) .setExecutor(App.THREAD_POOL)
.setDownloadProgressListener(progress) .setDownloadProgressListener(progress)
.setErrorHandler((conn, e) -> progress.dlFail()) .setErrorHandler((conn, e) -> progress.dlFail())
.getAsFile(apk, listener); .getAsFile(apk, listener);

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.os.AsyncTask;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
@ -136,14 +135,14 @@ public class PatchAPK {
} }
public static void hideManager() { public static void hideManager() {
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { App.THREAD_POOL.execute(() -> {
App app = App.self; App app = App.self;
NotificationCompat.Builder progress = NotificationCompat.Builder progress =
Notifications.progress(app.getString(R.string.hide_manager_title)); Notifications.progress(app.getString(R.string.hide_manager_title));
Notifications.mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build()); Notifications.mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build());
boolean b = patchAndHide(); if(!patchAndHide())
Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
Notifications.mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID); Notifications.mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID);
if (!b) Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
}); });
} }
} }