新增 app.intent()支持flags

This commit is contained in:
hyb1996
2018-11-11 21:58:59 +08:00
parent 0464e3e49b
commit 76fa44960c
4 changed files with 90 additions and 62 deletions

View File

@@ -4,6 +4,7 @@
<w>autojs</w> <w>autojs</w>
<w>capturer</w> <w>capturer</w>
<w>dismissable</w> <w>dismissable</w>
<w>fileprovider</w>
<w>flowable</w> <w>flowable</w>
<w>imgproc</w> <w>imgproc</w>
<w>interruptible</w> <w>interruptible</w>

View File

@@ -1,117 +1,132 @@
module.exports = function(runtime, global){ module.exports = function (runtime, global) {
importClass(android.content.Intent); importClass(android.content.Intent);
var app = Object.create(runtime.app); var app = Object.create(runtime.app);
var context = global.context; var context = global.context;
app.intent = function(i) { app.intent = function (i) {
var intent = new android.content.Intent(); var intent = new android.content.Intent();
if (i.className && i.packageName) { if (i.className && i.packageName) {
intent.setClassName(i.packageName, i.className); intent.setClassName(i.packageName, i.className);
} }
if (i.extras) { if (i.extras) {
for (var key in i.extras) { for (var key in i.extras) {
intent.putExtra(key, i.extras[key]); intent.putExtra(key, i.extras[key]);
} }
} }
if (i.category) { if (i.category) {
if(i.category instanceof Array){ if (i.category instanceof Array) {
for(var j = 0; i < i.category.length; j++){ for (var j = 0; i < i.category.length; j++) {
intent.addCategory(i.category[j]); intent.addCategory(i.category[j]);
} }
}else{ } else {
intent.addCategory(i.category); intent.addCategory(i.category);
} }
} }
if (i.action) { if (i.action) {
if(i.action.indexOf(".") == -1){ if (i.action.indexOf(".") == -1) {
i.action = "android.intent.action." + i.action; i.action = "android.intent.action." + i.action;
} }
intent.setAction(i.action); intent.setAction(i.action);
} }
if (i.type) { if (i.flags) {
if(i.data){ let flags = 0;
intent.setDataAndType(android.net.Uri.parse(i.data), i.type); if (Array.isArray(i.flags)) {
}else{ for (let j = 0; j < i.flags.length; j++) {
intent.setType(i.type); flags |= parseIntentFlag(i.flags[j]);
} }
}else if (i.data) { } else {
intent.setData(android.net.Uri.parse(i.data)); flags = parseIntentFlag(i.flags);
} }
return intent; intent.setFlags(flags);
}
if (i.type) {
if (i.data) {
intent.setDataAndType(android.net.Uri.parse(i.data), i.type);
} else {
intent.setType(i.type);
}
} else if (i.data) {
intent.setData(android.net.Uri.parse(i.data));
}
return intent;
} }
app.startActivity = function(i){ app.startActivity = function (i) {
if(typeof(i) == "string"){ if (typeof (i) == "string") {
if(runtime.getProperty("class." + i)){ if (runtime.getProperty("class." + i)) {
context.startActivity(new Intent(context, runtime.getProperty("class." + i)) context.startActivity(new Intent(context, runtime.getProperty("class." + i))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
return; return;
}else{ } else {
throw new Error("class " + i + " not found"); throw new Error("class " + i + " not found");
} }
} }
context.startActivity(app.intent(i).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); context.startActivity(app.intent(i).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} }
app.sendBroadcast = function(i){ app.sendBroadcast = function (i) {
if(typeof(i) == "string"){ if (typeof (i) == "string") {
if(runtime.getProperty("broadcast." + i)){ if (runtime.getProperty("broadcast." + i)) {
app.sendLocalBroadcastSync(app.intent({action: runtime.getProperty("broadcast." + i)})); app.sendLocalBroadcastSync(app.intent({ action: runtime.getProperty("broadcast." + i) }));
} }
} }
context.sendBroadcast(app.intent(i)); context.sendBroadcast(app.intent(i));
} }
app.sendEmail = function(options){ app.sendEmail = function (options) {
options = options || {}; options = options || {};
var i = new Intent(Intent.ACTION_SEND); var i = new Intent(Intent.ACTION_SEND);
if(options.email){ if (options.email) {
i.putExtra(Intent.EXTRA_EMAIL, toArray(options.email)); i.putExtra(Intent.EXTRA_EMAIL, toArray(options.email));
} }
if(options.cc){ if (options.cc) {
i.putExtra(Intent.EXTRA_CC, toArray(options.cc)); i.putExtra(Intent.EXTRA_CC, toArray(options.cc));
} }
if(options.bcc){ if (options.bcc) {
i.putExtra(Intent.EXTRA_BCC, toArray(options.bcc)); i.putExtra(Intent.EXTRA_BCC, toArray(options.bcc));
} }
if(options.subject){ if (options.subject) {
i.putExtra(Intent.EXTRA_SUBJECT, options.subject); i.putExtra(Intent.EXTRA_SUBJECT, options.subject);
} }
if(options.text){ if (options.text) {
i.putExtra(Intent.EXTRA_TEXT, options.text); i.putExtra(Intent.EXTRA_TEXT, options.text);
} }
if(options.attachment){ if (options.attachment) {
i.putExtra(Intent.EXTRA_STREAM, app.parseUri(options.attachment)); i.putExtra(Intent.EXTRA_STREAM, app.parseUri(options.attachment));
} }
i.setType("message/rfc822"); i.setType("message/rfc822");
context.startActivity(Intent.createChooser(i, "发送邮件").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); context.startActivity(Intent.createChooser(i, "发送邮件").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} }
function toArray(arg){ function toArray(arg) {
if(typeof(arg) == 'string'){ if (typeof (arg) == 'string') {
arg = [arg]; arg = [arg];
} }
let arr = util.java.array("string", arg.length); let arr = util.java.array("string", arg.length);
for(let i = 0; i < arg.length; i++){ for (let i = 0; i < arg.length; i++) {
arr[i] = arg; arr[i] = arg;
} }
return arr; return arr;
} }
app.parseUri = function(uri){ app.parseUri = function (uri) {
if(uri.startsWith("file://")){ if (uri.startsWith("file://")) {
return app.getUriForFile(uri); return app.getUriForFile(uri);
} }
return android.net.Uri.parse(uri); return android.net.Uri.parse(uri);
} }
app.getUriForFile = function(path){ app.getUriForFile = function (path) {
if(path.startsWith("file://")){ if (path.startsWith("file://")) {
path = path.substring(7); path = path.substring(7);
} }
let file = new java.io.File(files.path(path));
if (app.fileProviderAuthority == null) {
return android.net.Uri.fromFile(file);
}
return android.support.v4.content.FileProvider.getUriForFile(context, return android.support.v4.content.FileProvider.getUriForFile(context,
"org.autojs.autojs.fileprovider", new java.io.File(files.path(path))); app.fileProviderAuthority, file);
}; };
app.launch = app.launchPackage; app.launch = app.launchPackage;
@@ -126,6 +141,13 @@ module.exports = function(runtime, global){
global.__asGlobal__(app, ['launchPackage', 'launch', 'launchApp', 'getPackageName', 'getAppName', 'openAppSetting']); global.__asGlobal__(app, ['launchPackage', 'launch', 'launchApp', 'getPackageName', 'getAppName', 'openAppSetting']);
function parseIntentFlag(flag) {
if (typeof (flag) == 'string') {
return android.content.Intent["FLAG_" + flag.toUpperCase()];
}
return flag;
}
return app; return app;
} }

View File

@@ -7,6 +7,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
@@ -14,6 +15,7 @@ import com.stardust.autojs.annotation.ScriptInterface;
import com.stardust.util.IntentUtil; import com.stardust.util.IntentUtil;
import com.stardust.util.MimeTypes; import com.stardust.util.MimeTypes;
import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
@@ -51,7 +53,7 @@ public class AppUtils {
} }
@ScriptInterface @ScriptInterface
public void sendLocalBroadcastSync(Intent intent){ public void sendLocalBroadcastSync(Intent intent) {
LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(intent); LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(intent);
} }
@@ -92,6 +94,10 @@ public class AppUtils {
return IntentUtil.goToAppDetailSettings(mContext, packageName); return IntentUtil.goToAppDetailSettings(mContext, packageName);
} }
public String getFileProviderAuthority() {
return mFileProviderAuthority;
}
@Nullable @Nullable
public Activity getCurrentActivity() { public Activity getCurrentActivity() {
Log.d("App", "getCurrentActivity: " + mCurrentActivity.get()); Log.d("App", "getCurrentActivity: " + mCurrentActivity.get());

View File

@@ -150,7 +150,6 @@ public class IntentUtil {
return false; return false;
} }
} }
} }
public static boolean viewFile(Context context, String path, String mimeType, String fileProviderAuthority) { public static boolean viewFile(Context context, String path, String mimeType, String fileProviderAuthority) {