Android Open Source - singly-android Base Authentication Web View Client






From Project

Back to project page singly-android.

License

The source code is released under:

MIT License

If you think the Android project singly-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.singly.android.client;
/*  w w  w . jav a  2s  . c  o m*/
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;

import android.content.Context;
import android.content.SharedPreferences;
import android.webkit.WebViewClient;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.singly.android.util.JSON;
import com.singly.android.util.SinglyUtils;

/**
 * An abstract WebViewClient implementation that handles the final part of the
 * Singly Authentication process.  This involves making a call to singly to get
 * the access token once we have an authentication code.
 */
public abstract class BaseAuthenticationWebViewClient
  extends WebViewClient {

  /**
   * When extending this class, you will need to override the {@link 
   * #shouldOverrideUrlLoading(android.webkit.WebView, String)} method.  This
   * completeAuthentication method will then need to be called when the WebView 
   * is redirected to the success URL to complete the authentication process.
   * 
   * @param context The current android context.
   * @param authCode The authentication code.
   * @param clientId The Singly app client id.
   * @param clientSecret The Singly app client secret.
   * @param listener A callback class that notifies when the authentication 
   * process either completes or errors.
   */
  protected void completeAuthentication(final Context context, String authCode,
    String clientId, String clientSecret,
    final AuthenticationWebViewListener listener) {

    // create the post parameters
    RequestParams qparams = new RequestParams();
    qparams.put("client_id", clientId);
    qparams.put("client_secret", clientSecret);
    qparams.put("code", authCode);

    // create the access token url
    String accessTokenUrl = SinglyUtils.createSinglyURL("/oauth/access_token",
      null);

    // make an async http call to get the access token
    AsyncHttpClient client = SinglyUtils.getHttpClient();
    client.post(accessTokenUrl, qparams, new AsyncHttpResponseHandler() {

      @Override
      public void onSuccess(String response) {

        if (response != null) {

          // get the account and access token from JSON
          JsonNode root = JSON.parse(response);
          String accessToken = JSON.getString(root, "access_token");
          String account = JSON.getString(root, "account");

          // get the shared preferences
          SharedPreferences prefs = context.getSharedPreferences("singly",
            Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = prefs.edit();

          // save the account if there is one
          if (StringUtils.isNotBlank(account)) {
            editor.putString(SinglyClient.ACCOUNT, account);
          }

          // save the access token if there is one
          if (StringUtils.isNotBlank(accessToken)) {
            editor.putString(SinglyClient.ACCESS_TOKEN, accessToken);
          }

          // commit changes to shared preferences
          editor.commit();
        }

        // done with the authentication process, perform finish callback
        listener.onFinish();
      }

      @Override
      public void onFailure(Throwable error, String message) {

        // pass through the error and response
        listener.onError(error, message);
      }

    });
  }
}




Java Source Code List

com.singly.android.client.AsyncApiResponseHandler.java
com.singly.android.client.AuthenticationActivity.java
com.singly.android.client.AuthenticationWebViewListener.java
com.singly.android.client.BaseAuthenticationWebViewClient.java
com.singly.android.client.FacebookAuthenticationActivity.java
com.singly.android.client.SinglyClient.java
com.singly.android.component.AbstractCachingBlockLoadedListAdapter.java
com.singly.android.component.AuthenticatedServicesActivity.java
com.singly.android.component.AuthenticatedServicesAdapter.java
com.singly.android.component.AuthenticatedServicesFragment.java
com.singly.android.component.DeviceOwnerActivity.java
com.singly.android.component.Friend.java
com.singly.android.component.FriendsListActivity.java
com.singly.android.component.FriendsListAdapter.java
com.singly.android.component.FriendsListFragment.java
com.singly.android.component.FriendsListRowClickListener.java
com.singly.android.component.SinglyService.java
com.singly.android.component.TableOfContentsFragment.java
com.singly.android.component.TableOfContentsTouchListener.java
com.singly.android.examples.MainActivity.java
com.singly.android.util.BitmapUtils.java
com.singly.android.util.ImageCacheListener.java
com.singly.android.util.ImageInfo.java
com.singly.android.util.JSON.java
com.singly.android.util.RemoteImageCache.java
com.singly.android.util.SinglyUtils.java
com.singly.android.util.URLUtils.java