package tv.acfun.a63;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import tv.acfun.a63.base.BaseWebViewActivity;

public class DonateActivity extends BaseWebViewActivity {
    
    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mWeb.setWebChromeClient(new WebChromeClient());
        mWeb.setWebViewClient(new WebViewClient(){
            
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(uri);
                if("alipayqr".equals(uri.getScheme())){
                    try {
                        startActivity(intent);
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                        showErrorDialog();
                    }
                    return true;
                }else if(url.startsWith("http") && url.endsWith(".apk")){
                    startActivity(intent);
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                setSupportProgressBarIndeterminateVisibility(false);
            }
        });
    }
    
    @Override
    protected void initData() {
        super.initData();
        mWeb.loadUrl(getIntent().getDataString());
    }
    
    protected void showErrorDialog() {
        try {
            Drawable icon = Drawable.createFromStream(getAssets().open("emotion/ais/38.gif"), "38.gif");
            icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
            new AlertDialog.Builder(this)
                .setTitle("你的好意我心领了！")
                .setIcon(icon)
                .setMessage("仅接受支付宝捐助。。可是你都没有安装支付宝钱包=.=")
                .setPositiveButton("知道了", null)
                .show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void start(Activity activity){
        Intent intent = new Intent(activity.getString(R.string.donate_action),
                Uri.parse(activity.getString(R.string.donate_url)));
        intent.setClass(activity.getApplicationContext(), DonateActivity.class);
        activity.startActivity(intent);
    }
}

@@ -43,16 +43,41 @@
 ception;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -229,16 +229,41 @@
 et.Uri;%0A
+import android.os.Build;%0A
 import a
@@ -492,24 +492,299 @@
 ivity %7B%0A    
+static final String UA = %22Mozilla/5.0 (Linux; Android %22+ Build.VERSION.RELEASE+%22; %22+ Locale.getDefault().getLanguage()+%22-%22+Locale.getDefault().getCountry().toLowerCase()+%22; Nexus 5 Build/JOP40D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2307.2 Mobile Safari/537.36%22;
 %0A    @Overri
@@ -939,24 +939,75 @@
 eClient());%0A
+        mWeb.getSettings().setUserAgentString(UA);%0A
         mWeb
@@ -1049,21 +1049,9 @@
 ()%7B%0A
-            
 %0A
+
     
@@ -1405,17 +1405,57 @@
 cheme())
-)
+ %7C%7C %22alipays%22.endsWith(uri.getScheme())) 
 %7B%0A      

update ua