Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MakeCode - whitelist URLs; change back action #72

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 105 additions & 17 deletions app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.samsung.microbit.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Base64;
Expand All @@ -15,6 +13,7 @@
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand All @@ -27,11 +26,7 @@
import com.samsung.microbit.utils.ProjectsHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static android.content.ContentValues.TAG;

Expand All @@ -43,7 +38,7 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {

private WebView webView;
public static String makecodeUrl = "https://makecode.microbit.org/?androidapp=" + BuildConfig.VERSION_CODE;
public static Activity activityHandle = null;
public static MakeCodeWebView activityHandle = null;

boolean projectDownload = false;

Expand All @@ -53,6 +48,10 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
private byte[] dataToSave = null;
private ValueCallback<Uri[]> onShowFileChooser_filePathCallback;

private boolean mRelaunchOnFinishNavigation = false;
private String mRelaunchURL = makecodeUrl;


public static void setMakecodeUrl(String url) {
makecodeUrl = url;
}
Expand Down Expand Up @@ -93,13 +92,13 @@ protected void onCreate(Bundle savedInstanceState) {

webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v(TAG, "url: " + url);
if (url.contains("https://microbit.org/")) {
MBApp.getAppState().eventPairMakeCodeEnd();
activityHandle.finish();
}
return false;
public boolean shouldOverrideUrlLoading( WebView view, String url) {
Log.v(TAG, "shouldOverrideUrlLoading (legacy) " + url);
return overrideUri( Uri.parse( url));
}
public boolean shouldOverrideUrlLoading( WebView view, WebResourceRequest request ) {
Log.v(TAG, "shouldOverrideUrlLoading " + request);
return overrideUri( request.getUrl());
}

@Override
Expand All @@ -112,9 +111,26 @@ public void onLoadResource(WebView view, String url) {
public void onPageFinished (WebView view, String url) {
super.onPageFinished(view, url);
Log.v(TAG, "onPageFinished(" + url + ");");
onPageFinishedJS( view, url);
onPageFinishedJS(view, url);

if ( url.startsWith( makecodeUrl)) {
mRelaunchURL = makecodeUrl;
String hashEditor = "#editor";
if ( url.contains( hashEditor)) {
if ( mRelaunchURL.endsWith( "#")) {
mRelaunchURL = mRelaunchURL.substring( 0, mRelaunchURL.length() - 1);
}
mRelaunchURL = mRelaunchURL + hashEditor;
}
Log.v(TAG, "Remember relaunch URL " + mRelaunchURL);
}

if ( mRelaunchOnFinishNavigation) {
mRelaunchOnFinishNavigation = false;
webView.loadUrl( mRelaunchURL);
}
}
}); //setWebViewClient
}); //setWebViewClient

webView.setWebChromeClient(new WebChromeClient() {
@Override
Expand Down Expand Up @@ -251,6 +267,77 @@ else if ( !hexName.isEmpty()) {
MBApp.getAppState().eventPairMakeCodeBegin();
} // onCreate


private boolean overrideUri( final Uri uri) {
String url = uri.toString().toLowerCase();
Log.v(TAG, "overrideUri: " + url);
if ( url.contains("https://microbit.org/code")) {
MBApp.getAppState().eventPairMakeCodeEnd();
finish();
return true;
}

String host = uri.getHost();
String path = uri.getPath();
host = host == null ? "" : host.toLowerCase();
path = path == null ? "" : path.toLowerCase();

if ( host.equals("makecode.microbit.org")) {
if ( url.startsWith( makecodeUrl))
return false;
else if ( path.startsWith( "/oauth/login"))
return false;
else if ( path.equals( "/") && uri.getQueryParameter("authcallback") != null)
return false;
}
else if ( host.equals( "makecode.com")) {
if ( path.startsWith("/oauth/callback"))
return false;
else if ( path.startsWith("/auth/callback"))
return false;
}
else if ( host.equals( "login.live.com"))
return false;
else if ( host.equals( "login.microsoftonline.com"))
return false;
else if ( host.equals( "www.pxt.io"))
return false;
else if ( host.equals( "trg-microbit.userpxt.io"))
return false;
else if ( host.equals( "pxt.azureedge.net"))
return false;
else if ( host.equals( "accounts.google.com"))
return false;
else if ( host.equals( "clever.com"))
return false;
else if ( host.equals( "github.com")) {
if ( path.startsWith( "/login/oauth/"))
return false;
if ( path.equals( "/login"))
return false;
if ( path.startsWith( "/sessions/"))
return false;
if ( path.equals( "/logout"))
return false;
// When signing out of GitHub, relaunch MakeCode,
// otherwise it takes 2 or 3 "backs" to return to MakeCode
if ( path.equals( "/")) {
mRelaunchOnFinishNavigation = true;
return false;
}
}

openUri( uri);
return true;
}

void openUri( Uri uri) {
Log.v(TAG, "openUri: " + uri);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData( uri);
startActivity(intent);
}

private void saveData( String name, String mimetype, byte[] data) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Expand Down Expand Up @@ -297,7 +384,8 @@ public File getProjectFile( String hexName)

@Override
public void onBackPressed() {
if(webView.canGoBack()) {
String url = webView.getUrl();
if ( url != null && !url.startsWith( makecodeUrl) && webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
Expand Down