Example usage for android.webkit WebStorage getInstance

List of usage examples for android.webkit WebStorage getInstance

Introduction

In this page you can find the example usage for android.webkit WebStorage getInstance.

Prototype

public static WebStorage getInstance() 

Source Link

Document

Gets the singleton instance of this class.

Usage

From source file:com.stoutner.privacybrowser.MainWebViewActivity.java

@Override
// Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled.
@SuppressLint("SetJavaScriptEnabled")
// removeAllCookies is deprecated, but it is required for API < 21.
@SuppressWarnings("deprecation")
public boolean onOptionsItemSelected(MenuItem menuItem) {
    int menuItemId = menuItem.getItemId();

    // Set the commands that relate to the menu entries.
    switch (menuItemId) {
    case R.id.toggleJavaScript:
        // Switch the status of javaScriptEnabled.
        javaScriptEnabled = !javaScriptEnabled;

        // Apply the new JavaScript status.
        mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);

        // Update the privacy icon.
        updatePrivacyIcon();//from   ww  w .j  a  v a  2 s . c o m

        // Display a Snackbar.
        if (javaScriptEnabled) {
            Snackbar.make(findViewById(R.id.mainWebView), R.string.javascript_enabled, Snackbar.LENGTH_SHORT)
                    .show();
        } else {
            if (domStorageEnabled || firstPartyCookiesEnabled) {
                Snackbar.make(findViewById(R.id.mainWebView), R.string.javascript_disabled,
                        Snackbar.LENGTH_SHORT).show();
            } else {
                Snackbar.make(findViewById(R.id.mainWebView), R.string.privacy_mode, Snackbar.LENGTH_SHORT)
                        .show();
            }
        }

        // Reload the WebView.
        mainWebView.reload();
        return true;

    case R.id.toggleFirstPartyCookies:
        // Switch the status of firstPartyCookiesEnabled.
        firstPartyCookiesEnabled = !firstPartyCookiesEnabled;

        // Update the menu checkbox.
        menuItem.setChecked(firstPartyCookiesEnabled);

        // Apply the new cookie status.
        cookieManager.setAcceptCookie(firstPartyCookiesEnabled);

        // Update the privacy icon.
        updatePrivacyIcon();

        // Reload the WebView.
        mainWebView.reload();
        return true;

    case R.id.toggleThirdPartyCookies:
        if (Build.VERSION.SDK_INT >= 21) {
            // Switch the status of thirdPartyCookiesEnabled.
            thirdPartyCookiesEnabled = !thirdPartyCookiesEnabled;

            // Update the menu checkbox.
            menuItem.setChecked(thirdPartyCookiesEnabled);

            // Apply the new cookie status.
            cookieManager.setAcceptThirdPartyCookies(mainWebView, thirdPartyCookiesEnabled);

            // Reload the WebView.
            mainWebView.reload();
        } // Else do nothing because SDK < 21.
        return true;

    case R.id.toggleDomStorage:
        // Switch the status of domStorageEnabled.
        domStorageEnabled = !domStorageEnabled;

        // Update the menu checkbox.
        menuItem.setChecked(domStorageEnabled);

        // Apply the new DOM Storage status.
        mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);

        // Update the privacy icon.
        updatePrivacyIcon();

        // Reload the WebView.
        mainWebView.reload();
        return true;

    case R.id.clearCookies:
        if (Build.VERSION.SDK_INT < 21) {
            cookieManager.removeAllCookie();
        } else {
            cookieManager.removeAllCookies(null);
        }
        Snackbar.make(findViewById(R.id.mainWebView), R.string.cookies_deleted, Snackbar.LENGTH_SHORT).show();
        return true;

    case R.id.clearDomStorage:
        WebStorage webStorage = WebStorage.getInstance();
        webStorage.deleteAllData();
        Snackbar.make(findViewById(R.id.mainWebView), R.string.dom_storage_deleted, Snackbar.LENGTH_SHORT)
                .show();
        return true;

    case R.id.share:
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
        shareIntent.setType("text/plain");
        startActivity(Intent.createChooser(shareIntent, "Share URL"));
        return true;

    case R.id.addToHomescreen:
        // Show the CreateHomeScreenShortcut AlertDialog and name this instance createShortcut.
        AppCompatDialogFragment shortcutDialog = new CreateHomeScreenShortcut();
        shortcutDialog.show(getSupportFragmentManager(), "createShortcut");

        //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
        return true;

    case R.id.refresh:
        mainWebView.reload();
        return true;

    default:
        // Don't consume the event.
        return super.onOptionsItemSelected(menuItem);
    }
}

From source file:com.stoutner.privacybrowser.MainWebViewActivity.java

@Override
// removeAllCookies is deprecated, but it is required for API < 21.
@SuppressWarnings("deprecation")
public boolean onNavigationItemSelected(MenuItem menuItem) {
    int menuItemId = menuItem.getItemId();

    switch (menuItemId) {
    case R.id.home:
        mainWebView.loadUrl(homepage);//from ww w.  ja v  a  2s  .  c  om
        break;

    case R.id.back:
        if (mainWebView.canGoBack()) {
            mainWebView.goBack();
        }
        break;

    case R.id.forward:
        if (mainWebView.canGoForward()) {
            mainWebView.goForward();
        }
        break;

    case R.id.downloads:
        // Launch the system Download Manager.
        Intent downloadManagerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);

        // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
        downloadManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(downloadManagerIntent);
        break;

    case R.id.settings:
        // Launch SettingsActivity.
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivity(settingsIntent);
        break;

    case R.id.about:
        // Launch AboutActivity.
        Intent aboutIntent = new Intent(this, AboutActivity.class);
        startActivity(aboutIntent);
        break;

    case R.id.clearAndExit:
        // Clear DOM storage.
        WebStorage domStorage = WebStorage.getInstance();
        domStorage.deleteAllData();

        // Clear cookies.  The commands changed slightly in API 21.
        if (Build.VERSION.SDK_INT >= 21) {
            cookieManager.removeAllCookies(null);
        } else {
            cookieManager.removeAllCookie();
        }

        // Clear cache.  The argument of "true" includes disk files.
        mainWebView.clearCache(true);

        // Clear the back/forward history.
        mainWebView.clearHistory();

        // Destroy the internal state of the webview.
        mainWebView.destroy();

        // Close Privacy Browser.  finishAndRemoveTask also removes Privacy Browser from the recent app list.
        if (Build.VERSION.SDK_INT >= 21) {
            finishAndRemoveTask();
        } else {
            finish();
        }
        break;

    default:
        break;
    }

    // Close the navigation drawer.
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

From source file:com.dish.browser.activity.BrowserActivity.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public void clearCookies() {
    // TODO Break out web storage deletion into its own option/action
    // TODO clear web storage for all sites that are visited in Incognito mode
    WebStorage storage = WebStorage.getInstance();
    storage.deleteAllData();//from www. j  a  v a  2 s.  co m
    CookieManager c = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        c.removeAllCookies(null);
    } else {
        CookieSyncManager.createInstance(this);
        c.removeAllCookie();
    }
}