Use handler instead of weird callbacks

This commit is contained in:
topjohnwu 2017-11-09 01:43:29 +08:00
parent 3ae3d4926a
commit cca4347bf9
5 changed files with 31 additions and 59 deletions

View File

@ -15,7 +15,7 @@ import android.widget.TextView;
import com.topjohnwu.magisk.asyncs.FlashZip; import com.topjohnwu.magisk.asyncs.FlashZip;
import com.topjohnwu.magisk.asyncs.InstallMagisk; import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.components.Activity; 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.Const;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
@ -46,9 +46,9 @@ public class FlashActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash); setContentView(R.layout.activity_flash);
ButterKnife.bind(this); ButterKnife.bind(this);
AdaptiveList<String> rootShellOutput = new AdaptiveList<String>() { CallbackList<String> rootShellOutput = new CallbackList<String>() {
@Override @Override
public synchronized void updateView() { public synchronized void onAddElement() {
flashLogs.setText(TextUtils.join("\n", this)); flashLogs.setText(TextUtils.join("\n", this));
sv.postDelayed(() -> sv.fullScroll(ScrollView.FOCUS_DOWN), 10); sv.postDelayed(() -> sv.fullScroll(ScrollView.FOCUS_DOWN), 10);
} }

View File

@ -6,7 +6,6 @@ import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.container.AdaptiveList;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
@ -26,9 +25,9 @@ public class FlashZip extends ParallelTask<Void, Void, Integer> {
private Uri mUri; private Uri mUri;
private File mCachedFile; private File mCachedFile;
private AdaptiveList<String> mList; private List<String> mList;
public FlashZip(Activity context, Uri uri, AdaptiveList<String> list) { public FlashZip(Activity context, Uri uri, List<String> list) {
super(context); super(context);
mUri = uri; mUri = uri;
mList = list; mList = list;
@ -41,17 +40,6 @@ public class FlashZip extends ParallelTask<Void, Void, Integer> {
return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK"); 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 @Override
protected Integer doInBackground(Void... voids) { protected Integer doInBackground(Void... voids) {
MagiskManager mm = MagiskManager.get(); MagiskManager mm = MagiskManager.get();

View File

@ -9,7 +9,6 @@ import android.text.TextUtils;
import com.topjohnwu.crypto.SignBoot; import com.topjohnwu.crypto.SignBoot;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.container.AdaptiveList;
import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.container.TarEntry;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
@ -37,12 +36,12 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
private static final int DIRECT_MODE = 1; private static final int DIRECT_MODE = 1;
private Uri mBootImg, mZip; private Uri mBootImg, mZip;
private AdaptiveList<String> mList; private List<String> mList;
private String mBootLocation; private String mBootLocation;
private boolean mKeepEnc, mKeepVerity; private boolean mKeepEnc, mKeepVerity;
private int mode; private int mode;
private InstallMagisk(Activity context, AdaptiveList<String> list, Uri zip, boolean enc, boolean verity) { private InstallMagisk(Activity context, List<String> list, Uri zip, boolean enc, boolean verity) {
super(context); super(context);
mList = list; mList = list;
mZip = zip; mZip = zip;
@ -50,29 +49,18 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
mKeepVerity = verity; mKeepVerity = verity;
} }
public InstallMagisk(Activity context, AdaptiveList<String> list, Uri zip, boolean enc, boolean verity, Uri boot) { public InstallMagisk(Activity context, List<String> list, Uri zip, boolean enc, boolean verity, Uri boot) {
this(context, list, zip, enc, verity); this(context, list, zip, enc, verity);
mBootImg = boot; mBootImg = boot;
mode = PATCH_MODE; mode = PATCH_MODE;
} }
public InstallMagisk(Activity context, AdaptiveList<String> list, Uri zip, boolean enc, boolean verity, String boot) { public InstallMagisk(Activity context, List<String> list, Uri zip, boolean enc, boolean verity, String boot) {
this(context, list, zip, enc, verity); this(context, list, zip, enc, verity);
mBootLocation = boot; mBootLocation = boot;
mode = DIRECT_MODE; 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 @Override
protected Boolean doInBackground(Void... voids) { protected Boolean doInBackground(Void... voids) {
MagiskManager mm = MagiskManager.get(); MagiskManager mm = MagiskManager.get();

View File

@ -1,26 +0,0 @@
package com.topjohnwu.magisk.container;
import java.util.ArrayList;
public abstract class AdaptiveList<E> extends ArrayList<E> {
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;
}
}

View File

@ -0,0 +1,22 @@
package com.topjohnwu.magisk.container;
import android.os.Handler;
import java.util.ArrayList;
public abstract class CallbackList<E> extends ArrayList<E> {
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;
}
}