Skip to content
  • WebViewActivity.java

    package com.nutcore.mementoparking;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.webkit.CookieManager;
    import android.webkit.CookieSyncManager;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import okhttp3.Cookie;
    
    import static com.nutcore.mementoparking.Constants.USERINFO;
    
    public class WebViewActivity extends Activity {
    
        private WebView webView;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_social_side);
    
            webView = (WebView) findViewById(R.id.webView1);
            WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setBuiltInZoomControls(false);
            settings.setGeolocationEnabled(true);
            settings.setAppCacheEnabled(false);
            settings.setLoadsImagesAutomatically(true);
            webView.setWebViewClient(new WebViewClient());
            webView.loadUrl(Constants.SERVER_URL);
            webView.setWebViewClient(new WebViewClient() {
    
                public void onPageFinished(WebView view, String url) {
                //I save the cookies only when the user goes on login page
                    if(url.equals(Constants.LOGIN_URL)){
                        CookieSyncManager syncManager = CookieSyncManager.createInstance(webView.getContext());
                        CookieManager cookieManager = CookieManager.getInstance();
    
                        String cookie = cookieManager.getCookie(url);
                        SharedPreferences.Editor editor = getEditor(SocialSideActivity.this);
                        editor.putString(Constants.COOKIE, cookie);
                        editor.commit();
                        syncManager.sync();
                    }
                }
            });
    
        }
    
        private SharedPreferences.Editor getEditor(Context context) {
            SharedPreferences settings = getSharedPreferences(context);
            return settings.edit();
        }
    
        private SharedPreferences getSharedPreferences(Context context) {
            return context.getSharedPreferences(USERINFO, 0);
        }
    
    
    }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment