/*
 * Copyright 2010 Facebook, Inc.
 *
 * 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 com.facebook.android;

import com.facebook.android.Facebook.DialogListener;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

public class FbDialog extends Dialog {
    
    static final int FB_BLUE = 0xFF6D84B4;
    static final LayoutParams DEFAULT_LANDSCAPE = new LayoutParams(460, 260);
    static final LayoutParams DEFAULT_PORTRAIT = new LayoutParams(280, 420);
    static final LayoutParams FILL = 
        new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                         ViewGroup.LayoutParams.FILL_PARENT);
    static final int MARGIN = 4;
    
    private String mUrl;
    private DialogListener mListener;
    private WebView mWebView;
    ProgressDialog mSpinner;
    
    private LinearLayout mContent;
    private TextView mTitleLabel;
   
    public FbDialog(Context context, String url, DialogListener listener) {
        super(context);
        mUrl = url;
        mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");
        
        mContent = new LinearLayout(getContext());
        mContent.setOrientation(LinearLayout.VERTICAL);
        setUpTitle();
        setUpWebView();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        addContentView(mContent, display.getWidth() < display.getHeight() ?
                DEFAULT_PORTRAIT : DEFAULT_LANDSCAPE);
    }

    private void setUpTitle() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mTitleLabel = new TextView(getContext());
        mTitleLabel.setText("Facebook");
        mTitleLabel.setBackgroundColor(FB_BLUE);
        mTitleLabel.setTextColor(Color.WHITE);
        mTitleLabel.setTypeface(Typeface.DEFAULT_BOLD);
        mTitleLabel.setPadding(MARGIN, MARGIN, MARGIN, MARGIN);
        mContent.addView(mTitleLabel);
    }
    
    private void setUpWebView() {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new FbDialog.FbWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mContent.addView(mWebView);
    }

    private class FbWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("Facebook-WebView", "Redirect URL: " + url);
            if (url.startsWith(Facebook.REDIRECT_URI)) {
                Bundle values = Util.parseUrl(url);
                String error = values.getString("error_reason");
                if (error == null) {
                    mListener.onComplete(values);
                } else {
                    mListener.onFacebookError(new FacebookError(error));
                }
                FbDialog.this.dismiss();
                return true;
            } else if (url.startsWith(Facebook.CANCEL_URI)) {
                mListener.onCancel();
                FbDialog.this.dismiss();
                return true;
            }
            return false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            mListener.onError(
                    new DialogError(description, errorCode, failingUrl));
            FbDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d("Facebook-WebView", "Webview loading URL: " + url);
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mTitleLabel.setText(mWebView.getTitle());
            mSpinner.dismiss();
        }   
        
    }
}

@@ -771,16 +771,47 @@
 ontext;%0A
+import android.content.Intent;%0A
 import a
@@ -834,16 +834,16 @@
 Bitmap;%0A
-
 import a
@@ -899,16 +899,40 @@
 peface;%0A
+import android.net.Uri;%0A
 import a
@@ -1286,21 +1286,17 @@
 ialog %7B%0A
-    
 %0A
+
     stat
@@ -1647,16 +1647,16 @@
 ARENT);%0A
-
     stat
@@ -1680,16 +1680,74 @@
 IN = 4;%0A
+    static final String DISPLAY_STRING = %22display=touch%22;%0A
     %0A   
@@ -4476,32 +4476,282 @@
    %7D
-%0A            return fals
+ else if (url.contains(DISPLAY_STRING)) %7B%0A                return false;%0A            %7D%0A            // launch non-dialog URLs in a full browser%0A            getContext().startActivity(%0A                    new Intent(Intent.ACTION_VIEW, Uri.parse(url))); %0A            return tru
 e;%0A 

Fix URL loading for non-display=touch URLs