Example usage for android.webkit CookieSyncManager createInstance

List of usage examples for android.webkit CookieSyncManager createInstance

Introduction

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

Prototype

public static CookieSyncManager createInstance(Context context) 

Source Link

Document

Create a singleton CookieSyncManager within a context

Usage

From source file:ti.modules.titanium.network.NetworkModule.java

/**
 * Gets all the cookies with the domain, path and name matched with the given values. If name is null, gets all the cookies with
 * the domain and path matched./*from w  w  w  . ja  v a2  s .com*/
 * @param domain the domain of the cookie to get. It is case-insensitive.
 * @param path the path of the cookie to get. It is case-sensitive.
 * @param name the name of the cookie to get. It is case-sensitive.
 * @return an array of cookies only with name and value specified. If name is null, returns all the cookies with the domain and path matched.
 */
@Kroll.method
public CookieProxy[] getSystemCookies(String domain, String path, String name) {
    if (domain == null || domain.length() == 0) {
        if (Log.isDebugModeEnabled()) {
            Log.e(TAG, "Unable to get the HTTP cookies. Need to provide a valid domain.");
        }
        return null;
    }
    if (path == null || path.length() == 0) {
        path = "/";
    }

    ArrayList<CookieProxy> cookieList = new ArrayList<CookieProxy>();
    CookieSyncManager.createInstance(TiApplication.getInstance().getRootOrCurrentActivity());
    CookieManager cookieManager = CookieManager.getInstance();
    String url = domain.toLowerCase() + path;
    String cookieString = cookieManager.getCookie(url); // The cookieString is in the format of NAME=VALUE[;
    // NAME=VALUE]
    if (cookieString != null) {
        String[] cookieValues = cookieString.split("; ");
        for (int i = 0; i < cookieValues.length; i++) {
            String[] pair = cookieValues[i].split("=", 2);
            String cookieName = pair[0];
            String value = pair.length == 2 ? pair[1] : null;
            if (name == null || cookieName.equals(name)) {
                cookieList.add(new CookieProxy(cookieName, value, null, null));
            }
        }
    }
    if (!cookieList.isEmpty()) {
        return cookieList.toArray(new CookieProxy[cookieList.size()]);
    }
    return null;
}

From source file:ti.modules.titanium.network.NetworkModule.java

/**
 * Removes the cookie with the domain, path and name exactly the same as the given values.
 * @param domain the domain of the cookie to remove. It is case-insensitive.
 * @param path the path of the cookie to remove. It is case-sensitive.
 * @param name the name of the cookie to remove. It is case-sensitive.
 *//*from   w w  w .  ja  v a 2 s. com*/
@Kroll.method
public void removeSystemCookie(String domain, String path, String name) {
    if (domain == null || name == null) {
        if (Log.isDebugModeEnabled()) {
            Log.e(TAG, "Unable to remove the system cookie. Need to provide a valid domain / name.");
        }
        return;
    }
    String lower_domain = domain.toLowerCase();
    String cookieString = name + "=; domain=" + lower_domain + "; path=" + path + "; expires="
            + CookieProxy.systemExpiryDateFormatter.format(new Date(0));
    CookieSyncManager.createInstance(TiApplication.getInstance().getRootOrCurrentActivity());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setCookie(lower_domain, cookieString);
    CookieSyncManager.getInstance().sync();
}

From source file:com.suning.mobile.ebuy.lottery.network.util.Caller.java

/**
 * webviewcookie//  w  w w  .  j  a v  a 2s  .  com
 */
public void syncCookie() {
    List<Cookie> cookies = mClient.getCookieStore().getCookies();
    if (!cookies.isEmpty()) {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mApplication);
        CookieManager cookieManager = CookieManager.getInstance();
        for (Cookie cookie : cookies) {
            String domain = cookie.getDomain();
            String strCookies = cookie.getName() + "=" + cookie.getValue() + "; domain=" + domain;
            cookieManager.setCookie(domain, strCookies);
            cookieSyncManager.sync();
        }
    }
}

From source file:ti.modules.titanium.network.NetworkModule.java

/**
 * Removes all the cookies in the system cookie store.
 *///from w w w .  ja va  2  s.  c  om
@Kroll.method
public void removeAllSystemCookies() {
    CookieSyncManager.createInstance(TiApplication.getInstance().getRootOrCurrentActivity());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    CookieSyncManager.getInstance().sync();
}

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

/**
 * Logs out the given user./*from   w  w  w.j a v a 2 s .com*/
 *
 * 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);
}

From source file:com.zake.Weibo.net.Utility.java

/**
 * Clear current context cookies ./*from  w w w  .  j a  v a2s  . c o m*/
 * 
 * @param context
 *            : current activity context.
 * 
 * @return void
 */
public static void clearCookies(Context context) {
    @SuppressWarnings("unused")
    CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();

}

From source file:com.wanikani.androidnotifier.WebReviewActivity.java

/**
 * Called when the action is initially displayed. It initializes the objects
 * and starts loading the review page.//from w w  w .j a  v  a  2s  .  c  om
 *    @param bundle the saved bundle
 */
@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    Resources res;

    CookieSyncManager.createInstance(this);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    mh = new MenuHandler(this, new MenuListener());

    if (SettingsActivity.getFullscreen(this)) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    setContentView(R.layout.web_review);

    res = getResources();

    selectedColor = res.getColor(R.color.selected);
    unselectedColor = res.getColor(R.color.unselected);

    muteDrawable = res.getDrawable(R.drawable.ic_mute);
    notMutedDrawable = res.getDrawable(R.drawable.ic_not_muted);

    kbstatus = KeyboardStatus.INVISIBLE;

    bar = (ProgressBar) findViewById(R.id.pb_reviews);
    dbar = (ProgressBar) findViewById(R.id.pb_download);

    ignbtn = (ImageButton) findViewById(R.id.btn_ignore);
    ignbtn.setOnClickListener(new IgnoreButtonListener());

    /* First of all get references to views we'll need in the near future */
    splashView = findViewById(R.id.wv_splash);
    contentView = findViewById(R.id.wv_content);
    msgw = (TextView) findViewById(R.id.tv_message);
    wv = (FocusWebView) findViewById(R.id.wv_reviews);

    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wv.getSettings().setSupportMultipleWindows(false);
    wv.getSettings().setUseWideViewPort(false);
    wv.getSettings().setDatabaseEnabled(true);
    wv.getSettings().setDomStorageEnabled(true);
    wv.getSettings().setDatabasePath(getFilesDir().getPath() + "/wv");
    wv.addJavascriptInterface(new WKNKeyboard(), "wknKeyboard");
    wv.setScrollBarStyle(ScrollView.SCROLLBARS_OUTSIDE_OVERLAY);
    wv.setWebViewClient(new WebViewClientImpl());
    wv.setWebChromeClient(new WebChromeClientImpl());

    download = getIntent().getAction().equals(DOWNLOAD_ACTION);
    if (download) {
        downloadPrefix = getIntent().getStringExtra(EXTRA_DOWNLOAD_PREFIX);
        wv.setDownloadListener(fda = new FileDownloader());
    }

    wv.loadUrl(getIntent().getData().toString());

    nativeKeyboard = new NativeKeyboard(this, wv);
    localIMEKeyboard = new LocalIMEKeyboard(this, wv);

    muteH = (ImageButton) findViewById(R.id.kb_mute_h);
    muteH.setOnClickListener(new MuteListener());

    singleb = (Button) findViewById(R.id.kb_single);
    singleb.setOnClickListener(new SingleListener());

    if (SettingsActivity.getTimerReaper(this)) {
        reaper = new TimerThreadsReaper();
        rtask = reaper.createTask(new Handler(), 2, 7000);
        rtask.setListener(new ReaperTaskListener());
    }
}

From source file:com.example.office.ui.Office365DemoActivity.java

/**
 * Clears cookies used for AADAL authentication.
 *//*from   ww w . j  av a2s .c  om*/
private void clearCookies() {
    CookieSyncManager.createInstance(getApplicationContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    CookieSyncManager.getInstance().sync();
}

From source file:com.dongfang.dicos.sina.UtilSina.java

/**
 * Clear current context cookies ./*from   www . j  a  v a 2s .  c o m*/
 * 
 * @param context
 *            : current activity context.
 * 
 * @return void
 */
public static void clearCookies(Context context) {
    @SuppressWarnings("unused")
    CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
}

From source file:illab.nabal.NabalSimpleDemoActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nabal_simple_demo);

    CookieSyncManager.createInstance(NabalSimpleDemoActivity.this);

    mBackground = (RelativeLayout) findViewById(R.id.relativeLayout1);

    mImgBtn1 = (ImageButton) findViewById(R.id.imageButton1);
    mImgBtn2 = (ImageButton) findViewById(R.id.imageButton2);
    mImgBtn3 = (ImageButton) findViewById(R.id.imageButton3);
    mImgBtn4 = (ImageButton) findViewById(R.id.imageButton4);
    mImgBtn5 = (ImageButton) findViewById(R.id.imageButton5);
    mImgBtn7 = (ImageButton) findViewById(R.id.imageButton7);
    mImgBtn8 = (ImageButton) findViewById(R.id.imageButton8);
    mEditText1 = (EditText) findViewById(R.id.editText1);
    mEditText2 = (EditText) findViewById(R.id.editText2);
    mEditText3 = (EditText) findViewById(R.id.editText3);

    mDialogHelper.toast("Please log in first.");

    mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    mInputMethodManager.hideSoftInputFromWindow(mEditText1.getWindowToken(), 0);
    mInputMethodManager.hideSoftInputFromWindow(mEditText2.getWindowToken(), 0);

    // set a listener to background for a fling
    mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override//from  www.ja  v a 2  s. c o  m
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                    return false;
                }
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentSnsId == SocialNetwork.FACEBOOK) {
                        toggleSnsButtons(SocialNetwork.WEIBO);
                    } else if (mCurrentSnsId == SocialNetwork.TWITTER) {
                        toggleSnsButtons(SocialNetwork.FACEBOOK);
                    } else if (mCurrentSnsId == SocialNetwork.WEIBO) {
                        toggleSnsButtons(SocialNetwork.TWITTER);
                    }
                }
                // left to right swipe
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentSnsId == SocialNetwork.FACEBOOK) {
                        toggleSnsButtons(SocialNetwork.TWITTER);
                    } else if (mCurrentSnsId == SocialNetwork.TWITTER) {
                        toggleSnsButtons(SocialNetwork.WEIBO);
                    } else if (mCurrentSnsId == SocialNetwork.WEIBO) {
                        toggleSnsButtons(SocialNetwork.FACEBOOK);
                    }
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    });
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return mGestureDetector.onTouchEvent(event);
        }
    };
    mBackground.setOnTouchListener(gestureListener);

    // set a listener to background for a long-click
    mBackground.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Log.i(TAG, "#### background area long-clicked");
            if (mCurrentSnsId == SocialNetwork.FACEBOOK) {
                if (mFbBgBitmap != null) {
                    new PhotoDialog(NabalSimpleDemoActivity.this, mFbBgBitmap).show();
                }
            } else if (mCurrentSnsId == SocialNetwork.TWITTER) {
                if (mTwBgBitmap != null) {
                    new PhotoDialog(NabalSimpleDemoActivity.this, mTwBgBitmap).show();
                }
            } else if (mCurrentSnsId == SocialNetwork.WEIBO) {
                if (mWeBgBitmap != null) {
                    new PhotoDialog(NabalSimpleDemoActivity.this, mWeBgBitmap).show();
                }
            }
            return false;
        }
    });

    // set a listener to Facebook button
    mImgBtn3.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### Facebook button clicked");
            toggleSnsButtons(SocialNetwork.FACEBOOK);
        }
    });

    // set a listener to Twitter button
    mImgBtn4.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### Twitter button clicked");
            toggleSnsButtons(SocialNetwork.TWITTER);
        }
    });

    // set a listener to Weibo button
    mImgBtn5.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### Weibo button clicked");
            toggleSnsButtons(SocialNetwork.WEIBO);
        }
    });

    // set a listener to portrait button
    mImgBtn1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### portrait button clicked");
            clearOutProfileUi();
            clearProfileBufferData(mCurrentSnsId);
            mHasErrorOccurredWhileFetchingProfile = false;
            mImgBtn1.setVisibility(View.GONE);
            requestMyProfile();
            requestMyLastStatus();
        }
    });
    mImgBtn1.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View paramView) {
            //Log.i(TAG, "#### portrait button long-clicked");
            confirmJumpToSnsProfile();
            return true;
        }
    });

    // set a listener to photo attach button
    mImgBtn7.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### photo attach button clicked");

            // if not logged-in yet
            if (mOpAgent.isSessionValid(mCurrentSnsId) == false) {
                mEditText1.setText("");
                mDialogHelper.toast("Please log in first.");
            }

            // if logged-in already
            else {
                final String message = mEditText1.getText().toString();
                if (StringHelper.isEmpty(message) == true) {
                    mDialogHelper.toast("Please enter the photo description correctly.");
                } else {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, REQ_CODE_FETCH_BITMAP_FROM_GALLERY);
                }
            }
        }
    });

    // set a listener to status update button
    mImgBtn2.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### status update button clicked");

            // if not logged-in yet
            if (mOpAgent.isSessionValid(mCurrentSnsId) == false) {
                mEditText1.setText("");
                mDialogHelper.toast("Please log in first.");
            }

            // if logged-in already
            else {
                final String message = mEditText1.getText().toString();
                if (StringHelper.isEmpty(message) == true) {
                    mDialogHelper.toast("Please enter your status correctly.");
                } else {
                    postStatus(message);
                }
            }
        }
    });

    // set a listener to link button
    mImgBtn8.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //Log.i(TAG, "#### photo attach button clicked");

            // if not logged-in yet
            if (mOpAgent.isSessionValid(mCurrentSnsId) == false) {
            }

            // if logged-in already
            else {
                confirmJumpToLinkDialog();
            }
        }
    });
}