forked from qpython-android/qpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPyApi.java
More file actions
288 lines (237 loc) · 9.32 KB
/
Copy pathMPyApi.java
File metadata and controls
288 lines (237 loc) · 9.32 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package org.qpython.qpylib;
//package org.qpython.qpylib;
//
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import com.quseit.base.QBaseApp;
import org.qpython.qpy.console.ScriptExec;
import com.quseit.util.FileHelper;
import com.quseit.util.NAction;
import com.quseit.util.NUtil;
import org.qpython.qpy.main.activity.BaseActivity;
import org.qpython.qpysdk.QPyConstants;
import java.io.File;
public class MPyApi extends BaseActivity {
public static final int SleepFinish = 1;
protected static final String TAG = "mpyapi";
protected String param;
protected String flag = "";
protected int runMode = 0;
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == SleepFinish) {
finish();
}
super.handleMessage(msg);
}
};
private boolean live = false;
private String logF = QPyConstants.ABSOLUTE_LOG;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "process");
process();
}
};
@SuppressWarnings("unused")
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//if (!BASE_CONF.DEBUG)
Log.d(TAG, "onServiceConnected");
//binded = true;
handler.postDelayed(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
}, 202);
}
@Override
public void onServiceDisconnected(ComponentName name) {
//if (!BASE_CONF.DEBUG)
Log.d(TAG, "onServiceDisconnected");
//binded = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
IntentFilter filter = new IntentFilter(".MPyApi");
// registerReceiver(mReceiver, filter);
if (!NUtil.isRunning(getApplicationContext(), "org.qpython.qsl4a.QPyScriptService")) {
Intent intent = new Intent();
intent.setClassName(this, "org.qpython.sl4alib.QPyScriptService");
startService(intent);
}
process();
}
private void process() {
Intent intent = this.getIntent();
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(this,path, null, true);
} else if (type.equals("project")) {
ScriptExec.getInstance().playProject(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(this,pyfile, null, false);
} else {
// Compatibility Mode
if (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(this,script, null, false);
}
}
}
}
}
new SleepThread().start();
}
void handleSendUrl(Intent intent) {
Uri myURI = intent.getData();
if (myURI != null) {
String script = getApplicationContext().getFilesDir() + "/bin/share.py";
String param = myURI.toString();
ScriptExec.getInstance().playQScript(this,script, param, false);
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
//Toast.makeText(this,"sharedText:"+sharedText, Toast.LENGTH_SHORT).show();
//onMenu();
String script = getApplicationContext().getFilesDir() + "/bin/share.py";
String param = sharedText;
//new QPyTask().execute(script, param);
ScriptExec.getInstance().playQScript(this,script, param, false);
// Update UI to reflect text being shared
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
//onMenu();
ScriptExec.getInstance().playQScript(this, getApplicationContext().getFilesDir() + "/bin/share.py", imageUri.toString(), false);
// Update UI to reflect image being shared
}
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
onAPIEnd();
super.onDestroy();
}
@Override
public void onResume() {
Log.d(TAG, "onResume");
//onAPIEnd();
onAPIEnd();
super.onResume();
Intent intent = this.getIntent();
String param0 = intent.getStringExtra(QPyConstants.EXTRA_CONTENT_URL0);
if (param0 != null && param0.equals("shortcut")) {
finish();
} else {
// viewLog();
new SleepThread().start();
}
//flag = "end";
}
@SuppressWarnings("deprecation")
protected void onAPIEnd() {
if (!live) {
Intent rIntent = new Intent();
Bundle rBundle = new Bundle();
if (flag.equals("qedit")) {
if (param != null && !param.equals("")) {
} else {
try {
String root = QBaseApp.getInstance().getOrCreateRoot(QPyConstants.DFROM_RUN);
File f = new File(root, ".last_tmp.py");
param = f.getAbsolutePath().toString();
} catch (Exception e) {
e.printStackTrace();
}
}
}
String result = FileHelper.getFileContents(logF);
rBundle.putString("result", result);
rBundle.putString("param", param);
rBundle.putString("flag", flag);
rBundle.putString("log", QPyConstants.ABSOLUTE_LOG);
rIntent.putExtras(rBundle);
//NotificationManager notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
//notificationManager.cancel(1000);
//LogUtil.d(TAG, "onAPIEnd:"+logF);
File logFile = new File(logF);
if (logFile.exists()) {
live = true;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
MPyApi.this.setResult(RESULT_OK, rIntent);
MPyApi.this.finish();
} else {
if (runMode == 3) { // console 模式
MPyApi.this.setResult(RESULT_OK, rIntent);
MPyApi.this.finish();
}
}
}
}
class SleepThread extends Thread implements Runnable {
@Override
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg = new Message();
msg.what = SleepFinish;
MPyApi.this.myHandler.sendMessage(msg);
}
}
}