11
11
import java .util .List ;
12
12
import java .util .ArrayList ;
13
13
14
+ import android .view .animation .AlphaAnimation ;
15
+ import android .view .animation .Animation ;
14
16
import android .view .ViewGroup ;
15
17
import android .view .KeyEvent ;
16
18
import android .app .Activity ;
25
27
import android .content .Context ;
26
28
import android .content .pm .PackageManager ;
27
29
import android .widget .ImageView ;
30
+ import android .widget .ViewSwitcher ;
28
31
import android .graphics .Bitmap ;
29
32
import android .graphics .BitmapFactory ;
30
33
import android .graphics .Color ;
@@ -54,7 +57,9 @@ public class PythonActivity extends Activity {
54
57
public static boolean mBrokenLibraries ;
55
58
56
59
protected static ViewGroup mLayout ;
60
+ protected static ViewSwitcher mWebViewSwitcher ;
57
61
public static WebView mWebView ;
62
+ public static WebView mLoadingWebView ;
58
63
59
64
protected static Thread mPythonThread ;
60
65
@@ -67,6 +72,14 @@ public String getAppRoot() {
67
72
return app_root ;
68
73
}
69
74
75
+ public WebView getMainWebView () {
76
+ return mWebView ;
77
+ }
78
+
79
+ public WebView getLoadingWebView () {
80
+ return mLoadingWebView ;
81
+ }
82
+
70
83
public String getEntryPoint (String search_dir ) {
71
84
/* Get the main file (.pyc|.pyo|.py) depending on if we
72
85
* have a compiled version or not.
@@ -88,6 +101,7 @@ public static void initialize() {
88
101
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
89
102
mWebView = null ;
90
103
mLayout = null ;
104
+ mWebViewSwitcher = null ;
91
105
mBrokenLibraries = false ;
92
106
}
93
107
@@ -102,6 +116,21 @@ protected void onCreate(Bundle savedInstanceState) {
102
116
new UnpackFilesTask ().execute (getAppRoot ());
103
117
}
104
118
119
+ public boolean shouldLoadUri (Uri uri ) {
120
+ return uri .getScheme ().equals ("file" ) || uri .getHost ().equals ("127.0.0.1" );
121
+ }
122
+
123
+ public boolean tryOpenExternalLink (String uriString ) {
124
+ Uri uri = Uri .parse (uriString );
125
+ if (mOpenExternalLinksInBrowser && !shouldLoadUri (uri )) {
126
+ Intent i = new Intent (Intent .ACTION_VIEW , uri );
127
+ startActivity (i );
128
+ return true ;
129
+ } else {
130
+ return false ;
131
+ }
132
+ }
133
+
105
134
private class UnpackFilesTask extends AsyncTask <String , Void , String > {
106
135
@ Override
107
136
protected String doInBackground (String ... params ) {
@@ -158,38 +187,59 @@ public void onClick(DialogInterface dialog,int id) {
158
187
// Set up the webview
159
188
String app_root_dir = getAppRoot ();
160
189
190
+ mLayout = new AbsoluteLayout (PythonActivity .mActivity ) {
191
+ @ Override
192
+ protected ViewGroup .LayoutParams generateDefaultLayoutParams () {
193
+ return new ViewGroup .LayoutParams (LayoutParams .FILL_PARENT , LayoutParams .FILL_PARENT );
194
+ }
195
+ };
196
+ setContentView (mLayout );
197
+
198
+ /* TODO: It would be nice to do a crossfade animation here */
199
+ mWebViewSwitcher = new ViewSwitcher (PythonActivity .mActivity );
200
+ mLayout .addView (mWebViewSwitcher );
201
+
202
+ mLoadingWebView = new WebView (PythonActivity .mActivity );
203
+ mLoadingWebView .getSettings ().setJavaScriptEnabled (true );
204
+ mLoadingWebView .getSettings ().setDomStorageEnabled (true );
205
+ mLoadingWebView .getSettings ().setAllowFileAccessFromFileURLs (true );
206
+ mLoadingWebView .getSettings ().setAllowUniversalAccessFromFileURLs (true );
207
+ mLoadingWebView .getSettings ().setMediaPlaybackRequiresUserGesture (false );
208
+ mLoadingWebView .loadUrl ("file:///android_asset/_load.html" );
209
+ mLoadingWebView .setWebViewClient (new WebViewClient () {
210
+ @ Override
211
+ public boolean shouldOverrideUrlLoading (WebView view , String url ) {
212
+ return tryOpenExternalLink (url );
213
+ }
214
+ });
215
+ mWebViewSwitcher .addView (mLoadingWebView , 0 );
216
+
161
217
mWebView = new WebView (PythonActivity .mActivity );
162
218
mWebView .getSettings ().setJavaScriptEnabled (true );
163
219
mWebView .getSettings ().setDomStorageEnabled (true );
164
220
mWebView .getSettings ().setAllowFileAccessFromFileURLs (true );
165
221
mWebView .getSettings ().setAllowUniversalAccessFromFileURLs (true );
166
222
mWebView .getSettings ().setMediaPlaybackRequiresUserGesture (false );
167
- mWebView .loadUrl ("file:///android_asset/_load.html" );
168
-
169
- mWebView .setLayoutParams (new LayoutParams (LayoutParams .FILL_PARENT , LayoutParams .FILL_PARENT ));
170
223
mWebView .setWebViewClient (new WebViewClient () {
171
- @ Override
172
- public boolean shouldOverrideUrlLoading (WebView view , String url ) {
173
- Uri u = Uri .parse (url );
174
- if (mOpenExternalLinksInBrowser ) {
175
- if (!(u .getScheme ().equals ("file" ) || u .getHost ().equals ("127.0.0.1" ))) {
176
- Intent i = new Intent (Intent .ACTION_VIEW , u );
177
- startActivity (i );
178
- return true ;
179
- }
180
- }
181
- return false ;
182
- }
183
-
184
- @ Override
185
- public void onPageFinished (WebView view , String url ) {
186
- CookieManager .getInstance ().flush ();
187
- }
188
- });
189
- mLayout = new AbsoluteLayout (PythonActivity .mActivity );
190
- mLayout .addView (mWebView );
224
+ @ Override
225
+ public boolean shouldOverrideUrlLoading (WebView view , String url ) {
226
+ return tryOpenExternalLink (url );
227
+ }
191
228
192
- setContentView (mLayout );
229
+ @ Override
230
+ public void onPageFinished (WebView view , String url ) {
231
+ Log .i (TAG , "MainPythonWebViewClient onPageFinished" );
232
+ displayMainWebView ();
233
+ // FIXME: We should use postVisualStateCallback here...
234
+ // mWebView.postVisualStateCallback(123, new WebView.VisualStateCallback() {
235
+ // @Override
236
+ // public void onComplete(long requestId) {
237
+ // displayMainWebView();
238
+ // }
239
+ // });
240
+ }
241
+ });
242
+ mWebViewSwitcher .addView (mWebView , 1 );
193
243
194
244
String mFilesDirectory = mActivity .getFilesDir ().getAbsolutePath ();
195
245
String entry_point = getEntryPoint (app_root_dir );
@@ -256,6 +306,26 @@ public void run() {
256
306
mActivity .runOnUiThread (new LoadUrl (url ));
257
307
}
258
308
309
+ public static void displayLoadingWebView () {
310
+ mActivity .runOnUiThread (new Runnable () {
311
+ @ Override
312
+ public void run () {
313
+ Log .i (TAG , "displayLoadingWebView" );
314
+ mWebViewSwitcher .setDisplayedChild (0 );
315
+ }
316
+ });
317
+ }
318
+
319
+ public static void displayMainWebView () {
320
+ mActivity .runOnUiThread (new Runnable () {
321
+ @ Override
322
+ public void run () {
323
+ Log .i (TAG , "displayMainWebView" );
324
+ mWebViewSwitcher .setDisplayedChild (1 );
325
+ }
326
+ });
327
+ }
328
+
259
329
public static void enableZoom () {
260
330
mActivity .runOnUiThread (new Runnable () {
261
331
@ Override
0 commit comments