-
Notifications
You must be signed in to change notification settings - Fork 28
/
MainActivity.java
121 lines (103 loc) · 4.28 KB
/
MainActivity.java
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
package org.qpython.sample;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText code = (EditText)findViewById(R.id.edit_text);
code.setText("#qpy:console\n" +
"try:\n" +
" import androidhelper\n" +
"\n" +
" droid = androidhelper.Android()\n" +
" line = droid.dialogGetInput()\n" +
" s = 'Hello %s' % line.result\n" +
" droid.makeToast(s)\n" +
"except:\n" +
" print(\"Hello, Please update to newest QPython version from (http://play.qpython.com/qrcode-python.html) to use this feature\")\n");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/*
*
*
*/
private final int SCRIPT_EXEC_PY = 40001;
private final String extPlgPlusName = "org.qpython.qpy";
public static boolean checkAppInstalledByName(Context context, String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
Log.d("QPYMAIN", "checkAppInstalledByName:"+packageName+" found");
return true;
} catch (NameNotFoundException e) {
Log.d("QPYMAIN", "checkAppInstalledByName:"+packageName+" not found");
return false;
}
}
public void onQPyExec(View v) {
if (checkAppInstalledByName(getApplicationContext(), extPlgPlusName)) {
Toast.makeText(this, "Sample of calling QPython API", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClassName(extPlgPlusName, "org.qpython.qpylib.MPyApi");
intent.setAction(extPlgPlusName + ".action.MPyApi");
Bundle mBundle = new Bundle();
mBundle.putString("app", "myappid");
mBundle.putString("act", "onPyApi");
mBundle.putString("flag", "onQPyExec"); // any String flag you may use in your context
mBundle.putString("param", ""); // param String param you may use in your context
/*
* The String Python code, you can put your py file in res or raw or intenet, so that you can get it the same way, which can make it scalable
*/
EditText codeTxt = (EditText)findViewById(R.id.edit_text);
String code = codeTxt.getText().toString();
mBundle.putString("pycode", code);
intent.putExtras(mBundle);
startActivityForResult(intent, SCRIPT_EXEC_PY);
} else {
Toast.makeText(getApplicationContext(), "Please install QPython first", Toast.LENGTH_LONG).show();
try {
Uri uLink = Uri.parse("market://details?id=com.hipipal.qpyplus");
Intent intent = new Intent( Intent.ACTION_VIEW, uLink );
startActivity(intent);
} catch (Exception e) {
Uri uLink = Uri.parse("http://qpython.com");
Intent intent = new Intent( Intent.ACTION_VIEW, uLink );
startActivity(intent);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SCRIPT_EXEC_PY) {
if (data!=null) {
Bundle bundle = data.getExtras();
String flag = bundle.getString("flag"); // flag you set
String param = bundle.getString("param"); // param you set
String result = bundle.getString("result"); // Result your Pycode generate
Toast.makeText(this, "onQPyExec: return ("+result+")", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "onQPyExec: data is null", Toast.LENGTH_SHORT).show();
}
}
}
}