Skip to content

Commit fa8c371

Browse files
committed
Added java loader for python webserver
1 parent 331b6b6 commit fa8c371

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
package org.kivy.android;
33

4+
import java.net.Socket;
5+
import java.net.InetSocketAddress;
6+
7+
import android.os.SystemClock;
8+
49
import java.io.InputStream;
510
import java.io.FileInputStream;
611
import java.io.FileOutputStream;
@@ -42,10 +47,11 @@
4247

4348
import org.kivy.android.PythonUtil;
4449

50+
import org.kivy.android.WebViewLoader;
51+
4552
import org.renpy.android.ResourceManager;
4653
import org.renpy.android.AssetExtract;
4754

48-
4955
public class PythonActivity extends Activity {
5056
// This activity is modified from a mixture of the SDLActivity and
5157
// PythonActivity in the SDL2 bootstrap, but removing all the SDL2
@@ -130,7 +136,9 @@ public void onClick(DialogInterface dialog,int id) {
130136
// Set up the webview
131137
mWebView = new WebView(this);
132138
mWebView.getSettings().setJavaScriptEnabled(true);
139+
mWebView.getSettings().setDomStorageEnabled(true);
133140
mWebView.loadUrl("file:///" + mActivity.getFilesDir().getAbsolutePath() + "/_load.html");
141+
// mWebView.loadUrl("http://localhost:5000/");
134142

135143
mWebView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
136144
mWebView.setWebViewClient(new WebViewClient() {
@@ -154,7 +162,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
154162
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
155163
PythonActivity.nativeSetEnv("PYTHONHOME", mFilesDirectory);
156164
PythonActivity.nativeSetEnv("PYTHONPATH", mFilesDirectory + ":" + mFilesDirectory + "/lib");
157-
165+
158166
try {
159167
Log.v(TAG, "Access to our meta-data...");
160168
this.mMetaData = this.mActivity.getPackageManager().getApplicationInfo(
@@ -170,9 +178,12 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
170178
final Thread pythonThread = new Thread(new PythonMain(), "PythonThread");
171179
PythonActivity.mPythonThread = pythonThread;
172180
pythonThread.start();
173-
174-
}
175181

182+
final Thread wvThread = new Thread(new TestMain(), "WvThread");
183+
wvThread.start();
184+
185+
}
186+
176187
public void loadLibraries() {
177188
PythonUtil.loadLibraries(getFilesDir());
178189
}
@@ -374,3 +385,10 @@ public void run() {
374385
PythonActivity.nativeInit(new String[0]);
375386
}
376387
}
388+
389+
class TestMain implements Runnable {
390+
@Override
391+
public void run() {
392+
WebViewLoader.testConnection();
393+
}
394+
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,21 @@ public static void testConnection() {
1919

2020
while (true) {
2121
if (WebViewLoader.pingHost("localhost", 5000, 100)) {
22-
Log.v(TAG, "successfully pinged localhost");
23-
// PythonActivity.mWebView.loadUrl("http://localhost:5000");
24-
// PythonActivity.mWebView.loadUrl("http://www.google.com");
25-
22+
Log.v(TAG, "Cuccessfully pinged localhost:5000");
2623
Handler mainHandler = new Handler(PythonActivity.mActivity.getMainLooper());
2724

2825
Runnable myRunnable = new Runnable() {
2926
@Override
3027
public void run() {
31-
// PythonActivity.mActivity.mWebView.loadUrl("http://127.0.0.1:5000/");
32-
// PythonActivity.mActivity.mWebView.loadUrl("http://www.google.com");
33-
PythonActivity.mActivity.mWebView.loadUrl("file:///" + PythonActivity.mActivity.getFilesDir().getAbsolutePath() + "/load_immediate.html");
34-
// PythonActivity.mActivity.mWebView.loadUrl("file:///" + PythonActivity.mActivity.getFilesDir().getAbsolutePath() + "/_load2.html");
35-
Log.v(TAG, "did webview load!");
28+
PythonActivity.mActivity.mWebView.loadUrl("http://127.0.0.1:5000/");
29+
Log.v(TAG, "Loaded webserver in webview");
3630
}
37-
// public void run() {PythonActivity.mActivity.mWebView.loadUrl("http://127.0.0.1:5000");}
3831
};
3932
mainHandler.post(myRunnable);
4033
break;
4134

4235
} else {
43-
Log.v(TAG, "could not ping localhost");
36+
Log.v(TAG, "Could not ping localhost:5000");
4437
try {
4538
Thread.sleep(100);
4639
} catch(InterruptedException e) {
@@ -57,8 +50,10 @@ public static boolean pingHost(String host, int port, int timeout) {
5750
Socket socket = new Socket();
5851
try {
5952
socket.connect(new InetSocketAddress(host, port), timeout);
53+
socket.close();
6054
return true;
6155
} catch (IOException e) {
56+
try {socket.close();} catch (IOException f) {return false;}
6257
return false; // Either timeout or unreachable or failed DNS lookup.
6358
}
6459
}

pythonforandroid/bootstraps/webview/build/webview_includes/_load.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
}
5353

5454
/* window.setInterval(queryFlask, 500); */
55-
window.setTimeout(function() {location.replace('http://127.0.0.1:5000/')}, 1000)
55+
/* window.setTimeout(function() {location.replace('http://127.0.0.1:5000/')}, 1000) */
5656
</script>
5757

5858
</body>

0 commit comments

Comments
 (0)