illab.nabal.proxy.AuthWebDialog.java Source code

Java tutorial

Introduction

Here is the source code for illab.nabal.proxy.AuthWebDialog.java

Source

/*
 * Copyright (C) 2013-2014 Tan Jung
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package illab.nabal.proxy;

import illab.nabal.proxy.AbstractContext.AbstractNetworkWebViewClient;
import illab.nabal.settings.SocialNetwork;
import illab.nabal.settings.SystemProperties;

import org.apache.http.protocol.HTTP;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Web dialog for OAuth authorization.
 * 
 * @version 1.0, 02/10/14
 * @author <a href="mailto:tanito.jung@gmail.com">Tan Jung</a>
 */
class AuthWebDialog extends Dialog {
    final static String TAG = "AuthWebDialog";

    /**
     * System properties.
     */
    private SystemProperties mSystemProperties;

    /**
     * URL of the authorization web page.
     */
    private String mUrl;

    /**
     * Layout for web view dialog.
     */
    private LinearLayout mLayout;

    /**
     * Web view for OAuth.
     */
    private WebView mWebView;

    /**
     * Web view control.
     */
    private AbstractNetworkWebViewClient mNetworkWebViewClient;

    /**
     * Title area of web view.
     */
    private TextView mTitle;

    /**
     * Margin setting for web view title.
     */
    private final static int MARGIN = 4;

    /**
     * Padding setting for web view title.
     */
    private final static int PADDING = 2;

    /**
     * Layout parameters for web view.
     */
    private final static FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    public AuthWebDialog(Context context, String url, AbstractNetworkWebViewClient networkWebViewClient,
            SystemProperties systemProperties) {
        //super(context, android.R.style.Theme_Translucent_NoTitleBar);
        super(context, android.R.style.Theme_Dialog);
        mSystemProperties = systemProperties;
        mNetworkWebViewClient = networkWebViewClient;
        mUrl = url;
    }

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

        // prepare web view layout
        mLayout = new LinearLayout(getContext());
        mLayout.setOrientation(LinearLayout.VERTICAL);

        // set up title bar
        setUpTitle();

        // set up web view
        setUpWebView();

        // determine screen width and height
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int screenWidth = displaymetrics.widthPixels;
        int screenHeight = displaymetrics.heightPixels;

        // add layout to screen
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) (screenWidth * 0.90),
                (int) (screenHeight * 0.90));
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(mLayout, layoutParams);
    }

    /**
     * Override hardware back key to close this dialog.
     * 
     * @deprecated
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //Log.d(TAG, "keyCode : " + keyCode + " | event.getAction() : " + event.getAction());
            Log.d(TAG, "BACK key has been pressed on WebView");
            mNetworkWebViewClient.onCancel();
        }
        return true;
    }

    /**
     * Sets up dialog title bar.
     */
    private void setUpTitle() {
        mTitle = new TextView(getContext());
        mTitle.setBackgroundColor(Color.parseColor(mNetworkWebViewClient.getWebViewTitleColor()));
        mTitle.setText(mSystemProperties.getLocalizedOAuthDialogTitle());
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
        mLayout.addView(mTitle);
    }

    /**
     * Sets up web view.
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void setUpWebView() {
        mWebView = new WebView(getContext());
        mWebView.setLayoutParams(FILL);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultTextEncodingName(HTTP.UTF_8);

        // DO NOT set user agent to 'Android' if current web view is up for Facebook
        // it will cause blank screen problem on Facebook auth page
        // (problem found and fixed on 2013-04-23)
        //
        // YOU MUST set user agent to 'Android' if current web view is up for Weibo
        // otherwise log-in page will redirect to open.weibo.cn and not work properly
        // (problem found and fixed on 2013-08-23)
        if (mNetworkWebViewClient.getSnsUid() == SocialNetwork.WEIBO) {
            mWebView.getSettings().setUserAgentString(SystemProperties.USER_AGENT_ANDROID);
        }

        mWebView.setWebViewClient(mNetworkWebViewClient);
        mWebView.loadUrl(mUrl);
        mLayout.addView(mWebView);
    }

}