Android Open Source - xing-android-sdk Auth Web Dialog






From Project

Back to project page xing-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project xing-android-sdk 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 de.hdodenhof.xingapi.internal.widget;
/*from   w  w w  .j a  va  2 s  .c  o m*/
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import de.hdodenhof.xingapi.R;
import de.hdodenhof.xingapi.internal.OAuthConstants;

// Based on Facebook SDK WebDialog
public class AuthWebDialog extends Dialog {

    private static final int NO_PADDING_SCREEN_WIDTH = 480;
    private static final int MAX_PADDING_SCREEN_WIDTH = 800;
    private static final int NO_PADDING_SCREEN_HEIGHT = 800;
    private static final int MAX_PADDING_SCREEN_HEIGHT = 1280;

    private static final double MIN_SCALE_FACTOR = 0.5;

    private static final int DEFAULT_THEME = android.R.style.Theme_Translucent_NoTitleBar;
    private static final int BACKGROUND_GRAY = 0xCC000000;

    private String mUrl;

    private FrameLayout mContentFrameLayout;
    private WebView mWebView;
    private ImageView mCrossImageView;
    private ProgressDialog mSpinner;

    private OnCompleteListener mOnCompleteListener;

    private boolean mListenerCalled = false;
    private boolean mIsDetached = false;

    public interface OnCompleteListener {

        public void onComplete(String verifier);

        public void onAbort();

        public void onError();
    }

    public AuthWebDialog(Context context, String url, OnCompleteListener listener) {
        super(context, DEFAULT_THEME);

        mUrl = url;
        mOnCompleteListener = listener;
    }

    @Override
    public void dismiss() {
        if (mWebView != null) {
            mWebView.stopLoading();
        }
        if (!mIsDetached) {
            if (mSpinner.isShowing()) {
                mSpinner.dismiss();
            }
            super.dismiss();
        }
    }

    @Override
    public void onDetachedFromWindow() {
        mIsDetached = true;
        super.onDetachedFromWindow();
    }

    @Override
    public void onAttachedToWindow() {
        mIsDetached = false;
        super.onAttachedToWindow();
    }

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

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                AuthWebDialog.this.sendCancelToListener();
            }
        });

        mSpinner = new ProgressDialog(getContext(), resolveDialogTheme());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading");
        mSpinner.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                AuthWebDialog.this.sendCancelToListener();
                AuthWebDialog.this.dismiss();
            }
        });

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContentFrameLayout = new FrameLayout(getContext());

        calculateSize();
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        createCrossImage();
        int crossWidth = mCrossImageView.getDrawable().getIntrinsicWidth();

        setUpWebView(crossWidth / 2 + 1);

        mContentFrameLayout.addView(mCrossImageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        setContentView(mContentFrameLayout);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private int resolveDialogTheme() {
        int build = Build.VERSION.SDK_INT;
        if (build <= Build.VERSION_CODES.HONEYCOMB) {
            return 0;
        } else if (build <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return AlertDialog.THEME_HOLO_LIGHT;
        } else {
            return AlertDialog.THEME_DEVICE_DEFAULT_LIGHT;
        }
    }

    private void calculateSize() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        // always use the portrait dimensions to do the scaling calculations so we always get a portrait shaped
        // web dialog
        int width = metrics.widthPixels < metrics.heightPixels ? metrics.widthPixels : metrics.heightPixels;
        int height = metrics.widthPixels < metrics.heightPixels ? metrics.heightPixels : metrics.widthPixels;

        int dialogWidth = Math.min(getScaledSize(width, metrics.density, NO_PADDING_SCREEN_WIDTH, MAX_PADDING_SCREEN_WIDTH), metrics.widthPixels);
        int dialogHeight = Math.min(getScaledSize(height, metrics.density, NO_PADDING_SCREEN_HEIGHT, MAX_PADDING_SCREEN_HEIGHT), metrics.heightPixels);

        getWindow().setLayout(dialogWidth, dialogHeight);
    }

    private int getScaledSize(int screenSize, float density, int noPaddingSize, int maxPaddingSize) {
        int scaledSize = (int) ((float) screenSize / density);
        double scaleFactor;
        if (scaledSize <= noPaddingSize) {
            scaleFactor = 1.0;
        } else if (scaledSize >= maxPaddingSize) {
            scaleFactor = MIN_SCALE_FACTOR;
        } else {
            // between the noPadding and maxPadding widths, we take a linear reduction to go from 100%
            // of screen size down to MIN_SCALE_FACTOR
            scaleFactor = MIN_SCALE_FACTOR + ((double) (maxPaddingSize - scaledSize)) / ((double) (maxPaddingSize - noPaddingSize)) * (1.0 - MIN_SCALE_FACTOR);
        }
        return (int) (screenSize * scaleFactor);
    }

    private void sendSuccessToListener(String verifier) {
        if (mOnCompleteListener != null && !mListenerCalled) {
            mListenerCalled = true;
            mOnCompleteListener.onComplete(verifier);
        }
    }

    private void sendErrorToListener() {
        if (mOnCompleteListener != null && !mListenerCalled) {
            mListenerCalled = true;
            mOnCompleteListener.onError();
        }
    }

    private void sendCancelToListener() {
        if (mOnCompleteListener != null && !mListenerCalled) {
            mListenerCalled = true;
            mOnCompleteListener.onAbort();
        }
    }

    private void createCrossImage() {
        mCrossImageView = new ImageView(getContext());
        mCrossImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCancelToListener();
                AuthWebDialog.this.dismiss();
            }
        });
        Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.ic_close);
        mCrossImageView.setImageDrawable(crossDrawable);
        mCrossImageView.setVisibility(View.INVISIBLE);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setUpWebView(int margin) {
        LinearLayout webViewContainer = new LinearLayout(getContext());
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new DialogWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mWebView.setVisibility(View.INVISIBLE);
        mWebView.getSettings().setSavePassword(false);
        mWebView.getSettings().setSaveFormData(false);

        webViewContainer.setPadding(margin, margin, margin, margin);
        webViewContainer.addView(mWebView);
        webViewContainer.setBackgroundColor(BACKGROUND_GRAY);
        mContentFrameLayout.addView(webViewContainer);
    }

    private class DialogWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (isRedirectUrl(url)) {
                return false;
            } else if (url.startsWith(OAuthConstants.CALLBACK_URL)) {
                Uri uri = Uri.parse(url);
                String verifier = uri.getQueryParameter(OAuthConstants.VERIFIER_KEY);

                if (verifier != null) {
                    sendSuccessToListener(verifier);
                } else {
                    sendErrorToListener();
                }

                AuthWebDialog.this.dismiss();
                return true;
            }
            getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }

        private boolean isRedirectUrl(String url) {
            for (String redirectUrl : OAuthConstants.WEBVIEW_INTERNAL_REDIRECT_URLS) {
                if (url.startsWith(redirectUrl)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            sendErrorToListener();
            AuthWebDialog.this.dismiss();
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            sendErrorToListener();
            handler.cancel();
            AuthWebDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!mIsDetached) {
                mSpinner.show();
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!mIsDetached) {
                mSpinner.dismiss();
            }

            mContentFrameLayout.setBackgroundColor(Color.TRANSPARENT);
            mWebView.setVisibility(View.VISIBLE);
            mCrossImageView.setVisibility(View.VISIBLE);
        }
    }
}




Java Source Code List

de.hdodenhof.xingapi.BaseAPI.java
de.hdodenhof.xingapi.ContactsAPI.java
de.hdodenhof.xingapi.MessagesAPI.java
de.hdodenhof.xingapi.NetworkFeedAPI.java
de.hdodenhof.xingapi.ProfileVisitsAPI.java
de.hdodenhof.xingapi.UserProfilesAPI.java
de.hdodenhof.xingapi.XingApiClient.java
de.hdodenhof.xingapi.enums.ActivityAction.java
de.hdodenhof.xingapi.enums.ActivityAggregator.java
de.hdodenhof.xingapi.enums.ActivityObjectType.java
de.hdodenhof.xingapi.enums.ActivityVerb.java
de.hdodenhof.xingapi.enums.Badge.java
de.hdodenhof.xingapi.enums.CarrerLevel.java
de.hdodenhof.xingapi.enums.CompanyIndustry.java
de.hdodenhof.xingapi.enums.CompanySize.java
de.hdodenhof.xingapi.enums.EmploymentStatus.java
de.hdodenhof.xingapi.enums.ErrorName.java
de.hdodenhof.xingapi.enums.FormOfEmployment.java
de.hdodenhof.xingapi.enums.IMAccount.java
de.hdodenhof.xingapi.enums.LanguageSkill.java
de.hdodenhof.xingapi.enums.Language.java
de.hdodenhof.xingapi.enums.PremiumService.java
de.hdodenhof.xingapi.enums.Reason.java
de.hdodenhof.xingapi.enums.UserField.java
de.hdodenhof.xingapi.enums.WebProfile.java
de.hdodenhof.xingapi.exceptions.ApiException.java
de.hdodenhof.xingapi.exceptions.BaseException.java
de.hdodenhof.xingapi.exceptions.ConversionException.java
de.hdodenhof.xingapi.exceptions.NetworkException.java
de.hdodenhof.xingapi.internal.AuthHandler.java
de.hdodenhof.xingapi.internal.OAuthConstants.java
de.hdodenhof.xingapi.internal.communication.ErrorHandler.java
de.hdodenhof.xingapi.internal.communication.SigningOkClient.java
de.hdodenhof.xingapi.internal.communication.XingApiInterface.java
de.hdodenhof.xingapi.internal.response.ActivityWrapper.java
de.hdodenhof.xingapi.internal.response.CommentsWrapper.java
de.hdodenhof.xingapi.internal.response.ContactsWrapper.java
de.hdodenhof.xingapi.internal.response.ConversationWrapper.java
de.hdodenhof.xingapi.internal.response.ConversationsWrapper.java
de.hdodenhof.xingapi.internal.response.IdCardWrapper.java
de.hdodenhof.xingapi.internal.response.LikesWrapper.java
de.hdodenhof.xingapi.internal.response.MessageWrapper.java
de.hdodenhof.xingapi.internal.response.MessagesWrapper.java
de.hdodenhof.xingapi.internal.response.NetworkActivityWrapper.java
de.hdodenhof.xingapi.internal.response.ProfileMessageWrapper.java
de.hdodenhof.xingapi.internal.response.ResponseWrapper.java
de.hdodenhof.xingapi.internal.response.ResultWrapper.java
de.hdodenhof.xingapi.internal.response.SharedContactsWrapper.java
de.hdodenhof.xingapi.internal.response.TagsWrapper.java
de.hdodenhof.xingapi.internal.response.UserWrapper.java
de.hdodenhof.xingapi.internal.response.VisitsWrapper.java
de.hdodenhof.xingapi.internal.util.HTTPUtils.java
de.hdodenhof.xingapi.internal.util.URLUtils.java
de.hdodenhof.xingapi.internal.widget.AuthWebDialog.java
de.hdodenhof.xingapi.models.ActivityObject.java
de.hdodenhof.xingapi.models.Address.java
de.hdodenhof.xingapi.models.ApiError.java
de.hdodenhof.xingapi.models.Award.java
de.hdodenhof.xingapi.models.BirthDate.java
de.hdodenhof.xingapi.models.Bookmark.java
de.hdodenhof.xingapi.models.Comment.java
de.hdodenhof.xingapi.models.CompanyProfileUpdate.java
de.hdodenhof.xingapi.models.CompanyProfile.java
de.hdodenhof.xingapi.models.Company.java
de.hdodenhof.xingapi.models.Contacts.java
de.hdodenhof.xingapi.models.Conversation.java
de.hdodenhof.xingapi.models.Conversations.java
de.hdodenhof.xingapi.models.EducationalBackground.java
de.hdodenhof.xingapi.models.ErrorResponse.java
de.hdodenhof.xingapi.models.Event.java
de.hdodenhof.xingapi.models.FindByEmailsResult.java
de.hdodenhof.xingapi.models.Group.java
de.hdodenhof.xingapi.models.IdCard.java
de.hdodenhof.xingapi.models.JobPosting.java
de.hdodenhof.xingapi.models.Message.java
de.hdodenhof.xingapi.models.Messages.java
de.hdodenhof.xingapi.models.MetaComment.java
de.hdodenhof.xingapi.models.MetaLike.java
de.hdodenhof.xingapi.models.NetworkActivity.java
de.hdodenhof.xingapi.models.PhotoUrls.java
de.hdodenhof.xingapi.models.ProfessionalExperience.java
de.hdodenhof.xingapi.models.ProfileMessage.java
de.hdodenhof.xingapi.models.Results.java
de.hdodenhof.xingapi.models.School.java
de.hdodenhof.xingapi.models.Status.java
de.hdodenhof.xingapi.models.Tag.java
de.hdodenhof.xingapi.models.Tags.java
de.hdodenhof.xingapi.models.Thread.java
de.hdodenhof.xingapi.models.TimeZone.java
de.hdodenhof.xingapi.models.User.java
de.hdodenhof.xingapi.models.Visit.java
de.hdodenhof.xingapi.sample.MainActivity.java
de.hdodenhof.xingapi.sample.OAuthFragment.java
de.hdodenhof.xingapi.sample.ProfileFragment.java
de.hdodenhof.xingapi.typeadapter.ActivityObjectTypeAdapter.java