-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathMPyService.java
More file actions
128 lines (106 loc) · 4.59 KB
/
Copy pathMPyService.java
File metadata and controls
128 lines (106 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.qpython.qpylib;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.quseit.util.FileHelper;
import com.quseit.util.NAction;
import com.quseit.util.NUtil;
import org.qpython.qpy.console.ScriptExec;
import org.qpython.qpy.main.app.App;
import org.qpython.qpysdk.QPyConstants;
public class MPyService extends Service {
protected static final String TAG = "mpyapi";
protected String param;
protected String flag = "";
protected int runMode = 0;
@Override
public void onCreate() {
if (!NUtil.isRunning(getApplicationContext(), "org.qpython.qsl4a.QPyScriptService")) {
Intent intent = new Intent();
intent.setClassName(this, "org.qpython.sl4alib.QPyScriptService");
startService(intent);
}
}
private void process(Intent intent) {
String action = intent.getAction();
String xtype = intent.getType();
if (Intent.ACTION_SEND.equals(action) && xtype != null) {
if ("text/plain".equals(xtype)) {
handleSendText(intent);
} else if (xtype.startsWith("image/")) {
handleSendImage(intent);
}
} else if (Intent.ACTION_VIEW.equals(action)) {
handleSendUrl(intent);
} else {
Bundle bundle = intent.getExtras();
String param0 = intent.getStringExtra(QPyConstants.EXTRA_CONTENT_URL0);
if (param0 != null && param0.equals("shortcut")) {
String type = intent.getStringExtra(QPyConstants.EXTRA_CONTENT_URL1);
String path = intent.getStringExtra(QPyConstants.EXTRA_CONTENT_URL2);
if (type.equals("script")) {
ScriptExec.getInstance().playScript(MPyService.this, path, null, true);
} else if (type.equals("project")) {
ScriptExec.getInstance().playProject(MPyService.this, path, false);
}
} else if (bundle != null) {
String act = bundle.getString("act");
if (act != null) {
if (act.equals("onPyApi")) {//
String pycode = bundle.getString("pycode");
String pyfile = bundle.getString("pyfile");
param = bundle.getString("param");
if (param != null && param.equals("fileapi")) {
runMode = 2;
ScriptExec.getInstance().playScript(MPyService.this, pyfile, null, false);
} else {
// Compatibility Mode
if (pycode != null && pycode.contains("#qpy:console\n") || NAction.isQPy3(getApplicationContext())) {
runMode = 3;
} else {
runMode = 1;
}
String script = QPyConstants.ABSOLUTE_PATH + "/cache/last.py";
FileHelper.putFileContents(this, script, pycode);
ScriptExec.getInstance().playScript(MPyService.this, script, null, false);
}
}
}
}
}
}
void handleSendUrl(Intent intent) {
Uri myURI = intent.getData();
if (myURI != null) {
String script = getApplicationContext().getFilesDir() + "/bin/share.py";
String param = myURI.toString();
ScriptExec.getInstance().playScript(MPyService.this, script, param, false);
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
String script = getApplicationContext().getFilesDir() + "/bin/share.py";
ScriptExec.getInstance().playScript(MPyService.this, script, sharedText, false);
}
}
void handleSendImage(Intent intent) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
ScriptExec.getInstance().playQScript(MPyService.this, getApplicationContext().getFilesDir() + "/bin/share.py", imageUri.toString(), false);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
process(intent);
return super.onStartCommand(intent, flags, startId);
}
}