Android Open Source - EBrowser Custom Web View Client






From Project

Back to project page EBrowser.

License

The source code is released under:

GNU General Public License

If you think the Android project EBrowser 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

/*
 * Zirco Browser for Android//from  w  w w . ja va2s  . co  m
 * 
 * Copyright (C) 2010 - 2012 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.zirco.ui.components;

import com.mrpej.ebrowser.R;
import org.zirco.controllers.Controller;
import org.zirco.ui.activities.MainActivity;
import org.zirco.utils.ApplicationUtils;
import org.zirco.utils.Constants;
import org.zirco.utils.UrlUtils;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebView.HitTestResult;
import android.widget.EditText;

/**
 * Convenient extension of WebViewClient.
 */
public class CustomWebViewClient extends WebViewClient {
  
  private MainActivity mMainActivity;
  
  public CustomWebViewClient(MainActivity mainActivity) {
    super();
    mMainActivity = mainActivity;
  }
  
  @Override
  public void onPageFinished(WebView view, String url) {      
    ((CustomWebView) view).notifyPageFinished();
    mMainActivity.onPageFinished(url);
    
    super.onPageFinished(view, url);
  }

  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
    // Some magic here: when performing WebView.loadDataWithBaseURL, the url is "file:///android_asset/startpage,
    // whereas when the doing a "previous" or "next", the url is "about:start", and we need to perform the
    // loadDataWithBaseURL here, otherwise it won't load.
    if (url.equals(Constants.URL_ABOUT_START)) {
      view.loadDataWithBaseURL("file:///android_asset/startpage/",
          ApplicationUtils.getStartPage(view.getContext()), "text/html", "UTF-8", "about:start");
    }
    
    ((CustomWebView) view).notifyPageStarted();
    mMainActivity.onPageStarted(url);
    
    super.onPageStarted(view, url, favicon);
  }

  @Override
  public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
    
    StringBuilder sb = new StringBuilder();
    
    sb.append(view.getResources().getString(R.string.Commons_SslWarningsHeader));
    sb.append("\n\n");
    
    if (error.hasError(SslError.SSL_UNTRUSTED)) {
      sb.append(" - ");
      sb.append(view.getResources().getString(R.string.Commons_SslUntrusted));
      sb.append("\n");
    }
    
    if (error.hasError(SslError.SSL_IDMISMATCH)) {
      sb.append(" - ");
      sb.append(view.getResources().getString(R.string.Commons_SslIDMismatch));
      sb.append("\n");
    }
    
    if (error.hasError(SslError.SSL_EXPIRED)) {
      sb.append(" - ");
      sb.append(view.getResources().getString(R.string.Commons_SslExpired));
      sb.append("\n");
    }
    
    if (error.hasError(SslError.SSL_NOTYETVALID)) {
      sb.append(" - ");
      sb.append(view.getResources().getString(R.string.Commons_SslNotYetValid));
      sb.append("\n");
    }
    
    ApplicationUtils.showContinueCancelDialog(view.getContext(),
        android.R.drawable.ic_dialog_info,
        view.getResources().getString(R.string.Commons_SslWarning),
        sb.toString(),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            handler.proceed();
          }

        },
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            handler.cancel();
          }
    });
  }

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {        
    
    if (isExternalApplicationUrl(url)) {
      mMainActivity.onExternalApplicationUrl(url);
      return true;
      
    } else if (url.startsWith(Constants.URL_ACTION_SEARCH)) {
      String searchTerm = url.replace(Constants.URL_ACTION_SEARCH, "");
      
      String searchUrl = Controller.getInstance().getPreferences().getString(Constants.PREFERENCES_GENERAL_SEARCH_URL, Constants.URL_SEARCH_GOOGLE);
      String newUrl = String.format(searchUrl, searchTerm);
      
      view.loadUrl(newUrl);
      return true;
      
    } else if (view.getHitTestResult().getType() == HitTestResult.EMAIL_TYPE) {
      mMainActivity.onMailTo(url);
      return true;
      
    } else {
            
      // If the url is not from GWT mobile view, and is in the mobile view url list, then load it with GWT.      
      if ((!url.startsWith(Constants.URL_GOOGLE_MOBILE_VIEW_NO_FORMAT)) &&
          (UrlUtils.checkInMobileViewUrlList(view.getContext(), url))) {
        
        String newUrl = String.format(Constants.URL_GOOGLE_MOBILE_VIEW, url);
        view.loadUrl(newUrl);
        return true;
        
      } else {      
        ((CustomWebView) view).resetLoadedUrl();
        mMainActivity.onUrlLoading(url);
        return false;
      }
    }
  }
  
  @Override
  public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host, final String realm) {
    String username = null;
        String password = null;
        
        boolean reuseHttpAuthUsernamePassword = handler.useHttpAuthUsernamePassword();
        
        if (reuseHttpAuthUsernamePassword && view != null) {
            String[] credentials = view.getHttpAuthUsernamePassword(
                    host, realm);
            if (credentials != null && credentials.length == 2) {
                username = credentials[0];
                password = credentials[1];
            }
        }

        if (username != null && password != null) {
            handler.proceed(username, password);
        } else {
          LayoutInflater factory = LayoutInflater.from(mMainActivity);
            final View v = factory.inflate(R.layout.http_authentication_dialog, null);
            
            if (username != null) {
                ((EditText) v.findViewById(R.id.username_edit)).setText(username);
            }
            if (password != null) {
                ((EditText) v.findViewById(R.id.password_edit)).setText(password);
            }
            
            AlertDialog dialog = new AlertDialog.Builder(mMainActivity)
            .setTitle(String.format(mMainActivity.getString(R.string.HttpAuthenticationDialog_DialogTitle), host, realm))
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setView(v)
            .setPositiveButton(R.string.Commons_Proceed,
                    new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                 int whichButton) {
                            String nm = ((EditText) v
                                    .findViewById(R.id.username_edit))
                                    .getText().toString();
                            String pw = ((EditText) v
                                    .findViewById(R.id.password_edit))
                                    .getText().toString();
                            mMainActivity.setHttpAuthUsernamePassword(host, realm, nm, pw);
                            handler.proceed(nm, pw);
                        }})
            .setNegativeButton(R.string.Commons_Cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            handler.cancel();
                        }})
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        handler.cancel();
                    }})
            .create();
            
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            dialog.show();
                        
            v.findViewById(R.id.username_edit).requestFocus();            
        }
  }

  private boolean isExternalApplicationUrl(String url) {
    return url.startsWith("vnd.") ||
        url.startsWith("rtsp://") ||
        url.startsWith("itms://") ||
        url.startsWith("itpc://");
  }

}




Java Source Code List

org.emergent.android.weave.client.Base32.java
org.emergent.android.weave.client.Base64Encoder.java
org.emergent.android.weave.client.Base64.java
org.emergent.android.weave.client.BulkKeyCouplet.java
org.emergent.android.weave.client.Dbg.java
org.emergent.android.weave.client.HexEncoder.java
org.emergent.android.weave.client.Hex.java
org.emergent.android.weave.client.QueryParams.java
org.emergent.android.weave.client.QueryResult.java
org.emergent.android.weave.client.UserWeave.java
org.emergent.android.weave.client.WeaveAccountInfo.java
org.emergent.android.weave.client.WeaveBasicObject.java
org.emergent.android.weave.client.WeaveConstants.java
org.emergent.android.weave.client.WeaveCryptoUtil.java
org.emergent.android.weave.client.WeaveException.java
org.emergent.android.weave.client.WeaveFactory.java
org.emergent.android.weave.client.WeaveHeader.java
org.emergent.android.weave.client.WeaveResponse.java
org.emergent.android.weave.client.WeaveSSLSocketFactory.java
org.emergent.android.weave.client.WeaveTransport.java
org.emergent.android.weave.client.WeaveUtil.java
org.greendroid.QuickActionGrid.java
org.greendroid.QuickActionWidget.java
org.greendroid.QuickAction.java
org.zirco.controllers.Controller.java
org.zirco.events.EventConstants.java
org.zirco.events.EventController.java
org.zirco.events.IDownloadEventsListener.java
org.zirco.model.DbAdapter.java
org.zirco.model.UrlSuggestionItemComparator.java
org.zirco.model.adapters.BookmarksCursorAdapter.java
org.zirco.model.adapters.DownloadListAdapter.java
org.zirco.model.adapters.HistoryExpandableListAdapter.java
org.zirco.model.adapters.UrlSuggestionCursorAdapter.java
org.zirco.model.adapters.WeaveBookmarksCursorAdapter.java
org.zirco.model.items.BookmarkItem.java
org.zirco.model.items.DownloadItem.java
org.zirco.model.items.HistoryItem.java
org.zirco.model.items.UrlSuggestionItem.java
org.zirco.model.items.WeaveBookmarkItem.java
org.zirco.providers.BookmarksProviderWrapper.java
org.zirco.providers.WeaveColumns.java
org.zirco.providers.WeaveContentProvider.java
org.zirco.providers.ZircoBookmarksContentProvider.java
org.zirco.sync.ISyncListener.java
org.zirco.sync.WeaveSyncTask.java
org.zirco.ui.activities.AboutActivity.java
org.zirco.ui.activities.AdBlockerWhiteListActivity.java
org.zirco.ui.activities.BookmarksHistoryActivity.java
org.zirco.ui.activities.BookmarksListActivity.java
org.zirco.ui.activities.ChangelogActivity.java
org.zirco.ui.activities.DownloadsListActivity.java
org.zirco.ui.activities.EditBookmarkActivity.java
org.zirco.ui.activities.HistoryListActivity.java
org.zirco.ui.activities.IToolbarsContainer.java
org.zirco.ui.activities.MainActivity.java
org.zirco.ui.activities.MobileViewListActivity.java
org.zirco.ui.activities.WeaveBookmarksListActivity.java
org.zirco.ui.activities.preferences.BaseSpinnerCustomPreferenceActivity.java
org.zirco.ui.activities.preferences.HomepagePreferenceActivity.java
org.zirco.ui.activities.preferences.PreferencesActivity.java
org.zirco.ui.activities.preferences.SearchUrlPreferenceActivity.java
org.zirco.ui.activities.preferences.UserAgentPreferenceActivity.java
org.zirco.ui.activities.preferences.WeavePreferencesActivity.java
org.zirco.ui.activities.preferences.WeaveServerPreferenceActivity.java
org.zirco.ui.components.CustomWebViewClient.java
org.zirco.ui.components.CustomWebView.java
org.zirco.ui.runnables.DownloadRunnable.java
org.zirco.ui.runnables.FaviconUpdaterRunnable.java
org.zirco.ui.runnables.HideToolbarsRunnable.java
org.zirco.ui.runnables.HistoryUpdater.java
org.zirco.ui.runnables.XmlHistoryBookmarksExporter.java
org.zirco.ui.runnables.XmlHistoryBookmarksImporter.java
org.zirco.utils.AnimationManager.java
org.zirco.utils.ApplicationUtils.java
org.zirco.utils.Constants.java
org.zirco.utils.DateUtils.java
org.zirco.utils.IOUtils.java
org.zirco.utils.ProxyChangeReceiver.java
org.zirco.utils.ProxySettings.java
org.zirco.utils.UrlUtils.java