Skip to content

Commit

Permalink
Use Weak reference for refreshing threads
Browse files Browse the repository at this point in the history
Signed-off-by: sunilpaulmathew <sunil.kde@gmail.com>
  • Loading branch information
sunilpaulmathew committed Oct 23, 2022
1 parent f5ffbd8 commit aa88c4c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.smartpack.kernelmanager.utils.tools.Scripts;
import com.smartpack.kernelmanager.views.dialog.Dialog;

import java.lang.ref.WeakReference;
import java.util.ConcurrentModificationException;

/*
Expand All @@ -61,32 +62,40 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mScriptTitle.setText(getString(R.string.executing) + " " + Scripts.mScriptName);

mBackButton.setOnClickListener(v -> onBackPressed());
refreshStatus();

Thread mRefreshThread = new RefreshThread(this);
mRefreshThread.start();
}

public void refreshStatus() {
new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(100);
runOnUiThread(() -> {
try {
mOutput.setText(Utils.getOutput(Scripts.mOutput).isEmpty() ? getString(R.string.executing) + " ..." : Utils.getOutput(Scripts.mOutput));
} catch (ConcurrentModificationException ignored) {}
if (Scripts.mApplying) {
mScriptTitle.setText(getString(R.string.executing));
mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
} else {
mScriptTitle.setText(mCancelled ? getString(R.string.exceute_cancel_title, Scripts.mScriptName) : getString(R.string.script_executed, Scripts.mScriptName));
mOutput.setText(Utils.getOutput(Scripts.mOutput).isEmpty() ? getString(R.string.script_executed, Scripts.mScriptName) : Utils.getOutput(Scripts.mOutput));
}
});
private class RefreshThread extends Thread {
WeakReference<ApplyScriptActivity> mInstallerActivityRef;
RefreshThread(ApplyScriptActivity activity) {
mInstallerActivityRef = new WeakReference<>(activity);
}
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(250);
final ApplyScriptActivity activity = mInstallerActivityRef.get();
if(activity == null){
break;
}
} catch (InterruptedException ignored) {}
}
}.start();
activity.runOnUiThread(() -> {
try {
mOutput.setText(Utils.getOutput(Scripts.mOutput).isEmpty() ? getString(R.string.executing) + " ..." : Utils.getOutput(Scripts.mOutput));
} catch (ConcurrentModificationException ignored) {}
if (Scripts.mApplying) {
mScriptTitle.setText(getString(R.string.executing));
mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
} else {
mScriptTitle.setText(mCancelled ? getString(R.string.exceute_cancel_title, Scripts.mScriptName) : getString(R.string.script_executed, Scripts.mScriptName));
mOutput.setText(Utils.getOutput(Scripts.mOutput).isEmpty() ? getString(R.string.script_executed, Scripts.mScriptName) : Utils.getOutput(Scripts.mOutput));
}
});
}
} catch (InterruptedException ignored) {}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.smartpack.kernelmanager.utils.Utils;

import java.io.File;
import java.lang.ref.WeakReference;

/**
* Created by sunilpaulmathew <sunil.kde@gmail.com> on April 23, 2020
Expand Down Expand Up @@ -76,34 +77,42 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
onBackPressed();
Utils.rebootCommand(this);
});
refreshStatus();

Thread mRefreshThread = new RefreshThread(this);
mRefreshThread.start();
}

public void refreshStatus() {
new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(100);
runOnUiThread(() -> {
if (Common.isFlashing()) {
mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
} else {
mProgressLayout.setVisibility(View.GONE);
mSaveButton.setVisibility(Utils.getOutput(Common.getFlashingOutput()).endsWith("\nsuccess") ? View.VISIBLE : View.GONE);
mRebootButton.setVisibility(Utils.getOutput(Common.getFlashingOutput()).endsWith("\nsuccess") ? View.VISIBLE : View.GONE);
mCancelButton.setVisibility(View.VISIBLE);
}
mFlashingHeading.setText(Common.isFlashing() ? getString(R.string.flashing) : Utils.getOutput(Common.getFlashingOutput()).endsWith("\nsuccess") ?
getString(R.string.flashing_finished) : getString(R.string.flashing_failed));
mFlashingResult.setText(!Utils.getOutput(Common.getFlashingOutput()).isEmpty() ? Utils.getOutput(Common.getFlashingOutput())
.replace("\nsuccess","") : Common.isFlashing() ? "" : Common.getFlashingResult().toString());
});
private class RefreshThread extends Thread {
WeakReference<FlashingActivity> mInstallerActivityRef;
RefreshThread(FlashingActivity activity) {
mInstallerActivityRef = new WeakReference<>(activity);
}
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(250);
final FlashingActivity activity = mInstallerActivityRef.get();
if(activity == null){
break;
}
} catch (InterruptedException ignored) {}
}
}.start();
activity.runOnUiThread(() -> {
if (Common.isFlashing()) {
mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
} else {
mProgressLayout.setVisibility(View.GONE);
mSaveButton.setVisibility(Utils.getOutput(Common.getFlashingOutput()).endsWith("\nsuccess") ? View.VISIBLE : View.GONE);
mRebootButton.setVisibility(Utils.getOutput(Common.getFlashingOutput()).endsWith("\nsuccess") ? View.VISIBLE : View.GONE);
mCancelButton.setVisibility(View.VISIBLE);
}
mFlashingHeading.setText(Common.isFlashing() ? getString(R.string.flashing) : Utils.getOutput(Common.getFlashingOutput()).endsWith("\nsuccess") ?
getString(R.string.flashing_finished) : getString(R.string.flashing_failed));
mFlashingResult.setText(!Utils.getOutput(Common.getFlashingOutput()).isEmpty() ? Utils.getOutput(Common.getFlashingOutput())
.replace("\nsuccess","") : Common.isFlashing() ? "" : Common.getFlashingResult().toString());
});
}
} catch (InterruptedException ignored) {}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.smartpack.kernelmanager.utils.Utils;
import com.smartpack.kernelmanager.utils.root.RootUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
Expand Down Expand Up @@ -122,34 +123,40 @@ public void afterTextChanged(Editable s) {
}
});

refreshStatus();
Thread mRefreshThread = new RefreshThread(this);
mRefreshThread.start();
}

public void refreshStatus() {
new Thread() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(100);
runOnUiThread(() -> {
if (mRunning) {
mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
mClearAll.setText(R.string.cancel);
try {
mShellOutput.setText(Utils.getOutput(mResult));
} catch (ConcurrentModificationException | NullPointerException ignored) {
}
} else {
mShellOutput.setTextIsSelectable(true);
mClearAll.setText(R.string.clear);
}
});
private class RefreshThread extends Thread {
WeakReference<TerminalActivity> mActivityRef;
RefreshThread(TerminalActivity activity) {
mActivityRef = new WeakReference<>(activity);
}
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(500);
final TerminalActivity activity = mActivityRef.get();
if(activity == null){
break;
}
} catch (InterruptedException ignored) {}
}
}.start();
activity.runOnUiThread(() -> {
if (mRunning) {
mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
mClearAll.setText(R.string.cancel);
try {
mShellOutput.setText(Utils.getOutput(mResult));
} catch (ConcurrentModificationException | NullPointerException ignored) {
}
} else {
mShellOutput.setTextIsSelectable(true);
mClearAll.setText(R.string.clear);
}
});
}
} catch (InterruptedException ignored) {}
}
}

@SuppressLint({"SetTextI18n", "StaticFieldLeak"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.smartpack.kernelmanager.activities;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

Expand Down

0 comments on commit aa88c4c

Please sign in to comment.