Example usage for android.webkit CookieSyncManager sync

List of usage examples for android.webkit CookieSyncManager sync

Introduction

In this page you can find the example usage for android.webkit CookieSyncManager sync.

Prototype

@Deprecated
public void sync() 

Source Link

Document

sync() forces sync manager to sync now

Usage

From source file:Main.java

private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;/* w w w .j  av  a2  s  .co m*/
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}

From source file:Main.java

public static String getCookieValue(Context context, String url, String key) {
    CookieSyncManager csm = CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    csm.sync();
    String cookieStr = cookieManager.getCookie(url);
    System.out.println("========cookied:" + cookieStr);
    String[] strs = cookieStr.split(";");
    String value = null;//  w  ww  .  j  a v  a 2s. c  om
    for (String string : strs) {
        if (string.trim().startsWith(key)) {
            value = string.substring(string.indexOf("=") + 1);
            break;
        }
    }
    return value;
}

From source file:com.appnexus.opensdk.ANJAMImplementation.java

private static void callRecordEvent(AdWebView webView, Uri uri) {
    String urlParam = uri.getQueryParameter("url");

    if ((urlParam == null) || (!urlParam.startsWith("http"))) {
        return;//from w  w  w. j  a  v  a2  s  .  c o  m
    }

    // Create a invisible webview to fire the url
    WebView recordEventWebView = new WebView(webView.getContext());
    recordEventWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Clog.d(Clog.baseLogTag, "RecordEvent completed loading: " + url);

            CookieSyncManager csm = CookieSyncManager.getInstance();
            if (csm != null)
                csm.sync();
        }
    });
    recordEventWebView.loadUrl(urlParam);
    recordEventWebView.setVisibility(View.GONE);
    webView.addView(recordEventWebView);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void clearCookies(Context context) {

    if (Build.VERSION.SDK_INT >= 21) {
        try {/* www .j  a v  a  2  s  .  c o  m*/
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } catch (Exception e) {
        }
    } else {
        try {
            CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        } catch (Exception e) {
        }
    }
}

From source file:org.catrobat.catroid.ui.WebViewActivity.java

@SuppressWarnings("deprecated")
@SuppressLint("NewApi")
public static void clearCookies(Context context) {
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
        cookieSyncMngr.startSync();//  www.  j a v  a 2 s . com
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    } else {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    }
}

From source file:com.google.android.gm.ay.java

public static void bk(final Context context) {
    final CookieSyncManager instance = CookieSyncManager.createInstance(context);
    CookieManager.getInstance().removeAllCookie();
    instance.sync();
}

From source file:com.facebook.login.WebViewLoginMethodHandler.java

void onWebDialogComplete(LoginClient.Request request, Bundle values, FacebookException error) {
    LoginClient.Result outcome;//from  w  ww. jav  a  2  s .c  om
    if (values != null) {
        // Actual e2e we got from the dialog should be used for logging.
        if (values.containsKey(ServerProtocol.DIALOG_PARAM_E2E)) {
            e2e = values.getString(ServerProtocol.DIALOG_PARAM_E2E);
        }

        try {
            AccessToken token = createAccessTokenFromWebBundle(request.getPermissions(), values,
                    AccessTokenSource.WEB_VIEW, request.getApplicationId());
            outcome = LoginClient.Result.createTokenResult(loginClient.getPendingRequest(), token);

            // Ensure any cookies set by the dialog are saved
            // This is to work around a bug where CookieManager may fail to instantiate if
            // CookieSyncManager has never been created.
            CookieSyncManager syncManager = CookieSyncManager.createInstance(loginClient.getActivity());
            syncManager.sync();
            saveCookieToken(token.getToken());
        } catch (FacebookException ex) {
            outcome = LoginClient.Result.createErrorResult(loginClient.getPendingRequest(), null,
                    ex.getMessage());
        }
    } else {
        if (error instanceof FacebookOperationCanceledException) {
            outcome = LoginClient.Result.createCancelResult(loginClient.getPendingRequest(),
                    "User canceled log in.");
        } else {
            // Something went wrong, don't log a completion event since it will skew timing
            // results.
            e2e = null;

            String errorCode = null;
            String errorMessage = error.getMessage();
            if (error instanceof FacebookServiceException) {
                FacebookRequestError requestError = ((FacebookServiceException) error).getRequestError();
                errorCode = String.format(Locale.ROOT, "%d", requestError.getErrorCode());
                errorMessage = requestError.toString();
            }
            outcome = LoginClient.Result.createErrorResult(loginClient.getPendingRequest(), null, errorMessage,
                    errorCode);
        }
    }

    if (!Utility.isNullOrEmpty(e2e)) {
        logWebLoginCompleted(e2e);
    }

    loginClient.completeAndValidate(outcome);
}

From source file:org.dmfs.oauth2.android.fragment.InteractiveGrantFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Nullable//from   ww w .j  a  v  a 2  s. com
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    if (mWebView == null) {
        // create and configure the WebView
        // Note using just getActivity would will leak the activity.
        mWebView = new WebView(getActivity().getApplicationContext());
        mWebView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setOnKeyListener(this);
        mWebView.setWebViewClient(mGrantWebViewClient);
        mWebView.loadUrl(mGrant.authorizationUrl().toASCIIString());

        // wipe cookies to enforce a new login
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else {
            CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(getActivity());
            cookieSyncMngr.startSync();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
    return mWebView;
}

From source file:com.popdeem.sdk.uikit.fragment.PDUIInstagramLoginFragment.java

@SuppressWarnings("deprecation")
private void clearCookies() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else {//  ww w  .j a  v a2  s . com
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(getActivity());
        cookieSyncManager.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncManager.stopSync();
        cookieSyncManager.sync();
    }
}

From source file:com.microsoft.services.msa.LiveAuthClient.java

/**
 * Logs out the given user.//ww  w.j  a v  a 2  s . c o  m
 *
 * Also, this method clears the previously created {@link LiveConnectSession}.
 * {@link LiveAuthListener#onAuthComplete(LiveStatus, LiveConnectSession, Object)} will be
 * called on completion. Otherwise,
 * {@link LiveAuthListener#onAuthError(LiveAuthException, Object)} will be called.
 *
 * @param userState arbitrary object that is used to determine the caller of the method.
 * @param listener called on either completion or error during the logout process.
 */
public void logout(Object userState, LiveAuthListener listener) {
    if (listener == null) {
        listener = NULL_LISTENER;
    }

    session.setAccessToken(null);
    session.setAuthenticationToken(null);
    session.setRefreshToken(null);
    session.setScopes(null);
    session.setTokenType(null);

    clearRefreshTokenFromPreferences();

    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this.applicationContext);
    CookieManager manager = CookieManager.getInstance();

    // clear cookies to force prompt on login
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        manager.removeAllCookies(null);
    else
        manager.removeAllCookie();

    cookieSyncManager.sync();
    listener.onAuthComplete(LiveStatus.UNKNOWN, null, userState);
}