Example usage for android.webkit CookieManager setAcceptCookie

List of usage examples for android.webkit CookieManager setAcceptCookie

Introduction

In this page you can find the example usage for android.webkit CookieManager setAcceptCookie.

Prototype

public abstract void setAcceptCookie(boolean accept);

Source Link

Document

Sets whether the application's WebView instances should send and accept cookies.

Usage

From source file:com.pindroid.fragment.ViewBookmarkFragment.java

private void showInWebView(String url) {
    String readingBackground = SettingsHelper.getReadingBackground(getActivity());
    String readingFont = SettingsHelper.getReadingFont(getActivity());
    String readingFontSize = SettingsHelper.getReadingFontSize(getActivity());
    String readingMargins = SettingsHelper.getReadingMargins(getActivity());

    mWebContent.loadUrl("about:blank");
    mWebContent.clearCache(true);// www . j  a v a 2 s  . co m

    CookieManager cookieManager = CookieManager.getInstance();
    CookieSyncManager.createInstance(getActivity());

    cookieManager.setAcceptCookie(true);
    cookieManager.setCookie("http://www.instapaper.com",
            "iptcolor=" + readingBackground + "; expires=Sat, 25-Mar-2023 00:00:00 GMT;path=/;");
    cookieManager.setCookie("http://www.instapaper.com",
            "iptfont=" + readingFont + "; expires=Sat, 25-Mar-2023 00:00:00 GMT;path=/;");
    cookieManager.setCookie("http://www.instapaper.com",
            "iptsize=" + readingFontSize + "; expires=Sat, 25-Mar-2023 00:00:00 GMT;path=/;");
    cookieManager.setCookie("http://www.instapaper.com",
            "iptwidth=" + readingMargins + "; expires=Sat, 25-Mar-2023 00:00:00 GMT;path=/;");

    CookieSyncManager.getInstance().sync();

    mWebContent.setWebViewClient(new WebViewClient() {
    });

    mWebContent.loadUrl(url);
}

From source file:com.nbplus.hybrid.BasicWebViewClient.java

/**
 * ??./*from w  ww  . j  av  a  2s .c o m*/
 * @param activity : context
 * @param view : ?? 
 */
public BasicWebViewClient(Activity activity, WebView view, String alertTitleString, String confirmTitleString) {
    mWebView = view;
    mContext = activity;

    // This will handle downloading. It requires Gingerbread, though
    mDownloadManager = (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE);
    mWebChromeClient = new BroadcastWebChromeClient();

    // Enable remote debugging via chrome://inspect
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        mWebView.setWebContentsDebuggingEnabled(true);
    }
    mWebView.setWebChromeClient(mWebChromeClient);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setGeolocationEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setDatabaseEnabled(true);
    // Use WideViewport and Zoom out if there is no viewport defined
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        webSettings.setMediaPlaybackRequiresUserGesture(false);
    }

    // Enable pinch to zoom without the zoom buttons
    webSettings.setBuiltInZoomControls(true);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        // Hide the zoom controls for HONEYCOMB+
        webSettings.setDisplayZoomControls(false);
    }

    webSettings.setAppCacheEnabled(true);
    mWebView.clearCache(true);
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Sets whether the WebView should allow third party cookies to be set.
        // Allowing third party cookies is a per WebView policy and can be set differently on different WebView instances.

        // Apps that target KITKAT or below default to allowing third party cookies.
        // Apps targeting LOLLIPOP or later default to disallowing third party cookies.
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.setAcceptThirdPartyCookies(mWebView, true);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        mWebView.getSettings().setTextZoom(100);
    }

    if (StringUtils.isEmptyString(alertTitleString)) {
        mAlertTitleString = activity.getString(R.string.default_webview_alert_title);
    } else {
        mAlertTitleString = alertTitleString;
    }

    if (StringUtils.isEmptyString(confirmTitleString)) {
        mConfirmTitleString = activity.getString(R.string.default_webview_confirm_title);
    } else {
        mConfirmTitleString = confirmTitleString;
    }

    mWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
                long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            mContext.startActivity(intent);

        }
    });

    Log.d(TAG, ">> user agent = " + mWebView.getSettings().getUserAgentString());
}

From source file:mgks.os.webview.MainActivity.java

public void get_info() {
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setCookie(ASWV_URL, "DEVICE=android");
    cookieManager.setCookie(ASWV_URL, "DEV_API=" + Build.VERSION.SDK_INT);
}

From source file:mgks.os.webview.MainActivity.java

public String get_location() {
    String newloc = "0,0";
    //Checking for location permissions
    if (ASWP_LOCATION && (Build.VERSION.SDK_INT < 23 || check_permission(1))) {
        GPSTrack gps;/*from  w ww  .ja  v a 2  s. co  m*/
        gps = new GPSTrack(MainActivity.this);
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();
        if (gps.canGetLocation()) {
            if (latitude != 0 || longitude != 0) {
                if (!ASWP_OFFLINE) {
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(true);
                    cookieManager.setCookie(ASWV_URL, "lat=" + latitude);
                    cookieManager.setCookie(ASWV_URL, "long=" + longitude);
                }
                //Log.w("New Updated Location:", latitude + "," + longitude);  //enable to test dummy latitude and longitude
                newloc = latitude + "," + longitude;
            } else {
                Log.w("New Updated Location:", "NULL");
            }
        } else {
            show_notification(1, 1);
            Log.w("New Updated Location:", "FAIL");
        }
    }
    return newloc;
}

From source file:com.microsoft.aad.adal.AuthenticationActivity.java

@SuppressLint("SetJavaScriptEnabled")
@Override/*from   ww w  .j  av a  2 s .  c o m*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(
            this.getResources().getIdentifier("activity_authentication", "layout", this.getPackageName()));
    CookieSyncManager.createInstance(getApplicationContext());
    CookieSyncManager.getInstance().sync();
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);

    // Get the message from the intent
    mAuthRequest = getAuthenticationRequestFromIntent(getIntent());
    if (mAuthRequest == null) {
        Log.d(TAG, "Request item is null, so it returns to caller");
        Intent resultIntent = new Intent();
        resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_ERROR_CODE,
                AuthenticationConstants.Browser.WEBVIEW_INVALID_REQUEST);
        resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_ERROR_MESSAGE,
                "Intent does not have request details");
        returnToCaller(AuthenticationConstants.UIResponse.BROWSER_CODE_ERROR, resultIntent);
        return;
    }

    if (mAuthRequest.getAuthority() == null || mAuthRequest.getAuthority().isEmpty()) {
        returnError(ADALError.ARGUMENT_EXCEPTION, AuthenticationConstants.Broker.ACCOUNT_AUTHORITY);
        return;
    }

    if (mAuthRequest.getResource() == null || mAuthRequest.getResource().isEmpty()) {
        returnError(ADALError.ARGUMENT_EXCEPTION, AuthenticationConstants.Broker.ACCOUNT_RESOURCE);
        return;
    }

    if (mAuthRequest.getClientId() == null || mAuthRequest.getClientId().isEmpty()) {
        returnError(ADALError.ARGUMENT_EXCEPTION, AuthenticationConstants.Broker.ACCOUNT_CLIENTID_KEY);
        return;
    }

    if (mAuthRequest.getRedirectUri() == null || mAuthRequest.getRedirectUri().isEmpty()) {
        returnError(ADALError.ARGUMENT_EXCEPTION, AuthenticationConstants.Broker.ACCOUNT_REDIRECT);
        return;
    }

    mRedirectUrl = mAuthRequest.getRedirectUri();
    Logger.v(TAG, "OnCreate redirectUrl:" + mRedirectUrl);
    // Create the Web View to show the page
    mWebView = (WebView) findViewById(
            this.getResources().getIdentifier("webView1", "id", this.getPackageName()));
    Logger.v(TAG, "User agent:" + mWebView.getSettings().getUserAgentString());
    mStartUrl = "about:blank";

    try {
        Oauth2 oauth = new Oauth2(mAuthRequest);
        mStartUrl = oauth.getCodeRequestUrl();
        mQueryParameters = oauth.getAuthorizationEndpointQueryParameters();
    } catch (UnsupportedEncodingException e) {
        Log.d(TAG, e.getMessage());
        Intent resultIntent = new Intent();
        resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_REQUEST_INFO, mAuthRequest);
        returnToCaller(AuthenticationConstants.UIResponse.BROWSER_CODE_ERROR, resultIntent);
        return;
    }

    // Create the broadcast receiver for cancel
    Logger.v(TAG, "Init broadcastReceiver with requestId:" + mAuthRequest.getRequestId() + " "
            + mAuthRequest.getLogInfo());
    mReceiver = new ActivityBroadcastReceiver();
    mReceiver.mWaitingRequestId = mAuthRequest.getRequestId();
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
            new IntentFilter(AuthenticationConstants.Browser.ACTION_CANCEL));

    String userAgent = mWebView.getSettings().getUserAgentString();
    mWebView.getSettings()
            .setUserAgentString(userAgent + AuthenticationConstants.Broker.CLIENT_TLS_NOT_SUPPORTED);
    userAgent = mWebView.getSettings().getUserAgentString();
    Logger.v(TAG, "UserAgent:" + userAgent);

    if (isBrokerRequest(getIntent())) {
        // This activity is started from calling app and running in
        // Authenticator's process
        mCallingPackage = getCallingPackage();
        Logger.i(TAG, "It is a broker request for package:" + mCallingPackage, "");

        if (mCallingPackage == null) {
            Logger.v(TAG, "startActivityForResult is not used to call this activity");
            Intent resultIntent = new Intent();
            resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_ERROR_CODE,
                    AuthenticationConstants.Browser.WEBVIEW_INVALID_REQUEST);
            resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_ERROR_MESSAGE,
                    "startActivityForResult is not used to call this activity");
            returnToCaller(AuthenticationConstants.UIResponse.BROWSER_CODE_ERROR, resultIntent);
            return;
        }

        mAccountAuthenticatorResponse = getIntent()
                .getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
        if (mAccountAuthenticatorResponse != null) {
            mAccountAuthenticatorResponse.onRequestContinued();
        }
        PackageHelper info = new PackageHelper(AuthenticationActivity.this);
        mCallingPackage = getCallingPackage();
        mCallingUID = info.getUIDForPackage(mCallingPackage);
        String signatureDigest = info.getCurrentSignatureForPackage(mCallingPackage);
        mStartUrl = getBrokerStartUrl(mStartUrl, mCallingPackage, signatureDigest);

        if (!isCallerBrokerInstaller()) {
            Logger.v(TAG, "Caller needs to be verified using special redirectUri");
            mRedirectUrl = PackageHelper.getBrokerRedirectUrl(mCallingPackage, signatureDigest);
        }
        Logger.v(TAG,
                "OnCreate redirectUrl:" + mRedirectUrl + " startUrl:" + mStartUrl + " calling package:"
                        + mCallingPackage + " signatureDigest:" + signatureDigest + " current Context Package: "
                        + getPackageName());
    }
    mRegisterReceiver = false;
    final String postUrl = mStartUrl;
    Logger.i(TAG, "OnCreate startUrl:" + mStartUrl + " calling package:" + mCallingPackage, " device:"
            + android.os.Build.VERSION.RELEASE + " " + android.os.Build.MANUFACTURER + android.os.Build.MODEL);

    setupWebView(mRedirectUrl, mQueryParameters, mAuthRequest);

    if (savedInstanceState == null) {
        mWebView.post(new Runnable() {
            @Override
            public void run() {
                // load blank first to avoid error for not loading webview
                mWebView.loadUrl("about:blank");
                mWebView.loadUrl(postUrl);
            }
        });
    } else {
        Logger.v(TAG, "Reuse webview");
    }
}

From source file:de.baumann.browser.helper.helper_webView.java

@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().setAppCacheEnabled(true);
    webView.getSettings().setMixedContentMode(MIXED_CONTENT_COMPATIBILITY_MODE);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setTextZoom(fontSize);
    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);

    from.registerForContextMenu(webView);

    if (sharedPref.getString("nav", "2").equals("1") || sharedPref.getString("nav", "2").equals("3")) {
        helper_webView.webView_Touch(from, webView);
    }//from  w w  w . j a  v  a 2 s .  c o  m

    if (sharedPref.getString("cookie", "1").equals("2") || sharedPref.getString("cookie", "1").equals("3")) {
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(webView, true);
    } else {
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(webView, false);
    }

    if (sharedPref.getString("started", "").equals("yes")) {
        if (sharedPref.getString("java_string", "True").equals(from.getString(R.string.app_yes))) {
            webView.getSettings().setJavaScriptEnabled(true);
            sharedPref.edit().putString("java_string", from.getString(R.string.app_yes)).apply();
        } else {
            webView.getSettings().setJavaScriptEnabled(false);
            sharedPref.edit().putString("java_string", from.getString(R.string.app_no)).apply();
        }

        if (sharedPref.getString("pictures_string", "True").equals(from.getString(R.string.app_yes))) {
            webView.getSettings().setLoadsImagesAutomatically(true);
            sharedPref.edit().putString("pictures_string", from.getString(R.string.app_yes)).apply();
        } else {
            webView.getSettings().setLoadsImagesAutomatically(false);
            sharedPref.edit().putString("pictures_string", from.getString(R.string.app_no)).apply();
        }

        if (sharedPref.getString("loc_string", "True").equals(from.getString(R.string.app_yes))) {
            webView.getSettings().setGeolocationEnabled(true);
            helper_main.grantPermissionsLoc(from);
            sharedPref.edit().putString("loc_string", from.getString(R.string.app_yes)).apply();
        } else {
            webView.getSettings().setGeolocationEnabled(false);
            sharedPref.edit().putString("loc_string", from.getString(R.string.app_no)).apply();
        }

        if (sharedPref.getString("cookie_string", "True").equals(from.getString(R.string.app_yes))) {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            sharedPref.edit().putString("cookie_string", from.getString(R.string.app_yes)).apply();
        } else {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(false);
            sharedPref.edit().putString("cookie_string", from.getString(R.string.app_no)).apply();
        }
    } else {
        if (sharedPref.getBoolean("java", false)) {
            webView.getSettings().setJavaScriptEnabled(true);
            sharedPref.edit().putString("java_string", from.getString(R.string.app_yes)).apply();
        } else {
            webView.getSettings().setJavaScriptEnabled(false);
            sharedPref.edit().putString("java_string", from.getString(R.string.app_no)).apply();
        }

        if (sharedPref.getBoolean("pictures", false)) {
            webView.getSettings().setLoadsImagesAutomatically(true);
            sharedPref.edit().putString("pictures_string", from.getString(R.string.app_yes)).apply();
        } else {
            webView.getSettings().setLoadsImagesAutomatically(false);
            sharedPref.edit().putString("pictures_string", from.getString(R.string.app_no)).apply();
        }

        if (sharedPref.getBoolean("loc", false)) {
            webView.getSettings().setGeolocationEnabled(true);
            helper_main.grantPermissionsLoc(from);
            sharedPref.edit().putString("loc_string", from.getString(R.string.app_yes)).apply();
        } else {
            webView.getSettings().setGeolocationEnabled(false);
            sharedPref.edit().putString("loc_string", from.getString(R.string.app_no)).apply();
        }

        if (sharedPref.getString("cookie", "1").equals("1")
                || sharedPref.getString("cookie", "1").equals("3")) {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            sharedPref.edit().putString("cookie_string", from.getString(R.string.app_yes)).apply();
        } else {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(false);
            sharedPref.edit().putString("cookie_string", from.getString(R.string.app_no)).apply();
        }
    }
}

From source file:de.baumann.browser.Browser_right.java

@SuppressLint("SetJavaScriptEnabled")
@Override//from w ww.  j a v a 2s  .  c om
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_search) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.showKeyboard(Browser_right.this, editText, 3, "", getString(R.string.app_search_hint));
    }

    if (id == R.id.action_history) {
        helper_main.switchToActivity(Browser_right.this, Popup_history.class, "", false);
    }

    if (id == R.id.action_search_chooseWebsite) {
        helper_editText.editText_searchWeb(editText, Browser_right.this);
    }

    if (id == R.id.action_pass) {
        helper_main.switchToActivity(Browser_right.this, Popup_pass.class, "", false);
        sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
        sharedPref.edit().putString("pass_copy_title", mWebView.getTitle()).apply();
    }

    if (id == R.id.action_toggle) {

        sharedPref.edit().putString("started", "yes").apply();

        if (Uri.parse(mWebView.getUrl()).getHost().length() == 0) {
            domain = getString(R.string.app_domain);
        } else {
            domain = Uri.parse(mWebView.getUrl()).getHost();
        }

        final String whiteList = sharedPref.getString("whiteList", "");

        AlertDialog.Builder builder = new AlertDialog.Builder(Browser_right.this);
        View dialogView = View.inflate(Browser_right.this, R.layout.dialog_toggle, null);

        Switch sw_java = (Switch) dialogView.findViewById(R.id.switch1);
        Switch sw_pictures = (Switch) dialogView.findViewById(R.id.switch2);
        Switch sw_location = (Switch) dialogView.findViewById(R.id.switch3);
        Switch sw_cookies = (Switch) dialogView.findViewById(R.id.switch4);
        final ImageButton whiteList_js = (ImageButton) dialogView.findViewById(R.id.imageButton_js);

        if (whiteList.contains(domain)) {
            whiteList_js.setImageResource(R.drawable.check_green);
        } else {
            whiteList_js.setImageResource(R.drawable.close_red);
        }
        if (sharedPref.getString("java_string", "True").equals(getString(R.string.app_yes))) {
            sw_java.setChecked(true);
        } else {
            sw_java.setChecked(false);
        }
        if (sharedPref.getString("pictures_string", "True").equals(getString(R.string.app_yes))) {
            sw_pictures.setChecked(true);
        } else {
            sw_pictures.setChecked(false);
        }
        if (sharedPref.getString("loc_string", "True").equals(getString(R.string.app_yes))) {
            sw_location.setChecked(true);
        } else {
            sw_location.setChecked(false);
        }
        if (sharedPref.getString("cookie_string", "True").equals(getString(R.string.app_yes))) {
            sw_cookies.setChecked(true);
        } else {
            sw_cookies.setChecked(false);
        }

        whiteList_js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (whiteList.contains(domain)) {
                    whiteList_js.setImageResource(R.drawable.close_red);
                    String removed = whiteList.replaceAll(domain, "");
                    sharedPref.edit().putString("whiteList", removed).apply();
                } else {
                    whiteList_js.setImageResource(R.drawable.check_green);
                    sharedPref.edit().putString("whiteList", whiteList + " " + domain).apply();
                }
            }
        });
        sw_java.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("java_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(true);
                } else {
                    sharedPref.edit().putString("java_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(false);
                }

            }
        });
        sw_pictures.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(true);
                } else {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(false);
                }

            }
        });
        sw_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setGeolocationEnabled(true);
                    helper_main.grantPermissionsLoc(Browser_right.this);
                } else {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setGeolocationEnabled(false);
                }

            }
        });
        sw_cookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_yes)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(true);
                } else {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_no)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(false);
                }

            }
        });

        builder.setView(dialogView);
        builder.setTitle(R.string.menu_toggle_title);
        builder.setPositiveButton(R.string.toast_yes, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                mWebView.reload();
            }
        });
        builder.setNegativeButton(R.string.menu_settings, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
                sharedPref.edit().putString("lastActivity", "browser_right").apply();
                helper_main.switchToActivity(Browser_right.this, Activity_settings.class, "", true);
            }
        });

        final AlertDialog dialog = builder.create();
        // Display the custom alert dialog on interface
        dialog.show();

    }

    if (id == R.id.menu_save_screenshot) {
        screenshot();
    }

    if (id == R.id.menu_save_bookmark) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.editText_saveBookmark(editText, Browser_right.this, mWebView);
    }

    if (id == R.id.menu_save_readLater) {
        DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_right.this);
        db.open();
        if (db.isExist(mWebView.getUrl())) {
            Snackbar.make(editText, getString(R.string.toast_newTitle), Snackbar.LENGTH_LONG).show();
        } else {
            db.insert(helper_webView.getTitle(mWebView), mWebView.getUrl(), "", "", helper_main.createDate());
            Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG).show();
        }
    }

    if (id == R.id.menu_save_pass) {
        helper_editText.editText_savePass(Browser_right.this, mWebView, mWebView.getTitle(), mWebView.getUrl());
    }

    if (id == R.id.menu_createShortcut) {
        Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.setClassName(Browser_right.this, "de.baumann.browser.Browser_left");
        i.setData(Uri.parse(mWebView.getUrl()));

        Intent shortcut = new Intent();
        shortcut.putExtra("android.intent.extra.shortcut.INTENT", i);
        shortcut.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mWebView.getTitle());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                .fromContext(Browser_right.this.getApplicationContext(), R.mipmap.ic_launcher));
        shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        Browser_right.this.sendBroadcast(shortcut);
        Snackbar.make(mWebView, R.string.menu_createShortcut_success, Snackbar.LENGTH_SHORT).show();
    }

    if (id == R.id.menu_share_screenshot) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/png");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
        Uri bmpUri = Uri.fromFile(shareFile);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        startActivity(Intent.createChooser(sharingIntent, (getString(R.string.app_share_screenshot))));
    }

    if (id == R.id.menu_share_link) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
        startActivity(Intent.createChooser(sharingIntent, (getString(R.string.app_share_link))));
    }

    if (id == R.id.menu_share_link_copy) {
        String url = mWebView.getUrl();
        ClipboardManager clipboard = (ClipboardManager) Browser_right.this
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
        Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT).show();
    }

    if (id == R.id.action_downloads) {
        helper_main.switchToActivity(Browser_right.this, Popup_files.class, "", false);
    }

    if (id == R.id.action_search_go) {

        String text = editText.getText().toString();
        helper_webView.openURL(Browser_right.this, mWebView, editText);
        helper_editText.hideKeyboard(Browser_right.this, editText, 0, text,
                getString(R.string.app_search_hint));
        helper_editText.editText_EditorAction(editText, Browser_right.this, mWebView, urlBar);
        urlBar.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
    }

    if (id == R.id.action_search_onSite) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.showKeyboard(Browser_right.this, editText, 1, "", getString(R.string.app_search_hint));
        helper_editText.editText_FocusChange_searchSite(editText, Browser_right.this);
        helper_editText.editText_searchSite(editText, Browser_right.this, mWebView, urlBar);
    }

    if (id == R.id.action_search_onSite_go) {

        String text = editText.getText().toString();

        if (text.startsWith(getString(R.string.app_search))) {
            helper_editText.editText_searchSite(editText, Browser_right.this, mWebView, urlBar);
        } else {
            mWebView.findAllAsync(text);
            helper_editText.hideKeyboard(Browser_right.this, editText, 1,
                    getString(R.string.app_search) + " " + text, getString(R.string.app_search_hint_site));
        }
    }

    if (id == R.id.action_prev) {
        mWebView.findNext(false);
    }

    if (id == R.id.action_next) {
        mWebView.findNext(true);
    }

    if (id == R.id.action_cancel) {
        urlBar.setVisibility(View.VISIBLE);
        urlBar.setText(mWebView.getTitle());
        editText.setVisibility(View.GONE);
        helper_editText.editText_FocusChange(editText, Browser_right.this);
        helper_editText.editText_EditorAction(editText, Browser_right.this, mWebView, urlBar);
        helper_editText.hideKeyboard(Browser_right.this, editText, 0, mWebView.getTitle(),
                getString(R.string.app_search_hint));
    }

    if (id == R.id.action_save_bookmark) {
        helper_editText.editText_saveBookmark_save(editText, Browser_right.this, mWebView);
        urlBar.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
    }

    return super.onOptionsItemSelected(item);
}

From source file:de.baumann.browser.Browser_left.java

@SuppressLint("SetJavaScriptEnabled")
@Override//from w  w w  .j a  v  a  2 s.  co m
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_search) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.showKeyboard(Browser_left.this, editText, 3, "", getString(R.string.app_search_hint));
    }

    if (id == R.id.action_history) {
        helper_main.switchToActivity(Browser_left.this, Popup_history.class, "", false);
    }

    if (id == R.id.action_search_chooseWebsite) {
        helper_editText.editText_searchWeb(editText, Browser_left.this);
    }

    if (id == R.id.action_pass) {
        helper_main.switchToActivity(Browser_left.this, Popup_pass.class, "", false);
        sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
        sharedPref.edit().putString("pass_copy_title", mWebView.getTitle()).apply();
    }

    if (id == R.id.action_toggle) {

        sharedPref.edit().putString("started", "yes").apply();

        if (Uri.parse(mWebView.getUrl()).getHost().length() == 0) {
            domain = getString(R.string.app_domain);
        } else {
            domain = Uri.parse(mWebView.getUrl()).getHost();
        }

        final String whiteList = sharedPref.getString("whiteList", "");

        AlertDialog.Builder builder = new AlertDialog.Builder(Browser_left.this);
        View dialogView = View.inflate(Browser_left.this, R.layout.dialog_toggle, null);

        Switch sw_java = (Switch) dialogView.findViewById(R.id.switch1);
        Switch sw_pictures = (Switch) dialogView.findViewById(R.id.switch2);
        Switch sw_location = (Switch) dialogView.findViewById(R.id.switch3);
        Switch sw_cookies = (Switch) dialogView.findViewById(R.id.switch4);
        final ImageButton whiteList_js = (ImageButton) dialogView.findViewById(R.id.imageButton_js);

        if (whiteList.contains(domain)) {
            whiteList_js.setImageResource(R.drawable.check_green);
        } else {
            whiteList_js.setImageResource(R.drawable.close_red);
        }
        if (sharedPref.getString("java_string", "True").equals(getString(R.string.app_yes))) {
            sw_java.setChecked(true);
        } else {
            sw_java.setChecked(false);
        }
        if (sharedPref.getString("pictures_string", "True").equals(getString(R.string.app_yes))) {
            sw_pictures.setChecked(true);
        } else {
            sw_pictures.setChecked(false);
        }
        if (sharedPref.getString("loc_string", "True").equals(getString(R.string.app_yes))) {
            sw_location.setChecked(true);
        } else {
            sw_location.setChecked(false);
        }
        if (sharedPref.getString("cookie_string", "True").equals(getString(R.string.app_yes))) {
            sw_cookies.setChecked(true);
        } else {
            sw_cookies.setChecked(false);
        }

        whiteList_js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (whiteList.contains(domain)) {
                    whiteList_js.setImageResource(R.drawable.close_red);
                    String removed = whiteList.replaceAll(domain, "");
                    sharedPref.edit().putString("whiteList", removed).apply();
                } else {
                    whiteList_js.setImageResource(R.drawable.check_green);
                    sharedPref.edit().putString("whiteList", whiteList + " " + domain).apply();
                }
            }
        });
        sw_java.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("java_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(true);
                } else {
                    sharedPref.edit().putString("java_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(false);
                }

            }
        });
        sw_pictures.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(true);
                } else {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(false);
                }

            }
        });
        sw_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setGeolocationEnabled(true);
                    helper_main.grantPermissionsLoc(Browser_left.this);
                } else {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setGeolocationEnabled(false);
                }

            }
        });
        sw_cookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_yes)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(true);
                } else {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_no)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(false);
                }

            }
        });

        builder.setView(dialogView);
        builder.setTitle(R.string.menu_toggle_title);
        builder.setPositiveButton(R.string.toast_yes, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                mWebView.reload();
            }
        });
        builder.setNegativeButton(R.string.menu_settings, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
                sharedPref.edit().putString("lastActivity", "browser_left").apply();
                helper_main.switchToActivity(Browser_left.this, Activity_settings.class, "", true);
            }
        });

        final AlertDialog dialog = builder.create();
        // Display the custom alert dialog on interface
        dialog.show();

    }

    if (id == R.id.menu_save_screenshot) {
        screenshot();
    }

    if (id == R.id.menu_save_bookmark) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.editText_saveBookmark(editText, Browser_left.this, mWebView);
    }

    if (id == R.id.menu_save_readLater) {
        DbAdapter_ReadLater db = new DbAdapter_ReadLater(Browser_left.this);
        db.open();
        if (db.isExist(mWebView.getUrl())) {
            Snackbar.make(editText, getString(R.string.toast_newTitle), Snackbar.LENGTH_LONG).show();
        } else {
            db.insert(helper_webView.getTitle(mWebView), mWebView.getUrl(), "", "", helper_main.createDate());
            Snackbar.make(mWebView, R.string.bookmark_added, Snackbar.LENGTH_LONG).show();
        }
    }

    if (id == R.id.menu_save_pass) {
        helper_editText.editText_savePass(Browser_left.this, mWebView, mWebView.getTitle(), mWebView.getUrl());
    }

    if (id == R.id.menu_createShortcut) {
        Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.setClassName(Browser_left.this, "de.baumann.browser.Browser_left");
        i.setData(Uri.parse(mWebView.getUrl()));

        Intent shortcut = new Intent();
        shortcut.putExtra("android.intent.extra.shortcut.INTENT", i);
        shortcut.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mWebView.getTitle());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                .fromContext(Browser_left.this.getApplicationContext(), R.mipmap.ic_launcher));
        shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        Browser_left.this.sendBroadcast(shortcut);
        Snackbar.make(mWebView, R.string.menu_createShortcut_success, Snackbar.LENGTH_SHORT).show();
    }

    if (id == R.id.menu_share_screenshot) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/png");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
        Uri bmpUri = Uri.fromFile(shareFile);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        startActivity(Intent.createChooser(sharingIntent, (getString(R.string.app_share_screenshot))));
    }

    if (id == R.id.menu_share_link) {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
        startActivity(Intent.createChooser(sharingIntent, (getString(R.string.app_share_link))));
    }

    if (id == R.id.menu_share_link_copy) {
        String url = mWebView.getUrl();
        ClipboardManager clipboard = (ClipboardManager) Browser_left.this
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
        Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT).show();
    }

    if (id == R.id.action_downloads) {
        helper_main.switchToActivity(Browser_left.this, Popup_files.class, "", false);
    }

    if (id == R.id.action_search_go) {

        String text = editText.getText().toString();
        helper_webView.openURL(Browser_left.this, mWebView, editText);
        helper_editText.hideKeyboard(Browser_left.this, editText, 0, text, getString(R.string.app_search_hint));
        helper_editText.editText_EditorAction(editText, Browser_left.this, mWebView, urlBar);
        urlBar.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
    }

    if (id == R.id.action_search_onSite) {
        urlBar.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        helper_editText.showKeyboard(Browser_left.this, editText, 1, "", getString(R.string.app_search_hint));
        helper_editText.editText_FocusChange_searchSite(editText, Browser_left.this);
        helper_editText.editText_searchSite(editText, Browser_left.this, mWebView, urlBar);
    }

    if (id == R.id.action_search_onSite_go) {

        String text = editText.getText().toString();

        if (text.startsWith(getString(R.string.app_search))) {
            helper_editText.editText_searchSite(editText, Browser_left.this, mWebView, urlBar);
        } else {
            mWebView.findAllAsync(text);
            helper_editText.hideKeyboard(Browser_left.this, editText, 1,
                    getString(R.string.app_search) + " " + text, getString(R.string.app_search_hint_site));
        }
    }

    if (id == R.id.action_prev) {
        mWebView.findNext(false);
    }

    if (id == R.id.action_next) {
        mWebView.findNext(true);
    }

    if (id == R.id.action_cancel) {
        urlBar.setVisibility(View.VISIBLE);
        urlBar.setText(mWebView.getTitle());
        editText.setVisibility(View.GONE);
        helper_editText.editText_FocusChange(editText, Browser_left.this);
        helper_editText.editText_EditorAction(editText, Browser_left.this, mWebView, urlBar);
        helper_editText.hideKeyboard(Browser_left.this, editText, 0, mWebView.getTitle(),
                getString(R.string.app_search_hint));
    }

    if (id == R.id.action_save_bookmark) {
        helper_editText.editText_saveBookmark_save(editText, Browser_left.this, mWebView);
        urlBar.setVisibility(View.VISIBLE);
        editText.setVisibility(View.GONE);
    }

    return super.onOptionsItemSelected(item);
}

From source file:de.baumann.browser.Browser.java

@SuppressLint("SetJavaScriptEnabled")
@Override// w w w  .j a va  2s. c  o  m
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_search) {

        mWebView.stopLoading();
        String text = editText.getText().toString();
        String searchEngine = sharedPref.getString("searchEngine", "https://startpage.com/do/search?query=");
        String wikiLang = sharedPref.getString("wikiLang", "en");

        if (text.length() > 3) {
            subStr = text.substring(3);
        }

        if (text.equals(mWebView.getTitle()) || text.isEmpty()) {
            helper_editText.showKeyboard(Browser.this, editText, 3, "", getString(R.string.app_search_hint));

        } else {
            helper_editText.hideKeyboard(Browser.this, editText, 0, text, getString(R.string.app_search_hint));
            helper_editText.editText_EditorAction(editText, Browser.this, mWebView);

            if (text.startsWith("www")) {
                mWebView.loadUrl("http://" + text);
            } else if (text.contains("http")) {
                mWebView.loadUrl(text);
            } else if (text.contains(".w ")) {
                mWebView.loadUrl("https://" + wikiLang + ".wikipedia.org/wiki/Spezial:Suche?search=" + subStr);
            } else if (text.startsWith(".f ")) {
                mWebView.loadUrl("https://www.flickr.com/search/?advanced=1&license=2%2C3%2C4%2C5%2C6%2C9&text="
                        + subStr);
            } else if (text.startsWith(".m ")) {
                mWebView.loadUrl("https://metager.de/meta/meta.ger3?focus=web&eingabe=" + subStr);
            } else if (text.startsWith(".g ")) {
                mWebView.loadUrl("https://github.com/search?utf8=&q=" + subStr);
            } else if (text.startsWith(".s ")) {
                if (Locale.getDefault().getLanguage().contentEquals("de")) {
                    mWebView.loadUrl(
                            "https://startpage.com/do/search?query=" + subStr + "&lui=deutsch&l=deutsch");
                } else {
                    mWebView.loadUrl("https://startpage.com/do/search?query=" + subStr);
                }
            } else if (text.startsWith(".G ")) {
                if (Locale.getDefault().getLanguage().contentEquals("de")) {
                    mWebView.loadUrl("https://www.google.de/search?&q=" + subStr);
                } else {
                    mWebView.loadUrl("https://www.google.com/search?&q=" + subStr);
                }
            } else if (text.startsWith(".y ")) {
                if (Locale.getDefault().getLanguage().contentEquals("de")) {
                    mWebView.loadUrl("https://www.youtube.com/results?hl=de&gl=DE&search_query=" + subStr);
                } else {
                    mWebView.loadUrl("https://www.youtube.com/results?search_query=" + subStr);
                }
            } else if (text.startsWith(".d ")) {
                if (Locale.getDefault().getLanguage().contentEquals("de")) {
                    mWebView.loadUrl("https://duckduckgo.com/?q=" + subStr
                            + "&kl=de-de&kad=de_DE&k1=-1&kaj=m&kam=osm&kp=-1&kak=-1&kd=1&t=h_&ia=web");
                } else {
                    mWebView.loadUrl("https://duckduckgo.com/?q=" + subStr);
                }
            } else {
                if (searchEngine.contains("https://duckduckgo.com/?q=")) {
                    if (Locale.getDefault().getLanguage().contentEquals("de")) {
                        mWebView.loadUrl("https://duckduckgo.com/?q=" + text
                                + "&kl=de-de&kad=de_DE&k1=-1&kaj=m&kam=osm&kp=-1&kak=-1&kd=1&t=h_&ia=web");
                    } else {
                        mWebView.loadUrl("https://duckduckgo.com/?q=" + text);
                    }
                } else if (searchEngine.contains("https://metager.de/meta/meta.ger3?focus=web&eingabe=")) {
                    if (Locale.getDefault().getLanguage().contentEquals("de")) {
                        mWebView.loadUrl("https://metager.de/meta/meta.ger3?focus=web&eingabe=" + text);
                    } else {
                        mWebView.loadUrl("https://metager.de/meta/meta.ger3?focus=web&eingabe=" + text
                                + "&focus=web&encoding=utf8&lang=eng");
                    }
                } else if (searchEngine.contains("https://startpage.com/do/search?query=")) {
                    if (Locale.getDefault().getLanguage().contentEquals("de")) {
                        mWebView.loadUrl(
                                "https://startpage.com/do/search?query=" + text + "&lui=deutsch&l=deutsch");
                    } else {
                        mWebView.loadUrl("https://startpage.com/do/search?query=" + text);
                    }
                } else {
                    mWebView.loadUrl(searchEngine + text);
                }
            }
        }
    }

    if (id == R.id.action_history) {
        helper_main.switchToActivity(Browser.this, Popup_history.class, "", false);
    }

    if (id == R.id.action_search3) {
        helper_editText.editText_searchWeb(editText, Browser.this);
    }

    if (id == R.id.action_pass) {
        helper_main.switchToActivity(Browser.this, Popup_pass.class, "", false);
        sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
        sharedPref.edit().putString("pass_copy_title", mWebView.getTitle()).apply();
    }

    if (id == R.id.action_toggle) {

        sharedPref.edit().putString("started", "yes").apply();
        String link = mWebView.getUrl();
        int domainInt = link.indexOf("//") + 2;
        final String domain = link.substring(domainInt, link.indexOf('/', domainInt));
        final String whiteList = sharedPref.getString("whiteList", "");

        AlertDialog.Builder builder = new AlertDialog.Builder(Browser.this);
        View dialogView = View.inflate(Browser.this, R.layout.dialog_toggle, null);

        Switch sw_java = (Switch) dialogView.findViewById(R.id.switch1);
        Switch sw_pictures = (Switch) dialogView.findViewById(R.id.switch2);
        Switch sw_location = (Switch) dialogView.findViewById(R.id.switch3);
        Switch sw_cookies = (Switch) dialogView.findViewById(R.id.switch4);
        final ImageButton whiteList_js = (ImageButton) dialogView.findViewById(R.id.imageButton_js);

        if (whiteList.contains(domain)) {
            whiteList_js.setImageResource(R.drawable.check_green);
        } else {
            whiteList_js.setImageResource(R.drawable.close_red);
        }
        if (sharedPref.getString("java_string", "True").equals(getString(R.string.app_yes))) {
            sw_java.setChecked(true);
        } else {
            sw_java.setChecked(false);
        }
        if (sharedPref.getString("pictures_string", "True").equals(getString(R.string.app_yes))) {
            sw_pictures.setChecked(true);
        } else {
            sw_pictures.setChecked(false);
        }
        if (sharedPref.getString("loc_string", "True").equals(getString(R.string.app_yes))) {
            sw_location.setChecked(true);
        } else {
            sw_location.setChecked(false);
        }
        if (sharedPref.getString("cookie_string", "True").equals(getString(R.string.app_yes))) {
            sw_cookies.setChecked(true);
        } else {
            sw_cookies.setChecked(false);
        }

        whiteList_js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (whiteList.contains(domain)) {
                    whiteList_js.setImageResource(R.drawable.close_red);
                    String removed = whiteList.replaceAll(domain, "");
                    sharedPref.edit().putString("whiteList", removed).apply();
                } else {
                    whiteList_js.setImageResource(R.drawable.check_green);
                    sharedPref.edit().putString("whiteList", whiteList + " " + domain).apply();
                }
            }
        });
        sw_java.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("java_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(true);
                } else {
                    sharedPref.edit().putString("java_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setJavaScriptEnabled(false);
                }

            }
        });
        sw_pictures.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(true);
                } else {
                    sharedPref.edit().putString("pictures_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setLoadsImagesAutomatically(false);
                }

            }
        });
        sw_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_yes)).apply();
                    mWebView.getSettings().setGeolocationEnabled(true);
                    helper_main.grantPermissionsLoc(Browser.this);
                } else {
                    sharedPref.edit().putString("loc_string", getString(R.string.app_no)).apply();
                    mWebView.getSettings().setGeolocationEnabled(false);
                }

            }
        });
        sw_cookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_yes)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(true);
                } else {
                    sharedPref.edit().putString("cookie_string", getString(R.string.app_no)).apply();
                    CookieManager cookieManager = CookieManager.getInstance();
                    cookieManager.setAcceptCookie(false);
                }

            }
        });

        builder.setView(dialogView);
        builder.setTitle(R.string.menu_toggle_title);
        builder.setPositiveButton(R.string.toast_yes, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                mWebView.reload();
            }
        });
        builder.setNegativeButton(R.string.menu_settings, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                sharedPref.edit().putString("pass_copy_url", mWebView.getUrl()).apply();
                sharedPref.edit().putString("lastActivity", "browser").apply();
                helper_main.switchToActivity(Browser.this, Activity_settings.class, "", true);
            }
        });

        final AlertDialog dialog = builder.create();
        // Display the custom alert dialog on interface
        dialog.show();

    }

    if (id == R.id.action_save) {
        final CharSequence[] options = { getString(R.string.menu_save_screenshot),
                getString(R.string.menu_save_bookmark), getString(R.string.menu_save_readLater),
                getString(R.string.menu_save_pass), getString(R.string.menu_createShortcut) };
        new AlertDialog.Builder(Browser.this)
                .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                }).setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals(getString(R.string.menu_save_bookmark))) {
                            helper_editText.editText_saveBookmark(editText, Browser.this, mWebView);
                        }
                        if (options[item].equals(getString(R.string.menu_save_pass))) {
                            helper_editText.editText_savePass(Browser.this, mWebView, mWebView.getTitle(),
                                    mWebView.getUrl());
                        }
                        if (options[item].equals(getString(R.string.menu_save_readLater))) {
                            try {
                                final Database_ReadLater db = new Database_ReadLater(Browser.this);
                                db.addBookmark(mWebView.getTitle(), mWebView.getUrl());
                                db.close();
                                Snackbar.make(mWebView, R.string.readLater_added, Snackbar.LENGTH_SHORT).show();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        if (options[item].equals(getString(R.string.menu_save_screenshot))) {
                            screenshot();
                        }
                        if (options[item].equals(getString(R.string.menu_createShortcut))) {
                            Intent i = new Intent();
                            i.setAction(Intent.ACTION_VIEW);
                            i.setClassName(Browser.this, "de.baumann.browser.Browser");
                            i.setData(Uri.parse(mWebView.getUrl()));

                            Intent shortcut = new Intent();
                            shortcut.putExtra("android.intent.extra.shortcut.INTENT", i);
                            shortcut.putExtra("android.intent.extra.shortcut.NAME",
                                    "THE NAME OF SHORTCUT TO BE SHOWN");
                            shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mWebView.getTitle());
                            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                                    .fromContext(Browser.this.getApplicationContext(), R.mipmap.ic_launcher));
                            shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                            Browser.this.sendBroadcast(shortcut);
                            Snackbar.make(mWebView, R.string.menu_createShortcut_success, Snackbar.LENGTH_SHORT)
                                    .show();
                        }
                    }
                }).show();
    }

    if (id == R.id.action_share) {
        final CharSequence[] options = { getString(R.string.menu_share_screenshot),
                getString(R.string.menu_share_link), getString(R.string.menu_share_link_copy) };
        new AlertDialog.Builder(Browser.this)
                .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                }).setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals(getString(R.string.menu_share_link))) {
                            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                            sharingIntent.setType("text/plain");
                            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
                            sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
                            startActivity(
                                    Intent.createChooser(sharingIntent, (getString(R.string.app_share_link))));
                        }
                        if (options[item].equals(getString(R.string.menu_share_screenshot))) {
                            screenshot();

                            if (shareFile.exists()) {
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("image/png");
                                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mWebView.getTitle());
                                sharingIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
                                Uri bmpUri = Uri.fromFile(shareFile);
                                sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                                startActivity(Intent.createChooser(sharingIntent,
                                        (getString(R.string.app_share_screenshot))));
                            }
                        }
                        if (options[item].equals(getString(R.string.menu_share_link_copy))) {
                            String url = mWebView.getUrl();
                            ClipboardManager clipboard = (ClipboardManager) Browser.this
                                    .getSystemService(Context.CLIPBOARD_SERVICE);
                            clipboard.setPrimaryClip(ClipData.newPlainText("text", url));
                            Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT)
                                    .show();
                        }
                    }
                }).show();
    }

    if (id == R.id.action_downloads) {
        String startDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .getPath();
        helper_main.openFilePicker(Browser.this, mWebView, startDir);
    }

    if (id == R.id.action_searchSite) {
        mWebView.stopLoading();
        helper_editText.editText_FocusChange_searchSite(editText, Browser.this);
        helper_editText.editText_searchSite(editText, Browser.this, mWebView);
    }

    if (id == R.id.action_search2) {

        String text = editText.getText().toString();

        if (text.startsWith(getString(R.string.app_search))) {
            helper_editText.editText_searchSite(editText, Browser.this, mWebView);
        } else {
            mWebView.findAllAsync(text);
            helper_editText.hideKeyboard(Browser.this, editText, 1, getString(R.string.app_search) + " " + text,
                    getString(R.string.app_search_hint_site));
        }

    }

    if (id == R.id.action_prev) {
        mWebView.findNext(false);
    }

    if (id == R.id.action_next) {
        mWebView.findNext(true);
    }

    if (id == R.id.action_cancel) {
        helper_editText.editText_FocusChange(editText, Browser.this);
        helper_editText.editText_EditorAction(editText, Browser.this, mWebView);
        helper_editText.hideKeyboard(Browser.this, editText, 0, mWebView.getTitle(),
                getString(R.string.app_search_hint));
    }

    if (id == R.id.action_save_bookmark) {
        helper_editText.editText_saveBookmark_save(editText, Browser.this, mWebView);
    }

    return super.onOptionsItemSelected(item);
}