github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/renderers/apk/activity.java (about)

     1  package {{ .AppID }};
     2  
     3  import android.annotation.TargetApi;
     4  import android.app.Activity;
     5  import android.content.Intent;
     6  import android.net.Uri;
     7  import android.os.Build;
     8  import android.os.Bundle;
     9  import android.view.KeyEvent;
    10  import android.webkit.PermissionRequest;
    11  import android.webkit.ValueCallback;
    12  import android.webkit.WebChromeClient;
    13  import android.webkit.WebResourceRequest;
    14  import android.webkit.WebSettings;
    15  import android.webkit.WebView;
    16  import android.webkit.WebViewClient;
    17  import android.widget.LinearLayout;
    18  import android.view.Window;
    19  
    20  public class MainActivity extends Activity {
    21    private ValueCallback<Uri[]> fileChooserCallback;
    22  
    23    static {
    24      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    25        // Support for modern Android and hardware floats
    26        System.loadLibrary("backend");
    27      } else {
    28        // Support for legacy Android and software floats
    29        System.loadLibrary("backend_compat");
    30      }
    31    }
    32  
    33    private native String LaunchBackend(String filesDir);
    34  
    35    @Override
    36    protected void onCreate(Bundle savedInstanceState) {
    37      requestWindowFeature(Window.FEATURE_NO_TITLE);
    38    
    39      super.onCreate(savedInstanceState);
    40  
    41      Uri home = Uri.parse(LaunchBackend(getFilesDir().getAbsolutePath()));
    42  
    43      WebView view = new WebView(this);
    44      view.setLayoutParams(
    45          new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
    46                                        LinearLayout.LayoutParams.MATCH_PARENT));
    47  
    48      WebSettings settings = view.getSettings();
    49      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    50        settings.setAllowContentAccess(true);
    51      }
    52      settings.setAllowFileAccess(true);
    53      settings.setDatabaseEnabled(true);
    54      settings.setDomStorageEnabled(true);
    55      settings.setGeolocationEnabled(true);
    56      settings.setJavaScriptCanOpenWindowsAutomatically(true);
    57      settings.setJavaScriptEnabled(true);
    58      settings.setLoadsImagesAutomatically(true);
    59      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    60        settings.setMediaPlaybackRequiresUserGesture(false);
    61      }
    62      settings.setSupportMultipleWindows(true);
    63  
    64      view.setWebViewClient(new WebViewClient() {
    65        @SuppressWarnings("deprecation")
    66        @Override
    67        public boolean shouldOverrideUrlLoading(WebView vw, String url) {
    68          if (url.contains(home.getHost())) {
    69            vw.loadUrl(url);
    70          } else {
    71            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    72            vw.getContext().startActivity(intent);
    73          }
    74  
    75          return true;
    76        }
    77  
    78        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    79        @Override
    80        public boolean shouldOverrideUrlLoading(WebView vw,
    81                                                WebResourceRequest request) {
    82          if (request.getUrl().toString().contains(home.getHost())) {
    83            vw.loadUrl(request.getUrl().toString());
    84          } else {
    85            Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
    86            vw.getContext().startActivity(intent);
    87          }
    88  
    89          return true;
    90        }
    91      });
    92      view.setWebChromeClient(new WebChromeClient() {
    93        @Override
    94        public void onPermissionRequest(final PermissionRequest request) {
    95          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    96            request.grant(request.getResources());
    97          }
    98        }
    99  
   100        @Override
   101        public boolean onShowFileChooser(WebView vw,
   102                                         ValueCallback<Uri[]> filePathCallback,
   103                                         FileChooserParams fileChooserParams) {
   104          if (fileChooserCallback != null) {
   105            fileChooserCallback.onReceiveValue(null);
   106          }
   107          fileChooserCallback = filePathCallback;
   108  
   109          Intent selectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
   110          selectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
   111          selectionIntent.setType("*/*");
   112  
   113          Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
   114          chooserIntent.putExtra(Intent.EXTRA_INTENT, selectionIntent);
   115          startActivityForResult(chooserIntent, 0);
   116  
   117          return true;
   118        }
   119      });
   120      view.setOnKeyListener((v, keyCode, event) -> {
   121        WebView vw = (WebView)v;
   122        if (event.getAction() == KeyEvent.ACTION_DOWN &&
   123            keyCode == KeyEvent.KEYCODE_BACK && vw.canGoBack()) {
   124          vw.goBack();
   125  
   126          return true;
   127        }
   128  
   129        return false;
   130      });
   131      view.setDownloadListener((uri, userAgent, contentDisposition, mimetype,
   132                                contentLength) -> handleURI(uri));
   133      view.setOnLongClickListener(v -> {
   134        handleURI(((WebView)v).getHitTestResult().getExtra());
   135  
   136        return true;
   137      });
   138  
   139      view.loadUrl(home.toString());
   140  
   141      setContentView(view);
   142    }
   143  
   144    @Override
   145    protected void onActivityResult(int requestCode, int resultCode,
   146                                    Intent intent) {
   147      super.onActivityResult(requestCode, resultCode, intent);
   148  
   149      fileChooserCallback.onReceiveValue(
   150          new Uri[] {Uri.parse(intent.getDataString())});
   151      fileChooserCallback = null;
   152    }
   153  
   154    private void handleURI(String uri) {
   155      if (uri != null) {
   156        Intent i = new Intent(Intent.ACTION_VIEW);
   157        i.setData(Uri.parse(uri.replaceFirst("^blob:", "")));
   158  
   159        startActivity(i);
   160      }
   161    }
   162  }