1
1
package com .samsung .microbit .ui .activity ;
2
2
3
3
import android .app .Activity ;
4
- import android .content .Context ;
5
4
import android .content .Intent ;
6
5
import android .net .Uri ;
7
6
import android .os .Bundle ;
8
- import android .os .Environment ;
9
7
import android .os .Handler ;
10
8
import android .os .Looper ;
11
9
import android .util .Base64 ;
15
13
import android .webkit .JavascriptInterface ;
16
14
import android .webkit .ValueCallback ;
17
15
import android .webkit .WebChromeClient ;
16
+ import android .webkit .WebResourceRequest ;
18
17
import android .webkit .WebSettings ;
19
18
import android .webkit .WebView ;
20
19
import android .webkit .WebViewClient ;
27
26
import com .samsung .microbit .utils .ProjectsHelper ;
28
27
29
28
import java .io .File ;
30
- import java .io .FileInputStream ;
31
29
import java .io .FileOutputStream ;
32
- import java .io .IOException ;
33
- import java .io .InputStream ;
34
- import java .io .OutputStream ;
35
30
36
31
import static android .content .ContentValues .TAG ;
37
32
@@ -43,7 +38,7 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
43
38
44
39
private WebView webView ;
45
40
public static String makecodeUrl = "https://makecode.microbit.org/?androidapp=" + BuildConfig .VERSION_CODE ;
46
- public static Activity activityHandle = null ;
41
+ public static MakeCodeWebView activityHandle = null ;
47
42
48
43
boolean projectDownload = false ;
49
44
@@ -53,6 +48,10 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
53
48
private byte [] dataToSave = null ;
54
49
private ValueCallback <Uri []> onShowFileChooser_filePathCallback ;
55
50
51
+ private boolean mRelaunchOnFinishNavigation = false ;
52
+ private String mRelaunchURL = makecodeUrl ;
53
+
54
+
56
55
public static void setMakecodeUrl (String url ) {
57
56
makecodeUrl = url ;
58
57
}
@@ -93,13 +92,13 @@ protected void onCreate(Bundle savedInstanceState) {
93
92
94
93
webView .setWebViewClient (new WebViewClient () {
95
94
@ Override
96
- public boolean shouldOverrideUrlLoading (WebView view , String url ) {
97
- Log .v (TAG , "url: " + url );
98
- if ( url . contains ( "https://microbit.org/" )) {
99
- MBApp . getAppState (). eventPairMakeCodeEnd ();
100
- activityHandle . finish ();
101
- }
102
- return false ;
95
+ public boolean shouldOverrideUrlLoading ( WebView view , String url ) {
96
+ Log .v (TAG , "shouldOverrideUrlLoading (legacy) " + url );
97
+ return overrideUri ( Uri . parse ( url ));
98
+ }
99
+ public boolean shouldOverrideUrlLoading ( WebView view , WebResourceRequest request ) {
100
+ Log . v ( TAG , "shouldOverrideUrlLoading " + request );
101
+ return overrideUri ( request . getUrl ()) ;
103
102
}
104
103
105
104
@ Override
@@ -112,9 +111,26 @@ public void onLoadResource(WebView view, String url) {
112
111
public void onPageFinished (WebView view , String url ) {
113
112
super .onPageFinished (view , url );
114
113
Log .v (TAG , "onPageFinished(" + url + ");" );
115
- onPageFinishedJS ( view , url );
114
+ onPageFinishedJS (view , url );
115
+
116
+ if ( url .startsWith ( makecodeUrl )) {
117
+ mRelaunchURL = makecodeUrl ;
118
+ String hashEditor = "#editor" ;
119
+ if ( url .contains ( hashEditor )) {
120
+ if ( mRelaunchURL .endsWith ( "#" )) {
121
+ mRelaunchURL = mRelaunchURL .substring ( 0 , mRelaunchURL .length () - 1 );
122
+ }
123
+ mRelaunchURL = mRelaunchURL + hashEditor ;
124
+ }
125
+ Log .v (TAG , "Remember relaunch URL " + mRelaunchURL );
126
+ }
127
+
128
+ if ( mRelaunchOnFinishNavigation ) {
129
+ mRelaunchOnFinishNavigation = false ;
130
+ webView .loadUrl ( mRelaunchURL );
131
+ }
116
132
}
117
- }); //setWebViewClient
133
+ }); //setWebViewClient
118
134
119
135
webView .setWebChromeClient (new WebChromeClient () {
120
136
@ Override
@@ -251,6 +267,77 @@ else if ( !hexName.isEmpty()) {
251
267
MBApp .getAppState ().eventPairMakeCodeBegin ();
252
268
} // onCreate
253
269
270
+
271
+ private boolean overrideUri ( final Uri uri ) {
272
+ String url = uri .toString ().toLowerCase ();
273
+ Log .v (TAG , "overrideUri: " + url );
274
+ if ( url .contains ("https://microbit.org/code" )) {
275
+ MBApp .getAppState ().eventPairMakeCodeEnd ();
276
+ finish ();
277
+ return true ;
278
+ }
279
+
280
+ String host = uri .getHost ();
281
+ String path = uri .getPath ();
282
+ host = host == null ? "" : host .toLowerCase ();
283
+ path = path == null ? "" : path .toLowerCase ();
284
+
285
+ if ( host .equals ("makecode.microbit.org" )) {
286
+ if ( url .startsWith ( makecodeUrl ))
287
+ return false ;
288
+ else if ( path .startsWith ( "/oauth/login" ))
289
+ return false ;
290
+ else if ( path .equals ( "/" ) && uri .getQueryParameter ("authcallback" ) != null )
291
+ return false ;
292
+ }
293
+ else if ( host .equals ( "makecode.com" )) {
294
+ if ( path .startsWith ("/oauth/callback" ))
295
+ return false ;
296
+ else if ( path .startsWith ("/auth/callback" ))
297
+ return false ;
298
+ }
299
+ else if ( host .equals ( "login.live.com" ))
300
+ return false ;
301
+ else if ( host .equals ( "login.microsoftonline.com" ))
302
+ return false ;
303
+ else if ( host .equals ( "www.pxt.io" ))
304
+ return false ;
305
+ else if ( host .equals ( "trg-microbit.userpxt.io" ))
306
+ return false ;
307
+ else if ( host .equals ( "pxt.azureedge.net" ))
308
+ return false ;
309
+ else if ( host .equals ( "accounts.google.com" ))
310
+ return false ;
311
+ else if ( host .equals ( "clever.com" ))
312
+ return false ;
313
+ else if ( host .equals ( "github.com" )) {
314
+ if ( path .startsWith ( "/login/oauth/" ))
315
+ return false ;
316
+ if ( path .equals ( "/login" ))
317
+ return false ;
318
+ if ( path .startsWith ( "/sessions/" ))
319
+ return false ;
320
+ if ( path .equals ( "/logout" ))
321
+ return false ;
322
+ // When signing out of GitHub, relaunch MakeCode,
323
+ // otherwise it takes 2 or 3 "backs" to return to MakeCode
324
+ if ( path .equals ( "/" )) {
325
+ mRelaunchOnFinishNavigation = true ;
326
+ return false ;
327
+ }
328
+ }
329
+
330
+ openUri ( uri );
331
+ return true ;
332
+ }
333
+
334
+ void openUri ( Uri uri ) {
335
+ Log .v (TAG , "openUri: " + uri );
336
+ Intent intent = new Intent (Intent .ACTION_VIEW );
337
+ intent .setData ( uri );
338
+ startActivity (intent );
339
+ }
340
+
254
341
private void saveData ( String name , String mimetype , byte [] data ) {
255
342
Intent intent = new Intent (Intent .ACTION_CREATE_DOCUMENT );
256
343
intent .addCategory (Intent .CATEGORY_OPENABLE );
@@ -297,7 +384,8 @@ public File getProjectFile( String hexName)
297
384
298
385
@ Override
299
386
public void onBackPressed () {
300
- if (webView .canGoBack ()) {
387
+ String url = webView .getUrl ();
388
+ if ( url != null && !url .startsWith ( makecodeUrl ) && webView .canGoBack ()) {
301
389
webView .goBack ();
302
390
} else {
303
391
super .onBackPressed ();
0 commit comments