de.baumann.hhsmoodle.helper.helper_webView.java Source code

Java tutorial

Introduction

Here is the source code for de.baumann.hhsmoodle.helper.helper_webView.java

Source

/*
This file is part of the Browser WebApp.
    
Browser WebApp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Browser WebApp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with the Browser webview app.
    
If not, see <http://www.gnu.org/licenses/>.
 */

package de.baumann.hhsmoodle.helper;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.net.URISyntaxException;

import de.baumann.hhsmoodle.R;
import de.baumann.hhsmoodle.activities.Activity_settings;

import static android.content.ContentValues.TAG;

public class helper_webView {

    @SuppressLint("SetJavaScriptEnabled")
    public static void webView_Settings(final Activity from, final WebView webView) {

        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(from);
        String fontSizeST = sharedPref.getString("font", "100");
        int fontSize = Integer.parseInt(fontSizeST);

        webView.getSettings().setAppCachePath(from.getApplicationContext().getCacheDir().getAbsolutePath());
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setTextZoom(fontSize);
        webView.getSettings().setGeolocationEnabled(false);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
    }

    public static void webView_WebViewClient(final Activity from, final SwipeRefreshLayout swipeRefreshLayout,
            final WebView webView) {

        webView.setWebViewClient(new WebViewClient() {

            ProgressDialog progressDialog;
            int numb;

            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                ViewPager viewPager = (ViewPager) from.findViewById(R.id.viewpager);
                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(from);
                sharedPref.edit().putString("loadURL", webView.getUrl()).apply();

                if (viewPager.getCurrentItem() == 0) {
                    if (url != null) {
                        from.setTitle(webView.getTitle());
                    }
                }

                if (url != null && url.contains("moodle.huebsch.ka.schule-bw.de/moodle/")
                        && url.contains("/login/")) {

                    if (viewPager.getCurrentItem() == 0 && numb != 1) {
                        progressDialog = new ProgressDialog(from);
                        progressDialog.setTitle(from.getString(R.string.login_title));
                        progressDialog.setMessage(from.getString(R.string.login_text));
                        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                                from.getString(R.string.toast_settings), new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        helper_main.switchToActivity(from, Activity_settings.class, true);
                                    }
                                });
                        progressDialog.show();
                        numb = 1;
                        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                numb = 0;
                            }
                        });
                    }

                } else if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.cancel();
                    numb = 0;
                }

                swipeRefreshLayout.setRefreshing(false);

                class_SecurePreferences sharedPrefSec = new class_SecurePreferences(from, "sharedPrefSec",
                        "Ywn-YM.XK$b:/:&CsL8;=L,y4", true);
                String username = sharedPrefSec.getString("username");
                String password = sharedPrefSec.getString("password");

                final String js = "javascript:" + "document.getElementById('password').value = '" + password + "';"
                        + "document.getElementById('username').value = '" + username + "';"
                        + "var ans = document.getElementsByName('answer');"
                        + "document.getElementById('loginbtn').click()";

                if (Build.VERSION.SDK_INT >= 19) {
                    view.evaluateJavascript(js, new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {

                        }
                    });
                } else {
                    view.loadUrl(js);
                }
            }

            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                final Uri uri = Uri.parse(url);
                return handleUri(uri);
            }

            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                final Uri uri = request.getUrl();
                return handleUri(uri);
            }

            private boolean handleUri(final Uri uri) {
                Log.i(TAG, "Uri =" + uri);
                final String url = uri.toString();
                // Based on some condition you need to determine if you are going to load the url
                // in your web view itself or in a browser.
                // You can use `host` or `scheme` or any part of the `uri` to decide.

                if (url.startsWith("http"))
                    return false;//open web links as usual
                //try to find browse activity to handle uri
                Uri parsedUri = Uri.parse(url);
                PackageManager packageManager = from.getPackageManager();
                Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
                if (browseIntent.resolveActivity(packageManager) != null) {
                    from.startActivity(browseIntent);
                    return true;
                }
                //if not activity found, try to parse intent://
                if (url.startsWith("intent:")) {
                    try {
                        Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                        if (intent.resolveActivity(from.getPackageManager()) != null) {
                            from.startActivity(intent);
                            return true;
                        }
                        //try to find fallback url
                        String fallbackUrl = intent.getStringExtra("browser_fallback_url");
                        if (fallbackUrl != null) {
                            webView.loadUrl(fallbackUrl);
                            return true;
                        }
                        //invite to install
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW)
                                .setData(Uri.parse("market://details?id=" + intent.getPackage()));
                        if (marketIntent.resolveActivity(packageManager) != null) {
                            from.startActivity(marketIntent);
                            return true;
                        }
                    } catch (URISyntaxException e) {
                        //not an intent uri
                    }
                }
                return true;//do nothing in other cases
            }
        });
    }
}