From cca4347bf930b54044409ab1ce024044388a65cd Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 9 Nov 2017 01:43:29 +0800 Subject: [PATCH] Use handler instead of weird callbacks --- .../com/topjohnwu/magisk/FlashActivity.java | 6 ++--- .../com/topjohnwu/magisk/asyncs/FlashZip.java | 16 ++---------- .../magisk/asyncs/InstallMagisk.java | 20 +++----------- .../magisk/container/AdaptiveList.java | 26 ------------------- .../magisk/container/CallbackList.java | 22 ++++++++++++++++ 5 files changed, 31 insertions(+), 59 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/container/AdaptiveList.java create mode 100644 app/src/main/java/com/topjohnwu/magisk/container/CallbackList.java diff --git a/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java b/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java index 445cd31f0..83d4ce369 100644 --- a/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java @@ -15,7 +15,7 @@ import android.widget.TextView; import com.topjohnwu.magisk.asyncs.FlashZip; import com.topjohnwu.magisk.asyncs.InstallMagisk; import com.topjohnwu.magisk.components.Activity; -import com.topjohnwu.magisk.container.AdaptiveList; +import com.topjohnwu.magisk.container.CallbackList; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Shell; @@ -46,9 +46,9 @@ public class FlashActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flash); ButterKnife.bind(this); - AdaptiveList rootShellOutput = new AdaptiveList() { + CallbackList rootShellOutput = new CallbackList() { @Override - public synchronized void updateView() { + public synchronized void onAddElement() { flashLogs.setText(TextUtils.join("\n", this)); sv.postDelayed(() -> sv.fullScroll(ScrollView.FOCUS_DOWN), 10); } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java index da0b900dd..cfc43cc31 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java @@ -6,7 +6,6 @@ import android.text.TextUtils; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; -import com.topjohnwu.magisk.container.AdaptiveList; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; @@ -26,9 +25,9 @@ public class FlashZip extends ParallelTask { private Uri mUri; private File mCachedFile; - private AdaptiveList mList; + private List mList; - public FlashZip(Activity context, Uri uri, AdaptiveList list) { + public FlashZip(Activity context, Uri uri, List list) { super(context); mUri = uri; mList = list; @@ -41,17 +40,6 @@ public class FlashZip extends ParallelTask { return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK"); } - @Override - protected void onPreExecute() { - // UI updates must run in the UI thread - mList.setCallback(this::publishProgress); - } - - @Override - protected void onProgressUpdate(Void... values) { - mList.updateView(); - } - @Override protected Integer doInBackground(Void... voids) { MagiskManager mm = MagiskManager.get(); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 3a395f97f..4dbea4151 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -9,7 +9,6 @@ import android.text.TextUtils; import com.topjohnwu.crypto.SignBoot; import com.topjohnwu.magisk.MagiskManager; -import com.topjohnwu.magisk.container.AdaptiveList; import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Shell; @@ -37,12 +36,12 @@ public class InstallMagisk extends ParallelTask { private static final int DIRECT_MODE = 1; private Uri mBootImg, mZip; - private AdaptiveList mList; + private List mList; private String mBootLocation; private boolean mKeepEnc, mKeepVerity; private int mode; - private InstallMagisk(Activity context, AdaptiveList list, Uri zip, boolean enc, boolean verity) { + private InstallMagisk(Activity context, List list, Uri zip, boolean enc, boolean verity) { super(context); mList = list; mZip = zip; @@ -50,29 +49,18 @@ public class InstallMagisk extends ParallelTask { mKeepVerity = verity; } - public InstallMagisk(Activity context, AdaptiveList list, Uri zip, boolean enc, boolean verity, Uri boot) { + public InstallMagisk(Activity context, List list, Uri zip, boolean enc, boolean verity, Uri boot) { this(context, list, zip, enc, verity); mBootImg = boot; mode = PATCH_MODE; } - public InstallMagisk(Activity context, AdaptiveList list, Uri zip, boolean enc, boolean verity, String boot) { + public InstallMagisk(Activity context, List list, Uri zip, boolean enc, boolean verity, String boot) { this(context, list, zip, enc, verity); mBootLocation = boot; mode = DIRECT_MODE; } - @Override - protected void onPreExecute() { - // UI updates must run in the UI thread - mList.setCallback(this::publishProgress); - } - - @Override - protected void onProgressUpdate(Void... values) { - mList.updateView(); - } - @Override protected Boolean doInBackground(Void... voids) { MagiskManager mm = MagiskManager.get(); diff --git a/app/src/main/java/com/topjohnwu/magisk/container/AdaptiveList.java b/app/src/main/java/com/topjohnwu/magisk/container/AdaptiveList.java deleted file mode 100644 index 41557b313..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/container/AdaptiveList.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.topjohnwu.magisk.container; - -import java.util.ArrayList; - -public abstract class AdaptiveList extends ArrayList { - - private Runnable callback; - - public abstract void updateView(); - - public void setCallback(Runnable cb) { - callback = cb; - } - - public synchronized boolean add(E e) { - boolean ret = super.add(e); - if (ret) { - if (callback == null) { - updateView(); - } else { - callback.run(); - } - } - return ret; - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/container/CallbackList.java b/app/src/main/java/com/topjohnwu/magisk/container/CallbackList.java new file mode 100644 index 000000000..dbc492ff3 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/container/CallbackList.java @@ -0,0 +1,22 @@ +package com.topjohnwu.magisk.container; + +import android.os.Handler; + +import java.util.ArrayList; + +public abstract class CallbackList extends ArrayList { + + private Handler handler; + + protected CallbackList() { + handler = new Handler(); + } + + public abstract void onAddElement(); + + public synchronized boolean add(E e) { + boolean ret = super.add(e); + handler.post(this::onAddElement); + return ret; + } +}