Skip to content

Commit

Permalink
Merge pull request #17 from endlessm/T34576-add-view-switcher
Browse files Browse the repository at this point in the history
Add a separate webview to display a loading screen
  • Loading branch information
manuq authored Mar 21, 2023
2 parents 638b235 + 51f2107 commit b3c326e
Showing 1 changed file with 94 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.List;
import java.util.ArrayList;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.ViewGroup;
import android.view.KeyEvent;
import android.app.Activity;
Expand All @@ -25,6 +27,7 @@
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
Expand Down Expand Up @@ -54,7 +57,9 @@ public class PythonActivity extends Activity {
public static boolean mBrokenLibraries;

protected static ViewGroup mLayout;
protected static ViewSwitcher mWebViewSwitcher;
public static WebView mWebView;
public static WebView mLoadingWebView;

protected static Thread mPythonThread;

Expand All @@ -67,6 +72,14 @@ public String getAppRoot() {
return app_root;
}

public WebView getMainWebView() {
return mWebView;
}

public WebView getLoadingWebView() {
return mLoadingWebView;
}

public String getEntryPoint(String search_dir) {
/* Get the main file (.pyc|.pyo|.py) depending on if we
* have a compiled version or not.
Expand All @@ -88,6 +101,7 @@ public static void initialize() {
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
mWebView = null;
mLayout = null;
mWebViewSwitcher = null;
mBrokenLibraries = false;
}

Expand All @@ -102,6 +116,21 @@ protected void onCreate(Bundle savedInstanceState) {
new UnpackFilesTask().execute(getAppRoot());
}

public boolean shouldLoadUri(Uri uri) {
return uri.getScheme().equals("file") || uri.getHost().equals("127.0.0.1");
}

public boolean tryOpenExternalLink(String uriString) {
Uri uri = Uri.parse(uriString);
if (mOpenExternalLinksInBrowser && !shouldLoadUri(uri)) {
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
return true;
} else {
return false;
}
}

private class UnpackFilesTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Expand Down Expand Up @@ -158,38 +187,59 @@ public void onClick(DialogInterface dialog,int id) {
// Set up the webview
String app_root_dir = getAppRoot();

mLayout = new AbsoluteLayout(PythonActivity.mActivity) {
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
};
setContentView(mLayout);

/* TODO: It would be nice to do a crossfade animation here */
mWebViewSwitcher = new ViewSwitcher(PythonActivity.mActivity);
mLayout.addView(mWebViewSwitcher);

mLoadingWebView = new WebView(PythonActivity.mActivity);
mLoadingWebView.getSettings().setJavaScriptEnabled(true);
mLoadingWebView.getSettings().setDomStorageEnabled(true);
mLoadingWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mLoadingWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mLoadingWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
mLoadingWebView.loadUrl("file:///android_asset/_load.html");
mLoadingWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return tryOpenExternalLink(url);
}
});
mWebViewSwitcher.addView(mLoadingWebView, 0);

mWebView = new WebView(PythonActivity.mActivity);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
mWebView.loadUrl("file:///android_asset/_load.html");

mWebView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri u = Uri.parse(url);
if (mOpenExternalLinksInBrowser) {
if (!(u.getScheme().equals("file") || u.getHost().equals("127.0.0.1"))) {
Intent i = new Intent(Intent.ACTION_VIEW, u);
startActivity(i);
return true;
}
}
return false;
}

@Override
public void onPageFinished(WebView view, String url) {
CookieManager.getInstance().flush();
}
});
mLayout = new AbsoluteLayout(PythonActivity.mActivity);
mLayout.addView(mWebView);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return tryOpenExternalLink(url);
}

setContentView(mLayout);
@Override
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "MainPythonWebViewClient onPageFinished");
displayMainWebView();
// FIXME: We should use postVisualStateCallback here...
// mWebView.postVisualStateCallback(123, new WebView.VisualStateCallback() {
// @Override
// public void onComplete(long requestId) {
// displayMainWebView();
// }
// });
}
});
mWebViewSwitcher.addView(mWebView, 1);

String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
String entry_point = getEntryPoint(app_root_dir);
Expand Down Expand Up @@ -256,6 +306,26 @@ public void run() {
mActivity.runOnUiThread(new LoadUrl(url));
}

public static void displayLoadingWebView() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "displayLoadingWebView");
mWebViewSwitcher.setDisplayedChild(0);
}
});
}

public static void displayMainWebView() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "displayMainWebView");
mWebViewSwitcher.setDisplayedChild(1);
}
});
}

public static void enableZoom() {
mActivity.runOnUiThread(new Runnable() {
@Override
Expand Down

0 comments on commit b3c326e

Please sign in to comment.