Skip to content

Java WebView

Will Morgan edited this page Sep 24, 2021 · 2 revisions
package com.iproov.webview;

import android.Manifest;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_PERMISSIONS_REQUEST_CODE = 12345;
    private static final String URL = "https://demo.iproov.com";
    private WebView webView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView.setWebContentsDebuggingEnabled(true);

        webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
        webView.setWebViewClient(new WebViewClient());
        //This is really important part. Our custom implementation of chrome client will allow webView to go full screen,
        //and will restore the same state of host app after we exit from full screen mode.
        webView.setWebChromeClient(new WebChromeClientFullScreen());

        final int cameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (cameraPermission == PackageManager.PERMISSION_GRANTED) {
            webView.loadUrl(URL);
        } else {
            requestPermissions();
        }
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private void requestPermissions() {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA},
                CAMERA_PERMISSIONS_REQUEST_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CAMERA_PERMISSIONS_REQUEST_CODE && grantResults.length > 0) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                webView.loadUrl(URL);
        }
    }

    private  class WebChromeClientFullScreen extends WebChromeClient {
        private View customView = null;
        private CustomViewCallback customViewCallback = null;
        int originalOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
        int originalVisibility = View.INVISIBLE;

        @Override
        public void onPermissionRequest(PermissionRequest request) {
           if (request != null) request.grant(request.getResources());
        }

        /**
         * Callback will tell the host application that the current page would
         * like to show a custom View in a particular orientation
         */
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            //If we have custom view, that means that we are already in full screen, and need to go to original state
            if (customView != null) {
                onHideCustomView();
                return;
            }
            //going full screen
            customView = view;
            //We need to store there parameters, so we can restore app state, after we exit full screen mode
            originalVisibility = getWindow().getDecorView().getWindowSystemUiVisibility();
            originalOrientation = getRequestedOrientation();
            ((FrameLayout)getWindow().getDecorView()).addView(customView, new FrameLayout.LayoutParams(-1, -1));
            getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }

        /**
         * Callback will tell the host application that the current page exited full screen mode,
         * and the app has to hide custom view.
         */
        @Override
        public void onHideCustomView() {
            ((FrameLayout)getWindow().getDecorView()).removeView(customView);
            customView = null;
            //Restoring aps state, as it was before we go to full screen
            getWindow().getDecorView().setSystemUiVisibility(originalVisibility);
            setRequestedOrientation(originalOrientation);
            if (customViewCallback != null) customViewCallback.onCustomViewHidden();
            customViewCallback = null;
        }
    }
}
Clone this wiki locally