新增 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>capturer</w>
<w>dismissable</w>
<w>fileprovider</w>
<w>flowable</w>
<w>imgproc</w>
<w>interruptible</w>

View File

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

View File

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

View File

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