1. 程式人生 > >cordova應用在遇到https下安全證書有問題的解決方式

cordova應用在遇到https下安全證書有問題的解決方式

package com.aviva_cofco.crowdfunding.ssl;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;

import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;

public class SSLAcceptingCordovaWebViewClient
extends CordovaWebViewClient {
public SSLAcceptingCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) { super(cordova, view); // TODO Auto-generated constructor stub } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { // testing against getPrimaryError() or hasErrors() will fail on
// Honeycomb or older. // You might check for something different, such as specific info in the // certificate, // if (error.getPrimaryError() == SslError.SSL_IDMISMATCH) { handler.proceed(); // } else { // super.onReceivedSslError(view, handler, error); // }
} }
package com.aviva_cofco.crowdfunding.ssl;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.IceCreamCordovaWebViewClient;

import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;

public class SSLAcceptingIceCreamCordovaWebViewClient extends
        IceCreamCordovaWebViewClient {
    public SSLAcceptingIceCreamCordovaWebViewClient(CordovaInterface cordova,
            CordovaWebView view) {
        super(cordova, view);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler,
            SslError error) {
        handler.proceed();
    }
}
/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you 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.aviva_cofco.crowdfunding;

import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewClient;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.aviva_cofco.crowdfunding.ssl.SSLAcceptingCordovaWebViewClient;
import com.aviva_cofco.crowdfunding.ssl.SSLAcceptingIceCreamCordovaWebViewClient;

public class CordovaApp extends CordovaActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.init();
        // super.i
        setOriention();
        /*
         * appView.setWebViewClient(new WebViewClient(){
         * 
         * @Override public void onReceivedSslError(WebView view,
         * SslErrorHandler handler, SslError error) { // TODO Auto-generated
         * method stub handler.proceed(); } });
         * appView.getSettings().setDomStorageEnabled(true);
         */
        // appView.setWebContentsDebuggingEnabled(true);
        // 修改瀏覽器UA
        String ua = appView.getSettings().getUserAgentString();
        // 加入當前App的版本號
        ua += "&=AppVersion:0.1";
        appView.getSettings().setUserAgentString(ua);

        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setOriention();
    }

    private void setOriention() {
        if (isPad() || isCustomerDevice()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 判斷是否是某些特殊裝置
     * 
     * @return
     */
    private boolean isCustomerDevice() {
        String model = Build.MODEL;
        if ("N5100".equals(model)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean isPad() {
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);
        // 螢幕寬度
        float screenWidth = p.x;
        float screenHeight = p.y;
        screenWidth = Math.max(screenWidth, screenHeight);
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        int cssWidth = (int) (screenWidth / dm.density + 0.5f);

        if (cssWidth > 700) {
            return true;
        }

        return false;
    }

    @Override
    protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {
        // TODO Auto-generated method stub
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
            return new SSLAcceptingCordovaWebViewClient(this, webView);
        } else {
            return new SSLAcceptingIceCreamCordovaWebViewClient(this, webView);
        }
    }
}