Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.autojs.autojs.script.JavaScriptFileSource
import org.autojs.autojs.script.ScriptSource
import org.autojs.autojs.script.SequenceScriptSource
import org.autojs.autojs.script.StringScriptSource
import org.autojs.autojs.util.EnvironmentUtils
import org.autojs.autojs.util.WorkingDirectoryUtils
import org.json.JSONException
import org.json.JSONObject
Expand All @@ -33,7 +34,7 @@ object ScriptIntents {

@JvmStatic
fun handleIntent(context: Context?, intent: Intent) {
var path = getPath(intent)
var path = EnvironmentUtils.normalizePath(getPath(intent))
var script = intent.getStringExtra(EXTRA_KEY_PRE_EXECUTE_SCRIPT)

if (intent.hasExtra(EXTRA_KEY_JSON)) {
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/org/autojs/autojs/util/EnvironmentUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,45 @@ object EnvironmentUtils {
val externalStoragePath: String
get() = externalStorageDirectory.path

/**
* Normalize a file path that starts with /sdcard (or legacy /mnt/sdcard) to the
* canonical external storage path returned by Environment.getExternalStorageDirectory().
*
* On standard Android, /sdcard is a symlink to /storage/emulated/0, so File(path).exists()
* returns true and canonicalPath resolves the symlink. On devices without the symlink
* (Fire OS, some custom ROMs), the path is remapped to the real storage root.
*
* Path traversal (..) components are resolved by calling canonicalPath on existing files.
*
* Returns null when path is null; returns the original path when neither the given
* path nor the normalized path resolves.
*/
@JvmStatic
fun normalizePath(path: String?): String? {
if (path == null) return null
val exists = try {
val file = File(path)
file.exists() && file.canonicalPath.let { true }
} catch (_: Exception) {
false
}
if (exists) return File(path).canonicalPath
val externalPath = Environment.getExternalStorageDirectory().absolutePath
if (externalPath == "/sdcard") return path
val normalized = path.replaceFirst(
Regex("^/sdcard(?=/|$)"),
externalPath
).replaceFirst(
Regex("^/mnt/sdcard(?=/|$)"),
externalPath
)
if (normalized == path) return path
return try {
val nf = File(normalized)
if (nf.exists()) nf.canonicalPath else path
} catch (_: Exception) {
path
}
}

}