Skip to content

Commit 90658f7

Browse files
committed
load an url in the webview from python code
1 parent ac25fab commit 90658f7

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

pythonforandroid/bootstraps/webview/build/src/org/kivy/android/PythonActivity.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected void onCreate(Bundle savedInstanceState) {
9494
Log.v("Python", "Device: " + android.os.Build.DEVICE);
9595
Log.v("Python", "Model: " + android.os.Build.MODEL);
9696
super.onCreate(savedInstanceState);
97-
97+
9898
PythonActivity.initialize();
9999

100100
// Load shared libraries
@@ -161,7 +161,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
161161
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
162162
PythonActivity.nativeSetEnv("PYTHONHOME", mFilesDirectory);
163163
PythonActivity.nativeSetEnv("PYTHONPATH", mFilesDirectory + ":" + mFilesDirectory + "/lib");
164-
164+
165165
try {
166166
Log.v(TAG, "Access to our meta-data...");
167167
this.mMetaData = this.mActivity.getPackageManager().getApplicationInfo(
@@ -173,7 +173,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
173173
}
174174
} catch (PackageManager.NameNotFoundException e) {
175175
}
176-
176+
177177
final Thread pythonThread = new Thread(new PythonMain(), "PythonThread");
178178
PythonActivity.mPythonThread = pythonThread;
179179
pythonThread.start();
@@ -182,7 +182,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
182182
wvThread.start();
183183

184184
}
185-
185+
186186
public void loadLibraries() {
187187
PythonUtil.loadLibraries(getFilesDir());
188188
}
@@ -276,6 +276,23 @@ public void unpackData(final String resource, File target) {
276276
}
277277
}
278278

279+
public static void loadUrl(String url) {
280+
class LoadUrl implements Runnable {
281+
private String mUrl;
282+
283+
public LoadUrl(String url) {
284+
mUrl = url;
285+
}
286+
287+
public void run() {
288+
mWebView.loadUrl(mUrl);
289+
}
290+
}
291+
292+
Log.i(TAG, "Opening URL: " + url);
293+
mActivity.runOnUiThread(new LoadUrl(url));
294+
}
295+
279296
public static ViewGroup getLayout() {
280297
return mLayout;
281298
}

pythonforandroid/bootstraps/webview/build/templates/WebViewLoader.tmpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@ public class WebViewLoader {
1616
private static final String TAG = "WebViewLoader";
1717

1818
public static void testConnection() {
19-
19+
2020
while (true) {
2121
if (WebViewLoader.pingHost("localhost", {{ args.port }}, 100)) {
2222
Log.v(TAG, "Successfully pinged localhost:{{ args.port }}");
2323
Handler mainHandler = new Handler(PythonActivity.mActivity.getMainLooper());
24-
2524
Runnable myRunnable = new Runnable() {
2625
@Override
2726
public void run() {
28-
PythonActivity.mActivity.mWebView.loadUrl("http://127.0.0.1:{{ args.port }}/");
27+
PythonActivity.mActivity.loadUrl("http://127.0.0.1:{{ args.port }}/");
2928
Log.v(TAG, "Loaded webserver in webview");
3029
}
3130
};
3231
mainHandler.post(myRunnable);
3332
break;
34-
33+
3534
} else {
3635
Log.v(TAG, "Could not ping localhost:{{ args.port }}");
3736
try {
@@ -55,5 +54,3 @@ public static boolean pingHost(String host, int port, int timeout) {
5554
}
5655
}
5756
}
58-
59-

pythonforandroid/toolchain.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ def build_dist_from_args(ctx, dist, args):
118118
ctx.recipe_build_order = build_order
119119
ctx.python_modules = python_modules
120120

121-
if python_modules and hasattr(sys, 'real_prefix'):
122-
error('virtualenv is needed to install pure-Python modules, but')
123-
error('virtualenv does not support nesting, and you are running')
124-
error('python-for-android in one. Please run p4a outside of a')
125-
error('virtualenv instead.')
126-
exit(1)
127-
128121
info('The selected bootstrap is {}'.format(bs.name))
129122
info_main('# Creating dist with {} bootstrap'.format(bs.name))
130123
bs.distribution = dist

testapps/testapp_flask/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ def vibrate():
6161
vibrator.vibrate(float(args['time']) * 1000)
6262
print('vibrated')
6363

64+
@app.route('/loadUrl')
65+
def loadUrl():
66+
args = request.args
67+
if 'url' not in args:
68+
print('ERROR: asked to open an url but without url argument')
69+
print('asked to open url', args['url'])
70+
activity.loadUrl(args['url'])
71+
6472
@app.route('/orientation')
6573
def orientation():
6674
args = request.args
@@ -69,7 +77,7 @@ def orientation():
6977
direction = args['dir']
7078
if direction not in ('horizontal', 'vertical'):
7179
print('ERROR: asked to orient to neither horizontal nor vertical')
72-
80+
7381
if direction == 'horizontal':
7482
activity.setRequestedOrientation(
7583
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

testapps/testapp_flask/templates/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ <h2>Page one</h2>
2424
}
2525
</script>
2626

27+
<button onClick="loadUrl()">
28+
open url
29+
</button>
30+
<input type="text" value="http://www.google.com" id="url_field"/>
31+
32+
<script>
33+
function loadUrl() {
34+
var request = new XMLHttpRequest();
35+
var url = document.getElementById("url_field").value
36+
request.open('GET', 'loadUrl?url=' + url, true);
37+
request.send();
38+
}
39+
</script>
40+
2741
<button onClick="vertical()">
2842
vertical orientation
2943
</button>

0 commit comments

Comments
 (0)