Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调整主页搜索和旧编辑器 #762

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/assets/js/js-beautify/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,8 @@ if (typeof define === "function" && define.amd) {
function beautify(source){
return js_beautify(source, {
'indent_size': 4,
'e4x': true
'e4x': true,
max_preserve_newlines:2
});
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/autojs/autojs/Pref.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.autojs.autojs.autojs.key.GlobalKeyObserver;
import org.autojs.autoxjs.R;

import java.io.File;
import java.util.concurrent.TimeUnit;

Expand All @@ -24,6 +25,15 @@ public class Pref {
private static final String KEY_FLOATING_MENU_SHOWN = "KEY_FLOATING_MENU_SHOWN";
private static final String KEY_EDITOR_THEME = "editor.theme";
private static final String KEY_EDITOR_TEXT_SIZE = "editor.textSize";
private static final String KEY_EDITOR_NEW = "KEY_EDITOR_NEW";

public static boolean getEditor() {
return def().getBoolean(KEY_EDITOR_NEW, true);
}

public static void setEditor(boolean shouldOpen) {
def().edit().putBoolean(KEY_EDITOR_NEW, shouldOpen).apply();
}

private static SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
Expand Down
123 changes: 76 additions & 47 deletions app/src/main/java/org/autojs/autojs/model/script/Scripts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ package org.autojs.autojs.model.script
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.annotation.Nullable
import android.os.Build
import android.widget.Toast

import androidx.annotation.Nullable
import com.stardust.app.GlobalAppContext
import com.stardust.autojs.execution.ExecutionConfig
import com.stardust.autojs.execution.ScriptExecution
import com.stardust.autojs.execution.SimpleScriptExecutionListener
import com.stardust.autojs.runtime.exception.ScriptInterruptedException
import com.stardust.autojs.script.ScriptSource
import com.stardust.util.IntentUtil

import org.autojs.autojs.Pref
import org.autojs.autoxjs.R
import org.autojs.autojs.autojs.AutoJs
import org.autojs.autojs.external.ScriptIntents
import org.autojs.autojs.external.fileprovider.AppFileProvider
import org.autojs.autojs.external.shortcut.Shortcut
import org.autojs.autojs.external.shortcut.ShortcutActivity
import org.autojs.autojs.ui.edit.EditActivity

import org.autojs.autoxjs.R
import org.mozilla.javascript.RhinoException

import java.io.File
import java.io.FileFilter
import com.aiselp.autojs.codeeditor.EditActivity as EditActivity2
import org.autojs.autojs.ui.edit.EditActivity as EditActivity1

/**
* Created by Stardust on 2017/5/3.
Expand All @@ -44,33 +42,38 @@ object Scripts {
|| file.name.endsWith(".auto")
}

private val BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER = object : SimpleScriptExecutionListener() {
private val BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER =
object : SimpleScriptExecutionListener() {

override fun onSuccess(execution: ScriptExecution, result: Any?) {
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED))
}

override fun onException(execution: ScriptExecution, e: Throwable) {
val rhinoException = getRhinoException(e)
var line = -1
var col = 0
if (rhinoException != null) {
line = rhinoException.lineNumber()
col = rhinoException.columnNumber()
override fun onSuccess(execution: ScriptExecution, result: Any?) {
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED))
}
if (ScriptInterruptedException.causedByInterrupted(e)) {
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED)
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col))
} else {
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED)
.putExtra(EXTRA_EXCEPTION_MESSAGE, e.message)
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col))

override fun onException(execution: ScriptExecution, e: Throwable) {
val rhinoException = getRhinoException(e)
var line = -1
var col = 0
if (rhinoException != null) {
line = rhinoException.lineNumber()
col = rhinoException.columnNumber()
}
if (ScriptInterruptedException.causedByInterrupted(e)) {
GlobalAppContext.get().sendBroadcast(
Intent(ACTION_ON_EXECUTION_FINISHED)
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col)
)
} else {
GlobalAppContext.get().sendBroadcast(
Intent(ACTION_ON_EXECUTION_FINISHED)
.putExtra(EXTRA_EXCEPTION_MESSAGE, e.message)
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col)
)
}
}
}

}
}


fun openByOtherApps(uri: Uri) {
Expand All @@ -83,15 +86,19 @@ object Scripts {

fun createShortcut(scriptFile: ScriptFile) {
Shortcut(GlobalAppContext.get()).name(scriptFile.simplifiedName)
.targetClass(ShortcutActivity::class.java)
.iconRes(R.drawable.ic_node_js_black)
.extras(Intent().putExtra(ScriptIntents.EXTRA_KEY_PATH, scriptFile.path))
.send()
.targetClass(ShortcutActivity::class.java)
.iconRes(R.drawable.ic_node_js_black)
.extras(Intent().putExtra(ScriptIntents.EXTRA_KEY_PATH, scriptFile.path))
.send()
}


fun edit(context: Context, file: ScriptFile) {
EditActivity.editFile(context, file.simplifiedName, file.path, true)
if (Pref.getEditor() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
EditActivity2.editFile(context, file)
} else {
EditActivity1.editFile(context, file.simplifiedName, file.path, false)
}
}

fun edit(context: Context, path: String) {
Expand All @@ -100,8 +107,10 @@ object Scripts {

fun run(file: ScriptFile): ScriptExecution? {
return try {
AutoJs.getInstance().scriptEngineService.execute(file.toSource(),
ExecutionConfig(workingDirectory = file.parent))
AutoJs.getInstance().scriptEngineService.execute(
file.toSource(),
ExecutionConfig(workingDirectory = file.parent)
)
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(GlobalAppContext.get(), e.message, Toast.LENGTH_LONG).show()
Expand All @@ -113,7 +122,10 @@ object Scripts {

fun run(source: ScriptSource): ScriptExecution? {
return try {
AutoJs.getInstance().scriptEngineService.execute(source, ExecutionConfig(workingDirectory = Pref.getScriptDirPath()))
AutoJs.getInstance().scriptEngineService.execute(
source,
ExecutionConfig(workingDirectory = Pref.getScriptDirPath())
)
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(GlobalAppContext.get(), e.message, Toast.LENGTH_LONG).show()
Expand All @@ -123,16 +135,27 @@ object Scripts {
}

fun runWithBroadcastSender(file: File): ScriptExecution {
return AutoJs.getInstance().scriptEngineService.execute(ScriptFile(file).toSource(), BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER,
ExecutionConfig(workingDirectory = file.parent))
return AutoJs.getInstance().scriptEngineService.execute(
ScriptFile(file).toSource(), BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER,
ExecutionConfig(workingDirectory = file.parent)
)
}


fun runRepeatedly(scriptFile: ScriptFile, loopTimes: Int, delay: Long, interval: Long): ScriptExecution {
fun runRepeatedly(
scriptFile: ScriptFile,
loopTimes: Int,
delay: Long,
interval: Long
): ScriptExecution {
val source = scriptFile.toSource()
val directoryPath = scriptFile.parent
return AutoJs.getInstance().scriptEngineService.execute(source, ExecutionConfig(workingDirectory = directoryPath,
delay = delay, loopTimes = loopTimes, interval = interval))
return AutoJs.getInstance().scriptEngineService.execute(
source, ExecutionConfig(
workingDirectory = directoryPath,
delay = delay, loopTimes = loopTimes, interval = interval
)
)
}

@Nullable
Expand All @@ -149,11 +172,17 @@ object Scripts {

fun send(file: ScriptFile) {
val context = GlobalAppContext.get()
context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_STREAM, IntentUtil.getUriOfFile(context, file.path, AppFileProvider.AUTHORITY)),
context.startActivity(
Intent.createChooser(
Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(
Intent.EXTRA_STREAM,
IntentUtil.getUriOfFile(context, file.path, AppFileProvider.AUTHORITY)
),
GlobalAppContext.getString(R.string.text_send)
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)

}
}
25 changes: 19 additions & 6 deletions app/src/main/java/org/autojs/autojs/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,19 @@ private fun TopBar(
)
}
}
IconButton(onClick = { isSearch = true }) {
Icon(
imageVector = Icons.Default.Search,
contentDescription = stringResource(id = R.string.text_search)
)
if (currentPage == 0) {
IconButton(onClick = { isSearch = true }) {
Icon(
imageVector = Icons.Default.Search,
contentDescription = stringResource(id = R.string.text_search)
)
}
}
} else {
IconButton(onClick = { isSearch = false }) {
IconButton(onClick = {
isSearch = false
onSearch("")
}) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = stringResource(id = R.string.text_exit_search)
Expand All @@ -380,6 +385,14 @@ private fun TopBar(
onSearch(keyword)
})
)
if (keyword.isNotEmpty()) {
IconButton(onClick = { keyword = "" }) {
Icon(
imageVector = Icons.Default.Clear,
contentDescription = null
)
}
}
}
LogButton()
when (currentPage) {
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/org/autojs/autojs/ui/main/drawer/DrawerPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
Expand All @@ -17,6 +16,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.ExitToApp
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Settings
Expand Down Expand Up @@ -787,7 +787,21 @@ private fun AccessibilityServiceSwitch() {
).show()
}
}

var editor by remember { mutableStateOf(Pref.getEditor()) }
SwitchItem(
icon = {
MyIcon(
Icons.Default.Edit,
contentDescription = null,
)
},
text = { Text(text = "启用新编辑器") },
checked = editor,
onCheckedChange = { isChecked ->
editor = isChecked
Pref.setEditor(isChecked)
}
)
SwitchItem(
icon = {
MyIcon(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.autojs.autojs.ui.main.scripts

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -26,7 +25,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import com.aiselp.autojs.codeeditor.EditActivity
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.leinardi.android.speeddial.compose.FabWithLabel
import com.leinardi.android.speeddial.compose.SpeedDial
Expand All @@ -48,7 +46,6 @@ import org.autojs.autojs.ui.main.showExternalStoragePermissionToast
import org.autojs.autojs.ui.viewmodel.ExplorerItemList.SortConfig
import org.autojs.autojs.ui.widget.fillMaxSize
import org.autojs.autoxjs.R
import java.io.File

/**
* Created by wilinz on 2022/7/15.
Expand Down Expand Up @@ -240,9 +237,7 @@ class ScriptListFragment : Fragment() {
setOnItemClickListener { _, item ->
item?.let {
if (item.isEditable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
EditActivity.editFile(requireContext(), File(item.path))
} else edit(requireContext(), item.toScriptFile())
edit(requireContext(), item.toScriptFile());
} else {
IntentUtil.viewFile(get(), item.path, AppFileProvider.AUTHORITY)
}
Expand Down
Loading