Search in sources :

Example 56 with WebSettings

use of android.webkit.WebSettings in project habpanelviewer by vbier.

the class ClientWebView method initialize.

synchronized void initialize(final IConnectionListener cl) {
    setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            if (consoleMessage.message().contains("SSE error, closing EventSource")) {
                cl.disconnected();
            }
            return super.onConsoleMessage(consoleMessage);
        }
    });
    setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (isHabPanelUrl(url)) {
                evaluateJavascript("angular.element(document.body).scope().$root.kioskMode", s -> {
                    mKioskMode = Boolean.parseBoolean(s);
                    Log.d(TAG, "HABPanel page loaded. kioskMode=" + mKioskMode);
                });
            }
        }

        @Override
        public void onReceivedSslError(WebView view, final SslErrorHandler handler, final SslError error) {
            Log.d(TAG, "onReceivedSslError: " + error.getUrl());
            SslCertificate cert = error.getCertificate();
            if (ConnectionUtil.isTrusted(error.getCertificate())) {
                Log.d(TAG, "certificate is trusted: " + error.getUrl());
                handler.proceed();
                return;
            }
            String h;
            try {
                URL url = new URL(error.getUrl());
                h = url.getHost();
            } catch (MalformedURLException e) {
                h = getContext().getString(R.string.unknownHost);
            }
            final String host = h;
            String r = getContext().getString(R.string.notValid);
            switch(error.getPrimaryError()) {
                case SslError.SSL_DATE_INVALID:
                    r = getContext().getString(R.string.invalidDate);
                    break;
                case SslError.SSL_EXPIRED:
                    r = getContext().getString(R.string.expired);
                    break;
                case SslError.SSL_IDMISMATCH:
                    r = getContext().getString(R.string.hostnameMismatch);
                    break;
                case SslError.SSL_NOTYETVALID:
                    r = getContext().getString(R.string.notYetValid);
                    break;
                case SslError.SSL_UNTRUSTED:
                    r = getContext().getString(R.string.untrusted);
                    break;
            }
            final String reason = r;
            String c = getContext().getString(R.string.issuedBy) + cert.getIssuedBy().getDName() + "<br/>";
            c += getContext().getString(R.string.issuedTo) + cert.getIssuedTo().getDName() + "<br/>";
            c += getContext().getString(R.string.validFrom) + SimpleDateFormat.getDateInstance().format(cert.getValidNotBeforeDate()) + "<br/>";
            c += getContext().getString(R.string.validUntil) + SimpleDateFormat.getDateInstance().format(cert.getValidNotAfterDate()) + "<br/>";
            final String certInfo = c;
            new AlertDialog.Builder(ClientWebView.this.getContext()).setTitle(getContext().getString(R.string.certInvalid)).setMessage(getContext().getString(R.string.sslCert) + "https://" + host + " " + reason + ".\n\n" + certInfo.replaceAll("<br/>", "\n") + "\n" + getContext().getString(R.string.storeSecurityException)).setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton(android.R.string.yes, (dialog, whichButton) -> {
                try {
                    ConnectionUtil.addCertificate(error.getCertificate());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                handler.proceed();
            }).setNegativeButton(android.R.string.no, (dialog, whichButton) -> loadData("<html><body><h1>" + getContext().getString(R.string.certInvalid) + "</h1><h2>" + getContext().getString(R.string.sslCert) + "https://" + host + " " + reason + ".</h2>" + certInfo + "</body></html>", "text/html", "UTF-8")).show();
        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host, final String realm) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(getContext()).setCancelable(false).setTitle(R.string.login_required).setMessage(getContext().getString(R.string.host_realm, host, realm)).setView(R.layout.dialog_login).setPositiveButton(R.string.okay, (dialog12, id) -> {
                EditText userT = ((AlertDialog) dialog12).findViewById(R.id.username);
                EditText passT = ((AlertDialog) dialog12).findViewById(R.id.password);
                handler.proceed(userT.getText().toString(), passT.getText().toString());
            }).setNegativeButton(R.string.cancel, (dialog1, which) -> handler.cancel());
            final AlertDialog alert = dialog.create();
            alert.show();
        }
    });
    setOnTouchListener((v, event) -> (event.getAction() == MotionEvent.ACTION_MOVE && mDraggingPrevented));
    CookieManager.getInstance().setAcceptCookie(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CookieManager.getInstance().setAcceptThirdPartyCookies(this, true);
    }
    WebSettings webSettings = getSettings();
    webSettings.setDomStorageEnabled(true);
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    getContext().registerReceiver(mNetworkReceiver, intentFilter);
}
Also used : Context(android.content.Context) WebViewDatabase(android.webkit.WebViewDatabase) ConsoleMessage(android.webkit.ConsoleMessage) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IConnectionListener(de.vier_bier.habpanelviewer.openhab.IConnectionListener) SimpleDateFormat(java.text.SimpleDateFormat) Intent(android.content.Intent) SslErrorHandler(android.webkit.SslErrorHandler) AttributeSet(android.util.AttributeSet) WebSettings(android.webkit.WebSettings) MotionEvent(android.view.MotionEvent) CookieManager(android.webkit.CookieManager) WebViewClient(android.webkit.WebViewClient) View(android.view.View) URI(java.net.URI) Build(android.os.Build) WebView(android.webkit.WebView) TargetApi(android.annotation.TargetApi) Log(android.util.Log) WebChromeClient(android.webkit.WebChromeClient) HttpAuthHandler(android.webkit.HttpAuthHandler) ConnectivityManager(android.net.ConnectivityManager) SslError(android.net.http.SslError) MalformedURLException(java.net.MalformedURLException) IntentFilter(android.content.IntentFilter) NetworkInfo(android.net.NetworkInfo) InputType(android.text.InputType) BroadcastReceiver(android.content.BroadcastReceiver) ConnectionUtil(de.vier_bier.habpanelviewer.ssl.ConnectionUtil) AlertDialog(android.support.v7.app.AlertDialog) SharedPreferences(android.content.SharedPreferences) SslCertificate(android.net.http.SslCertificate) EditText(android.widget.EditText) AlertDialog(android.support.v7.app.AlertDialog) EditText(android.widget.EditText) SslErrorHandler(android.webkit.SslErrorHandler) IntentFilter(android.content.IntentFilter) MalformedURLException(java.net.MalformedURLException) SslError(android.net.http.SslError) ConsoleMessage(android.webkit.ConsoleMessage) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) SslCertificate(android.net.http.SslCertificate) HttpAuthHandler(android.webkit.HttpAuthHandler) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 57 with WebSettings

use of android.webkit.WebSettings in project habpanelviewer by vbier.

the class ClientWebView method updateFromPreferences.

void updateFromPreferences(SharedPreferences prefs) {
    Boolean isDesktop = prefs.getBoolean("pref_desktop_mode", false);
    Boolean isJavascript = prefs.getBoolean("pref_javascript", false);
    mDraggingPrevented = prefs.getBoolean("pref_prevent_dragging", false);
    WebSettings webSettings = getSettings();
    webSettings.setUseWideViewPort(isDesktop);
    webSettings.setLoadWithOverviewMode(isDesktop);
    webSettings.setJavaScriptEnabled(isJavascript);
    ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm == null ? null : cm.getActiveNetworkInfo();
    boolean loadStartUrl = false;
    boolean reloadUrl = false;
    if (mStartPage == null || !mStartPage.equalsIgnoreCase(prefs.getString("pref_start_url", ""))) {
        mStartPage = prefs.getString("pref_start_url", "");
        loadStartUrl = true;
    }
    if (mServerURL == null || !mServerURL.equalsIgnoreCase(prefs.getString("pref_server_url", "!$%"))) {
        mServerURL = prefs.getString("pref_server_url", "");
        loadStartUrl = mStartPage == null || mStartPage.isEmpty();
    }
    if (mAllowMixedContent != prefs.getBoolean("pref_allow_mixed_content", false)) {
        mAllowMixedContent = prefs.getBoolean("pref_allow_mixed_content", false);
        reloadUrl = true;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webSettings.setMixedContentMode(mAllowMixedContent ? WebSettings.MIXED_CONTENT_ALWAYS_ALLOW : WebSettings.MIXED_CONTENT_NEVER_ALLOW);
    }
    if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
        if (loadStartUrl) {
            loadStartUrl();
        } else if (reloadUrl) {
            reload();
        }
    } else {
        loadData("<html><body><h1>" + getContext().getString(R.string.waitingNetwork) + "</h1><h2>" + getContext().getString(R.string.notConnected) + "</h2></body></html>", "text/html", "UTF-8");
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) WebSettings(android.webkit.WebSettings) ConnectivityManager(android.net.ConnectivityManager)

Example 58 with WebSettings

use of android.webkit.WebSettings in project AmazMod by edotassi.

the class StravaAuthActivity method onCreate.

protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    WebSettings webSettings = this.mWebView.getSettings();
    String string = webSettings.getUserAgentString();
    Log.d((String) "Strava-AuthActivity", (String) ("UserAgent before : " + string), (Object[]) new Object[0]);
    if (string.contains(" wv")) {
        webSettings.setUserAgentString(string.replace(" wv", ""));
    } else {
        webSettings.setUserAgentString(string.replace("Version/", "xxx/"));
    }
    Log.d((String) "Strava-AuthActivity", (String) ("UserAgent after : " + string), (Object[]) new Object[0]);
    this.mWebView.setWebViewClient((WebViewClient) new a(this));
}
Also used : WebSettings(android.webkit.WebSettings)

Example 59 with WebSettings

use of android.webkit.WebSettings in project connect-android-sdk by telenordigital.

the class WebViewHelper method setupWebView.

// 1. HtmlToAndroidInstructionsInterface has no public fields.
// 2. We need JS for the web page.
@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setupWebView(final WebView webView, ConnectWebViewClient client, final String pageToLoad) {
    webView.setWebViewClient(client);
    webView.setVerticalScrollBarEnabled(true);
    webView.setHorizontalScrollBarEnabled(false);
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setSaveFormData(false);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    }
    webView.addJavascriptInterface(new HtmlToAndroidInstructionsInterface(client), "AndroidInterface");
    webView.setFocusable(true);
    webView.setFocusableInTouchMode(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
    acceptAllCookies(webView);
    webView.loadUrl(pageToLoad);
    webView.postDelayed(new RepeatingDelayedPageReloader(webView, pageToLoad), WEB_VIEW_TIMEOUT);
}
Also used : WebSettings(android.webkit.WebSettings) HtmlToAndroidInstructionsInterface(com.telenor.connect.sms.HtmlToAndroidInstructionsInterface) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Example 60 with WebSettings

use of android.webkit.WebSettings in project Stringlate by LonamiWebs.

the class BrowserActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_online_help);
    webview = findViewById(R.id.webview);
    WebSettings webSettings = webview.getSettings();
    webSettings.setAllowFileAccess(false);
    webSettings.setUserAgentString("Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
    webview.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    Intent intent = getIntent();
    if (isShowingStringlateHelp = intent.getBooleanExtra(EXTRA_DO_SHOW_STRINGLATE_HELP, false)) {
        webSettings.setAllowFileAccess(true);
        String locale = Locale.getDefault().getLanguage();
        if (!helpAvailableForLocale(locale))
            locale = ONLINE_HELP_DEFAULT_LOCALE;
        webview.loadUrl(String.format("file:///android_res/raw/%s.html", locale));
    }
    if (!TextUtils.isEmpty(intent.getStringExtra(EXTRA_LOAD_URL))) {
        webview.loadUrl(intent.getStringExtra(EXTRA_LOAD_URL));
    }
}
Also used : WebSettings(android.webkit.WebSettings) Intent(android.content.Intent) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Aggregations

WebSettings (android.webkit.WebSettings)213 WebView (android.webkit.WebView)110 WebViewClient (android.webkit.WebViewClient)77 SuppressLint (android.annotation.SuppressLint)52 WebChromeClient (android.webkit.WebChromeClient)49 View (android.view.View)38 Intent (android.content.Intent)32 Bitmap (android.graphics.Bitmap)23 WebResourceRequest (android.webkit.WebResourceRequest)15 KeyEvent (android.view.KeyEvent)11 JsResult (android.webkit.JsResult)11 LinearLayout (android.widget.LinearLayout)11 WebResourceError (android.webkit.WebResourceError)10 CookieManager (android.webkit.CookieManager)9 TextView (android.widget.TextView)9 SslError (android.net.http.SslError)8 SslErrorHandler (android.webkit.SslErrorHandler)8 WebResourceResponse (android.webkit.WebResourceResponse)8 ProgressDialog (android.app.ProgressDialog)7 Toolbar (android.support.v7.widget.Toolbar)7