6.7.0 - Alpha14 - Fine tuning

This commit is contained in:
SuperMonster003
2026-01-11 23:08:43 +08:00
parent 3aa2bb8915
commit 6f97d73940
15 changed files with 121 additions and 86 deletions

View File

@@ -29,6 +29,8 @@ public class ScriptEngineManager {
}
private final Set<ScriptEngine<? extends ScriptSource>> mEngines = new HashSet<>();
private final Object mEnginesLock = new Object();
private EngineLifecycleCallback mEngineLifecycleCallback;
private final Map<String, Supplier<ScriptEngine<? extends ScriptSource>>> mEngineSuppliers = new HashMap<>();
private final Map<String, Object> mGlobalVariableMap = new HashMap<>();
@@ -41,7 +43,7 @@ public class ScriptEngineManager {
private void addEngine(ScriptEngine<? extends ScriptSource> engine) {
engine.setOnDestroyListener(mOnEngineDestroyListener);
synchronized (mEngines) {
synchronized (mEnginesLock) {
mEngines.add(engine);
if (mEngineLifecycleCallback != null) {
mEngineLifecycleCallback.onEngineCreate(engine);
@@ -54,7 +56,7 @@ public class ScriptEngineManager {
}
public Set<ScriptEngine<? extends ScriptSource>> getEngines() {
synchronized (mEngines) {
synchronized (mEnginesLock) {
return Collections.unmodifiableSet(new LinkedHashSet<>(mEngines));
}
}
@@ -64,7 +66,7 @@ public class ScriptEngineManager {
}
public void removeEngine(ScriptEngine<? extends ScriptSource> engine) {
synchronized (mEngines) {
synchronized (mEnginesLock) {
if (mEngines.remove(engine) && mEngineLifecycleCallback != null) {
mEngineLifecycleCallback.onEngineRemove(engine);
}
@@ -72,7 +74,7 @@ public class ScriptEngineManager {
}
public int stopAll() {
synchronized (mEngines) {
synchronized (mEnginesLock) {
int n = mEngines.size();
for (ScriptEngine<? extends ScriptSource> engine : mEngines) {
engine.forceStop();

View File

@@ -41,7 +41,7 @@ import java.util.List;
/**
* Created by Stardust on Mar 24, 2017.
* Modified by SuperMonster003 as of May 26, 2022.
* Modified by SuperMonster003 as of Jan 7, 2026.
*/
public class TaskListRecyclerView extends ThemeColorRecyclerView {
@@ -59,9 +59,13 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView {
try {
int i = mRunningTaskGroup.addTask(execution);
if (i != -1) {
final Adapter adapter = mAdapter;
post(() -> {
mAdapter.notifyChildInserted(0, i);
mAdapter.notifyParentChanged(0);
if (adapter != mAdapter) {
return;
}
adapter.notifyChildInserted(0, i);
notifyParentChangedSafe(adapter, 0);
});
}
} catch (Exception e) {
@@ -80,11 +84,15 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView {
}
private void onFinish(ScriptExecution execution) {
final Adapter adapter = mAdapter;
post(() -> {
if (adapter != mAdapter) {
return;
}
final int i = mRunningTaskGroup.removeTask(execution);
if (i >= 0) {
mAdapter.notifyChildRemoved(0, i);
mAdapter.notifyParentChanged(0);
adapter.notifyChildRemoved(0, i);
notifyParentChangedSafe(adapter, 0);
} else {
refresh();
}
@@ -92,6 +100,33 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView {
}
};
private void notifyParentChangedSafe(@NonNull Adapter adapter, int parentPosition) {
// Avoid notifying an adapter that has been replaced asynchronously.
// zh-CN: 避免对已经被异步替换的适配器发送通知.
if (adapter != mAdapter) {
return;
}
// Avoid out-of-range parentPosition caused by transient state mismatch.
// zh-CN: 避免由于瞬时状态不一致导致的 parentPosition 越界.
if (parentPosition < 0 || parentPosition >= mTaskGroups.size()) {
return;
}
// RecyclerView may be computing layout/dispatching updates; postpone to next loop.
// zh-CN: RecyclerView 可能正在计算布局/分发更新, 将通知延迟到下一次消息循环.
if (isComputingLayout()) {
post(() -> notifyParentChangedSafe(adapter, parentPosition));
return;
}
try {
adapter.notifyParentChanged(parentPosition);
} catch (IndexOutOfBoundsException e) {
/* Ignored. */
}
}
public TaskListRecyclerView(Context context) {
super(context);
init();
@@ -169,14 +204,15 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView {
}
void onTaskChange(ModelChange<?> taskChange) {
final Adapter adapter = mAdapter;
if (taskChange.getAction() == ModelChange.INSERT) {
mAdapter.notifyChildInserted(1, mPendingTaskGroup.addTask(taskChange.getData()));
mAdapter.notifyParentChanged(1);
adapter.notifyChildInserted(1, mPendingTaskGroup.addTask(taskChange.getData()));
notifyParentChangedSafe(adapter, 1);
} else if (taskChange.getAction() == ModelChange.DELETE) {
final int i = mPendingTaskGroup.removeTask(taskChange.getData());
if (i >= 0) {
mAdapter.notifyChildRemoved(1, i);
mAdapter.notifyParentChanged(1);
adapter.notifyChildRemoved(1, i);
notifyParentChangedSafe(adapter, 1);
} else {
Log.w(LOG_TAG, "data inconsistent on change: " + taskChange);
refresh();
@@ -184,7 +220,7 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView {
} else if (taskChange.getAction() == ModelChange.UPDATE) {
final int i = mPendingTaskGroup.updateTask(taskChange.getData());
if (i >= 0) {
mAdapter.notifyChildChanged(1, i);
adapter.notifyChildChanged(1, i);
} else {
refresh();
}

View File

@@ -1,9 +1,7 @@
package org.autojs.autojs.util
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import com.afollestad.materialdialogs.DialogAction
import com.afollestad.materialdialogs.MaterialDialog
import org.autojs.autojs.app.DialogUtils
@@ -26,42 +24,6 @@ object UpdateUtils {
Pref.getLinkedHashSet(R.string.key_ignored_updates)
}
@JvmStatic
fun openUrl(context: Context, url: String) {
context.startActivity(
Intent(Intent.ACTION_VIEW)
.setData(url.toUri())
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
class IntervalChecker {
private val mMinCheckedIntervalNoNewer: Long = 120 * 60 * 1000
private val mMinCheckedIntervalPostponed: Long = 30 * 60 * 1000
private val mMinCheckedIntervalAutoChecked: Long = 15 * 1000
val isBeyondNoNewer: Boolean
get() {
val now = System.currentTimeMillis()
val lastNoNewer = lastNoNewerUpdatesTimestamp
return now - lastNoNewer > mMinCheckedIntervalNoNewer
}
val isBeyondPostponed: Boolean
get() {
val now = System.currentTimeMillis()
val lastPostponed = lastUpdatesPostponedTimestamp
return now - lastPostponed > mMinCheckedIntervalPostponed
}
val isBeyondAutoChecked: Boolean
get() {
val now = System.currentTimeMillis()
val lastAutoChecked = lastUpdatesAutoCheckedTimestamp
return now - lastAutoChecked > mMinCheckedIntervalAutoChecked
}
}
fun manageIgnoredUpdates(context: Context) {
MaterialDialog.Builder(context)
.title(R.string.text_ignored_updates)
@@ -167,15 +129,47 @@ object UpdateUtils {
@JvmStatic
@JvmOverloads
fun autoCheckForUpdatesIfNeededWithSnackbar(activity: AppCompatActivity, snackbarViewIdRes: Int = android.R.id.content) {
if (isAutoCheckForUpdatesEnabled) {
val checker = IntervalChecker()
if (checker.isBeyondNoNewer && checker.isBeyondPostponed && checker.isBeyondAutoChecked) {
UpdateChecker.Builder(activity.findViewById(snackbarViewIdRes))
.setPromptMode(PromptMode.SNACKBAR)
.build().checkNow()
Pref.refreshLastUpdatesAutoCheckedTimestamp()
if (!isAutoCheckForUpdatesEnabled) return
val checker = IntervalChecker()
if (!checker.isBeyondNoNewer || !checker.isBeyondPostponed || !checker.isBeyondAutoChecked) return
UpdateChecker.Builder(activity.findViewById(snackbarViewIdRes))
.setPromptMode(PromptMode.SNACKBAR)
.setGitHubMainBranch("master")
.build()
.checkNow()
Pref.refreshLastUpdatesAutoCheckedTimestamp()
}
private class IntervalChecker {
private val mMinCheckedIntervalNoNewer: Long = 120 * 60 * 1000
private val mMinCheckedIntervalPostponed: Long = 30 * 60 * 1000
private val mMinCheckedIntervalAutoChecked: Long = 15 * 1000
val isBeyondNoNewer: Boolean
get() {
val now = System.currentTimeMillis()
val lastNoNewer = lastNoNewerUpdatesTimestamp
return now - lastNoNewer > mMinCheckedIntervalNoNewer
}
}
val isBeyondPostponed: Boolean
get() {
val now = System.currentTimeMillis()
val lastPostponed = lastUpdatesPostponedTimestamp
return now - lastPostponed > mMinCheckedIntervalPostponed
}
val isBeyondAutoChecked: Boolean
get() {
val now = System.currentTimeMillis()
val lastAutoChecked = lastUpdatesAutoCheckedTimestamp
return now - lastAutoChecked > mMinCheckedIntervalAutoChecked
}
}
}