Android Open Source - cloudmine-android Authentication Dialog






From Project

Back to project page cloudmine-android.

License

The source code is released under:

Copyright (c) 2012 CloudMine LLC, http://cloudmine.me Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software")...

If you think the Android project cloudmine-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.cloudmine.api.gui;
/* w  w w .  j  av  a  2 s. co m*/
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.cloudmine.api.CMSessionToken;
import com.cloudmine.api.exceptions.NetworkException;
import com.cloudmine.api.rest.CMSocial;
import com.cloudmine.api.rest.CMURLBuilder;
import com.cloudmine.api.rest.CMWebService;
import com.cloudmine.api.rest.callbacks.Callback;
import com.cloudmine.api.rest.response.CMSocialLoginResponse;

import java.util.Map;
import java.util.UUID;

/**
 * A Dialog class that performs the authentication to a service, such as
 * facebook, through a WebView.
 *
 * From: https://raw.github.com/Singly/singly-android/a4cd750ebb255c0cb2879fc69828955569f43cc5/sdk/src/com/singly/android/client/AuthenticationDialog.java
 */
public class AuthenticationDialog
        extends Dialog {

    // components and layouts
    private static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    private ProgressDialog progressDialog;
    private String authUrl;
    private Callback<CMSocialLoginResponse> callback;
    private WebView webView;
    private Context context;
    private String challenge;

    private class AuthenticationWebViewClient
             extends WebViewClient {

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {

            // finish the activity on error
            super.onReceivedError(view, errorCode, description, failingUrl);
            callback.onFailure(new NetworkException("Webview received errorcode: " + errorCode + " with description: " + description + " while loading URL: " + failingUrl), failingUrl);
            // dismiss any progress dialog
            progressDialog.dismiss();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // don't show the web view until the authentication page loaded
            super.onPageFinished(view, url);
            progressDialog.dismiss();
            webView.setVisibility(View.VISIBLE);

            if (needsManualCompletion(url)) {
                AuthenticationDialog.this.dismiss();
                completeAuthentication(challenge);
            }
        }
    }
    protected void completeAuthentication(String challenge) {
        try {
            CMWebService.getService().asyncCompleteSocialLogin(challenge, callback);
        } catch(Throwable t) {
            failureCallback(t, "Trouble making auth redirect");
        }
    }

    protected void failureCallback(Throwable throwable,  String msg) {
        callback.onFailure(throwable, msg);
    }

    private boolean needsManualCompletion(String url) {
        return url.startsWith(CMURLBuilder.CLOUD_MINE_URL) && url.contains("challenge=");
    }

    private class AuthenticationWebChromeClient
            extends WebChromeClient {
        public void onProgressChanged(WebView view, int progress) {
            progressDialog.setProgress(progress);
        }
    }

    public AuthenticationDialog(Context context, String authUrl,
                                Callback<CMSocialLoginResponse> callback) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);

        this.context = context;
        this.authUrl = authUrl;
        this.callback = callback;
    }
    public AuthenticationDialog(Context context, CMSocial.Service service, CMSessionToken userSessionToken,
                                Callback<CMSocialLoginResponse> callback) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.authUrl = getAuthenticationUrl(service, userSessionToken.getSessionToken());
        this.callback = callback;
    }
    public AuthenticationDialog(Context context, CMSocial.Service service,
                                Callback<CMSocialLoginResponse> callback) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.authUrl = getAuthenticationUrl(service);
        this.callback = callback;
    }

    public AuthenticationDialog(Context context, CMSocial.Service service, Map<String, Object> params, Callback<CMSocialLoginResponse> callback) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.authUrl = getAuthenticationUrl(service, null, params);
        this.callback = callback;
    }

    public AuthenticationDialog(Context context, CMSocial.Service service, CMSessionToken userSessionToken, Map<String, Object> params, Callback<CMSocialLoginResponse> callback) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.authUrl = getAuthenticationUrl(service, userSessionToken.getSessionToken(), params);
        this.callback = callback;
    }

    AuthenticationDialog(Context context, CMSocial.Service service, CMSessionToken userSessionToken, Map<String, Object> params) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.authUrl = getAuthenticationUrl(service, userSessionToken == null ? null : userSessionToken.getSessionToken(), params);
    }

    private String getAuthenticationUrl(CMSocial.Service service) {
        return getAuthenticationUrl(service, null);
    }

    private String getAuthenticationUrl(CMSocial.Service service, String userSessionToken) {
        return getAuthenticationUrl(service, userSessionToken, null);
    }

    private String getAuthenticationUrl(CMSocial.Service service, String userSessionToken, Map<String, Object> params) {
        challenge = UUID.randomUUID().toString();
        CMURLBuilder url = new CMURLBuilder().account().social().login().service(service).apikey().challenge(challenge).sessionToken(userSessionToken);
        if (params != null && !params.isEmpty()) {
            url = url.mapToQuery(params);
        }
        return url.asUrlString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // no window title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // the main web view container layout
        LinearLayout mainLayout = new LinearLayout(getContext());
        mainLayout.setPadding(0, 0, 0, 0);


        // progress dialog for loading web page
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Loading Authentication");
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setProgress(0); // set percentage completed to 0%

        // the web view with the oauth web page
        webView = new WebView(getContext());
        webView.setVisibility(View.INVISIBLE);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setWebViewClient(new AuthenticationWebViewClient());
        webView.setWebChromeClient(new AuthenticationWebChromeClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setLayoutParams(FILL);
        webView.getSettings().setSavePassword(false);
        webView.loadUrl(authUrl);

        // add the web view to the main layout
        mainLayout.addView(webView);

        // set the main layout as the content view
        setContentView(mainLayout, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        // show the progress dialog
        progressDialog.show();
    }

    @Override
    public void cancel() {
        super.cancel();
        callback.onFailure(new IllegalStateException("Was cancelled!"), "Authentication was cancelled");
    }

}




Java Source Code List

com.cloudmine.api.AccessListController.java
com.cloudmine.api.BaseCMUser.java
com.cloudmine.api.BaseCacheableCMFile.java
com.cloudmine.api.CMAndroidSocial.java
com.cloudmine.api.CMCreditCard.java
com.cloudmine.api.CMSharedPreferencesConstants.java
com.cloudmine.api.CurrentHandlerHavers.java
com.cloudmine.api.DeviceIdentifier.java
com.cloudmine.api.HasHandler.java
com.cloudmine.api.LocallySavable.java
com.cloudmine.api.db.BaseLocallySavableCMAccessList.java
com.cloudmine.api.db.BaseLocallySavableCMObject.java
com.cloudmine.api.db.CMObjectDBOpenHelper.java
com.cloudmine.api.db.LocallySavableCMGeoPoint.java
com.cloudmine.api.db.RequestConstants.java
com.cloudmine.api.db.RequestDBObject.java
com.cloudmine.api.db.RequestDBOpenHelper.java
com.cloudmine.api.db.RequestPerformerService.java
com.cloudmine.api.gui.AuthenticationDialog.java
com.cloudmine.api.gui.VolleyAuthenticationDialog.java
com.cloudmine.api.loopj.AsyncHttpClient.java
com.cloudmine.api.loopj.AsyncHttpRequest.java
com.cloudmine.api.loopj.AsyncHttpResponseHandler.java
com.cloudmine.api.loopj.PersistentCookieStore.java
com.cloudmine.api.loopj.RequestParams.java
com.cloudmine.api.loopj.RetryHandler.java
com.cloudmine.api.loopj.SerializableCookie.java
com.cloudmine.api.loopj.SimpleMultipartEntity.java
com.cloudmine.api.rest.AndroidAsynchronousHttpClient.java
com.cloudmine.api.rest.AndroidBase64Encoder.java
com.cloudmine.api.rest.AndroidHeaderFactory.java
com.cloudmine.api.rest.AndroidUserCMWebService.java
com.cloudmine.api.rest.BaseAccessListCreateRequest.java
com.cloudmine.api.rest.BaseAccessListLoadRequest.java
com.cloudmine.api.rest.BaseAccessListModificationRequest.java
com.cloudmine.api.rest.BaseAddPaymentMethodRequest.java
com.cloudmine.api.rest.BaseChangeUserIdentifierRequest.java
com.cloudmine.api.rest.BaseChangeUserPasswordRequest.java
com.cloudmine.api.rest.BaseChargeCardRequest.java
com.cloudmine.api.rest.BaseFileCreationRequest.java
com.cloudmine.api.rest.BaseFileDeleteRequest.java
com.cloudmine.api.rest.BaseFileLoadRequest.java
com.cloudmine.api.rest.BaseImageLoadRequest.java
com.cloudmine.api.rest.BaseLoadPaymentMethodsRequest.java
com.cloudmine.api.rest.BaseLoadUserProfilesRequest.java
com.cloudmine.api.rest.BaseObjectDeleteRequest.java
com.cloudmine.api.rest.BaseObjectLoadRequest.java
com.cloudmine.api.rest.BaseObjectModificationRequest.java
com.cloudmine.api.rest.BaseProfileLoadRequest.java
com.cloudmine.api.rest.BaseProfileUpdateRequest.java
com.cloudmine.api.rest.BaseRemovePaymentMethodRequest.java
com.cloudmine.api.rest.BaseResetPasswordRequest.java
com.cloudmine.api.rest.BaseSnippetRequest.java
com.cloudmine.api.rest.BaseUserCreationRequest.java
com.cloudmine.api.rest.BaseUserLoginRequest.java
com.cloudmine.api.rest.BaseUserLogoutRequest.java
com.cloudmine.api.rest.CMImageLoader.java
com.cloudmine.api.rest.CMNetwork.java
com.cloudmine.api.rest.CloudMineRequest.java
com.cloudmine.api.rest.CompleteSocialLoginRequest.java
com.cloudmine.api.rest.DialogListener.java
com.cloudmine.api.rest.DiskBitmapCache.java
com.cloudmine.api.rest.ObjectLoadRequestBuilder.java
com.cloudmine.api.rest.OkHttpStack.java
com.cloudmine.api.rest.RequestBuilder.java
com.cloudmine.api.rest.SharedRequestQueueHolders.java
com.cloudmine.api.rest.VolleyAsynchronousHttpClient.java
com.cloudmine.api.rest.callbacks.AndroidCallback.java
com.cloudmine.api.rest.callbacks.PaymentResponseCallback.java
com.cloudmine.api.rest.response.PaymentResponse.java