Utility Functions
Overview
Clipboard
setClipboardText Set Clipboard
- Set clipboard text. Note: enable Picture-in-Picture or call
takeMeToFrontto bring this app to the foreground - @param text Text content
- @param type 1 = text, 2 = link
- @return
{boolean}true on success, false on failure
function main() {
takeMeToFront()
sleep(1000)
var r = utils.setClipboardText("22222", 1);
logd(r)
}
main();
getClipboardText Read Clipboard
- Read clipboard text. Note: enable Picture-in-Picture or call
takeMeToFrontto bring this app to the foreground - @return
{string}Clipboard data
function main() {
takeMeToFront()
sleep(1000)
var r = utils.getClipboardText();
logd(r)
}
main();
Open URL
openUrl Open URL
- Open a URL. Note: bring the app to the foreground first with
takeMeToFront - @param url URL address
- @return
{boolean}true on success, false on failure
function main() {
takeMeToFront()
sleep(1000)
var r = utils.openUrl("http://baidu.com");
logd(r)
}
main();
Photo Library
saveImageToAlbum Save Image to Album
- Save an image to the photo library
- @param img AutoImage object
- @return
{boolean}true on success, false on failure
function main() {
// Request photo permission once at script start; grant manually or in Settings → EC app → Photos
utils.requestPhotoAuthorization()
sleep(1000)
let img = image.captureFullScreen()
logd("img getHeight " + image.getHeight(img))
logd("img getWidth " + image.getWidth(img))
var r = utils.saveImageToAlbum(img);
logd(r)
}
main();
saveImageToAlbumPath Save Image Path to Album
- Save an image file path to the photo library
- @param path File path
- @return
{boolean}true on success, false on failure
function main() {
// Request photo permission once at script start; grant manually or in Settings → EC app → Photos
utils.requestPhotoAuthorization()
sleep(1000)
let url = "http://192.168.2.10:8199/resource/image/wx.png"
let box = file.getInternalDir("documents")
logd("sandbox " + box)
let filex = box + "/bb.png"
logd("file " + filex)
let r = http.downloadFile(url, filex, 10000, null)
logd("download " + r)
let rx = utils.saveImageToAlbumPath(filex)
logd("r " + rx)
}
main();
saveVideoToAlbumPath Save Video Path to Album
- Save a video file path to the photo library
- @param path Video file path
- @return
{boolean}true on success, false on failure
function main() {
// Request photo permission once at script start; grant manually or in Settings → EC app → Photos
utils.requestPhotoAuthorization()
sleep(1000)
let url = "http://192.168.2.10:8199/resource/image/wx.mp4"
let box = file.getInternalDir("documents")
logd("sandbox " + box)
let filex = box + "/bb.mp4"
logd("file " + filex)
let r = http.downloadFile(url, filex, 10000, null)
logd("download " + r)
let rx = utils.saveVideoToAlbumPath(filex)
logd("r " + rx)
}
main();
utils.requestPhotoAuthorization Request Photo Library Permission
- Request photo library permission
- First request shows a permission dialog — allow it, or go to Settings → scroll to the bottom → EC app → enable Photos
- Note: These are async; ignore return values to avoid blocking simulated taps
- Requires EC standalone 4.9.0+
- @return
{bool}true on success, false on failure
function main() {
let rx = utils.requestPhotoAuthorization()
logd("r " + rx)
// Then call delete functions
let a = utils.deleteAllPhotos()
logd("a " + a)
}
main();
utils.deleteAllPhotos Delete All Photos
- Delete all photos in the library
- A confirmation dialog appears — simulate tapping Delete
- Note: These are async; ignore return values to avoid blocking simulated taps
- Requires EC standalone 4.9.0+
- @return
{bool}true on success, false on failure
function main() {
let rx = utils.requestPhotoAuthorization()
logd("r " + rx)
// Then call delete functions
let a = utils.deleteAllPhotos()
logd("a " + a)
}
main();
utils.deleteAllVideos Delete All Videos
- Delete all videos in the library
- A confirmation dialog appears — simulate tapping Delete
- Note: These are async; ignore return values to avoid blocking simulated taps
- Requires EC standalone 4.9.0+
- @return
{bool}true on success, false on failure
function main() {
let rx = utils.requestPhotoAuthorization()
logd("r " + rx)
// Then call delete functions
let a = utils.deleteAllVideos()
logd("a " + a)
}
main();
Other
utils.dataMd5 Data MD5
- Compute MD5 of data
- Requires EC 4.10.0+
- @param data Data string
- @return
{string}MD5 string or null
function main() {
let docs = file.getInternalDir("documents")
var md5 = utils.fileMd5(docs + "/aaa.png");
logd(md5)
}
main();
utils.fileMd5 File MD5
- Compute MD5 of a file
- @param file File path
- @returns
{string}File MD5 string or null
function main() {
let docs = file.getInternalDir("documents")
var md5 = utils.fileMd5(docs + "/aaa.png");
logd(md5)
}
main();
Random
utils.getRangeInt Get Random Value in Range
- Get a random integer within a range
- @param min Minimum value
- @param max Maximum value
- @return Integer between min and max, inclusive of min, exclusive of max
function main() {
var r = utils.getRangeInt(2, 10);
}
main();
Base64
utils.base64Encode Base64 Encode
- Base64 encode
- @param data String to encode
- @returns
{string}Encoded result
function main() {
var r = utils.base64Encode("111");
logd(r)
}
main();
utils.base64Decode Base64 Decode
- Base64 decode
- @param data String to decode
- @returns
{string}Decoded result
function main() {
var r = utils.base64Decode("MjIy");
logd(r)
}
main();
Audio Playback
utils.playMp3 Play MP3
- Play MP3 asynchronously
- Requires EC standalone 4.5.0+
- @param path File path
- @param loop Loop playback; true to loop
- @return
{bool}true on success, false on failure
function main() {
let path = file.getSandBoxFilePath("test.mp3");
// Copy test.mp3 from script res directory to device path
saveResToFile("test.mp3", path)
let d = utils.playMp3(path, false)
logd("dd " + d)
// Wait for playback to start
sleep(30 * 1000)
utils.stopMp3()
logd("stop play ")
}
main();
utils.playMp3WaitEnd Play MP3 Synchronously
- Play MP3 and wait until finished
- Requires EC standalone 4.5.0+
- @param path File path
- @param loop Loop playback; true to loop
- @return
{bool}true on success, false on failure
function main() {
let path = file.getSandBoxFilePath("test.mp3");
// Copy test.mp3 from script res directory to device path
saveResToFile("test.mp3", path)
let d = utils.playMp3WaitEnd(path, false)
logd("dd " + d)
}
main();
utils.stopMp3 Stop MP3 Playback
- Stop MP3 playback
- Requires EC standalone 4.5.0+
- @return
{bool}true on success, false on failure
function main() {
let path = file.getSandBoxFilePath("test.mp3");
// Copy test.mp3 from script res directory to device path
saveResToFile("test.mp3", path)
let d = utils.playMp3(path, false)
logd("dd " + d)
// Wait for playback to start
sleep(30 * 1000)
utils.stopMp3()
logd("stop play ")
}
main();
Notifications
utils.requestNotificationAuthorization Request Notification Permission
- Request notification permission
- A permission dialog appears — simulate tapping Allow
- Note: These are async; ignore return values to avoid blocking simulated taps
- Requires EC standalone 4.10.0+
- @return
{bool}true on success, false on failure
function main() {
let rx = utils.requestNotificationAuthorization()
logd("r " + rx)
// Then show notification
let a = utils.showNotification("Title", "Content", 1)
logd("a " + a)
}
main();
utils.showNotification Show Notification
- Show a notification
- Requires EC standalone 4.10.0+
- @param title Title
- @param content Content
- @param delay Delay before showing, in seconds (integer)
- @return
{string}Notification ID for later cancel operations
function main() {
let rx = utils.requestNotificationAuthorization()
logd("r " + rx)
// Then show notification
let a = utils.showNotification("Title", "Content", 1)
logd("a " + a)
}
main();
utils.removeNotification Remove Notification
- Remove a notification
- Requires EC standalone 4.10.0+
- Async function; ignore return value
- @param id Notification ID
- @return
{bool}true on success, false on failure
function main() {
let rx = utils.requestNotificationAuthorization()
logd("r " + rx)
// Then show notification
let a = utils.showNotification("Title", "Content", 1)
logd("a " + a)
sleep(5000)
utils.removeNotification(a)
}
main();
utils.removeAllNotification Remove All Notifications
- Remove all displayed notifications
- Note: These are async; ignore return values to avoid blocking simulated taps
- Requires EC standalone 4.10.0+
- @return
{bool}true on success, false on failure
function main() {
let rx = utils.requestNotificationAuthorization()
logd("r " + rx)
// Then show notification
let a = utils.showNotification("Title", "Content", 1)
logd("a " + a)
sleep(5000)
utils.removeAllNotification()
}
main();
QR Code
utils.createQRCode Generate QR Code
- Generate a QR code
- Requires EC standalone 4.12.0+
- @param content QR code string content
- @param width Image width
- @param height Image height
- @param logoImage Optional center logo AutoImage object; see image module
- @return
{AutoImage}To save to file, see image module
function main() {
let da = "test qrcode content"
let logo = readResAutoImage("logo.png")
let img = utils.createQRCode(da,200,100,logo)
logd(img)
// Decode result
logd(utils.decodeQRCode(img));
// Save to file
image.saveTo(img,file.getSandBoxFilePath("/qr.code.jpg"))
image.recycle(img)
}
main();
utils.decodeQRCode Decode QR Code
- Decode a QR code
- Requires EC standalone 4.12.0+
- @param img AutoImage object; see image module
- @return
{string}Decoded string
function main() {
let da = "test qrcode content"
let logo = readResAutoImage("logo.png")
// Generate
let img = utils.createQRCode(da,200,100,logo)
logd(img)
// Decode result
logd(utils.decodeQRCode(img));
// Save to file
image.saveTo(img,file.getSandBoxFilePath("/qr.code.jpg"))
image.recycle(img)
}
main();