Skip to content

Commit dd21e9d

Browse files
committed
Upload file
1 parent a795ed2 commit dd21e9d

1 file changed

Lines changed: 99 additions & 1 deletion

File tree

krscript/src/main/java/com/omarea/krscript/model/ShellHandlerBase.java

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public abstract class ShellHandlerBase extends Handler {
3838
* 处理Exitvalue
3939
*/
4040
public static final int EVENT_EXIT = -2;
41+
42+
protected abstract void onAm(String type, String args);
4143

4244
protected abstract void onToast(String text);
4345

@@ -99,7 +101,7 @@ protected void onReaderMsg(Object msg) {
99101
}
100102

101103
// toast:[text...]
102-
if (log.startsWith("toast:[")) {
104+
if (log.matches("^toast:\\[.*]$")) {
103105
int end = log.lastIndexOf(']');
104106
if (end > "toast:[".length()) {
105107
String text = log.substring("toast:[".length(), end)
@@ -108,6 +110,21 @@ protected void onReaderMsg(Object msg) {
108110
}
109111
return;
110112
}
113+
114+
if (log.matches("^am:\\[(start|broadcast|service)\\|.+]$")) {
115+
String prefix = "am:[";
116+
int end = log.lastIndexOf("]");
117+
if (end > prefix.length()) {
118+
String body = log.substring(prefix.length(), end);
119+
int sep = body.indexOf("|");
120+
if (sep > 0) {
121+
String type = body.substring(0, sep).trim();
122+
String args = body.substring(sep + 1).trim();
123+
onAm(type, args);
124+
}
125+
}
126+
return;
127+
}
111128

112129
// log thường
113130
onReader(msg);
@@ -125,6 +142,87 @@ protected void onError(Object msg) {
125142
updateLog(msg, "#ff0000");
126143
}
127144

145+
@Override
146+
protected void onAm(String type, String args) {
147+
try {
148+
Intent intent = parseIntentArgs(args);
149+
150+
switch (type) {
151+
case "start":
152+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
153+
context.startActivity(intent);
154+
break;
155+
156+
case "broadcast":
157+
context.sendBroadcast(intent);
158+
break;
159+
160+
case "service":
161+
if (Build.VERSION.SDK_INT >= 26) {
162+
context.startForegroundService(intent);
163+
} else {
164+
context.startService(intent);
165+
}
166+
break;
167+
}
168+
} catch (Exception e) {
169+
e.printStackTrace();
170+
}
171+
}
172+
173+
private Intent parseIntentArgs(String args) {
174+
Intent intent = new Intent();
175+
176+
String[] tokens = args.split("\\s+");
177+
for (int i = 0; i < tokens.length; i++) {
178+
switch (tokens[i]) {
179+
180+
case "-a":
181+
intent.setAction(tokens[++i]);
182+
break;
183+
184+
case "-d":
185+
intent.setData(Uri.parse(tokens[++i]));
186+
break;
187+
188+
case "-n": {
189+
String[] cn = tokens[++i].split("/");
190+
intent.setComponent(new ComponentName(cn[0], cn[1]));
191+
break;
192+
}
193+
194+
case "--es": {
195+
String key = tokens[++i];
196+
String val = tokens[++i];
197+
intent.putExtra(key, val);
198+
break;
199+
}
200+
201+
case "--ei": {
202+
String key = tokens[++i];
203+
int val = Integer.parseInt(tokens[++i]);
204+
intent.putExtra(key, val);
205+
break;
206+
}
207+
208+
case "--el": {
209+
String key = tokens[++i];
210+
long val = Long.parseLong(tokens[++i]);
211+
intent.putExtra(key, val);
212+
break;
213+
}
214+
215+
case "--ez": {
216+
String key = tokens[++i];
217+
boolean val = Boolean.parseBoolean(tokens[++i]);
218+
intent.putExtra(key, val);
219+
break;
220+
}
221+
}
222+
}
223+
return intent;
224+
}
225+
128226
/**
129227
* 输出指定颜色的内容
130228
*

0 commit comments

Comments
 (0)