11package com .samsung .microbit .ui .activity ;
22
33import android .app .Activity ;
4- import android .content .Context ;
54import android .content .Intent ;
65import android .net .Uri ;
76import android .os .Bundle ;
8- import android .os .Environment ;
97import android .os .Handler ;
108import android .os .Looper ;
119import android .util .Base64 ;
1513import android .webkit .JavascriptInterface ;
1614import android .webkit .ValueCallback ;
1715import android .webkit .WebChromeClient ;
16+ import android .webkit .WebResourceRequest ;
1817import android .webkit .WebSettings ;
1918import android .webkit .WebView ;
2019import android .webkit .WebViewClient ;
2726import com .samsung .microbit .utils .ProjectsHelper ;
2827
2928import java .io .File ;
30- import java .io .FileInputStream ;
3129import java .io .FileOutputStream ;
32- import java .io .IOException ;
33- import java .io .InputStream ;
34- import java .io .OutputStream ;
3530
3631import static android .content .ContentValues .TAG ;
3732
@@ -43,7 +38,7 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
4338
4439 private WebView webView ;
4540 public static String makecodeUrl = "https://makecode.microbit.org/?androidapp=" + BuildConfig .VERSION_CODE ;
46- public static Activity activityHandle = null ;
41+ public static MakeCodeWebView activityHandle = null ;
4742
4843 boolean projectDownload = false ;
4944
@@ -53,6 +48,10 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
5348 private byte [] dataToSave = null ;
5449 private ValueCallback <Uri []> onShowFileChooser_filePathCallback ;
5550
51+ private boolean mRelaunchOnFinishNavigation = false ;
52+ private String mRelaunchURL = makecodeUrl ;
53+
54+
5655 public static void setMakecodeUrl (String url ) {
5756 makecodeUrl = url ;
5857 }
@@ -93,13 +92,13 @@ protected void onCreate(Bundle savedInstanceState) {
9392
9493 webView .setWebViewClient (new WebViewClient () {
9594 @ 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 ()) ;
103102 }
104103
105104 @ Override
@@ -112,9 +111,26 @@ public void onLoadResource(WebView view, String url) {
112111 public void onPageFinished (WebView view , String url ) {
113112 super .onPageFinished (view , url );
114113 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+ }
116132 }
117- }); //setWebViewClient
133+ }); //setWebViewClient
118134
119135 webView .setWebChromeClient (new WebChromeClient () {
120136 @ Override
@@ -251,6 +267,77 @@ else if ( !hexName.isEmpty()) {
251267 MBApp .getAppState ().eventPairMakeCodeBegin ();
252268 } // onCreate
253269
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+
254341 private void saveData ( String name , String mimetype , byte [] data ) {
255342 Intent intent = new Intent (Intent .ACTION_CREATE_DOCUMENT );
256343 intent .addCategory (Intent .CATEGORY_OPENABLE );
@@ -297,7 +384,8 @@ public File getProjectFile( String hexName)
297384
298385 @ Override
299386 public void onBackPressed () {
300- if (webView .canGoBack ()) {
387+ String url = webView .getUrl ();
388+ if ( url != null && !url .startsWith ( makecodeUrl ) && webView .canGoBack ()) {
301389 webView .goBack ();
302390 } else {
303391 super .onBackPressed ();
0 commit comments