Search in sources :

Example 1 with WebView

use of com.github.pockethub.android.ui.WebView in project PocketHub by pockethub.

the class LoginWebViewActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView webView = new WebView(this);
    // Needs the be activated to allow GitHub to perform their requests.
    webView.getSettings().setJavaScriptEnabled(true);
    String userAgent = webView.getSettings().getUserAgentString();
    // Remove chrome from the user agent since GitHub checks it incorrectly
    userAgent = userAgent.replaceAll("Chrome/\\d{2}\\.\\d\\.\\d\\.\\d", "");
    webView.getSettings().setUserAgentString(userAgent);
    String url = getIntent().getStringExtra(LoginActivity.INTENT_EXTRA_URL);
    webView.loadUrl(url);
    webView.setWebViewClient(new WebViewClient() {

        MaterialDialog dialog = new MaterialDialog.Builder(LoginWebViewActivity.this).content(R.string.loading).progress(true, 0).build();

        @Override
        public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
            dialog.show();
        }

        @Override
        public void onPageFinished(android.webkit.WebView view, String url) {
            dialog.dismiss();
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public WebResourceResponse shouldInterceptRequest(android.webkit.WebView view, WebResourceRequest request) {
            return shouldIntercept(request.getUrl().toString());
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(android.webkit.WebView view, String url) {
            return shouldIntercept(url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
            Uri uri = Uri.parse(url);
            return overrideOAuth(uri) || super.shouldOverrideUrlLoading(view, url);
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebView view, WebResourceRequest request) {
            return overrideOAuth(request.getUrl()) || super.shouldOverrideUrlLoading(view, request);
        }

        /**
         * This method will inject polyfills to the auth javascript if the version is
         * below Lollipop. After Lollipop WebView is updated via the Play Store so the polyfills
         * are not needed.
         *
         * @param url The requests url
         * @return null if there request should not be altered or a new response
         *     instance with polyfills.
         */
        private WebResourceResponse shouldIntercept(String url) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return null;
            }
            if (url.matches(".*frameworks.*.js")) {
                InputStream in1 = null;
                InputStream in2 = null;
                Response response = null;
                try {
                    response = new OkHttpClient.Builder().build().newCall(new Request.Builder().get().url(url).build()).execute();
                    if (response.body() != null) {
                        in1 = response.body().byteStream();
                    }
                    in2 = getAssets().open("polyfills.js");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (response == null) {
                    return null;
                }
                SequenceInputStream inputStream = new SequenceInputStream(in2, in1);
                return new WebResourceResponse("text/javascript", "utf-8", inputStream);
            } else {
                return null;
            }
        }

        private boolean overrideOAuth(Uri uri) {
            if (uri.getScheme().equals(getString(R.string.github_oauth_scheme))) {
                Intent data = new Intent();
                data.setData(uri);
                setResult(RESULT_OK, data);
                finish();
                return true;
            }
            return false;
        }
    });
    setContentView(webView);
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) OkHttpClient(okhttp3.OkHttpClient) WebResourceRequest(android.webkit.WebResourceRequest) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) Request(okhttp3.Request) WebResourceRequest(android.webkit.WebResourceRequest) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) WebResourceResponse(android.webkit.WebResourceResponse) Response(okhttp3.Response) Bitmap(android.graphics.Bitmap) WebResourceResponse(android.webkit.WebResourceResponse) SequenceInputStream(java.io.SequenceInputStream) WebView(com.github.pockethub.android.ui.WebView) TargetApi(android.annotation.TargetApi) WebViewClient(android.webkit.WebViewClient)

Aggregations

TargetApi (android.annotation.TargetApi)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 WebResourceRequest (android.webkit.WebResourceRequest)1 WebResourceResponse (android.webkit.WebResourceResponse)1 WebViewClient (android.webkit.WebViewClient)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 WebView (com.github.pockethub.android.ui.WebView)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1