Android Open Source - beansight-mobile-android Web View Activity






From Project

Back to project page beansight-mobile-android.

License

The source code is released under:

Apache License

If you think the Android project beansight-mobile-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.beansight.android;
/*from  w ww .  ja v a  2s  . c o  m*/

import com.beansight.android.api.BeansightApi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity {

  public static final String FRAGMENT = "?access_token=";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        
        String url = BeansightApi.DOMAIN + "/api/authenticate?";
        
        WebView webView = (WebView)findViewById(R.id.webkitWebView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                int start = url.indexOf(FRAGMENT);
                if (start > -1) {
                    // You can use the accessToken for api calls now.
                    String accessToken = url.substring(start + FRAGMENT.length(), url.length());
              
                    Log.v("WebViewActivity", "OAuth complete, token: [" + accessToken + "].");
                  
                    SharedPreferences prefs = getSharedPreferences(BeansightApplication.BEANSIGHT_PREFS, 0);
                    Editor editor = prefs.edit();
                    editor.putString("access_token", accessToken);
                    editor.commit();
                    
                    Intent homeActivity = new Intent(WebViewActivity.this, HomeActivity.class);
                    startActivity(homeActivity);
                    finish();
                }
            }
            
            public void onPageFinished(WebView view, String url) {
                int start = url.indexOf(FRAGMENT);
                if (start > -1) {
                  // remove cookies to logout
                  CookieManager.getInstance().removeAllCookie();
                    Log.v("WebViewActivity", "removed cookie");
                }
            }
        });
        webView.loadUrl(url);
  }
  
}




Java Source Code List

com.beansight.android.BeansightApplication.java
com.beansight.android.HomeActivity.java
com.beansight.android.WebViewActivity.java
com.beansight.android.api.BeansightApi.java
com.beansight.android.api.NotAuthenticatedException.java
com.beansight.android.api.ServerErrorException.java
com.beansight.android.api.responses.InsightDetailResponse.java
com.beansight.android.api.responses.InsightListResponse.java
com.beansight.android.api.responses.InsightVoteResponse.java
com.beansight.android.api.responses.Meta.java
com.beansight.android.api.responses.Response.java
com.beansight.android.api.responses.UserProfileResponse.java
com.beansight.android.http.Http.java
com.beansight.android.models.InsightDetail.java
com.beansight.android.models.InsightListItem.java
com.beansight.android.models.InsightVote.java
com.beansight.android.models.UserProfile.java