Search in sources :

Example 26 with CookieManager

use of android.webkit.CookieManager in project GT by Tencent.

the class ShowhtmlActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gt_showhtml);
    Intent intent = getIntent();
    String action = intent.getStringExtra("uri");
    String uri = Env.GT_HOMEPAGE;
    if (action != null && !action.equals("")) {
        uri = action;
    }
    String cookies = intent.getStringExtra("cookies");
    webview = (WebView) findViewById(R.id.showhtml);
    if (Build.VERSION.SDK_INT >= 11) {
        // WebView安全性修改
        try {
            Method m = webview.getClass().getMethod("removeJavascriptInterface", String.class);
            m.invoke(webview, "searchBoxJavaBridge_");
            m.invoke(webview, "accessibility");
            m.invoke(webview, "accessibilityTraversal");
        } catch (NoSuchMethodException e) {
        // 说明是Andorid2.3以下,do nothing
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
    }
    webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    webview.getSettings().setSupportZoom(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setDatabaseEnabled(true);
    if (cookies != null) {
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        //			String oldCookie = cookieManager.getCookie(uri);
        //			ToastUtil.ShowLongToast(this, oldCookie);
        cookieManager.setAcceptCookie(true);
        // 移除
        cookieManager.removeSessionCookie();
        String[] cookieArray = cookies.split(";");
        for (String cookie : cookieArray) {
            if (!cookie.trim().isEmpty()) {
                //cookies是在HttpClient中获得的cookie
                cookieManager.setCookie(uri, cookie);
            }
        }
        CookieSyncManager.getInstance().sync();
    }
    try {
        webview.loadUrl(uri);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Intent(android.content.Intent) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) CookieManager(android.webkit.CookieManager) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with CookieManager

use of android.webkit.CookieManager in project SeriesGuide by UweTrottmann.

the class BaseOAuthActivity method activateWebView.

@SuppressLint("SetJavaScriptEnabled")
protected void activateWebView() {
    buttonContainer.setVisibility(View.GONE);
    webview.setVisibility(View.VISIBLE);
    webview.setWebViewClient(webViewClient);
    webview.getSettings().setJavaScriptEnabled(true);
    // make sure we start fresh
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    webview.clearCache(true);
    // load the authorization page
    Timber.d("Initiating authorization request...");
    String authUrl = getAuthorizationUrl();
    if (authUrl != null) {
        webview.loadUrl(authUrl);
    }
}
Also used : CookieManager(android.webkit.CookieManager) SuppressLint(android.annotation.SuppressLint)

Example 28 with CookieManager

use of android.webkit.CookieManager in project react-native-video by react-native-community.

the class ReactVideoView method setSrc.

public void setSrc(final String uriString, final String type, final boolean isNetwork, final boolean isAsset, final int expansionMainVersion, final int expansionPatchVersion) {
    mSrcUriString = uriString;
    mSrcType = type;
    mSrcIsNetwork = isNetwork;
    mSrcIsAsset = isAsset;
    mMainVer = expansionMainVersion;
    mPatchVer = expansionPatchVersion;
    mMediaPlayerValid = false;
    mVideoDuration = 0;
    mVideoBufferedDuration = 0;
    initializeMediaPlayerIfNeeded();
    mMediaPlayer.reset();
    try {
        if (isNetwork) {
            // Use the shared CookieManager to access the cookies
            // set by WebViews inside the same app
            CookieManager cookieManager = CookieManager.getInstance();
            Uri parsedUrl = Uri.parse(uriString);
            Uri.Builder builtUrl = parsedUrl.buildUpon();
            String cookie = cookieManager.getCookie(builtUrl.build().toString());
            Map<String, String> headers = new HashMap<String, String>();
            if (cookie != null) {
                headers.put("Cookie", cookie);
            }
            setDataSource(mThemedReactContext, parsedUrl, headers);
        } else if (isAsset) {
            if (uriString.startsWith("content://")) {
                Uri parsedUrl = Uri.parse(uriString);
                setDataSource(mThemedReactContext, parsedUrl);
            } else {
                setDataSource(uriString);
            }
        } else {
            ZipResourceFile expansionFile = null;
            AssetFileDescriptor fd = null;
            if (mMainVer > 0) {
                try {
                    expansionFile = APKExpansionSupport.getAPKExpansionZipFile(mThemedReactContext, mMainVer, mPatchVer);
                    fd = expansionFile.getAssetFileDescriptor(uriString.replace(".mp4", "") + ".mp4");
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
            if (fd == null) {
                int identifier = mThemedReactContext.getResources().getIdentifier(uriString, "drawable", mThemedReactContext.getPackageName());
                if (identifier == 0) {
                    identifier = mThemedReactContext.getResources().getIdentifier(uriString, "raw", mThemedReactContext.getPackageName());
                }
                setRawData(identifier);
            } else {
                setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    WritableMap src = Arguments.createMap();
    src.putString(ReactVideoViewManager.PROP_SRC_URI, uriString);
    src.putString(ReactVideoViewManager.PROP_SRC_TYPE, type);
    src.putBoolean(ReactVideoViewManager.PROP_SRC_IS_NETWORK, isNetwork);
    if (mMainVer > 0) {
        src.putInt(ReactVideoViewManager.PROP_SRC_MAINVER, mMainVer);
        if (mPatchVer > 0) {
            src.putInt(ReactVideoViewManager.PROP_SRC_PATCHVER, mPatchVer);
        }
    }
    WritableMap event = Arguments.createMap();
    event.putMap(ReactVideoViewManager.PROP_SRC, src);
    mEventEmitter.receiveEvent(getId(), Events.EVENT_LOAD_START.toString(), event);
    // not async to prevent random crashes on Android playback from local resource due to race conditions
    try {
        prepare(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ZipResourceFile(com.android.vending.expansion.zipfile.ZipResourceFile) AssetFileDescriptor(android.content.res.AssetFileDescriptor) WritableMap(com.facebook.react.bridge.WritableMap) HashMap(java.util.HashMap) IOException(java.io.IOException) Uri(android.net.Uri) CookieManager(android.webkit.CookieManager) IOException(java.io.IOException)

Example 29 with CookieManager

use of android.webkit.CookieManager in project robolectric by robolectric.

the class ShadowCookieManagerTest method shouldGetCookieForUrl.

@Test
public void shouldGetCookieForUrl() {
    CookieManager cookieManager = CookieManager.getInstance();
    String url1 = "http://www.google.com";
    String value1 = "my cookie";
    cookieManager.setCookie(url1, value1);
    String url2 = "http://www.hotbot.com";
    String value2 = "some special value: thing";
    cookieManager.setCookie(url2, value2);
    assertThat(cookieManager.getCookie("http://www.google.com")).isEqualTo(value1);
    assertThat(cookieManager.getCookie(url2)).isEqualTo(value2);
}
Also used : CookieManager(android.webkit.CookieManager) Test(org.junit.Test)

Example 30 with CookieManager

use of android.webkit.CookieManager in project robolectric by robolectric.

the class ShadowCookieManagerTest method shouldGetCookieForHostInDomain.

@Test
public void shouldGetCookieForHostInDomain() {
    CookieManager cookieManager = CookieManager.getInstance();
    String value1 = "my cookie";
    cookieManager.setCookie("foo.com/this%20is%20a%20test", value1);
    assertThat(cookieManager.getCookie(".foo.com")).isEqualTo(value1);
}
Also used : CookieManager(android.webkit.CookieManager) Test(org.junit.Test)

Aggregations

CookieManager (android.webkit.CookieManager)36 CookieSyncManager (android.webkit.CookieSyncManager)9 Test (org.junit.Test)7 WebView (android.webkit.WebView)4 SuppressLint (android.annotation.SuppressLint)3 WebSettings (android.webkit.WebSettings)3 Context (android.content.Context)2 Intent (android.content.Intent)2 Editor (android.content.SharedPreferences.Editor)2 Uri (android.net.Uri)2 View (android.view.View)2 WebViewClient (android.webkit.WebViewClient)2 TextView (android.widget.TextView)2 TargetApi (android.annotation.TargetApi)1 ActionBar (android.app.ActionBar)1 AlarmManager (android.app.AlarmManager)1 AlertDialog (android.app.AlertDialog)1 PendingIntent (android.app.PendingIntent)1 DialogInterface (android.content.DialogInterface)1 SharedPreferences (android.content.SharedPreferences)1