BrowserActivity.java revision e1fb96676cd2404c5de091d60828185228ef4c90
10c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project/*
20c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
30c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *
40c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
50c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * you may not use this file except in compliance with the License.
60c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * You may obtain a copy of the License at
70c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *
80c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
90c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *
100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * See the License for the specific language governing permissions and
140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * limitations under the License.
150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project */
160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectpackage com.android.browser;
180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport com.google.android.googleapps.IGoogleLoginService;
200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport com.google.android.googlelogin.GoogleLoginServiceConstants;
210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.app.Activity;
230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.app.AlertDialog;
240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.app.ProgressDialog;
250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.app.SearchManager;
260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.ActivityNotFoundException;
270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.BroadcastReceiver;
280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.ComponentName;
290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.ContentResolver;
30b6b7f9ef3b4ed220063f555d8b7c205210a61a04Leon Scrogginsimport android.content.ContentUris;
310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.ContentValues;
320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.Context;
330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.DialogInterface;
340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.Intent;
350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.IntentFilter;
360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.ServiceConnection;
370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.DialogInterface.OnCancelListener;
38b4da0ad0351996f639b50f9f29b123a9b5c24fd0Grace Klobaimport android.content.pm.PackageInfo;
390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.pm.PackageManager;
400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.pm.ResolveInfo;
410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.res.AssetManager;
420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.res.Configuration;
430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.res.Resources;
440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.database.Cursor;
450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.database.sqlite.SQLiteDatabase;
460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.database.sqlite.SQLiteException;
470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.Bitmap;
480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.Canvas;
490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.Color;
500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.DrawFilter;
510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.Paint;
520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.PaintFlagsDrawFilter;
530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.Picture;
540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.drawable.BitmapDrawable;
550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.drawable.Drawable;
560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.drawable.LayerDrawable;
570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.drawable.PaintDrawable;
580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.hardware.SensorListener;
590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.hardware.SensorManager;
600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.net.ConnectivityManager;
610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.net.Uri;
620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.net.WebAddress;
630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.net.http.EventHandler;
640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.net.http.SslCertificate;
650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.net.http.SslError;
660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.AsyncTask;
670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.Bundle;
680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.Debug;
690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.Environment;
700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.Handler;
710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.IBinder;
720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.Message;
730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.PowerManager;
740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.Process;
750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.RemoteException;
760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.ServiceManager;
770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.os.SystemClock;
780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.provider.Browser;
790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.provider.Contacts;
800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.provider.Downloads;
810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.provider.MediaStore;
820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.provider.Contacts.Intents.Insert;
830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.text.IClipboard;
840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.text.TextUtils;
850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.text.format.DateFormat;
860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.text.util.Regex;
870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.util.Log;
880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.ContextMenu;
890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.Gravity;
900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.KeyEvent;
910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.LayoutInflater;
920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.Menu;
930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.MenuInflater;
940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.MenuItem;
950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.View;
960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.ViewGroup;
970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.Window;
980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.WindowManager;
990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.ContextMenu.ContextMenuInfo;
1000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.MenuItem.OnMenuItemClickListener;
1010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.animation.AlphaAnimation;
1020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.animation.Animation;
1030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.animation.AnimationSet;
1040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.animation.DecelerateInterpolator;
1050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.animation.ScaleAnimation;
1060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.animation.TranslateAnimation;
1070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.CookieManager;
1080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.CookieSyncManager;
1090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.DownloadListener;
1102bc69918d97a767a02132090e58718077381ba1aSteve Blockimport android.webkit.GeolocationPermissions;
1110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.HttpAuthHandler;
112b4da0ad0351996f639b50f9f29b123a9b5c24fd0Grace Klobaimport android.webkit.PluginManager;
1130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.SslErrorHandler;
1140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.URLUtil;
1150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.WebChromeClient;
116c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescuimport android.webkit.WebChromeClient.CustomViewCallback;
1170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.WebHistoryItem;
1180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.WebIconDatabase;
119092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdochimport android.webkit.WebStorage;
1200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.WebView;
1210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.webkit.WebViewClient;
1220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.EditText;
1230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.FrameLayout;
1240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.LinearLayout;
1250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.TextView;
1260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.Toast;
1270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.io.BufferedOutputStream;
129b6b7f9ef3b4ed220063f555d8b7c205210a61a04Leon Scrogginsimport java.io.ByteArrayOutputStream;
1300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.io.File;
1310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.io.FileInputStream;
1320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.io.FileOutputStream;
1330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.io.IOException;
1340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.io.InputStream;
1350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.net.MalformedURLException;
1360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.net.URI;
1379918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackbornimport java.net.URISyntaxException;
1380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.net.URL;
1390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.net.URLEncoder;
1400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.text.ParseException;
1410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.Date;
1420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.Enumeration;
1430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.HashMap;
14437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scottimport java.util.LinkedList;
1450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.Vector;
1460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.regex.Matcher;
1470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.regex.Pattern;
1480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.zip.ZipEntry;
1490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport java.util.zip.ZipFile;
1500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectpublic class BrowserActivity extends Activity
1520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    implements KeyTracker.OnKeyTracker,
1530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        View.OnCreateContextMenuListener,
1540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        DownloadListener {
1550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15631a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort    /* Define some aliases to make these debugging flags easier to refer to.
15731a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort     * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
15831a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort     */
15931a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort    private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
16031a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort    private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
16131a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort    private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
16231a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort
1630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private IGoogleLoginService mGls = null;
1640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private ServiceConnection mGlsConnection = null;
1650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private SensorManager mSensorManager = null;
1670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
168565505b1a2c1d3099496af5910760087602e994aSatish Sampath    // These are single-character shortcuts for searching popular sources.
169565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private static final int SHORTCUT_INVALID = 0;
170565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private static final int SHORTCUT_GOOGLE_SEARCH = 1;
171565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
172565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
173565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
174565505b1a2c1d3099496af5910760087602e994aSatish Sampath
1750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* Whitelisted webpages
1760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private static HashSet<String> sWhiteList;
1770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    static {
1790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList = new HashSet<String>();
1800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("cnn.com/");
1810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("espn.go.com/");
1820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("nytimes.com/");
1830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("engadget.com/");
1840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("yahoo.com/");
1850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("msn.com/");
1860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("amazon.com/");
1870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("consumerist.com/");
1880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        sWhiteList.add("google.com/m/news");
1890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
1900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    */
1910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void setupHomePage() {
1930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final Runnable getAccount = new Runnable() {
1940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            public void run() {
1950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // Lower priority
1960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // get the default home page
1980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String homepage = mSettings.getHomePage();
1990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                try {
2010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (mGls == null) return;
2020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
203f2c5c1b555904400f4092888ae81b8ed74bfe026Grace Kloba                    if (!homepage.startsWith("http://www.google.")) return;
204f2c5c1b555904400f4092888ae81b8ed74bfe026Grace Kloba                    if (homepage.indexOf('?') == -1) return;
205f2c5c1b555904400f4092888ae81b8ed74bfe026Grace Kloba
2060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String hostedUser = mGls.getAccount(GoogleLoginServiceConstants.PREFER_HOSTED);
2070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String googleUser = mGls.getAccount(GoogleLoginServiceConstants.REQUIRE_GOOGLE);
2080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // three cases:
2100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //
2110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //   hostedUser == googleUser
2120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //      The device has only a google account
2130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //
2140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //   hostedUser != googleUser
2150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //      The device has a hosted account and a google account
2160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //
2170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //   hostedUser != null, googleUser == null
2180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //      The device has only a hosted account (so far)
2190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // developers might have no accounts at all
2210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (hostedUser == null) return;
2220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (googleUser == null || !hostedUser.equals(googleUser)) {
2240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        String domain = hostedUser.substring(hostedUser.lastIndexOf('@')+1);
225f2c5c1b555904400f4092888ae81b8ed74bfe026Grace Kloba                        homepage = homepage.replace("?", "/a/" + domain + "?");
2260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
2270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } catch (RemoteException ignore) {
2280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // Login service died; carry on
2290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } catch (RuntimeException ignore) {
2300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // Login service died; carry on
2310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } finally {
2320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    finish(homepage);
2330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
2340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
2350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            private void finish(final String homepage) {
2370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mHandler.post(new Runnable() {
2380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void run() {
2390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mSettings.setHomePage(BrowserActivity.this, homepage);
2400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        resumeAfterCredentials();
2410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // as this is running in a separate thread,
2430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // BrowserActivity's onDestroy() may have been called,
2440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // which also calls unbindService().
2450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (mGlsConnection != null) {
2460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // we no longer need to keep GLS open
2470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            unbindService(mGlsConnection);
2480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            mGlsConnection = null;
2490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
2500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    } });
2510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } };
2520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final boolean[] done = { false };
2540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Open a connection to the Google Login Service.  The first
2560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // time the connection is established, set up the homepage depending on
2570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // the account in a background thread.
2580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mGlsConnection = new ServiceConnection() {
2590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            public void onServiceConnected(ComponentName className, IBinder service) {
2600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mGls = IGoogleLoginService.Stub.asInterface(service);
2610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (done[0] == false) {
2620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    done[0] = true;
2630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    Thread account = new Thread(getAccount);
2640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    account.setName("GLSAccount");
2650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    account.start();
2660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
2670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
2680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            public void onServiceDisconnected(ComponentName className) {
2690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mGls = null;
2700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
2710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        };
2720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
2730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        bindService(GoogleLoginServiceConstants.SERVICE_INTENT,
2740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mGlsConnection, Context.BIND_AUTO_CREATE);
2750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
2760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
277a977124eb29b195127fd161120ebbc58b255fb48Cary Clark    private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
2780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
2790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public Void doInBackground(File... files) {
2800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (files != null) {
2810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                for (File f : files) {
282d6be1758b38d5b066ff10b6af906aa66c63670cdCary Clark                    if (!f.delete()) {
283d6be1758b38d5b066ff10b6af906aa66c63670cdCary Clark                      Log.e(LOGTAG, f.getPath() + " was not deleted");
284d6be1758b38d5b066ff10b6af906aa66c63670cdCary Clark                    }
2850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
2860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
2870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return null;
2880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
2890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
2900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29181db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins    // Flag to enable the touchable browser bar with buttons
29281db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins    private final boolean CUSTOM_BROWSER_BAR = true;
29381db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins
2940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override public void onCreate(Bundle icicle) {
29531a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort        if (LOGV_ENABLED) {
2960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.v(LOGTAG, this + " onStart");
2970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
2980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onCreate(icicle);
29981db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
30081db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
30181db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        } else {
30281db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
30381db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            this.requestWindowFeature(Window.FEATURE_RIGHT_ICON);
30481db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            this.requestWindowFeature(Window.FEATURE_PROGRESS);
30581db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
30681db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        }
3070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // test the browser in OpenGL
3080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // requestWindowFeature(Window.FEATURE_OPENGL);
3090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
3110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mResolver = getContentResolver();
3130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //
3150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // start MASF proxy service
3160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //
3170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //Intent proxyServiceIntent = new Intent();
3180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //proxyServiceIntent.setComponent
3190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //    (new ComponentName(
3200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //        "com.android.masfproxyservice",
3210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //        "com.android.masfproxyservice.MasfProxyService"));
3220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //startService(proxyServiceIntent, null);
3230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSecLockIcon = Resources.getSystem().getDrawable(
3250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                android.R.drawable.ic_secure);
3260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mMixLockIcon = Resources.getSystem().getDrawable(
3270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                android.R.drawable.ic_partial_secure);
3280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mGenericFavicon = getResources().getDrawable(
3290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                R.drawable.app_web_browser_sm);
3300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33181db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
33281db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins                .findViewById(com.android.internal.R.id.content);
33381db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
334adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // This FrameLayout will hold the custom FrameLayout and a LinearLayout
335adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // that contains the title bar and a FrameLayout, which
33681db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            // holds everything else.
337adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            FrameLayout browserFrameLayout = (FrameLayout) LayoutInflater.from(this)
338e4b3bda762ab616b2899c8de084e515f01b99c8aLeon Scroggins                    .inflate(R.layout.custom_screen, null);
3391f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            mTitleBar = (TitleBarSet) browserFrameLayout.findViewById(R.id.title_bar);
340adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mContentView = (FrameLayout) browserFrameLayout.findViewById(
341e4b3bda762ab616b2899c8de084e515f01b99c8aLeon Scroggins                    R.id.main_content);
342bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            mErrorConsoleContainer = (LinearLayout) browserFrameLayout.findViewById(
343bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                    R.id.error_console);
344adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomViewContainer = (FrameLayout) browserFrameLayout
345adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                    .findViewById(R.id.fullscreen_custom_content);
346adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            frameLayout.addView(browserFrameLayout, COVER_SCREEN_PARAMS);
34781db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        } else {
348adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomViewContainer = new FrameLayout(this);
34978f757042015063d016b15893a650c44fdfd4e5cAndrei Popescu            mCustomViewContainer.setBackgroundColor(Color.BLACK);
350adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mContentView = new FrameLayout(this);
351bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
352bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            LinearLayout linearLayout = new LinearLayout(this);
353bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            linearLayout.setOrientation(LinearLayout.VERTICAL);
354bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            mErrorConsoleContainer = new LinearLayout(this);
355bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            linearLayout.addView(mErrorConsoleContainer, new LinearLayout.LayoutParams(
356bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
357bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            linearLayout.addView(mContentView, COVER_SCREEN_PARAMS);
358adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            frameLayout.addView(mCustomViewContainer, COVER_SCREEN_PARAMS);
359bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            frameLayout.addView(linearLayout, COVER_SCREEN_PARAMS);
36081db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        }
3610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Create the tab control and our initial tab
3630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTabControl = new TabControl(this);
3640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Open the icon database and retain all the bookmark urls for favicons
3660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        retainIconsOnStartup();
3670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Keep a settings instance handy.
3690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSettings = BrowserSettings.getInstance();
3700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSettings.setTabControl(mTabControl);
3710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSettings.loadFromDb(this);
3720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
3740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
3750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
376a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba        /* enables registration for changes in network status from
377a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba           http stack */
378a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba        mNetworkStateChangedFilter = new IntentFilter();
379a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba        mNetworkStateChangedFilter.addAction(
380a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                ConnectivityManager.CONNECTIVITY_ACTION);
381a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba        mNetworkStateIntentReceiver = new BroadcastReceiver() {
382a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                @Override
383a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                public void onReceive(Context context, Intent intent) {
384a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                    if (intent.getAction().equals(
385a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                            ConnectivityManager.CONNECTIVITY_ACTION)) {
386a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                        boolean down = intent.getBooleanExtra(
387a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
388a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                        onNetworkToggle(!down);
389a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                    }
390a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba                }
391a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba            };
392a34f686112fb4f9fbea00b8c2c47153d34e93e41Grace Kloba
393615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
394615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
395615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba        filter.addDataScheme("package");
396615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba        mPackageInstallationReceiver = new BroadcastReceiver() {
397615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba            @Override
398615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba            public void onReceive(Context context, Intent intent) {
399615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                final String action = intent.getAction();
400615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                final String packageName = intent.getData()
401615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                        .getSchemeSpecificPart();
402615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                final boolean replacing = intent.getBooleanExtra(
403615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                        Intent.EXTRA_REPLACING, false);
404615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
405615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    // if it is replacing, refreshPlugins() when adding
406615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    return;
407615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                }
408615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                PackageManager pm = BrowserActivity.this.getPackageManager();
409615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                PackageInfo pkgInfo = null;
410615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                try {
411615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    pkgInfo = pm.getPackageInfo(packageName,
412615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                            PackageManager.GET_PERMISSIONS);
413615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                } catch (PackageManager.NameNotFoundException e) {
414615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    return;
415615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                }
416615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                if (pkgInfo != null) {
417615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    String permissions[] = pkgInfo.requestedPermissions;
418615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    if (permissions == null) {
419615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                        return;
420615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    }
421615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    boolean permissionOk = false;
422615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    for (String permit : permissions) {
423615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                        if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
424615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                            permissionOk = true;
425615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                            break;
426615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                        }
427615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    }
428615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    if (permissionOk) {
429615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                        PluginManager.getInstance(BrowserActivity.this)
430615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                                .refreshPlugins(
431615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                                        Intent.ACTION_PACKAGE_ADDED
432615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                                                .equals(action));
433615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                    }
434615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba                }
435615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba            }
436615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba        };
437615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba        registerReceiver(mPackageInstallationReceiver, filter);
438615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba
439565505b1a2c1d3099496af5910760087602e994aSatish Sampath        // If this was a web search request, pass it on to the default web search provider.
440565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (handleWebSearchIntent(getIntent())) {
441565505b1a2c1d3099496af5910760087602e994aSatish Sampath            moveTaskToBack(true);
442565505b1a2c1d3099496af5910760087602e994aSatish Sampath            return;
443565505b1a2c1d3099496af5910760087602e994aSatish Sampath        }
444565505b1a2c1d3099496af5910760087602e994aSatish Sampath
4450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (!mTabControl.restoreState(icicle)) {
4460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // clear up the thumbnail directory if we can't restore the state as
4470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // none of the files in the directory are referenced any more.
4480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new ClearThumbnails().execute(
4490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mTabControl.getThumbnailDir().listFiles());
450aab3f09b5c2d39b19067e57635614080b2403a46Grace Kloba            // there is no quit on Android. But if we can't restore the state,
451aab3f09b5c2d39b19067e57635614080b2403a46Grace Kloba            // we can treat it as a new Browser, remove the old session cookies.
452aab3f09b5c2d39b19067e57635614080b2403a46Grace Kloba            CookieManager.getInstance().removeSessionCookie();
4530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final Intent intent = getIntent();
4540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final Bundle extra = intent.getExtras();
4550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Create an initial tab.
4560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // If the intent is ACTION_VIEW and data is not null, the Browser is
4570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // invoked to view the content by another application. In this case,
4580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // the tab will be close when exit.
45925ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            UrlData urlData = getUrlDataFromIntent(intent);
46025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima
4610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final TabControl.Tab t = mTabControl.createNewTab(
4620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    Intent.ACTION_VIEW.equals(intent.getAction()) &&
463f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    intent.getData() != null,
46425ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                    intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
4650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mTabControl.setCurrentTab(t);
4660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            attachTabToContentView(t);
4670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            WebView webView = t.getWebView();
4680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (extra != null) {
4690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
4700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (scale > 0 && scale <= 1000) {
4710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    webView.setInitialScale(scale);
4720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
4730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
4740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // If we are not restoring from an icicle, then there is a high
4750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // likely hood this is the first run. So, check to see if the
4760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // homepage needs to be configured and copy any plugins from our
4770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // asset directory to the data partition.
4780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if ((extra == null || !extra.getBoolean("testing"))
4790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    && !mSettings.isLoginInitialized()) {
4800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                setupHomePage();
4810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
4820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
48325ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            if (urlData.isEmpty()) {
484160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                bookmarksOrHistoryPicker(false, true);
4850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
48681678d9216458266ea70cd62d52f28f744873556Grace Kloba                if (extra != null) {
48781678d9216458266ea70cd62d52f28f744873556Grace Kloba                    urlData.setPostData(extra
48881678d9216458266ea70cd62d52f28f744873556Grace Kloba                            .getByteArray(Browser.EXTRA_POST_DATA));
48981678d9216458266ea70cd62d52f28f744873556Grace Kloba                }
49025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                urlData.loadIn(webView);
4910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
4920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
4930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // TabControl.restoreState() will create a new tab even if
4941f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            // restoring the state fails.
4950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            attachTabToContentView(mTabControl.getCurrentTab());
4960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
497615c6c9130a1bd21c63cd37d09f8f4ea3229ca13Grace Kloba
4981f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
4991f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            // Create title bars for all of the tabs that have been created
5001f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            for (int i = 0; i < mTabControl.getTabCount(); i ++) {
5011f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                WebView view = mTabControl.getTab(i).getWebView();
5021f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                mTitleBar.addTab(view, false);
5031f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            }
5041f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins
5051f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            mTitleBar.setBrowserActivity(this);
5061f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            mTitleBar.setCurrentTab(mTabControl.getCurrentIndex());
5071f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        }
5081f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins
509b3c02da4553b1a629b16377970fdabae28394c94Feng Qian        // Read JavaScript flags if it exists.
510b3c02da4553b1a629b16377970fdabae28394c94Feng Qian        String jsFlags = mSettings.getJsFlags();
511b3c02da4553b1a629b16377970fdabae28394c94Feng Qian        if (jsFlags.trim().length() != 0) {
512b3c02da4553b1a629b16377970fdabae28394c94Feng Qian            mTabControl.getCurrentWebView().setJsFlags(jsFlags);
513b3c02da4553b1a629b16377970fdabae28394c94Feng Qian        }
5140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
5150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
5160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
5170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    protected void onNewIntent(Intent intent) {
5180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        TabControl.Tab current = mTabControl.getCurrentTab();
5190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // When a tab is closed on exit, the current tab index is set to -1.
5200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Reset before proceed as Browser requires the current tab to be set.
5210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (current == null) {
5220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Try to reset the tab in case the index was incorrect.
5230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            current = mTabControl.getTab(0);
5240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (current == null) {
5250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // No tabs at all so just ignore this intent.
5260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return;
5270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
5280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mTabControl.setCurrentTab(current);
5291f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            if (CUSTOM_BROWSER_BAR) {
5301f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                mTitleBar.setCurrentTab(mTabControl.getTabIndex(current));
5311f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            }
5320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            attachTabToContentView(current);
5330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            resetTitleAndIcon(current.getWebView());
5340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
5350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final String action = intent.getAction();
5360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final int flags = intent.getFlags();
5370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (Intent.ACTION_MAIN.equals(action) ||
5380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
5390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // just resume the browser
5400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
5410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
5420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (Intent.ACTION_VIEW.equals(action)
5430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                || Intent.ACTION_SEARCH.equals(action)
5440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
5450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                || Intent.ACTION_WEB_SEARCH.equals(action)) {
546565505b1a2c1d3099496af5910760087602e994aSatish Sampath            // If this was a search request (e.g. search query directly typed into the address bar),
547565505b1a2c1d3099496af5910760087602e994aSatish Sampath            // pass it on to the default web search provider.
548565505b1a2c1d3099496af5910760087602e994aSatish Sampath            if (handleWebSearchIntent(intent)) {
549565505b1a2c1d3099496af5910760087602e994aSatish Sampath                return;
550565505b1a2c1d3099496af5910760087602e994aSatish Sampath            }
551565505b1a2c1d3099496af5910760087602e994aSatish Sampath
55225ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            UrlData urlData = getUrlDataFromIntent(intent);
55325ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            if (urlData.isEmpty()) {
55425ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                urlData = new UrlData(mSettings.getHomePage());
5550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
55681678d9216458266ea70cd62d52f28f744873556Grace Kloba            urlData.setPostData(intent
55781678d9216458266ea70cd62d52f28f744873556Grace Kloba                    .getByteArrayExtra(Browser.EXTRA_POST_DATA));
55825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima
559cc63403643786905262f505ab5266d8e80b9924cGrace Kloba            final String appId = intent
560cc63403643786905262f505ab5266d8e80b9924cGrace Kloba                    .getStringExtra(Browser.EXTRA_APPLICATION_ID);
561cc63403643786905262f505ab5266d8e80b9924cGrace Kloba            if (Intent.ACTION_VIEW.equals(action)
562cc63403643786905262f505ab5266d8e80b9924cGrace Kloba                    && !getPackageName().equals(appId)
563cc63403643786905262f505ab5266d8e80b9924cGrace Kloba                    && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
564cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                TabControl.Tab appTab = mTabControl.getTabFromId(appId);
565f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                if (appTab != null) {
566f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    Log.i(LOGTAG, "Reusing tab for " + appId);
567f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    // Dismiss the subwindow if applicable.
568f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    dismissSubWindow(appTab);
569f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    // Since we might kill the WebView, remove it from the
570f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    // content view first.
571f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    removeTabFromContentView(appTab);
572f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    // Recreate the main WebView after destroying the old one.
573f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    // If the WebView has the same original url and is on that
574f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    // page, it can be reused.
575f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    boolean needsLoad =
57625ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                            mTabControl.recreateWebView(appTab, urlData.mUrl);
577bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
578f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    if (current != appTab) {
5791f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        switchToTab(mTabControl.getTabIndex(appTab));
5801f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        if (needsLoad) {
5811f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                            urlData.loadIn(appTab.getWebView());
5821f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        }
583f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    } else {
5841f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        // If the tab was the current tab, we have to attach
5851f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        // it to the view system again.
5861f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        attachTabToContentView(appTab);
5871f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        if (needsLoad) {
5881f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                            urlData.loadIn(appTab.getWebView());
589f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                        }
590f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    }
591f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                    return;
592cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                } else {
593cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                    // No matching application tab, try to find a regular tab
594cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                    // with a matching url.
595cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                    appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
59625515f8bd458a2471d13d876cfb0e18d8ff2a00fLeon Scroggins                    if (appTab != null) {
59725515f8bd458a2471d13d876cfb0e18d8ff2a00fLeon Scroggins                        if (current != appTab) {
59825515f8bd458a2471d13d876cfb0e18d8ff2a00fLeon Scroggins                            switchToTab(mTabControl.getTabIndex(appTab));
59925515f8bd458a2471d13d876cfb0e18d8ff2a00fLeon Scroggins                        }
60025515f8bd458a2471d13d876cfb0e18d8ff2a00fLeon Scroggins                        // Otherwise, we are already viewing the correct tab.
601cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                    } else {
602cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                        // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
603cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                        // will be opened in a new tab unless we have reached
604cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                        // MAX_TABS. Then the url will be opened in the current
605cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                        // tab. If a new tab is created, it will have "true" for
606cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                        // exit on close.
6071f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        openTabAndShow(urlData, true, appId);
608cd11589fc3930906d4b9b7dd18aa52a9f1eb0c8aPatrick Scott                    }
609f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project                }
6100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
61125ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                if ("about:debug".equals(urlData.mUrl)) {
6120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mSettings.toggleDebugSettings();
6130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    return;
6140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
6151f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                // Get rid of the subwindow if it exists
6161f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                dismissSubWindow(current);
6171f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                urlData.loadIn(current.getWebView());
6180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
6190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
6200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
6210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
622565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private int parseUrlShortcut(String url) {
623565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (url == null) return SHORTCUT_INVALID;
624565505b1a2c1d3099496af5910760087602e994aSatish Sampath
625565505b1a2c1d3099496af5910760087602e994aSatish Sampath        // FIXME: quick search, need to be customized by setting
626565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (url.length() > 2 && url.charAt(1) == ' ') {
627565505b1a2c1d3099496af5910760087602e994aSatish Sampath            switch (url.charAt(0)) {
628565505b1a2c1d3099496af5910760087602e994aSatish Sampath            case 'g': return SHORTCUT_GOOGLE_SEARCH;
629565505b1a2c1d3099496af5910760087602e994aSatish Sampath            case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
630565505b1a2c1d3099496af5910760087602e994aSatish Sampath            case 'd': return SHORTCUT_DICTIONARY_SEARCH;
631565505b1a2c1d3099496af5910760087602e994aSatish Sampath            case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
632565505b1a2c1d3099496af5910760087602e994aSatish Sampath            }
633565505b1a2c1d3099496af5910760087602e994aSatish Sampath        }
634565505b1a2c1d3099496af5910760087602e994aSatish Sampath        return SHORTCUT_INVALID;
635565505b1a2c1d3099496af5910760087602e994aSatish Sampath    }
636565505b1a2c1d3099496af5910760087602e994aSatish Sampath
637565505b1a2c1d3099496af5910760087602e994aSatish Sampath    /**
638565505b1a2c1d3099496af5910760087602e994aSatish Sampath     * Launches the default web search activity with the query parameters if the given intent's data
639565505b1a2c1d3099496af5910760087602e994aSatish Sampath     * are identified as plain search terms and not URLs/shortcuts.
640565505b1a2c1d3099496af5910760087602e994aSatish Sampath     * @return true if the intent was handled and web search activity was launched, false if not.
641565505b1a2c1d3099496af5910760087602e994aSatish Sampath     */
642565505b1a2c1d3099496af5910760087602e994aSatish Sampath    private boolean handleWebSearchIntent(Intent intent) {
643565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (intent == null) return false;
644565505b1a2c1d3099496af5910760087602e994aSatish Sampath
645565505b1a2c1d3099496af5910760087602e994aSatish Sampath        String url = null;
646565505b1a2c1d3099496af5910760087602e994aSatish Sampath        final String action = intent.getAction();
647565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (Intent.ACTION_VIEW.equals(action)) {
648565505b1a2c1d3099496af5910760087602e994aSatish Sampath            url = intent.getData().toString();
649565505b1a2c1d3099496af5910760087602e994aSatish Sampath        } else if (Intent.ACTION_SEARCH.equals(action)
650565505b1a2c1d3099496af5910760087602e994aSatish Sampath                || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
651565505b1a2c1d3099496af5910760087602e994aSatish Sampath                || Intent.ACTION_WEB_SEARCH.equals(action)) {
652565505b1a2c1d3099496af5910760087602e994aSatish Sampath            url = intent.getStringExtra(SearchManager.QUERY);
653565505b1a2c1d3099496af5910760087602e994aSatish Sampath        }
65415e9f2dc92cf1621f95071b3c740f46079aa0f3bSatish Sampath        return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA));
655565505b1a2c1d3099496af5910760087602e994aSatish Sampath    }
656565505b1a2c1d3099496af5910760087602e994aSatish Sampath
657565505b1a2c1d3099496af5910760087602e994aSatish Sampath    /**
658565505b1a2c1d3099496af5910760087602e994aSatish Sampath     * Launches the default web search activity with the query parameters if the given url string
659565505b1a2c1d3099496af5910760087602e994aSatish Sampath     * was identified as plain search terms and not URL/shortcut.
660565505b1a2c1d3099496af5910760087602e994aSatish Sampath     * @return true if the request was handled and web search activity was launched, false if not.
661565505b1a2c1d3099496af5910760087602e994aSatish Sampath     */
66215e9f2dc92cf1621f95071b3c740f46079aa0f3bSatish Sampath    private boolean handleWebSearchRequest(String inUrl, Bundle appData) {
663565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (inUrl == null) return false;
664565505b1a2c1d3099496af5910760087602e994aSatish Sampath
665565505b1a2c1d3099496af5910760087602e994aSatish Sampath        // In general, we shouldn't modify URL from Intent.
666565505b1a2c1d3099496af5910760087602e994aSatish Sampath        // But currently, we get the user-typed URL from search box as well.
667565505b1a2c1d3099496af5910760087602e994aSatish Sampath        String url = fixUrl(inUrl).trim();
668565505b1a2c1d3099496af5910760087602e994aSatish Sampath
669565505b1a2c1d3099496af5910760087602e994aSatish Sampath        // URLs and site specific search shortcuts are handled by the regular flow of control, so
670565505b1a2c1d3099496af5910760087602e994aSatish Sampath        // return early.
671565505b1a2c1d3099496af5910760087602e994aSatish Sampath        if (Regex.WEB_URL_PATTERN.matcher(url).matches()
672bc5b9f358a2b75bf3a104b9f5cd477ad2f6f740fSatish Sampath                || ACCEPTED_URI_SCHEMA.matcher(url).matches()
673565505b1a2c1d3099496af5910760087602e994aSatish Sampath                || parseUrlShortcut(url) != SHORTCUT_INVALID) {
674565505b1a2c1d3099496af5910760087602e994aSatish Sampath            return false;
675565505b1a2c1d3099496af5910760087602e994aSatish Sampath        }
676565505b1a2c1d3099496af5910760087602e994aSatish Sampath
677565505b1a2c1d3099496af5910760087602e994aSatish Sampath        Browser.updateVisitedHistory(mResolver, url, false);
678565505b1a2c1d3099496af5910760087602e994aSatish Sampath        Browser.addSearchUrl(mResolver, url);
679565505b1a2c1d3099496af5910760087602e994aSatish Sampath
680565505b1a2c1d3099496af5910760087602e994aSatish Sampath        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
681565505b1a2c1d3099496af5910760087602e994aSatish Sampath        intent.addCategory(Intent.CATEGORY_DEFAULT);
682565505b1a2c1d3099496af5910760087602e994aSatish Sampath        intent.putExtra(SearchManager.QUERY, url);
68315e9f2dc92cf1621f95071b3c740f46079aa0f3bSatish Sampath        if (appData != null) {
68415e9f2dc92cf1621f95071b3c740f46079aa0f3bSatish Sampath            intent.putExtra(SearchManager.APP_DATA, appData);
68515e9f2dc92cf1621f95071b3c740f46079aa0f3bSatish Sampath        }
686cc63403643786905262f505ab5266d8e80b9924cGrace Kloba        intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
687565505b1a2c1d3099496af5910760087602e994aSatish Sampath        startActivity(intent);
688565505b1a2c1d3099496af5910760087602e994aSatish Sampath
689565505b1a2c1d3099496af5910760087602e994aSatish Sampath        return true;
690565505b1a2c1d3099496af5910760087602e994aSatish Sampath    }
691565505b1a2c1d3099496af5910760087602e994aSatish Sampath
69225ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    private UrlData getUrlDataFromIntent(Intent intent) {
6930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String url = null;
6940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (intent != null) {
6950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final String action = intent.getAction();
6960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (Intent.ACTION_VIEW.equals(action)) {
6970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url = smartUrlFilter(intent.getData());
6980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (url != null && url.startsWith("content:")) {
6990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    /* Append mimetype so webview knows how to display */
7000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String mimeType = intent.resolveType(getContentResolver());
7010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (mimeType != null) {
7020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        url += "?" + mimeType;
7030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
7040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
70525ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                if ("inline:".equals(url)) {
70625ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                    return new InlinedUrlData(
70725ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                            intent.getStringExtra(Browser.EXTRA_INLINE_CONTENT),
70825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                            intent.getType(),
70925ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                            intent.getStringExtra(Browser.EXTRA_INLINE_ENCODING),
71025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                            intent.getStringExtra(Browser.EXTRA_INLINE_FAILURL));
71125ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima                }
7120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else if (Intent.ACTION_SEARCH.equals(action)
7130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
7140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    || Intent.ACTION_WEB_SEARCH.equals(action)) {
7150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url = intent.getStringExtra(SearchManager.QUERY);
7160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (url != null) {
7170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mLastEnteredUrl = url;
7180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // Don't add Urls, just search terms.
7190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // Urls will get added when the page is loaded.
7200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (!Regex.WEB_URL_PATTERN.matcher(url).matches()) {
7210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        Browser.updateVisitedHistory(mResolver, url, false);
7220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
7230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // In general, we shouldn't modify URL from Intent.
7240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // But currently, we get the user-typed URL from search box as well.
7250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    url = fixUrl(url);
7260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    url = smartUrlFilter(url);
7270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
7280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (url.contains(searchSource)) {
7290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        String source = null;
7300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
7310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (appData != null) {
7320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            source = appData.getString(SearchManager.SOURCE);
7330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
7340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (TextUtils.isEmpty(source)) {
7350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
7360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
7370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        url = url.replace(searchSource, "&source=android-"+source+"&");
7380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
7390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
7400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
7410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
74225ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        return new UrlData(url);
7430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
7440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
7450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ static String fixUrl(String inUrl) {
7460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
7470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return inUrl;
7480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (inUrl.startsWith("http:") ||
7490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                inUrl.startsWith("https:")) {
7500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
7510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                inUrl = inUrl.replaceFirst("/", "//");
7520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else inUrl = inUrl.replaceFirst(":", "://");
7530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
7540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return inUrl;
7550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
7560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
7570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
7580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Looking for the pattern like this
7590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
7600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *          *
7610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *         * *
7620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *      ***   *     *******
7630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *             *   *
7640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *              * *
7650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *               *
7660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
7670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final SensorListener mSensorListener = new SensorListener() {
7680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private long mLastGestureTime;
7690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private float[] mPrev = new float[3];
7700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private float[] mPrevDiff = new float[3];
7710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private float[] mDiff = new float[3];
7720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private float[] mRevertDiff = new float[3];
7730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
7740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onSensorChanged(int sensor, float[] values) {
7750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            boolean show = false;
7760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            float[] diff = new float[3];
7770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
7780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            for (int i = 0; i < 3; i++) {
7790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                diff[i] = values[i] - mPrev[i];
7800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (Math.abs(diff[i]) > 1) {
7810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    show = true;
7820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
7830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if ((diff[i] > 1.0 && mDiff[i] < 0.2)
7840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        || (diff[i] < -1.0 && mDiff[i] > -0.2)) {
7850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // start track when there is a big move, or revert
7860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mRevertDiff[i] = mDiff[i];
7870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mDiff[i] = 0;
7880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } else if (diff[i] > -0.2 && diff[i] < 0.2) {
7890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // reset when it is flat
7900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mDiff[i] = mRevertDiff[i]  = 0;
7910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
7920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mDiff[i] += diff[i];
7930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mPrevDiff[i] = diff[i];
7940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mPrev[i] = values[i];
7950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
7960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
7970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (false) {
7980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // only shows if we think the delta is big enough, in an attempt
7990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // to detect "serious" moves left/right or up/down
8000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Log.d("BrowserSensorHack", "sensorChanged " + sensor + " ("
8010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + values[0] + ", " + values[1] + ", " + values[2] + ")"
8020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + " diff(" + diff[0] + " " + diff[1] + " " + diff[2]
8030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + ")");
8040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Log.d("BrowserSensorHack", "      mDiff(" + mDiff[0] + " "
8050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + mDiff[1] + " " + mDiff[2] + ")" + " mRevertDiff("
8060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + mRevertDiff[0] + " " + mRevertDiff[1] + " "
8070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + mRevertDiff[2] + ")");
8080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
8090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            long now = android.os.SystemClock.uptimeMillis();
8110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (now - mLastGestureTime > 1000) {
8120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mLastGestureTime = 0;
8130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float y = mDiff[1];
8150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float z = mDiff[2];
8160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float ay = Math.abs(y);
8170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float az = Math.abs(z);
8180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float ry = mRevertDiff[1];
8190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float rz = mRevertDiff[2];
8200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float ary = Math.abs(ry);
8210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                float arz = Math.abs(rz);
8220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean gestY = ay > 2.5f && ary > 1.0f && ay > ary;
8230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean gestZ = az > 3.5f && arz > 1.0f && az > arz;
8240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if ((gestY || gestZ) && !(gestY && gestZ)) {
8260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    WebView view = mTabControl.getCurrentWebView();
8270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (view != null) {
8290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (gestZ) {
8300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            if (z < 0) {
8310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                view.zoomOut();
8320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            } else {
8330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                view.zoomIn();
8340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
8350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        } else {
8360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            view.flingScroll(0, Math.round(y * 100));
8370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
8380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
8390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mLastGestureTime = now;
8400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
8410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
8420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onAccuracyChanged(int sensor, int accuracy) {
8450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // TODO Auto-generated method stub
8460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    };
8490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override protected void onResume() {
8510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onResume();
85231a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort        if (LOGV_ENABLED) {
8530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
8540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (!mActivityInPause) {
8570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.e(LOGTAG, "BrowserActivity is already resumed.");
8580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
8590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8617bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed        mTabControl.resumeCurrentTab();
8620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mActivityInPause = false;
8637bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed        resumeWebViewTimers();
8640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mWakeLock.isHeld()) {
8660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mHandler.removeMessages(RELEASE_WAKELOCK);
8670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mWakeLock.release();
8680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mCredsDlg != null) {
8710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (!mHandler.hasMessages(CANCEL_CREDS_REQUEST)) {
8720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             // In case credential request never comes back
8730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mHandler.sendEmptyMessageDelayed(CANCEL_CREDS_REQUEST, 6000);
8740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
8750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        registerReceiver(mNetworkStateIntentReceiver,
8780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                         mNetworkStateChangedFilter);
8790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView.enablePlatformNotifications();
8800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mSettings.doFlick()) {
8820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mSensorManager == null) {
8830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mSensorManager = (SensorManager) getSystemService(
8840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        Context.SENSOR_SERVICE);
8850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
8860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mSensorManager.registerListener(mSensorListener,
8870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    SensorManager.SENSOR_ACCELEROMETER,
8880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    SensorManager.SENSOR_DELAY_FASTEST);
8890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
8900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mSensorManager = null;
8910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
8920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
8930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
8940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
8950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  onSaveInstanceState(Bundle map)
8960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  onSaveInstanceState is called right before onStop(). The map contains
8970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  the saved state.
8980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
8990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override protected void onSaveInstanceState(Bundle outState) {
90031a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort        if (LOGV_ENABLED) {
9010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
9020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // the default implementation requires each view to have an id. As the
9040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // browser handles the state itself and it doesn't use id for the views,
9050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // don't call the default implementation. Otherwise it will trigger the
9060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // warning like this, "couldn't save which view has focus because the
9070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // focused view XXX has no id".
9080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Save all the tabs
9100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTabControl.saveState(outState);
9110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
9120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override protected void onPause() {
9140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onPause();
9150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mActivityInPause) {
9170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.e(LOGTAG, "BrowserActivity is already paused.");
9180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
9190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9217bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed        mTabControl.pauseCurrentTab();
9220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mActivityInPause = true;
9237bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed        if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
9240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mWakeLock.acquire();
9250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mHandler.sendMessageDelayed(mHandler
9260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
9270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Clear the credentials toast if it is up
9300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mCredsDlg != null && mCredsDlg.isShowing()) {
9310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mCredsDlg.dismiss();
9320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg = null;
9340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        cancelStopToast();
9360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // unregister network state listener
9380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        unregisterReceiver(mNetworkStateIntentReceiver);
9390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView.disablePlatformNotifications();
9400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mSensorManager != null) {
9420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mSensorManager.unregisterListener(mSensorListener);
9430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
9450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override protected void onDestroy() {
94731a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort        if (LOGV_ENABLED) {
9480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
9490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onDestroy();
9510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Remove the current tab and sub window
9520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        TabControl.Tab t = mTabControl.getCurrentTab();
953fb5e77fac7b8b311fe72827183f8988f5ee92a5fPatrick Scott        if (t != null) {
954fb5e77fac7b8b311fe72827183f8988f5ee92a5fPatrick Scott            dismissSubWindow(t);
955fb5e77fac7b8b311fe72827183f8988f5ee92a5fPatrick Scott            removeTabFromContentView(t);
956fb5e77fac7b8b311fe72827183f8988f5ee92a5fPatrick Scott        }
9570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Destroy all the tabs
9580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTabControl.destroy();
9590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebIconDatabase.getInstance().close();
9600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mGlsConnection != null) {
9610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            unbindService(mGlsConnection);
9620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mGlsConnection = null;
9630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //
9660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // stop MASF proxy service
9670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //
9680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //Intent proxyServiceIntent = new Intent();
9690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //proxyServiceIntent.setComponent
9700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //   (new ComponentName(
9710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //        "com.android.masfproxyservice",
9720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //        "com.android.masfproxyservice.MasfProxyService"));
9730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //stopService(proxyServiceIntent);
974b4da0ad0351996f639b50f9f29b123a9b5c24fd0Grace Kloba
975b4da0ad0351996f639b50f9f29b123a9b5c24fd0Grace Kloba        unregisterReceiver(mPackageInstallationReceiver);
9760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
9770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
9790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void onConfigurationChanged(Configuration newConfig) {
9800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onConfigurationChanged(newConfig);
9810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
9820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mPageInfoDialog != null) {
9830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mPageInfoDialog.dismiss();
9840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            showPageInfo(
9850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mPageInfoView,
9860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mPageInfoFromShowSSLCertificateOnError.booleanValue());
9870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mSSLCertificateDialog != null) {
9890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mSSLCertificateDialog.dismiss();
9900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            showSSLCertificate(
9910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mSSLCertificateView);
9920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
9930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mSSLCertificateOnErrorDialog != null) {
9940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mSSLCertificateOnErrorDialog.dismiss();
9950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            showSSLCertificateOnError(
9960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mSSLCertificateOnErrorView,
9970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mSSLCertificateOnErrorHandler,
9980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mSSLCertificateOnErrorError);
9990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mHttpAuthenticationDialog != null) {
10010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String title = ((TextView) mHttpAuthenticationDialog
10020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .findViewById(com.android.internal.R.id.alertTitle)).getText()
10030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .toString();
10040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String name = ((TextView) mHttpAuthenticationDialog
10050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .findViewById(R.id.username_edit)).getText().toString();
10060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String password = ((TextView) mHttpAuthenticationDialog
10070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .findViewById(R.id.password_edit)).getText().toString();
10080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            int focusId = mHttpAuthenticationDialog.getCurrentFocus()
10090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .getId();
10100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mHttpAuthenticationDialog.dismiss();
10110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            showHttpAuthentication(mHttpAuthHandler, null, null, title,
10120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    name, password, focusId);
10130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mFindDialog != null && mFindDialog.isShowing()) {
10150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mFindDialog.onConfigurationChanged(newConfig);
10160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
10180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override public void onLowMemory() {
10200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onLowMemory();
10210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTabControl.freeMemory();
10220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
10230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10247bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed    private boolean resumeWebViewTimers() {
10250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if ((!mActivityInPause && !mPageStarted) ||
10260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                (mActivityInPause && mPageStarted)) {
10270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            CookieSyncManager.getInstance().startSync();
10280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            WebView w = mTabControl.getCurrentWebView();
10290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (w != null) {
10300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                w.resumeTimers();
10310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
10320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return true;
10330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
10340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return false;
10350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
10370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10387bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed    private boolean pauseWebViewTimers() {
10390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mActivityInPause && !mPageStarted) {
10400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            CookieSyncManager.getInstance().stopSync();
10410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            WebView w = mTabControl.getCurrentWebView();
10420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (w != null) {
10430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                w.pauseTimers();
10440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
10450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return true;
10460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
10470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return false;
10480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
10500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10511f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    // FIXME: Do we want to call this when loading google for the first time?
10520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /*
10530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * This function is called when we are launching for the first time. We
10540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * are waiting for the login credentials before loading Google home
10550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * pages. This way the user will be logged in straight away.
10560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
10570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void waitForCredentials() {
10580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Show a toast
10590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg = new ProgressDialog(this);
10600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg.setIndeterminate(true);
10610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg.setMessage(getText(R.string.retrieving_creds_dlg_msg));
10620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // If the user cancels the operation, then cancel the Google
10630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Credentials request.
10640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg.setCancelMessage(mHandler.obtainMessage(CANCEL_CREDS_REQUEST));
10650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg.show();
10660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // We set a timeout for the retrieval of credentials in onResume()
10680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // as that is when we have freed up some CPU time to get
10690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // the login credentials.
10700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
10710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /*
10730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * If we have received the credentials or we have timed out and we are
10740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * showing the credentials dialog, then it is time to move on.
10750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
10760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void resumeAfterCredentials() {
10770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mCredsDlg == null) {
10780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
10790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Clear the toast
10820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mCredsDlg.isShowing()) {
10830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mCredsDlg.dismiss();
10840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCredsDlg = null;
10860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Clear any pending timeout
10880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mHandler.removeMessages(CANCEL_CREDS_REQUEST);
10890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Load the page
10910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView w = mTabControl.getCurrentWebView();
10920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (w != null) {
10930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            w.loadUrl(mSettings.getHomePage());
10940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
10950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
10960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Update the settings, need to do this last as it can take a moment
10970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // to persist the settings. In the mean time we could be loading
10980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // content.
10990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSettings.setLoginInitialized(this);
11000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Open the icon database and retain all the icons for visited sites.
11030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void retainIconsOnStartup() {
11040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final WebIconDatabase db = WebIconDatabase.getInstance();
11050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        db.open(getDir("icons", 0).getPath());
11060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        try {
11070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Cursor c = Browser.getAllBookmarks(mResolver);
11080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (!c.moveToFirst()) {
11090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                c.deactivate();
11100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return;
11110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
11120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
11130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            do {
11140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String url = c.getString(urlIndex);
11150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                db.retainIconForPageUrl(url);
11160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } while (c.moveToNext());
11170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            c.deactivate();
11180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } catch (IllegalStateException e) {
11190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.e(LOGTAG, "retainIconsOnStartup", e);
11200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
11210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Helper method for getting the top window.
11240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    WebView getTopWindow() {
11250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return mTabControl.getCurrentTopWebView();
11260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
11290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public boolean onCreateOptionsMenu(Menu menu) {
11300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onCreateOptionsMenu(menu);
11310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        MenuInflater inflater = getMenuInflater();
11330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        inflater.inflate(R.menu.browser, menu);
11340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mMenu = menu;
11350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        updateInLoadMenuItems();
11360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return true;
11370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
11400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * As the menu can be open when loading state changes
11410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * we must manually update the state of the stop/reload menu
11420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * item
11430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
11440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void updateInLoadMenuItems() {
11450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mMenu == null) {
11460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
11470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
11480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        MenuItem src = mInLoad ?
11490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mMenu.findItem(R.id.stop_menu_id):
11500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mMenu.findItem(R.id.reload_menu_id);
11510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
11520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        dest.setIcon(src.getIcon());
11530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        dest.setTitle(src.getTitle());
11540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
11570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public boolean onContextItemSelected(MenuItem item) {
11580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // chording is not an issue with context menus, but we use the same
11590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // options selector, so set mCanChord to true so we can access them.
11600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCanChord = true;
11610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        int id = item.getItemId();
11620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final WebView webView = getTopWindow();
11630d7ae0ed82e21d472cde8cf92b5657d8ee8ca0d8Leon Scroggins        if (null == webView) {
11640d7ae0ed82e21d472cde8cf92b5657d8ee8ca0d8Leon Scroggins            return false;
11650d7ae0ed82e21d472cde8cf92b5657d8ee8ca0d8Leon Scroggins        }
11660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final HashMap hrefMap = new HashMap();
11670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        hrefMap.put("webview", webView);
11680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final Message msg = mHandler.obtainMessage(
11690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                FOCUS_NODE_HREF, id, 0, hrefMap);
11700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        switch (id) {
11710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // -- Browser context menu
11720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.open_context_menu_id:
11730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.open_newtab_context_menu_id:
11740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.bookmark_context_menu_id:
11750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.save_link_context_menu_id:
11760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.share_link_context_menu_id:
11770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.copy_link_context_menu_id:
11780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                webView.requestFocusNodeHref(msg);
11790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
11800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            default:
11820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // For other context menus
11830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return onOptionsItemSelected(item);
11840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
11850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCanChord = false;
11860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return true;
11870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Bundle createGoogleSearchSourceBundle(String source) {
11900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        Bundle bundle = new Bundle();
11910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        bundle.putString(SearchManager.SOURCE, source);
11920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return bundle;
11930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
11940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
11950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
11964e5f58704099c37186d4825c3d55ea5b0527867fThe Android Open Source Project     * Overriding this to insert a local information bundle
11970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
11980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
11990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public boolean onSearchRequested() {
12005bbe98025a2c606a35c91b826760020890daadb1Leon Scroggins        String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
120183f473462fcd7869031a423c02b957c6e938a2f6Grace Kloba        startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
12024e5f58704099c37186d4825c3d55ea5b0527867fThe Android Open Source Project                createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_SEARCHKEY), false);
12030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return true;
12040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
12050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
12060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
12070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void startSearch(String initialQuery, boolean selectInitialQuery,
12080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Bundle appSearchData, boolean globalSearch) {
12090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (appSearchData == null) {
12100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
12110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
12120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
12130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
12140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
12151f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    /**
12161f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins     * Switch tabs.  Called by the TitleBarSet when sliding the title bar
12171f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins     * results in changing tabs.
1218160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     * @param index Index of the tab to change to, as defined by
1219160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     *              mTabControl.getTabIndex(Tab t).
1220160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     * @return boolean True if we successfully switched to a different tab.  If
1221160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     *                 the indexth tab is null, or if that tab is the same as
1222160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     *                 the current one, return false.
12231f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins     */
1224160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins    /* package */ boolean switchToTab(int index) {
12251f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        TabControl.Tab tab = mTabControl.getTab(index);
12261f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        TabControl.Tab currentTab = mTabControl.getCurrentTab();
12271f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (tab == null || tab == currentTab) {
1228160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            return false;
12291f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        }
12301f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (currentTab != null) {
12311f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            // currentTab may be null if it was just removed.  In that case,
12321f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            // we do not need to remove it
12331f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            removeTabFromContentView(currentTab);
12341f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        }
12351f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        removeTabFromContentView(tab);
12361f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        mTabControl.setCurrentTab(tab);
12371f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        attachTabToContentView(tab);
1238a11b75a4ef9cb8e264e59e3d2aadd56cf62e2afeLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
1239a11b75a4ef9cb8e264e59e3d2aadd56cf62e2afeLeon Scroggins            mTitleBar.setCurrentTab(index);
1240416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            WebView view = tab.getWebView();
1241416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            view.slideIntoFocus();
1242a11b75a4ef9cb8e264e59e3d2aadd56cf62e2afeLeon Scroggins        }
1243160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        return true;
12441f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    }
12451f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins
12461f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    /* package */ void closeCurrentWindow() {
12471f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        final TabControl.Tab current = mTabControl.getCurrentTab();
1248160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        if (mTabControl.getTabCount() == 1) {
1249160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            // This is the last tab.  Open a new one, as well as the history
1250160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            // picker, and close the current one.
1251160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            TabControl.Tab newTab = openTabAndShow(
1252160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                    BrowserActivity.EMPTY_URL_DATA, false, null);
1253160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            bookmarksOrHistoryPicker(false, true);
1254160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            closeTab(current);
1255160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            mTabControl.setCurrentTab(newTab);
1256160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            return;
1257160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        }
12581f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        final TabControl.Tab parent = current.getParentTab();
12591f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        int indexToShow = -1;
12601f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (parent != null) {
12611f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            indexToShow = mTabControl.getTabIndex(parent);
12621f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        } else {
1263160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            final int currentIndex = mTabControl.getCurrentIndex();
1264160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            // Try to move to the tab to the right
1265160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            indexToShow = currentIndex + 1;
1266160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            if (indexToShow > mTabControl.getTabCount() - 1) {
1267160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                // Try to move to the tab to the left
1268160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                indexToShow = currentIndex - 1;
12691f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            }
12701f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        }
1271160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        if (switchToTab(indexToShow)) {
1272160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            // Close window
1273160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            closeTab(current);
1274160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        }
12751f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    }
12761f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins
12770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
12780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
12790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (!mCanChord) {
12800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // The user has already fired a shortcut with this hold down of the
12810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // menu key.
12820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return false;
12830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
12841f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (null == getTopWindow()) {
12850d7ae0ed82e21d472cde8cf92b5657d8ee8ca0d8Leon Scroggins            return false;
12860d7ae0ed82e21d472cde8cf92b5657d8ee8ca0d8Leon Scroggins        }
12876ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba        if (mMenuIsDown) {
12886ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // The shortcut action consumes the MENU. Even if it is still down,
12896ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // it won't trigger the next shortcut action. In the case of the
12906ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // shortcut action triggering a new activity, like Bookmarks, we
12916ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // won't get onKeyUp for MENU. So it is important to reset it here.
12926ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            mMenuIsDown = false;
12936ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba        }
12940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        switch (item.getItemId()) {
12950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // -- Main menu
129664b80f3efd95d7b5192731e995ccde79c4c6c11fLeon Scroggins            case R.id.goto_menu_id:
1297160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                bookmarksOrHistoryPicker(false, false);
12980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
12990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13001f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            case R.id.add_bookmark_menu_id:
13011f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                Intent i = new Intent(BrowserActivity.this,
13021f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        AddBookmarkPage.class);
13031f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                WebView w = getTopWindow();
13041f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                i.putExtra("url", w.getUrl());
13051f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                i.putExtra("title", w.getTitle());
13061f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                startActivity(i);
13070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.stop_reload_menu_id:
13100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (mInLoad) {
13110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    stopLoading();
13120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } else {
13130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    getTopWindow().reload();
13140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
13150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.back_menu_id:
13180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getTopWindow().goBack();
13190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.forward_menu_id:
13220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getTopWindow().goForward();
13230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.close_menu_id:
13260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // Close the subwindow if it exists.
13270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (mTabControl.getCurrentSubWindow() != null) {
13280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    dismissSubWindow(mTabControl.getCurrentTab());
13290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
13300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
13311f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                closeCurrentWindow();
13320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.homepage_menu_id:
13350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                TabControl.Tab current = mTabControl.getCurrentTab();
13360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (current != null) {
13370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    dismissSubWindow(current);
13380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    current.getWebView().loadUrl(mSettings.getHomePage());
13390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
13400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.preferences_menu_id:
13430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Intent intent = new Intent(this,
13440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        BrowserPreferencesPage.class);
13450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                startActivityForResult(intent, PREFERENCES_PAGE);
13460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.find_menu_id:
13490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (null == mFindDialog) {
13500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mFindDialog = new FindDialog(this);
13510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
13520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mFindDialog.setWebView(getTopWindow());
13530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mFindDialog.show();
13540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mMenuState = EMPTY_MENU;
13550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.select_text_id:
13580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getTopWindow().emulateShiftHeld();
13590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.page_info_menu_id:
13610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                showPageInfo(mTabControl.getCurrentTab(), false);
13620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.classic_history_menu_id:
1365160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                bookmarksOrHistoryPicker(true, false);
13660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.share_page_menu_id:
13690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Browser.sendString(this, getTopWindow().getUrl());
13700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.dump_nav_menu_id:
13730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getTopWindow().debugDump();
13740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.zoom_in_menu_id:
13770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getTopWindow().zoomIn();
13780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.zoom_out_menu_id:
13810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getTopWindow().zoomOut();
13820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.view_downloads_menu_id:
13850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                viewDownloads(null);
13860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
13870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
13880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_one_menu_id:
13890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_two_menu_id:
13900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_three_menu_id:
13910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_four_menu_id:
13920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_five_menu_id:
13930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_six_menu_id:
13940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_seven_menu_id:
13950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case R.id.window_eight_menu_id:
13960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                {
13970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    int menuid = item.getItemId();
13980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
13990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
14000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            TabControl.Tab desiredTab = mTabControl.getTab(id);
14010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            if (desiredTab != null &&
14020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    desiredTab != mTabControl.getCurrentTab()) {
14031f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                                switchToTab(id);
14040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
14050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
14060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
14070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
14080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
14090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
14100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            default:
14120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (!super.onOptionsItemSelected(item)) {
14130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    return false;
14140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
14150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // Otherwise fall through.
14160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
14170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCanChord = false;
14180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return true;
14190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
14200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void closeFind() {
14220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mMenuState = R.id.MAIN_MENU;
14230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
14240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override public boolean onPrepareOptionsMenu(Menu menu)
14260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    {
14270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // This happens when the user begins to hold down the menu key, so
14280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // allow them to chord to get a shortcut.
14290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCanChord = true;
14300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Note: setVisible will decide whether an item is visible; while
14310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // setEnabled() will decide whether an item is enabled, which also means
14320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // whether the matching shortcut key will function.
14330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super.onPrepareOptionsMenu(menu);
14340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        switch (mMenuState) {
14350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case EMPTY_MENU:
14360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (mCurrentMenuState != mMenuState) {
14370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setGroupVisible(R.id.MAIN_MENU, false);
14380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setGroupEnabled(R.id.MAIN_MENU, false);
14390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
14400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
14410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
14420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            default:
14430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (mCurrentMenuState != mMenuState) {
14440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setGroupVisible(R.id.MAIN_MENU, true);
14450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setGroupEnabled(R.id.MAIN_MENU, true);
14460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
14470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
14480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final WebView w = getTopWindow();
14490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean canGoBack = false;
14500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean canGoForward = false;
14510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean isHome = false;
14520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (w != null) {
14530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    canGoBack = w.canGoBack();
14540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    canGoForward = w.canGoForward();
14550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    isHome = mSettings.getHomePage().equals(w.getUrl());
14560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
14570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final MenuItem back = menu.findItem(R.id.back_menu_id);
14580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                back.setEnabled(canGoBack);
14590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final MenuItem home = menu.findItem(R.id.homepage_menu_id);
14610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                home.setEnabled(!isHome);
14620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.forward_menu_id)
14640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setEnabled(canGoForward);
14650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // decide whether to show the share link option
14670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                PackageManager pm = getPackageManager();
14680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Intent send = new Intent(Intent.ACTION_SEND);
14690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                send.setType("text/plain");
14700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
14710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
14720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean isNavDump = mSettings.isNavDump();
14740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
14750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                nav.setVisible(isNavDump);
14760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                nav.setEnabled(isNavDump);
14770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
14780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
14790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mCurrentMenuState = mMenuState;
14800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return true;
14810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
14820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
14840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void onCreateContextMenu(ContextMenu menu, View v,
14850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ContextMenuInfo menuInfo) {
14860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView webview = (WebView) v;
14870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView.HitTestResult result = webview.getHitTestResult();
14880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (result == null) {
14890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
14900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
14910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
14920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        int type = result.getType();
14930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
14940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.w(LOGTAG,
14950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    "We should not show context menu when nothing is touched");
14960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
14970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
14980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
14990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // let TextView handles context menu
15000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
15010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
15020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Note, http://b/issue?id=1106666 is requesting that
15040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // an inflated menu can be used again. This is not available
15050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // yet, so inflate each time (yuk!)
15060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        MenuInflater inflater = getMenuInflater();
15070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        inflater.inflate(R.menu.browsercontext, menu);
15080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Show the correct menu group
15100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String extra = result.getExtra();
15110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        menu.setGroupVisible(R.id.PHONE_MENU,
15120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                type == WebView.HitTestResult.PHONE_TYPE);
15130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        menu.setGroupVisible(R.id.EMAIL_MENU,
15140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                type == WebView.HitTestResult.EMAIL_TYPE);
15150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        menu.setGroupVisible(R.id.GEO_MENU,
15160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                type == WebView.HitTestResult.GEO_TYPE);
15170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        menu.setGroupVisible(R.id.IMAGE_MENU,
15180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                type == WebView.HitTestResult.IMAGE_TYPE
15190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
15200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        menu.setGroupVisible(R.id.ANCHOR_MENU,
15210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                type == WebView.HitTestResult.SRC_ANCHOR_TYPE
15220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
15230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Setup custom handling depending on the type
15250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        switch (type) {
15260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case WebView.HitTestResult.PHONE_TYPE:
15270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.setHeaderTitle(Uri.decode(extra));
15280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.dial_context_menu_id).setIntent(
15290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Intent(Intent.ACTION_VIEW, Uri
15300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                .parse(WebView.SCHEME_TEL + extra)));
15310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
15320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
15330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                addIntent.setType(Contacts.People.CONTENT_ITEM_TYPE);
15340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.add_contact_context_menu_id).setIntent(
15350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        addIntent);
15360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
15370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Copy(extra));
15380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
15390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case WebView.HitTestResult.EMAIL_TYPE:
15410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.setHeaderTitle(extra);
15420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.email_context_menu_id).setIntent(
15430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Intent(Intent.ACTION_VIEW, Uri
15440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                .parse(WebView.SCHEME_MAILTO + extra)));
15450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
15460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Copy(extra));
15470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
15480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case WebView.HitTestResult.GEO_TYPE:
15500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.setHeaderTitle(extra);
15510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.map_context_menu_id).setIntent(
15520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Intent(Intent.ACTION_VIEW, Uri
15530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                .parse(WebView.SCHEME_GEO
15540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        + URLEncoder.encode(extra))));
15550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
15560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Copy(extra));
15570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
15580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case WebView.HitTestResult.SRC_ANCHOR_TYPE:
15600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
15610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                TextView titleView = (TextView) LayoutInflater.from(this)
15620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .inflate(android.R.layout.browser_link_context_header,
15630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        null);
15640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                titleView.setText(extra);
15650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.setHeaderView(titleView);
15660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // decide whether to show the open link in new tab option
15670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
15680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mTabControl.getTabCount() < TabControl.MAX_TABS);
15690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                PackageManager pm = getPackageManager();
15700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Intent send = new Intent(Intent.ACTION_SEND);
15710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                send.setType("text/plain");
15720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
15730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
15740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
15750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
15760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
15770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // otherwise fall through to handle image part
15780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case WebView.HitTestResult.IMAGE_TYPE:
15790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (type == WebView.HitTestResult.IMAGE_TYPE) {
15800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    menu.setHeaderTitle(extra);
15810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
15820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.view_image_context_menu_id).setIntent(
15830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
15840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                menu.findItem(R.id.download_context_menu_id).
15850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        setOnMenuItemClickListener(new Download(extra));
15860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
15870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            default:
15890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Log.w(LOGTAG, "We should not get here.");
15900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
15910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
15920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
15930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
15940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Attach the given tab to the content view.
15950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void attachTabToContentView(TabControl.Tab t) {
15962bc69918d97a767a02132090e58718077381ba1aSteve Block        // Attach the container that contains the main WebView and any other UI
15972bc69918d97a767a02132090e58718077381ba1aSteve Block        // associated with the tab.
15982bc69918d97a767a02132090e58718077381ba1aSteve Block        mContentView.addView(t.getContainer(), COVER_SCREEN_PARAMS);
1599bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
1600bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        if (mShouldShowErrorConsole) {
1601bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
1602bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            if (errorConsole.numberOfErrors() == 0) {
1603bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1604bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            } else {
1605bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1606bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            }
1607bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
1608bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            mErrorConsoleContainer.addView(errorConsole,
1609bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
1610bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                                                  ViewGroup.LayoutParams.WRAP_CONTENT));
1611bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        }
1612bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
16130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Attach the sub window if necessary
16140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        attachSubWindow(t);
16150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Request focus on the top window.
16160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        t.getTopWindow().requestFocus();
16170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
16180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
16190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Attach a sub window to the main WebView of the given tab.
16200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void attachSubWindow(TabControl.Tab t) {
16210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // If a sub window exists, attach it to the content view.
16220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final WebView subView = t.getSubWebView();
16230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (subView != null) {
16240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final View container = t.getSubWebViewContainer();
16250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mContentView.addView(container, COVER_SCREEN_PARAMS);
16260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            subView.requestFocus();
16270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
16280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
16290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
16300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Remove the given tab from the content view.
16310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void removeTabFromContentView(TabControl.Tab t) {
16322bc69918d97a767a02132090e58718077381ba1aSteve Block        // Remove the container that contains the main WebView.
16332bc69918d97a767a02132090e58718077381ba1aSteve Block        mContentView.removeView(t.getContainer());
1634bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
1635bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        if (mTabControl.getCurrentErrorConsole(false) != null) {
1636bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            mErrorConsoleContainer.removeView(mTabControl.getCurrentErrorConsole(false));
1637bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        }
1638bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
16390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Remove the sub window if it exists.
16400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (t.getSubWebView() != null) {
16410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mContentView.removeView(t.getSubWebViewContainer());
16420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
16430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
16440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
16450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Remove the sub window if it exists. Also called by TabControl when the
16460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // user clicks the 'X' to dismiss a sub window.
16470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void dismissSubWindow(TabControl.Tab t) {
16480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final WebView mainView = t.getWebView();
16490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (t.getSubWebView() != null) {
16500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Remove the container view and request focus on the main WebView.
16510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mContentView.removeView(t.getSubWebViewContainer());
16520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mainView.requestFocus();
16530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Tell the TabControl to dismiss the subwindow. This will destroy
16540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // the WebView.
16550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mTabControl.dismissSubWindow(t);
16560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
16570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
16580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
16591f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
166025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    // that accepts url as string.
16611f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    private TabControl.Tab openTabAndShow(String url, boolean closeOnExit,
16621f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            String appId) {
16631f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        return openTabAndShow(new UrlData(url), closeOnExit, appId);
16640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
16650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
16660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // This method does a ton of stuff. It will attempt to create a new tab
16670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
16681f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    // url isn't null, it will load the given url.
16691f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    /* package */ TabControl.Tab openTabAndShow(UrlData urlData,
1670f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project            boolean closeOnExit, String appId) {
16710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final boolean newTab = mTabControl.getTabCount() != TabControl.MAX_TABS;
16720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final TabControl.Tab currentTab = mTabControl.getCurrentTab();
16730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (newTab) {
16741f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            final TabControl.Tab tab = mTabControl.createNewTab(
16751f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                    closeOnExit, appId, urlData.mUrl);
16761f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            WebView webview = tab.getWebView();
16771f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            if (CUSTOM_BROWSER_BAR) {
16781f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                mTitleBar.addTab(webview, true);
16791f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            }
16801f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            removeTabFromContentView(currentTab);
16811f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            attachTabToContentView(tab);
16828bbd69f50abd537bfe7eb314b3a3e20241ac22bdPatrick Scott            // We must set the new tab as the current tab to reflect the old
16838bbd69f50abd537bfe7eb314b3a3e20241ac22bdPatrick Scott            // animation behavior.
16848bbd69f50abd537bfe7eb314b3a3e20241ac22bdPatrick Scott            mTabControl.setCurrentTab(tab);
1685160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            if (!urlData.isEmpty()) {
16861f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                urlData.loadIn(webview);
16870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
16881f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            return tab;
168964b80f3efd95d7b5192731e995ccde79c4c6c11fLeon Scroggins        } else {
16901f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            // Get rid of the subwindow if it exists
16911f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            dismissSubWindow(currentTab);
16921f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            if (!urlData.isEmpty()) {
16931f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                // Load the given url.
16941f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                urlData.loadIn(currentTab.getWebView());
16950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
16960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
1697c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba        return currentTab;
16980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
16990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1700c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba    private TabControl.Tab openTab(String url) {
17010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mSettings.openInBackground()) {
1702f59ec877363eaf43118677f249008eddc7a9ce11The Android Open Source Project            TabControl.Tab t = mTabControl.createNewTab();
17030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (t != null) {
17041f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                WebView view = t.getWebView();
17051f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                if (CUSTOM_BROWSER_BAR) {
17061f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                    mTitleBar.addTab(view, false);
17071f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                }
17081f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                view.loadUrl(url);
17090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
1710c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba            return t;
17110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
17121f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            return openTabAndShow(url, false, null);
17130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private class Copy implements OnMenuItemClickListener {
17170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private CharSequence mText;
17180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public boolean onMenuItemClick(MenuItem item) {
17200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            copy(mText);
17210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return true;
17220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public Copy(CharSequence toCopy) {
17250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mText = toCopy;
17260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private class Download implements OnMenuItemClickListener {
17300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private String mText;
17310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public boolean onMenuItemClick(MenuItem item) {
17330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            onDownloadStartNoStream(mText, null, null, null, -1);
17340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return true;
17350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public Download(String toDownload) {
17380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mText = toDownload;
17390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void copy(CharSequence text) {
17430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        try {
17440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
17450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (clip != null) {
17460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                clip.setClipboardText(text);
17470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
17480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } catch (android.os.RemoteException e) {
17490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.e(LOGTAG, "Copy failed", e);
17500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
17540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Resets the browser title-view to whatever it must be (for example, if we
17550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * load a page from history).
17560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
17570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void resetTitle() {
17580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        resetLockIcon();
17590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        resetTitleIconAndProgress();
17600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
17630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Resets the browser title-view to whatever it must be
17640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * (for example, if we had a loading error)
17650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * When we have a new page, we call resetTitle, when we
17660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * have to reset the titlebar to whatever it used to be
17670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * (for example, if the user chose to stop loading), we
17680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * call resetTitleAndRevertLockIcon.
17690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
17700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void resetTitleAndRevertLockIcon() {
17710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        revertLockIcon();
17720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        resetTitleIconAndProgress();
17730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
17760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Reset the title, favicon, and progress.
17770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
17780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void resetTitleIconAndProgress() {
17790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView current = mTabControl.getCurrentWebView();
17800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (current == null) {
17810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
17820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        resetTitleAndIcon(current);
17840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        int progress = current.getProgress();
17850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mWebChromeClient.onProgressChanged(current, progress);
17860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
17880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Reset the title and the icon based on the given item.
17890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void resetTitleAndIcon(WebView view) {
17900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
17910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (item != null) {
17921f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            setUrlTitle(item.getUrl(), item.getTitle(), view);
17930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            setFavicon(item.getFavicon());
17940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
17951f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            setUrlTitle(null, null, view);
17960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            setFavicon(null);
17970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
17980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
17990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
18010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Sets a title composed of the URL and the title string.
18020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param url The URL of the site being loaded.
18030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param title The title of the site being loaded.
18040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
18051f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    private void setUrlTitle(String url, String title, WebView view) {
18060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mUrl = url;
18070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTitle = title;
18080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18091f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
18101f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            mTitleBar.setTitleAndUrl(title, url, view);
18111f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        } else {
18121f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            setTitle(buildUrlTitle(url, title));
18130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
18140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
18150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
18170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Builds and returns the page title, which is some
18180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * combination of the page URL and title.
18190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param url The URL of the site being loaded.
18200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param title The title of the site being loaded.
18210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @return The page title.
18220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
18230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private String buildUrlTitle(String url, String title) {
18240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String urlTitle = "";
18250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (url != null) {
18270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String titleUrl = buildTitleUrl(url);
18280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (title != null && 0 < title.length()) {
18300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (titleUrl != null && 0 < titleUrl.length()) {
18310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    urlTitle = titleUrl + ": " + title;
18320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } else {
18330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    urlTitle = title;
18340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
18350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
18360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (titleUrl != null) {
18370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    urlTitle = titleUrl;
18380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
18390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
18400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
18410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return urlTitle;
18430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
18440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
18460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param url The URL to build a title version of the URL from.
18470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @return The title version of the URL or null if fails.
18480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * The title version of the URL can be either the URL hostname,
18490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * or the hostname with an "https://" prefix (for secure URLs),
18500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * or an empty string if, for example, the URL in question is a
18510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * file:// URL with no hostname.
18520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
185332e14a6deccfa75490b3032bb4ddd5ae90f89de8Leon Scroggins    /* package */ static String buildTitleUrl(String url) {
18540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String titleUrl = null;
18550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (url != null) {
18570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            try {
18580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // parse the url string
18590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                URL urlObj = new URL(url);
18600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (urlObj != null) {
18610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    titleUrl = "";
18620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String protocol = urlObj.getProtocol();
18640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String host = urlObj.getHost();
18650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (host != null && 0 < host.length()) {
18670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        titleUrl = host;
18680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (protocol != null) {
18690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // if a secure site, add an "https://" prefix!
18700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            if (protocol.equalsIgnoreCase("https")) {
18710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                titleUrl = protocol + "://" + host;
18720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
18730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
18740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
18750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
18760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } catch (MalformedURLException e) {}
18770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
18780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return titleUrl;
18800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
18810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
18820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Set the favicon in the title bar.
18830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void setFavicon(Bitmap icon) {
188481db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
188581db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            Drawable[] array = new Drawable[3];
188681db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            array[0] = new PaintDrawable(Color.BLACK);
188781db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            PaintDrawable p = new PaintDrawable(Color.WHITE);
188881db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            array[1] = p;
188981db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            if (icon == null) {
189081db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins                array[2] = mGenericFavicon;
189181db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            } else {
189281db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins                array[2] = new BitmapDrawable(icon);
189381db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            }
189481db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            LayerDrawable d = new LayerDrawable(array);
189581db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            d.setLayerInset(1, 1, 1, 1, 1);
189681db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            d.setLayerInset(2, 2, 2, 2, 2);
18971f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            mTitleBar.setFavicon(d, getTopWindow());
18980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
189981db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            Drawable[] array = new Drawable[2];
190081db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            PaintDrawable p = new PaintDrawable(Color.WHITE);
190181db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            p.setCornerRadius(3f);
190281db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            array[0] = p;
190381db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            if (icon == null) {
190481db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins                array[1] = mGenericFavicon;
190581db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            } else {
190681db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins                array[1] = new BitmapDrawable(icon);
190781db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            }
190881db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            LayerDrawable d = new LayerDrawable(array);
190981db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            d.setLayerInset(1, 2, 2, 2, 2);
191081db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins            getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON, d);
19110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
19120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
19130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
19140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
19150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Saves the current lock-icon state before resetting
19160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * the lock icon. If we have an error, we may need to
19170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * roll back to the previous state.
19180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
19190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void saveLockIcon() {
19200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mPrevLockType = mLockIconType;
19210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
19220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
19230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
19240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Reverts the lock-icon state to the last saved state,
19250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * for example, if we had an error, and need to cancel
19260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * the load.
19270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
19280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void revertLockIcon() {
19290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mLockIconType = mPrevLockType;
19300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
193131a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort        if (LOGV_ENABLED) {
19320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.v(LOGTAG, "BrowserActivity.revertLockIcon:" +
19330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                  " revert lock icon to " + mLockIconType);
19340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
19350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
19360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        updateLockIconImage(mLockIconType);
19370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
19380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
19391f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    /**
19401f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins     * Close the tab after removing its associated title bar.
19411f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins     */
19421f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    private void closeTab(TabControl.Tab t) {
1943a11b75a4ef9cb8e264e59e3d2aadd56cf62e2afeLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
1944a11b75a4ef9cb8e264e59e3d2aadd56cf62e2afeLeon Scroggins            mTitleBar.removeTab(mTabControl.getTabIndex(t));
1945a11b75a4ef9cb8e264e59e3d2aadd56cf62e2afeLeon Scroggins        }
19461f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        mTabControl.removeTab(t);
19470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
19480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
19490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void goBackOnePageOrQuit() {
19500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        TabControl.Tab current = mTabControl.getCurrentTab();
19510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (current == null) {
19520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            /*
19530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             * Instead of finishing the activity, simply push this to the back
19540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             * of the stack and let ActivityManager to choose the foreground
19550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             * activity. As BrowserActivity is singleTask, it will be always the
19560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             * root of the task. So we can use either true or false for
19570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             * moveTaskToBack().
19580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             */
19590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            moveTaskToBack(true);
19600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
19610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView w = current.getWebView();
19620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (w.canGoBack()) {
19630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            w.goBack();
19640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
19650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Check to see if we are closing a window that was created by
19660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // another window. If so, we switch back to that window.
19670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            TabControl.Tab parent = current.getParentTab();
19680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (parent != null) {
19691f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                switchToTab(mTabControl.getTabIndex(parent));
19701f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                // Now we close the other tab
19711f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                closeTab(current);
19720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
19730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (current.closeOnExit()) {
19740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (mTabControl.getTabCount() == 1) {
19750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        finish();
19760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        return;
19770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
19787bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                    // call pauseWebViewTimers() now, we won't be able to call
19797bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                    // it in onPause() as the WebView won't be valid.
1980ec1b5ad8d8283b1fbbc5d7fa1cd50ef95b9e594dGrace Kloba                    // Temporarily change mActivityInPause to be true as
1981ec1b5ad8d8283b1fbbc5d7fa1cd50ef95b9e594dGrace Kloba                    // pauseWebViewTimers() will do nothing if mActivityInPause
1982ec1b5ad8d8283b1fbbc5d7fa1cd50ef95b9e594dGrace Kloba                    // is false.
1983918e1d78b2db6ef21c71a07868572d9cda498476Grace Kloba                    boolean savedState = mActivityInPause;
1984918e1d78b2db6ef21c71a07868572d9cda498476Grace Kloba                    if (savedState) {
1985ec1b5ad8d8283b1fbbc5d7fa1cd50ef95b9e594dGrace Kloba                        Log.e(LOGTAG, "BrowserActivity is already paused "
1986ec1b5ad8d8283b1fbbc5d7fa1cd50ef95b9e594dGrace Kloba                                + "while handing goBackOnePageOrQuit.");
1987918e1d78b2db6ef21c71a07868572d9cda498476Grace Kloba                    }
1988918e1d78b2db6ef21c71a07868572d9cda498476Grace Kloba                    mActivityInPause = true;
19897bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                    pauseWebViewTimers();
1990918e1d78b2db6ef21c71a07868572d9cda498476Grace Kloba                    mActivityInPause = savedState;
19910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    removeTabFromContentView(current);
19920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mTabControl.removeTab(current);
19930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
19940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                /*
19950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 * Instead of finishing the activity, simply push this to the back
19960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 * of the stack and let ActivityManager to choose the foreground
19970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 * activity. As BrowserActivity is singleTask, it will be always the
19980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 * root of the task. So we can use either true or false for
19990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 * moveTaskToBack().
20000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 */
20010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                moveTaskToBack(true);
20020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
20030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
20040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
20050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public KeyTracker.State onKeyTracker(int keyCode,
20070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                         KeyEvent event,
20080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                         KeyTracker.Stage stage,
20090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                         int duration) {
20100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // if onKeyTracker() is called after activity onStop()
20110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // because of accumulated key events,
20120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // we should ignore it as browser is not active any more.
20130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView topWindow = getTopWindow();
2014adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu        if (topWindow == null && mCustomView == null)
20150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return KeyTracker.State.NOT_TRACKING;
20160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (keyCode == KeyEvent.KEYCODE_BACK) {
2018adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // Check if a custom view is currently showing and, if it is, hide it.
2019adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            if (mCustomView != null) {
2020adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                mWebChromeClient.onHideCustomView();
2021adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                return KeyTracker.State.DONE_TRACKING;
2022adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            }
20230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (stage == KeyTracker.Stage.LONG_REPEAT) {
2024160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                bookmarksOrHistoryPicker(true, false);
20250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return KeyTracker.State.DONE_TRACKING;
20260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else if (stage == KeyTracker.Stage.UP) {
20270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // FIXME: Currently, we do not have a notion of the
20280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // history picker for the subwindow, but maybe we
20290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // should?
20300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                WebView subwindow = mTabControl.getCurrentSubWindow();
20310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (subwindow != null) {
20320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (subwindow.canGoBack()) {
20330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        subwindow.goBack();
20340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    } else {
20350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        dismissSubWindow(mTabControl.getCurrentTab());
20360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
20370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } else {
20380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    goBackOnePageOrQuit();
20390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
20400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return KeyTracker.State.DONE_TRACKING;
20410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
20420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return KeyTracker.State.KEEP_TRACKING;
20430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
20440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return KeyTracker.State.NOT_TRACKING;
20450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
20460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
20480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (keyCode == KeyEvent.KEYCODE_MENU) {
20490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mMenuIsDown = true;
20506ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba        } else if (mMenuIsDown) {
20516ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
20526ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // still down, we don't want to trigger the search. Pretend to
20536ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            // consume the key and do nothing.
20546ee9c498b8cf648f1507281ed5a1834ba3390907Grace Kloba            return true;
20550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
20560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        boolean handled =  mKeyTracker.doKeyDown(keyCode, event);
20570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (!handled) {
20580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            switch (keyCode) {
20590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                case KeyEvent.KEYCODE_SPACE:
20600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (event.isShiftPressed()) {
20610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        getTopWindow().pageUp(false);
20620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    } else {
20630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        getTopWindow().pageDown(false);
20640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
20650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    handled = true;
20660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
20670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                default:
20690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
20700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
20710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
20720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return handled || super.onKeyDown(keyCode, event);
20730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
20740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override public boolean onKeyUp(int keyCode, KeyEvent event) {
20760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (keyCode == KeyEvent.KEYCODE_MENU) {
20770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mMenuIsDown = false;
20780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
20790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return mKeyTracker.doKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
20800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
20810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void stopLoading() {
20830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        resetTitleAndRevertLockIcon();
20840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView w = getTopWindow();
20850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        w.stopLoading();
20860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mWebViewClient.onPageFinished(w, w.getUrl());
20870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        cancelStopToast();
20890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mStopToast = Toast
20900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
20910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mStopToast.show();
20920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
20930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
20940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void cancelStopToast() {
20950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mStopToast != null) {
20960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mStopToast.cancel();
20970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mStopToast = null;
20980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
20990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
21000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // called by a non-UI thread to post the message
21020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void postMessage(int what, int arg1, int arg2, Object obj) {
21030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mHandler.sendMessage(mHandler.obtainMessage(what, arg1, arg2, obj));
21040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
21050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // public message ids
21070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public final static int LOAD_URL                = 1001;
21080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public final static int STOP_LOAD               = 1002;
21090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Message Ids
21110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private static final int FOCUS_NODE_HREF         = 102;
21120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private static final int CANCEL_CREDS_REQUEST    = 103;
211392c18a52ca7a79c114028b5ba22c3dfe443bd1a4Grace Kloba    private static final int RELEASE_WAKELOCK        = 107;
21140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Private handler for handling javascript and saving passwords
21160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Handler mHandler = new Handler() {
21170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void handleMessage(Message msg) {
21190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            switch (msg.what) {
21200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                case FOCUS_NODE_HREF:
21210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String url = (String) msg.getData().get("url");
21220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (url == null || url.length() == 0) {
21230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        break;
21240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
21250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    HashMap focusNodeMap = (HashMap) msg.obj;
21260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    WebView view = (WebView) focusNodeMap.get("webview");
21270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // Only apply the action if the top window did not change.
21280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (getTopWindow() != view) {
21290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        break;
21300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
21310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    switch (msg.arg1) {
21320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.open_context_menu_id:
21330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.view_image_context_menu_id:
21340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            loadURL(getTopWindow(), url);
21350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
21360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.open_newtab_context_menu_id:
2137c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                            final TabControl.Tab parent = mTabControl
2138c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                                    .getCurrentTab();
2139c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                            final TabControl.Tab newTab = openTab(url);
2140c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                            if (newTab != parent) {
2141c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                                parent.addChildTab(newTab);
2142c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                            }
21430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
21440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.bookmark_context_menu_id:
21450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Intent intent = new Intent(BrowserActivity.this,
21460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    AddBookmarkPage.class);
21470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            intent.putExtra("url", url);
21480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            startActivity(intent);
21490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
21500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.share_link_context_menu_id:
21510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Browser.sendString(BrowserActivity.this, url);
21520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
21530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.copy_link_context_menu_id:
21540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            copy(url);
21550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
21560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.save_link_context_menu_id:
21570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        case R.id.download_context_menu_id:
21580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            onDownloadStartNoStream(url, null, null, null, -1);
21590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            break;
21600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
21610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
21620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                case LOAD_URL:
21640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    loadURL(getTopWindow(), (String) msg.obj);
21650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
21660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                case STOP_LOAD:
21680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    stopLoading();
21690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
21700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                case CANCEL_CREDS_REQUEST:
21720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    resumeAfterCredentials();
21730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
21740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
21750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                case RELEASE_WAKELOCK:
21760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (mWakeLock.isHeld()) {
21770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mWakeLock.release();
21780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
21790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    break;
21800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
21810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
21820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    };
21830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
218489c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins    private void updateScreenshot(WebView view) {
218589c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins        // If this is a bookmarked site, add a screenshot to the database.
218689c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins        // FIXME: When should we update?  Every time?
218789c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins        // FIXME: Would like to make sure there is actually something to
218889c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins        // draw, but the API for that (WebViewCore.pictureReady()) is not
218989c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins        // currently accessible here.
21903918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        ContentResolver cr = getContentResolver();
21913918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(
2192a5d669e4c08f0e64387c2c539c41a26ac8c845abLeon Scroggins                cr, view.getOriginalUrl(), view.getUrl(), false);
21933918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        if (c != null) {
219489c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins            boolean succeed = c.moveToFirst();
219589c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins            ContentValues values = null;
219689c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins            while (succeed) {
219789c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                if (values == null) {
219889c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    final ByteArrayOutputStream os
219989c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                            = new ByteArrayOutputStream();
220089c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    Picture thumbnail = view.capturePicture();
220189c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    // Keep width and height in sync with BrowserBookmarksPage
220289c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    // and bookmark_thumb
220389c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    Bitmap bm = Bitmap.createBitmap(100, 80,
220489c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                            Bitmap.Config.ARGB_4444);
220589c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    Canvas canvas = new Canvas(bm);
220689c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    // May need to tweak these values to determine what is the
220789c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    // best scale factor
220889c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    canvas.scale(.5f, .5f);
220989c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    thumbnail.draw(canvas);
221089c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    bm.compress(Bitmap.CompressFormat.PNG, 100, os);
221189c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    values = new ContentValues();
221289c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                    values.put(Browser.BookmarkColumns.THUMBNAIL,
221389c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                            os.toByteArray());
221489c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                }
221589c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
221689c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                        c.getInt(0)), values, null, null);
221789c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins                succeed = c.moveToNext();
221889c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins            }
221989c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins            c.close();
222089c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins        }
222189c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins    }
222289c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins
22230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // -------------------------------------------------------------------------
22240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // WebViewClient implementation.
22250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    //-------------------------------------------------------------------------
22260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Use in overrideUrlLoading
22280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ final static String SCHEME_WTAI = "wtai://wp/";
22290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
22300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
22310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
22320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ WebViewClient getWebViewClient() {
22340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return mWebViewClient;
22350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
22360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22373918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott    private void updateIcon(WebView view, Bitmap icon) {
22380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (icon != null) {
22390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            BrowserBookmarksAdapter.updateBookmarkFavicon(mResolver,
22403918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                    view, icon);
22410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
22420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        setFavicon(icon);
22430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
22440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final WebViewClient mWebViewClient = new WebViewClient() {
22460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
22470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onPageStarted(WebView view, String url, Bitmap favicon) {
22480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            resetLockIcon(url);
22491f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            setUrlTitle(url, null, view);
2250bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
2251bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(false);
2252bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            if (errorConsole != null) {
2253bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                errorConsole.clearErrorMessages();
2254bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                if (mShouldShowErrorConsole) {
2255bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                    errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
2256bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                }
2257bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            }
2258bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
22590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Call updateIcon instead of setFavicon so the bookmark
22600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // database can be updated.
22613918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott            updateIcon(view, favicon);
22620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22634d7880fe3061ed691d220e64cd9e54a8c7b2d1bfGrace Kloba            if (mSettings.isTracing()) {
22640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String host;
22650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                try {
22660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    WebAddress uri = new WebAddress(url);
22670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    host = uri.mHost;
22680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                } catch (android.net.ParseException ex) {
22694d7880fe3061ed691d220e64cd9e54a8c7b2d1bfGrace Kloba                    host = "browser";
22700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
22710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                host = host.replace('.', '_');
22724d7880fe3061ed691d220e64cd9e54a8c7b2d1bfGrace Kloba                host += ".trace";
22730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mInTrace = true;
22744d7880fe3061ed691d220e64cd9e54a8c7b2d1bfGrace Kloba                Debug.startMethodTracing(host, 20 * 1024 * 1024);
22750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
22760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Performance probe
22780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (false) {
22790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mStart = SystemClock.uptimeMillis();
22800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mProcessStart = Process.getElapsedCpuTime();
22810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                long[] sysCpu = new long[7];
22820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
22830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        sysCpu, null)) {
22840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mUserStart = sysCpu[0] + sysCpu[1];
22850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mSystemStart = sysCpu[2];
22860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mIdleStart = sysCpu[3];
22870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
22880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
22890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mUiStart = SystemClock.currentThreadTimeMillis();
22900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
22910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (!mPageStarted) {
22930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mPageStarted = true;
22947bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                // if onResume() has been called, resumeWebViewTimers() does
22957bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                // nothing.
22967bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                resumeWebViewTimers();
22970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
22980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
22990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // reset sync timer to avoid sync starts during loading a page
23000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            CookieSyncManager.getInstance().resetSync();
23010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
23020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mInLoad = true;
2303416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            if (CUSTOM_BROWSER_BAR) {
2304416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins                mTitleBar.setVisibility(View.VISIBLE);
2305416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            }
23060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            updateInLoadMenuItems();
23070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (!mIsNetworkUp) {
23080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if ( mAlertDialog == null) {
23090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    mAlertDialog = new AlertDialog.Builder(BrowserActivity.this)
23100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setTitle(R.string.loadSuspendedTitle)
23110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setMessage(R.string.loadSuspended)
23120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setPositiveButton(R.string.ok, null)
23130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .show();
23140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
23150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (view != null) {
23160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    view.setNetworkAvailable(false);
23170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
23180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
23190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
23200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
23210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
23220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onPageFinished(WebView view, String url) {
23230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Reset the title and icon in case we stopped a provisional
23240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // load.
23250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            resetTitleAndIcon(view);
23260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
23270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Update the lock icon image only once we are done loading
23280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            updateLockIconImage(mLockIconType);
232989c6d36e2e6d6040540ee93ef4df29a6874a6a95Leon Scroggins            updateScreenshot(view);
2330b6b7f9ef3b4ed220063f555d8b7c205210a61a04Leon Scroggins
23310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Performance probe
2332cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project            if (false) {
23330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                long[] sysCpu = new long[7];
23340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
23350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        sysCpu, null)) {
23360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String uiInfo = "UI thread used "
23370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + (SystemClock.currentThreadTimeMillis() - mUiStart)
23380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + " ms";
233931a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort                    if (LOGD_ENABLED) {
23400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        Log.d(LOGTAG, uiInfo);
23410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
23420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    //The string that gets written to the log
23430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String performanceString = "It took total "
23440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + (SystemClock.uptimeMillis() - mStart)
23450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + " ms clock time to load the page."
23460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + "\nbrowser process used "
23470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + (Process.getElapsedCpuTime() - mProcessStart)
23480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + " ms, user processes used "
23490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
23500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + " ms, kernel used "
23510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + (sysCpu[2] - mSystemStart) * 10
23520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
23530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + " ms and irq took "
23540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
23550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            * 10 + " ms, " + uiInfo;
235631a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort                    if (LOGD_ENABLED) {
23570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
23580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
23590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (url != null) {
23600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // strip the url to maintain consistency
23610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        String newUrl = new String(url);
23620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (newUrl.startsWith("http://www.")) {
23630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            newUrl = newUrl.substring(11);
23640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        } else if (newUrl.startsWith("http://")) {
23650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            newUrl = newUrl.substring(7);
23660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        } else if (newUrl.startsWith("https://www.")) {
23670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            newUrl = newUrl.substring(12);
23680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        } else if (newUrl.startsWith("https://")) {
23690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            newUrl = newUrl.substring(8);
23700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
237131a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort                        if (LOGD_ENABLED) {
23720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Log.d(LOGTAG, newUrl + " loaded");
23730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
23740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        /*
23750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (sWhiteList.contains(newUrl)) {
23760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // The string that gets pushed to the statistcs
23770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // service
23780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            performanceString = performanceString
23790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    + "\nWebpage: "
23800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    + newUrl
23810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    + "\nCarrier: "
23820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    + android.os.SystemProperties
23830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            .get("gsm.sim.operator.alpha");
23840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            if (mWebView != null
23850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    && mWebView.getContext() != null
23860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    && mWebView.getContext().getSystemService(
23870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    Context.CONNECTIVITY_SERVICE) != null) {
23880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                ConnectivityManager cManager =
23890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        (ConnectivityManager) mWebView
23900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        .getContext().getSystemService(
23910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        Context.CONNECTIVITY_SERVICE);
23920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                NetworkInfo nInfo = cManager
23930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        .getActiveNetworkInfo();
23940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                if (nInfo != null) {
23950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    performanceString = performanceString
23960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            + "\nNetwork Type: "
23970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            + nInfo.getType().toString();
23980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                }
23990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
24000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Checkin.logEvent(mResolver,
24010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    Checkin.Events.Tag.WEBPAGE_LOAD,
24020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    performanceString);
24030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Log.w(LOGTAG, "pushed to the statistics service");
24040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
24050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        */
24060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
24070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
24080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             }
24090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
24100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mInTrace) {
24110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mInTrace = false;
24120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Debug.stopMethodTracing();
24130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
24140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
24150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mPageStarted) {
24160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mPageStarted = false;
24177bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                // pauseWebViewTimers() will do nothing and return false if
24187bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                // onPause() is not called yet.
24197bfa63b55644fbf2da1fe6a24b3be0267529202cMike Reed                if (pauseWebViewTimers()) {
24200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (mWakeLock.isHeld()) {
24210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mHandler.removeMessages(RELEASE_WAKELOCK);
24220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mWakeLock.release();
24230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
24240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
24250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
24260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
24270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
24280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // return true if want to hijack the url to let another app to handle it
24290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
24300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public boolean shouldOverrideUrlLoading(WebView view, String url) {
24310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (url.startsWith(SCHEME_WTAI)) {
24320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // wtai://wp/mc;number
24330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // number=string(phone-number)
24340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (url.startsWith(SCHEME_WTAI_MC)) {
24350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    Intent intent = new Intent(Intent.ACTION_VIEW,
24360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Uri.parse(WebView.SCHEME_TEL +
24370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            url.substring(SCHEME_WTAI_MC.length())));
24380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    startActivity(intent);
24390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    return true;
24400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
24410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // wtai://wp/sd;dtmf
24420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // dtmf=string(dialstring)
24430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (url.startsWith(SCHEME_WTAI_SD)) {
24440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // TODO
24450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // only send when there is active voice connection
24460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    return false;
24470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
24480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // wtai://wp/ap;number;name
24490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // number=string(phone-number)
24500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // name=string
24510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (url.startsWith(SCHEME_WTAI_AP)) {
24520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // TODO
24530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    return false;
24540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
24550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
24560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
24579918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            // The "about:" schemes are internal to the browser; don't
24589918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            // want these to be dispatched to other apps.
24599918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            if (url.startsWith("about:")) {
24609918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn                return false;
24619918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            }
2462bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
24639918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            Intent intent;
2464bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
24659918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            // perform generic parsing of the URI to turn it into an Intent.
24660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            try {
24679918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn                intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
24689918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            } catch (URISyntaxException ex) {
24699918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn                Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
24700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return false;
24710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
24720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
24735b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba            // check whether the intent can be resolved. If not, we will see
24745b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba            // whether we can download it from the Market.
24755b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba            if (getPackageManager().resolveActivity(intent, 0) == null) {
24765b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                String packagename = intent.getPackage();
24775b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                if (packagename != null) {
24785b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                    intent = new Intent(Intent.ACTION_VIEW, Uri
24795b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                            .parse("market://search?q=pname:" + packagename));
24805b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                    intent.addCategory(Intent.CATEGORY_BROWSABLE);
24815b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                    startActivity(intent);
24825b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                    return true;
24835b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                } else {
24845b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                    return false;
24855b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba                }
24865b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba            }
24875b078b5b8f166a14ab88137e478f868959c5befaGrace Kloba
24889918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            // sanitize the Intent, ensuring web pages can not bypass browser
24899918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            // security (only access to BROWSABLE activities).
24900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            intent.addCategory(Intent.CATEGORY_BROWSABLE);
24919918943559193e3e047a1c18442e2cdb0fe22b15Dianne Hackborn            intent.setComponent(null);
24920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            try {
24930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (startActivityIfNeeded(intent, -1)) {
24940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    return true;
24950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
24960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } catch (ActivityNotFoundException ex) {
24970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // ignore the error. If no application can handle the URL,
24980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // eg about:blank, assume the browser can handle it.
24990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
25000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
25010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mMenuIsDown) {
25020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                openTab(url);
25030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                closeOptionsMenu();
25040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return true;
25050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
25060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
25070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return false;
25080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
25090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
25100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
25110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Updates the lock icon. This method is called when we discover another
25120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * resource to be loaded for this page (for example, javascript). While
25130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * we update the icon type, we do not update the lock icon itself until
25140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * we are done loading, it is slightly more secure this way.
25150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
25160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
25170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onLoadResource(WebView view, String url) {
25180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (url != null && url.length() > 0) {
25190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // It is only if the page claims to be secure
25200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // that we may have to update the lock:
25210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (mLockIconType == LOCK_ICON_SECURE) {
25220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // If NOT a 'safe' url, change the lock to mixed content!
25230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url) || URLUtil.isAboutUrl(url))) {
25240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mLockIconType = LOCK_ICON_MIXED;
252531a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort                        if (LOGV_ENABLED) {
25260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            Log.v(LOGTAG, "BrowserActivity.updateLockIcon:" +
25270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                  " updated lock icon to " + mLockIconType + " due to " + url);
25280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
25290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
25300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
25310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
25320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
25330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
25340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
25350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Show the dialog, asking the user if they would like to continue after
25360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * an excessive number of HTTP redirects.
25370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
25380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
25390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onTooManyRedirects(WebView view, final Message cancelMsg,
25400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final Message continueMsg) {
25410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new AlertDialog.Builder(BrowserActivity.this)
25420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setTitle(R.string.browserFrameRedirect)
25430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setMessage(R.string.browserFrame307Post)
25440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
25450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onClick(DialogInterface dialog, int which) {
25460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        continueMsg.sendToTarget();
25470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }})
25480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
25490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onClick(DialogInterface dialog, int which) {
25500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        cancelMsg.sendToTarget();
25510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }})
25520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setOnCancelListener(new OnCancelListener() {
25530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onCancel(DialogInterface dialog) {
25540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        cancelMsg.sendToTarget();
25550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }})
25560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .show();
25570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
25580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
255937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        // Container class for the next error dialog that needs to be
256037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        // displayed.
256137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        class ErrorDialog {
256237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            public final int mTitle;
256337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            public final String mDescription;
256437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            public final int mError;
256537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            ErrorDialog(int title, String desc, int error) {
256637911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                mTitle = title;
256737911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                mDescription = desc;
256837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                mError = error;
256937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            }
257037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        };
257137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott
257237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        private void processNextError() {
257337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            if (mQueuedErrors == null) {
257437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                return;
257537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            }
257637911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            // The first one is currently displayed so just remove it.
257737911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            mQueuedErrors.removeFirst();
257837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            if (mQueuedErrors.size() == 0) {
257937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                mQueuedErrors = null;
258037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                return;
258137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            }
258237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            showError(mQueuedErrors.getFirst());
258337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        }
258437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott
258537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        private DialogInterface.OnDismissListener mDialogListener =
258637911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                new DialogInterface.OnDismissListener() {
258737911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    public void onDismiss(DialogInterface d) {
258837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                        processNextError();
258937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    }
259037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                };
259137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        private LinkedList<ErrorDialog> mQueuedErrors;
259237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott
259337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        private void queueError(int err, String desc) {
259437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            if (mQueuedErrors == null) {
259537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                mQueuedErrors = new LinkedList<ErrorDialog>();
259637911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            }
259737911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            for (ErrorDialog d : mQueuedErrors) {
259837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                if (d.mError == err) {
259937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    // Already saw a similar error, ignore the new one.
260037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    return;
260137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                }
260237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            }
260337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            ErrorDialog errDialog = new ErrorDialog(
260437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    err == EventHandler.FILE_NOT_FOUND_ERROR ?
260537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    R.string.browserFrameFileErrorLabel :
260637911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    R.string.browserFrameNetworkErrorLabel,
260737911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    desc, err);
260837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            mQueuedErrors.addLast(errDialog);
260937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott
261037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            // Show the dialog now if the queue was empty.
261137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            if (mQueuedErrors.size() == 1) {
261237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                showError(errDialog);
261337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            }
261437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        }
261537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott
261637911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        private void showError(ErrorDialog errDialog) {
261737911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            AlertDialog d = new AlertDialog.Builder(BrowserActivity.this)
261837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    .setTitle(errDialog.mTitle)
261937911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    .setMessage(errDialog.mDescription)
262037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    .setPositiveButton(R.string.ok, null)
262137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    .create();
262237911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            d.setOnDismissListener(mDialogListener);
262337911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            d.show();
262437911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott        }
262537911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott
26260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
26270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Show a dialog informing the user of the network error reported by
26280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * WebCore.
26290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
26300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
26310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onReceivedError(WebView view, int errorCode,
26320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String description, String failingUrl) {
26330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (errorCode != EventHandler.ERROR_LOOKUP &&
26340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    errorCode != EventHandler.ERROR_CONNECT &&
26350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    errorCode != EventHandler.ERROR_BAD_URL &&
26360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    errorCode != EventHandler.ERROR_UNSUPPORTED_SCHEME &&
26370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    errorCode != EventHandler.FILE_ERROR) {
263837911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                queueError(errorCode, description);
26390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
264037911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott            Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
264137911c78a0647567fd9fa49125e7de37455b2d5aPatrick Scott                    + " " + description);
26420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
26430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // We need to reset the title after an error.
26440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            resetTitleAndRevertLockIcon();
26450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
26460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
26470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
26480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Check with the user if it is ok to resend POST data as the page they
26490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * are trying to navigate to is the result of a POST.
26500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
26510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
26520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onFormResubmission(WebView view, final Message dontResend,
26530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                       final Message resend) {
26540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new AlertDialog.Builder(BrowserActivity.this)
26550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setTitle(R.string.browserFrameFormResubmitLabel)
26560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setMessage(R.string.browserFrameFormResubmitMessage)
26570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
26580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onClick(DialogInterface dialog, int which) {
26590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        resend.sendToTarget();
26600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }})
26610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
26620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onClick(DialogInterface dialog, int which) {
26630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        dontResend.sendToTarget();
26640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }})
26650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setOnCancelListener(new OnCancelListener() {
26660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onCancel(DialogInterface dialog) {
26670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        dontResend.sendToTarget();
26680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }})
26690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .show();
26700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
26710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
26720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
26730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Insert the url into the visited history database.
26740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * @param url The url to be inserted.
26750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * @param isReload True if this url is being reloaded.
26760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * FIXME: Not sure what to do when reloading the page.
26770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
26780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
26790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void doUpdateVisitedHistory(WebView view, String url,
26800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                boolean isReload) {
26810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (url.regionMatches(true, 0, "about:", 0, 6)) {
26820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return;
26830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
26840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Browser.updateVisitedHistory(mResolver, url, true);
26850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            WebIconDatabase.getInstance().retainIconForPageUrl(url);
26860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
26870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
26880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
26890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Displays SSL error(s) dialog to the user.
26900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
26910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
26920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onReceivedSslError(
26930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final WebView view, final SslErrorHandler handler, final SslError error) {
26940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
26950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mSettings.showSecurityWarnings()) {
26960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final LayoutInflater factory =
26970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    LayoutInflater.from(BrowserActivity.this);
26980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final View warningsView =
26990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    factory.inflate(R.layout.ssl_warnings, null);
27000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final LinearLayout placeholder =
27010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    (LinearLayout)warningsView.findViewById(R.id.placeholder);
27020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (error.hasError(SslError.SSL_UNTRUSTED)) {
27040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    LinearLayout ll = (LinearLayout)factory
27050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .inflate(R.layout.ssl_warning, null);
27060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    ((TextView)ll.findViewById(R.id.warning))
27070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setText(R.string.ssl_untrusted);
27080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    placeholder.addView(ll);
27090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
27100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (error.hasError(SslError.SSL_IDMISMATCH)) {
27120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    LinearLayout ll = (LinearLayout)factory
27130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .inflate(R.layout.ssl_warning, null);
27140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    ((TextView)ll.findViewById(R.id.warning))
27150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setText(R.string.ssl_mismatch);
27160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    placeholder.addView(ll);
27170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
27180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (error.hasError(SslError.SSL_EXPIRED)) {
27200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    LinearLayout ll = (LinearLayout)factory
27210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .inflate(R.layout.ssl_warning, null);
27220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    ((TextView)ll.findViewById(R.id.warning))
27230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setText(R.string.ssl_expired);
27240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    placeholder.addView(ll);
27250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
27260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (error.hasError(SslError.SSL_NOTYETVALID)) {
27280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    LinearLayout ll = (LinearLayout)factory
27290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .inflate(R.layout.ssl_warning, null);
27300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    ((TextView)ll.findViewById(R.id.warning))
27310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setText(R.string.ssl_not_yet_valid);
27320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    placeholder.addView(ll);
27330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
27340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                new AlertDialog.Builder(BrowserActivity.this)
27360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setTitle(R.string.security_warning)
27370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setIcon(android.R.drawable.ic_dialog_alert)
27380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setView(warningsView)
27390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setPositiveButton(R.string.ssl_continue,
27400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            new DialogInterface.OnClickListener() {
27410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                public void onClick(DialogInterface dialog, int whichButton) {
27420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    handler.proceed();
27430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                }
27440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            })
27450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setNeutralButton(R.string.view_certificate,
27460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            new DialogInterface.OnClickListener() {
27470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                public void onClick(DialogInterface dialog, int whichButton) {
27480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    showSSLCertificateOnError(view, handler, error);
27490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                }
27500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            })
27510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setNegativeButton(R.string.cancel,
27520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            new DialogInterface.OnClickListener() {
27530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                public void onClick(DialogInterface dialog, int whichButton) {
27540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    handler.cancel();
27550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    BrowserActivity.this.resetTitleAndRevertLockIcon();
27560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                }
27570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            })
27580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setOnCancelListener(
27590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            new DialogInterface.OnCancelListener() {
27600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                public void onCancel(DialogInterface dialog) {
27610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    handler.cancel();
27620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    BrowserActivity.this.resetTitleAndRevertLockIcon();
27630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                }
27640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            })
27650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .show();
27660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
27670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                handler.proceed();
27680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
27690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
27700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        /**
27720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * Handles an HTTP authentication request.
27730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         *
27740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * @param handler The authentication handler
27750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * @param host The host
27760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         * @param realm The realm
27770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project         */
27780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
27790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onReceivedHttpAuthRequest(WebView view,
27800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final HttpAuthHandler handler, final String host, final String realm) {
27810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String username = null;
27820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String password = null;
27830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            boolean reuseHttpAuthUsernamePassword =
27850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                handler.useHttpAuthUsernamePassword();
27860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (reuseHttpAuthUsernamePassword &&
27880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    (mTabControl.getCurrentWebView() != null)) {
27890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String[] credentials =
27900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mTabControl.getCurrentWebView()
27910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                .getHttpAuthUsernamePassword(host, realm);
27920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (credentials != null && credentials.length == 2) {
27930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    username = credentials[0];
27940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    password = credentials[1];
27950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
27960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
27970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
27980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (username != null && password != null) {
27990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                handler.proceed(username, password);
28000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
28010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                showHttpAuthentication(handler, host, realm, null, null, null, 0);
28020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
28030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
28040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
28050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
28060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
28070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mMenuIsDown) {
28080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // only check shortcut key when MENU is held
28090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return getWindow().isShortcutKey(event.getKeyCode(), event);
28100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
28110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return false;
28120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
28130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
28140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
28150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
28160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
28170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (view != mTabControl.getCurrentTopWebView()) {
28180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return;
28190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
28200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (event.isDown()) {
28210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                BrowserActivity.this.onKeyDown(event.getKeyCode(), event);
28220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
28230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                BrowserActivity.this.onKeyUp(event.getKeyCode(), event);
28240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
28250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
28260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    };
28270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
28280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    //--------------------------------------------------------------------------
28290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // WebChromeClient implementation
28300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    //--------------------------------------------------------------------------
28310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
28320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ WebChromeClient getWebChromeClient() {
28330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return mWebChromeClient;
28340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
28350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
28360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final WebChromeClient mWebChromeClient = new WebChromeClient() {
28370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Helper method to create a new tab or sub window.
28380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        private void createWindow(final boolean dialog, final Message msg) {
28390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (dialog) {
28400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mTabControl.createSubWindow();
28410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final TabControl.Tab t = mTabControl.getCurrentTab();
28420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                attachSubWindow(t);
28430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                WebView.WebViewTransport transport =
28440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        (WebView.WebViewTransport) msg.obj;
28450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                transport.setWebView(t.getSubWebView());
28460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                msg.sendToTarget();
28470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
28480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final TabControl.Tab parent = mTabControl.getCurrentTab();
28491f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                final TabControl.Tab newTab
28501f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        = openTabAndShow(EMPTY_URL_DATA, false, null);
2851c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                if (newTab != parent) {
2852c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                    parent.addChildTab(newTab);
2853c9181841c7ed6b9f7dfc953eab8dc7ba1047022cGrace Kloba                }
28540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                WebView.WebViewTransport transport =
28550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        (WebView.WebViewTransport) msg.obj;
28560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                transport.setWebView(mTabControl.getCurrentWebView());
28571f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                msg.sendToTarget();
28580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
28590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
28600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
28610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
2862416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins        public void onChangeViewingMode(WebView view, int newViewingMode) {
2863416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            if (!CUSTOM_BROWSER_BAR || view != getTopWindow()) {
28644943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins                return;
28654943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins            }
2866416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            switch (newViewingMode) {
2867416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            case WebView.NO_VIEWING_MODE:
2868416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins                break;
2869416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            case WebView.OVERVIEW_MODE:
2870416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            case WebView.READING_MODE_WITH_TITLE_BAR:
2871416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            case WebView.TITLE_BAR_DISMISS_MODE:
28724943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins                mTitleBar.setVisibility(View.VISIBLE);
2873416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins                break;
2874416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            case WebView.READING_MODE:
28754943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins                mTitleBar.setVisibility(View.GONE);
2876416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins                break;
2877416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins            default:
2878416b7cbd57715f5330fabe118aa8d1d3d7ac3ef9Leon Scroggins                break;
28794943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins            }
28804943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins        }
28814943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins
28824943a31bc64286a06b0659636cc13b4b67c30589Leon Scroggins        @Override
28830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public boolean onCreateWindow(WebView view, final boolean dialog,
28840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                final boolean userGesture, final Message resultMsg) {
28850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Short-circuit if we can't create any more tabs or sub windows.
28860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (dialog && mTabControl.getCurrentSubWindow() != null) {
28870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                new AlertDialog.Builder(BrowserActivity.this)
28880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setTitle(R.string.too_many_subwindows_dialog_title)
28890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setIcon(android.R.drawable.ic_dialog_alert)
28900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setMessage(R.string.too_many_subwindows_dialog_message)
28910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setPositiveButton(R.string.ok, null)
28920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .show();
28930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return false;
28940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else if (mTabControl.getTabCount() >= TabControl.MAX_TABS) {
28950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                new AlertDialog.Builder(BrowserActivity.this)
28960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setTitle(R.string.too_many_windows_dialog_title)
28970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setIcon(android.R.drawable.ic_dialog_alert)
28980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setMessage(R.string.too_many_windows_dialog_message)
28990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setPositiveButton(R.string.ok, null)
29000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .show();
29010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return false;
29020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
29030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Short-circuit if this was a user gesture.
29050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (userGesture) {
29060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                createWindow(dialog, resultMsg);
29070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return true;
29080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
29090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Allow the popup and create the appropriate window.
29110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final AlertDialog.OnClickListener allowListener =
29120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    new AlertDialog.OnClickListener() {
29130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        public void onClick(DialogInterface d,
29140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                int which) {
29150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            createWindow(dialog, resultMsg);
29160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
29170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    };
29180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Block the popup by returning a null WebView.
29200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final AlertDialog.OnClickListener blockListener =
29210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    new AlertDialog.OnClickListener() {
29220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        public void onClick(DialogInterface d, int which) {
29230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            resultMsg.sendToTarget();
29240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
29250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    };
29260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Build a confirmation dialog to display to the user.
29280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final AlertDialog d =
29290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    new AlertDialog.Builder(BrowserActivity.this)
29300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setTitle(R.string.attention)
29310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setIcon(android.R.drawable.ic_dialog_alert)
29320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setMessage(R.string.popup_window_attempt)
29330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setPositiveButton(R.string.allow, allowListener)
29340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setNegativeButton(R.string.block, blockListener)
29350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .setCancelable(false)
29360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    .create();
29370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Show the confirmation dialog.
29390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            d.show();
29400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return true;
29410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
29420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
29440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onCloseWindow(WebView window) {
29451f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            final TabControl.Tab current = mTabControl.getCurrentTab();
29461f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            final TabControl.Tab parent = current.getParentTab();
29470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (parent != null) {
29480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // JavaScript can only close popup window.
29491f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                switchToTab(mTabControl.getTabIndex(parent));
29501f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                // Now we need to close the window
29511f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                closeTab(current);
29520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
29530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
29540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
29560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onProgressChanged(WebView view, int newProgress) {
29571f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            if (CUSTOM_BROWSER_BAR) {
29581f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                mTitleBar.setProgress(newProgress, view);
29591f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            } else {
29601f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
29611f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        newProgress * 100);
29620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
29630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (newProgress == 100) {
29650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // onProgressChanged() is called for sub-frame too while
29660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // onPageFinished() is only called for the main frame. sync
29670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // cookie and cache promptly here.
29680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                CookieSyncManager.getInstance().sync();
2969cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                if (mInLoad) {
2970cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                    mInLoad = false;
2971cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                    updateInLoadMenuItems();
2972cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                }
2973cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project            } else {
2974cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                // onPageFinished may have already been called but a subframe
2975cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                // is still loading and updating the progress. Reset mInLoad
2976cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                // and update the menu items.
2977cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                if (!mInLoad) {
2978cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                    mInLoad = true;
2979cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                    updateInLoadMenuItems();
2980cb9a0bbc3a00a044fbfdabda920d3be80b0747f0The Android Open Source Project                }
29810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
29820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
29830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
29850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onReceivedTitle(WebView view, String title) {
2986598c9ccf8641e0e4563bba2fd5613bcb4de766ecPatrick Scott            String url = view.getUrl();
29870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // here, if url is null, we want to reset the title
29891f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            setUrlTitle(url, title, view);
29900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
29910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (url == null ||
29920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url.length() >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
29930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return;
29940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
2995fce182bff7713a12714018327f6da9500011ab17Leon Scroggins            // See if we can find the current url in our history database and
2996fce182bff7713a12714018327f6da9500011ab17Leon Scroggins            // add the new title to it.
29970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (url.startsWith("http://www.")) {
29980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url = url.substring(11);
29990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else if (url.startsWith("http://")) {
30000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url = url.substring(4);
30010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
30020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            try {
30030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url = "%" + url;
30040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String [] selArgs = new String[] { url };
30050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
30060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                String where = Browser.BookmarkColumns.URL + " LIKE ? AND "
30070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        + Browser.BookmarkColumns.BOOKMARK + " = 0";
30080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Cursor c = mResolver.query(Browser.BOOKMARKS_URI,
30090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    Browser.HISTORY_PROJECTION, where, selArgs, null);
30100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (c.moveToFirst()) {
30110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // Current implementation of database only has one entry per
30120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // url.
3013fce182bff7713a12714018327f6da9500011ab17Leon Scroggins                    ContentValues map = new ContentValues();
3014fce182bff7713a12714018327f6da9500011ab17Leon Scroggins                    map.put(Browser.BookmarkColumns.TITLE, title);
3015fce182bff7713a12714018327f6da9500011ab17Leon Scroggins                    mResolver.update(Browser.BOOKMARKS_URI, map,
3016fce182bff7713a12714018327f6da9500011ab17Leon Scroggins                            "_id = " + c.getInt(0), null);
30170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
30180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                c.close();
30190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } catch (IllegalStateException e) {
30200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Log.e(LOGTAG, "BrowserActivity onReceived title", e);
30210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } catch (SQLiteException ex) {
30220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Log.e(LOGTAG, "onReceivedTitle() caught SQLiteException: ", ex);
30230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
30240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
30250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
30260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        @Override
30270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        public void onReceivedIcon(WebView view, Bitmap icon) {
30283918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott            updateIcon(view, icon);
30293918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        }
30303918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott
30313918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        @Override
30323918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        public void onReceivedTouchIconUrl(WebView view, String url) {
30333918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott            final ContentResolver cr = getContentResolver();
30343918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott            final Cursor c =
30353918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                    BrowserBookmarksAdapter.queryBookmarksForUrl(cr,
3036a5d669e4c08f0e64387c2c539c41a26ac8c845abLeon Scroggins                            view.getOriginalUrl(), view.getUrl(), true);
30373918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott            if (c != null) {
30383918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                if (c.getCount() > 0) {
30393918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                    new DownloadTouchIcon(cr, c, view).execute(url);
30403918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                } else {
30413918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                    c.close();
30423918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott                }
30433918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott            }
30440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
3045092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch
3046adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu        @Override
3047c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
3048adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            if (mCustomView != null)
3049adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                return;
3050adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu
3051adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // Add the custom view to its container.
3052adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
3053adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomView = view;
3054c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mCustomViewCallback = callback;
3055adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // Save the menu state and set it to empty while the custom
3056adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // view is showing.
3057adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mOldMenuState = mMenuState;
3058adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mMenuState = EMPTY_MENU;
3059c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            // Hide the content view.
3060c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mContentView.setVisibility(View.GONE);
3061adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // Finally show the custom view container.
3062c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mCustomViewContainer.setVisibility(View.VISIBLE);
3063c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mCustomViewContainer.bringToFront();
3064adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu        }
3065adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu
3066adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu        @Override
3067adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu        public void onHideCustomView() {
3068adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            if (mCustomView == null)
3069adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                return;
3070adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu
3071c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            // Hide the custom view.
3072c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mCustomView.setVisibility(View.GONE);
3073adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // Remove the custom view from its container.
3074adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomViewContainer.removeView(mCustomView);
3075adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomView = null;
3076adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            // Reset the old menu state.
3077adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mMenuState = mOldMenuState;
3078adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mOldMenuState = EMPTY_MENU;
3079adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu            mCustomViewContainer.setVisibility(View.GONE);
3080c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mCustomViewCallback.onCustomViewHidden();
3081c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            // Show the content view.
3082c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu            mContentView.setVisibility(View.VISIBLE);
3083adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu        }
3084adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu
3085092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch        /**
308679e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * The origin has exceeded its database quota.
3087092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         * @param url the URL that exceeded the quota
3088092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         * @param databaseIdentifier the identifier of the database on
3089092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         *     which the transaction that caused the quota overflow was run
3090092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         * @param currentQuota the current quota for the origin.
309125a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch         * @param estimatedSize the estimated size of the database.
309279e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * @param totalUsedQuota is the sum of all origins' quota.
3093092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         * @param quotaUpdater The callback to run when a decision to allow or
3094092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         *     deny quota has been made. Don't forget to call this!
3095092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch         */
3096092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch        @Override
3097092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch        public void onExceededDatabaseQuota(String url,
309825a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch            String databaseIdentifier, long currentQuota, long estimatedSize,
309925a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch            long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
310079e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu            mSettings.getWebStorageSizeManager().onExceededDatabaseQuota(
310125a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch                    url, databaseIdentifier, currentQuota, estimatedSize,
310225a1523642bead2f7e7f929ba9d6d1143dce06a0Ben Murdoch                    totalUsedQuota, quotaUpdater);
310379e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        }
310479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu
310579e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        /**
310679e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * The Application Cache has exceeded its max size.
310779e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * @param spaceNeeded is the amount of disk space that would be needed
310879e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * in order for the last appcache operation to succeed.
310979e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * @param totalUsedQuota is the sum of all origins' quota.
311079e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * @param quotaUpdater A callback to inform the WebCore thread that a new
311179e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * app cache size is available. This callback must always be executed at
311279e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         * some point to ensure that the sleeping WebCore thread is woken up.
311379e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu         */
311479e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        @Override
311579e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu        public void onReachedMaxAppCacheSize(long spaceNeeded,
311679e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu                long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
311779e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu            mSettings.getWebStorageSizeManager().onReachedMaxAppCacheSize(
311879e82b7ba72a7278911edf0dd7b03c65c4ec0e9dAndrei Popescu                    spaceNeeded, totalUsedQuota, quotaUpdater);
3119092dd5d22d7f2ee0efcc68bd6e5a3afd1ea98953Ben Murdoch        }
31207db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch
31212bc69918d97a767a02132090e58718077381ba1aSteve Block        /**
31222bc69918d97a767a02132090e58718077381ba1aSteve Block         * Instructs the browser to show a prompt to ask the user to set the
31232bc69918d97a767a02132090e58718077381ba1aSteve Block         * Geolocation permission state for the specified origin.
31242bc69918d97a767a02132090e58718077381ba1aSteve Block         * @param origin The origin for which Geolocation permissions are
31252bc69918d97a767a02132090e58718077381ba1aSteve Block         *     requested.
31262bc69918d97a767a02132090e58718077381ba1aSteve Block         * @param callback The callback to call once the user has set the
31272bc69918d97a767a02132090e58718077381ba1aSteve Block         *     Geolocation permission state.
31282bc69918d97a767a02132090e58718077381ba1aSteve Block         */
31292bc69918d97a767a02132090e58718077381ba1aSteve Block        @Override
31302bc69918d97a767a02132090e58718077381ba1aSteve Block        public void onGeolocationPermissionsShowPrompt(String origin,
31312bc69918d97a767a02132090e58718077381ba1aSteve Block                GeolocationPermissions.Callback callback) {
31322bc69918d97a767a02132090e58718077381ba1aSteve Block            mTabControl.getCurrentTab().getGeolocationPermissionsPrompt().show(
31332bc69918d97a767a02132090e58718077381ba1aSteve Block                    origin, callback);
31342bc69918d97a767a02132090e58718077381ba1aSteve Block        }
31352bc69918d97a767a02132090e58718077381ba1aSteve Block
31362bc69918d97a767a02132090e58718077381ba1aSteve Block        /**
31372bc69918d97a767a02132090e58718077381ba1aSteve Block         * Instructs the browser to hide the Geolocation permissions prompt.
31382bc69918d97a767a02132090e58718077381ba1aSteve Block         */
31392bc69918d97a767a02132090e58718077381ba1aSteve Block        @Override
31402bc69918d97a767a02132090e58718077381ba1aSteve Block        public void onGeolocationPermissionsHidePrompt() {
31412bc69918d97a767a02132090e58718077381ba1aSteve Block            mTabControl.getCurrentTab().getGeolocationPermissionsPrompt().hide();
31422bc69918d97a767a02132090e58718077381ba1aSteve Block        }
31432bc69918d97a767a02132090e58718077381ba1aSteve Block
31447db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch        /* Adds a JavaScript error message to the system log.
31457db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch         * @param message The error message to report.
31467db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch         * @param lineNumber The line number of the error.
31477db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch         * @param sourceID The name of the source file that caused the error.
31487db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch         */
31497db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch        @Override
31507db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch        public void addMessageToConsole(String message, int lineNumber, String sourceID) {
3151bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
3152bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            errorConsole.addErrorMessage(message, sourceID, lineNumber);
3153bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                if (mShouldShowErrorConsole &&
3154bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                        errorConsole.getShowState() != ErrorConsoleView.SHOW_MAXIMIZED) {
3155bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                    errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3156bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                }
3157bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            Log.w(LOGTAG, "Console: " + message + " " + sourceID + ":" + lineNumber);
31587db26347577132b8c4def47ccd410e7b9d0821bbBen Murdoch        }
31590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    };
31600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
31610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
31620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Notify the host application a download should be done, or that
31630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * the data should be streamed if a streaming viewer is available.
31640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param url The full url to the content that should be downloaded
31650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param contentDisposition Content-disposition http header, if
31660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *                           present.
31670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param mimetype The mimetype of the content reported by the server
31680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param contentLength The file size reported by the server
31690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
31700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void onDownloadStart(String url, String userAgent,
31710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String contentDisposition, String mimetype, long contentLength) {
31720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // if we're dealing wih A/V content that's not explicitly marked
31730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        //     for download, check if it's streamable.
31740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (contentDisposition == null
3175e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                || !contentDisposition.regionMatches(
3176e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        true, 0, "attachment", 0, 10)) {
31770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // query the package manager to see if there's a registered handler
31780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            //     that matches.
31790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Intent intent = new Intent(Intent.ACTION_VIEW);
31800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            intent.setDataAndType(Uri.parse(url), mimetype);
3181e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott            ResolveInfo info = getPackageManager().resolveActivity(intent,
3182e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                    PackageManager.MATCH_DEFAULT_ONLY);
3183e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott            if (info != null) {
3184e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                ComponentName myName = getComponentName();
3185e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                // If we resolved to ourselves, we don't want to attempt to
3186e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                // load the url only to try and download it again.
3187e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                if (!myName.getPackageName().equals(
3188e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        info.activityInfo.packageName)
3189e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        || !myName.getClassName().equals(
3190e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                                info.activityInfo.name)) {
3191e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                    // someone (other than us) knows how to handle this mime
3192e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                    // type with this scheme, don't download.
3193e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                    try {
3194e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        startActivity(intent);
3195e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        return;
3196e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                    } catch (ActivityNotFoundException ex) {
3197e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        if (LOGD_ENABLED) {
3198e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                            Log.d(LOGTAG, "activity not found for " + mimetype
3199e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                                    + " over " + Uri.parse(url).getScheme(),
3200e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                                    ex);
3201e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        }
3202e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        // Best behavior is to fall back to a download in this
3203e1fb96676cd2404c5de091d60828185228ef4c90Patrick Scott                        // case
32040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
32050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
32060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
32070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
32080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
32090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
32100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
32120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Notify the host application a download should be done, even if there
32130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * is a streaming viewer available for thise type.
32140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param url The full url to the content that should be downloaded
32150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param contentDisposition Content-disposition http header, if
32160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *                           present.
32170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param mimetype The mimetype of the content reported by the server
32180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param contentLength The file size reported by the server
32190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
32200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /*package */ void onDownloadStartNoStream(String url, String userAgent,
32210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String contentDisposition, String mimetype, long contentLength) {
32220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String filename = URLUtil.guessFileName(url,
32240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                contentDisposition, mimetype);
32250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Check to see if we have an SDCard
32270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String status = Environment.getExternalStorageState();
32280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (!status.equals(Environment.MEDIA_MOUNTED)) {
32290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            int title;
32300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String msg;
32310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Check to see if the SDCard is busy, same as the music app
32330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (status.equals(Environment.MEDIA_SHARED)) {
32340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                msg = getString(R.string.download_sdcard_busy_dlg_msg);
32350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                title = R.string.download_sdcard_busy_dlg_title;
32360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            } else {
32370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
32380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                title = R.string.download_no_sdcard_dlg_title;
32390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
32400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new AlertDialog.Builder(this)
32420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setTitle(title)
32430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setIcon(android.R.drawable.ic_dialog_alert)
32440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setMessage(msg)
32450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setPositiveButton(R.string.ok, null)
32460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .show();
32470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
32480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
32490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // java.net.URI is a lot stricter than KURL so we have to undo
32510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // KURL's percent-encoding and redo the encoding using java.net.URI.
32520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        URI uri = null;
32530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        try {
32540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Undo the percent-encoding that KURL may have done.
32550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String newUrl = new String(URLUtil.decode(url.getBytes()));
32560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Parse the url into pieces
32570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            WebAddress w = new WebAddress(newUrl);
32580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String frag = null;
32590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String query = null;
32600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String path = w.mPath;
32610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // Break the path into path, query, and fragment
32620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (path.length() > 0) {
32630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                // Strip the fragment
32640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                int idx = path.lastIndexOf('#');
32650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (idx != -1) {
32660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    frag = path.substring(idx + 1);
32670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    path = path.substring(0, idx);
32680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
32690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                idx = path.lastIndexOf('?');
32700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (idx != -1) {
32710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    query = path.substring(idx + 1);
32720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    path = path.substring(0, idx);
32730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
32740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
32750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path,
32760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    query, frag);
32770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } catch (Exception e) {
32780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Log.e(LOGTAG, "Could not parse url for download: " + url, e);
32790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
32800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
32810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // XXX: Have to use the old url since the cookies were stored using the
32830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // old percent-encoded url.
32840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String cookies = CookieManager.getInstance().getCookie(url);
32850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
32860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        ContentValues values = new ContentValues();
32873dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_URI, uri.toString());
32883dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_COOKIE_DATA, cookies);
32893dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_USER_AGENT, userAgent);
32903dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE,
32910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                getPackageName());
32923dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_NOTIFICATION_CLASS,
32930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                BrowserDownloadPage.class.getCanonicalName());
32943dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
32953dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_MIME_TYPE, mimetype);
32963dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_FILE_NAME_HINT, filename);
32973dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru        values.put(Downloads.COLUMN_DESCRIPTION, uri.getHost());
32980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (contentLength > 0) {
32993dc09b269fe4af164371f7a8268a5554b32c1be2Jean-Baptiste Queru            values.put(Downloads.COLUMN_TOTAL_BYTES, contentLength);
33000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
33010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (mimetype == null) {
33020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // We must have long pressed on a link or image to download it. We
33030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // are not sure of the mimetype in this case, so do a head request
33040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new FetchUrlMimeType(this).execute(values);
33050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
33060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final Uri contentUri =
33070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    getContentResolver().insert(Downloads.CONTENT_URI, values);
33080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            viewDownloads(contentUri);
33090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
33100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
33120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
33140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Resets the lock icon. This method is called when we start a new load and
33150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * know the url to be loaded.
33160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
33170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void resetLockIcon(String url) {
33180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Save the lock-icon state (we revert to it if the load gets cancelled)
33190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        saveLockIcon();
33200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mLockIconType = LOCK_ICON_UNSECURE;
33220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (URLUtil.isHttpsUrl(url)) {
33230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mLockIconType = LOCK_ICON_SECURE;
332431a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort            if (LOGV_ENABLED) {
33250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                Log.v(LOGTAG, "BrowserActivity.resetLockIcon:" +
33260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                      " reset lock icon to " + mLockIconType);
33270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
33280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
33290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        updateLockIconImage(LOCK_ICON_UNSECURE);
33310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
33320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
33340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Resets the lock icon.  This method is called when the icon needs to be
33350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * reset but we do not know whether we are loading a secure or not secure
33360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * page.
33370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
33380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void resetLockIcon() {
33390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Save the lock-icon state (we revert to it if the load gets cancelled)
33400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        saveLockIcon();
33410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mLockIconType = LOCK_ICON_UNSECURE;
33430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
334431a6d1cb63a513712bad717a6239919fd5428fd1Dave Bort        if (LOGV_ENABLED) {
33450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          Log.v(LOGTAG, "BrowserActivity.resetLockIcon:" +
33460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                " reset lock icon to " + mLockIconType);
33470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
33480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        updateLockIconImage(LOCK_ICON_UNSECURE);
33500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
33510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
33530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Updates the lock-icon image in the title-bar.
33540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
33550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void updateLockIconImage(int lockIconType) {
33560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        Drawable d = null;
33570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (lockIconType == LOCK_ICON_SECURE) {
33580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            d = mSecLockIcon;
33590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else if (lockIconType == LOCK_ICON_MIXED) {
33600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            d = mMixLockIcon;
33610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
33621f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        if (CUSTOM_BROWSER_BAR) {
33631f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            mTitleBar.setLock(d, getTopWindow());
33641f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins        } else {
33651f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins            getWindow().setFeatureDrawable(Window.FEATURE_RIGHT_ICON, d);
33660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
33670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
33680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
33700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Displays a page-info dialog.
33710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param tab The tab to show info about
33720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param fromShowSSLCertificateOnError The flag that indicates whether
33730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * this dialog was opened from the SSL-certificate-on-error dialog or
33740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * not. This is important, since we need to know whether to return to
33750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * the parent dialog or simply dismiss.
33760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
33770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void showPageInfo(final TabControl.Tab tab,
33780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                              final boolean fromShowSSLCertificateOnError) {
33790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final LayoutInflater factory = LayoutInflater
33800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .from(this);
33810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final View pageInfoView = factory.inflate(R.layout.page_info, null);
33830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final WebView view = tab.getWebView();
33850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String url = null;
33870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String title = null;
33880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
33890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (view == null) {
33900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            url = tab.getUrl();
33910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            title = tab.getTitle();
33920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else if (view == mTabControl.getCurrentWebView()) {
33930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project             // Use the cached title and url if this is the current WebView
33940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            url = mUrl;
33950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            title = mTitle;
33960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
33970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            url = view.getUrl();
33980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            title = view.getTitle();
33990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
34000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (url == null) {
34020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            url = "";
34030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
34040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (title == null) {
34050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            title = "";
34060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
34070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
34090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
34100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mPageInfoView = tab;
34120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mPageInfoFromShowSSLCertificateOnError = new Boolean(fromShowSSLCertificateOnError);
34130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        AlertDialog.Builder alertDialogBuilder =
34150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new AlertDialog.Builder(this)
34160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
34170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setView(pageInfoView)
34180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setPositiveButton(
34190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                R.string.ok,
34200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                new DialogInterface.OnClickListener() {
34210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onClick(DialogInterface dialog,
34220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        int whichButton) {
34230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoDialog = null;
34240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoView = null;
34250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoFromShowSSLCertificateOnError = null;
34260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // if we came here from the SSL error dialog
34280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (fromShowSSLCertificateOnError) {
34290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // go back to the SSL error dialog
34300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            showSSLCertificateOnError(
34310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorView,
34320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorHandler,
34330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorError);
34340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
34350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
34360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                })
34370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setOnCancelListener(
34380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                new DialogInterface.OnCancelListener() {
34390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onCancel(DialogInterface dialog) {
34400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoDialog = null;
34410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoView = null;
34420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoFromShowSSLCertificateOnError = null;
34430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // if we came here from the SSL error dialog
34450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (fromShowSSLCertificateOnError) {
34460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // go back to the SSL error dialog
34470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            showSSLCertificateOnError(
34480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorView,
34490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorHandler,
34500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorError);
34510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
34520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
34530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                });
34540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // if we have a main top-level page SSL certificate set or a certificate
34560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // error
34570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (fromShowSSLCertificateOnError ||
34580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                (view != null && view.getCertificate() != null)) {
34590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // add a 'View Certificate' button
34600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            alertDialogBuilder.setNeutralButton(
34610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                R.string.view_certificate,
34620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                new DialogInterface.OnClickListener() {
34630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    public void onClick(DialogInterface dialog,
34640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        int whichButton) {
34650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoDialog = null;
34660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoView = null;
34670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        mPageInfoFromShowSSLCertificateOnError = null;
34680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        // if we came here from the SSL error dialog
34700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        if (fromShowSSLCertificateOnError) {
34710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // go back to the SSL error dialog
34720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            showSSLCertificateOnError(
34730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorView,
34740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorHandler,
34750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorError);
34760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        } else {
34770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // otherwise, display the top-most certificate from
34780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            // the chain
34790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            if (view.getCertificate() != null) {
34800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                showSSLCertificate(tab);
34810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
34820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
34830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
34840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                });
34850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
34860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mPageInfoDialog = alertDialogBuilder.show();
34880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
34890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
34900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project       /**
34910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Displays the main top-level page SSL certificate dialog
34920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * (accessible from the Page-Info dialog).
34930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param tab The tab to show certificate for.
34940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
34950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void showSSLCertificate(final TabControl.Tab tab) {
34960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final View certificateView =
34970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                inflateCertificateView(tab.getWebView().getCertificate());
34980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (certificateView == null) {
34990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
35000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
35010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        LayoutInflater factory = LayoutInflater.from(this);
35030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final LinearLayout placeholder =
35050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                (LinearLayout)certificateView.findViewById(R.id.placeholder);
35060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        LinearLayout ll = (LinearLayout) factory.inflate(
35080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            R.layout.ssl_success, placeholder);
35090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        ((TextView)ll.findViewById(R.id.success))
35100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setText(R.string.ssl_certificate_is_valid);
35110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSSLCertificateView = tab;
35130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSSLCertificateDialog =
35140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new AlertDialog.Builder(this)
35150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setTitle(R.string.ssl_certificate).setIcon(
35160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    R.drawable.ic_dialog_browser_certificate_secure)
35170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setView(certificateView)
35180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setPositiveButton(R.string.ok,
35190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnClickListener() {
35200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            public void onClick(DialogInterface dialog,
35210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    int whichButton) {
35220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateDialog = null;
35230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateView = null;
35240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                showPageInfo(tab, false);
35260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
35270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        })
35280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setOnCancelListener(
35290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnCancelListener() {
35300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            public void onCancel(DialogInterface dialog) {
35310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateDialog = null;
35320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateView = null;
35330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                showPageInfo(tab, false);
35350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
35360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        })
35370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .show();
35380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
35390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
35410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Displays the SSL error certificate dialog.
35420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param view The target web-view.
35430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param handler The SSL error handler responsible for cancelling the
35440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * connection that resulted in an SSL error or proceeding per user request.
35450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param error The SSL error object.
35460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
35470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void showSSLCertificateOnError(
35480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final WebView view, final SslErrorHandler handler, final SslError error) {
35490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final View certificateView =
35510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            inflateCertificateView(error.getCertificate());
35520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (certificateView == null) {
35530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
35540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
35550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        LayoutInflater factory = LayoutInflater.from(this);
35570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final LinearLayout placeholder =
35590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                (LinearLayout)certificateView.findViewById(R.id.placeholder);
35600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (error.hasError(SslError.SSL_UNTRUSTED)) {
35620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            LinearLayout ll = (LinearLayout)factory
35630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .inflate(R.layout.ssl_warning, placeholder);
35640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView)ll.findViewById(R.id.warning))
35650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(R.string.ssl_untrusted);
35660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
35670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (error.hasError(SslError.SSL_IDMISMATCH)) {
35690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            LinearLayout ll = (LinearLayout)factory
35700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .inflate(R.layout.ssl_warning, placeholder);
35710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView)ll.findViewById(R.id.warning))
35720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(R.string.ssl_mismatch);
35730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
35740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (error.hasError(SslError.SSL_EXPIRED)) {
35760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            LinearLayout ll = (LinearLayout)factory
35770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .inflate(R.layout.ssl_warning, placeholder);
35780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView)ll.findViewById(R.id.warning))
35790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(R.string.ssl_expired);
35800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
35810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (error.hasError(SslError.SSL_NOTYETVALID)) {
35830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            LinearLayout ll = (LinearLayout)factory
35840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .inflate(R.layout.ssl_warning, placeholder);
35850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView)ll.findViewById(R.id.warning))
35860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(R.string.ssl_not_yet_valid);
35870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
35880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
35890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSSLCertificateOnErrorHandler = handler;
35900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSSLCertificateOnErrorView = view;
35910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSSLCertificateOnErrorError = error;
35920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mSSLCertificateOnErrorDialog =
35930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            new AlertDialog.Builder(this)
35940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setTitle(R.string.ssl_certificate).setIcon(
35950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    R.drawable.ic_dialog_browser_certificate_partially_secure)
35960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setView(certificateView)
35970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setPositiveButton(R.string.ok,
35980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnClickListener() {
35990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            public void onClick(DialogInterface dialog,
36000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    int whichButton) {
36010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorDialog = null;
36020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorView = null;
36030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorHandler = null;
36040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorError = null;
36050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mWebViewClient.onReceivedSslError(
36070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    view, handler, error);
36080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
36090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        })
36100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                 .setNeutralButton(R.string.page_info_view,
36110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnClickListener() {
36120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            public void onClick(DialogInterface dialog,
36130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    int whichButton) {
36140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorDialog = null;
36150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                // do not clear the dialog state: we will
36170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                // need to show the dialog again once the
36180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                // user is done exploring the page-info details
36190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                showPageInfo(mTabControl.getTabFromView(view),
36210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        true);
36220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
36230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        })
36240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setOnCancelListener(
36250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnCancelListener() {
36260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            public void onCancel(DialogInterface dialog) {
36270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorDialog = null;
36280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorView = null;
36290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorHandler = null;
36300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mSSLCertificateOnErrorError = null;
36310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mWebViewClient.onReceivedSslError(
36330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    view, handler, error);
36340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }
36350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        })
36360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .show();
36370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
36380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
36400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Inflates the SSL certificate view (helper method).
36410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param certificate The SSL certificate.
36420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @return The resultant certificate view with issued-to, issued-by,
36430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * issued-on, expires-on, and possibly other fields set.
36440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * If the input certificate is null, returns null.
36450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
36460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private View inflateCertificateView(SslCertificate certificate) {
36470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (certificate == null) {
36480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return null;
36490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
36500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        LayoutInflater factory = LayoutInflater.from(this);
36520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        View certificateView = factory.inflate(
36540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            R.layout.ssl_certificate, null);
36550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // issued to:
36570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        SslCertificate.DName issuedTo = certificate.getIssuedTo();
36580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (issuedTo != null) {
36590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView) certificateView.findViewById(R.id.to_common))
36600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(issuedTo.getCName());
36610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView) certificateView.findViewById(R.id.to_org))
36620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(issuedTo.getOName());
36630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView) certificateView.findViewById(R.id.to_org_unit))
36640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(issuedTo.getUName());
36650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
36660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // issued by:
36680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        SslCertificate.DName issuedBy = certificate.getIssuedBy();
36690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (issuedBy != null) {
36700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView) certificateView.findViewById(R.id.by_common))
36710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(issuedBy.getCName());
36720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView) certificateView.findViewById(R.id.by_org))
36730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(issuedBy.getOName());
36740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((TextView) certificateView.findViewById(R.id.by_org_unit))
36750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setText(issuedBy.getUName());
36760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
36770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // issued on:
36790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String issuedOn = reformatCertificateDate(
36800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            certificate.getValidNotBefore());
36810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        ((TextView) certificateView.findViewById(R.id.issued_on))
36820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setText(issuedOn);
36830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // expires on:
36850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String expiresOn = reformatCertificateDate(
36860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            certificate.getValidNotAfter());
36870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        ((TextView) certificateView.findViewById(R.id.expires_on))
36880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            .setText(expiresOn);
36890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return certificateView;
36910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
36920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
36930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
36940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Re-formats the certificate date (Date.toString()) string to
36950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * a properly localized date string.
36960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @return Properly localized version of the certificate date string and
36970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * the original certificate date string if fails to localize.
36980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * If the original string is null, returns an empty string "".
36990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
37000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private String reformatCertificateDate(String certificateDate) {
37010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      String reformattedDate = null;
37020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      if (certificateDate != null) {
37040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          Date date = null;
37050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          try {
37060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project              date = java.text.DateFormat.getInstance().parse(certificateDate);
37070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          } catch (ParseException e) {
37080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project              date = null;
37090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          }
37100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          if (date != null) {
37120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project              reformattedDate =
37130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                  DateFormat.getDateFormat(this).format(date);
37140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          }
37150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      }
37160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      return reformattedDate != null ? reformattedDate :
37180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project          (certificateDate != null ? certificateDate : "");
37190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
37200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
37220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Displays an http-authentication dialog.
37230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
37240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void showHttpAuthentication(final HttpAuthHandler handler,
37250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final String host, final String realm, final String title,
37260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            final String name, final String password, int focusId) {
37270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        LayoutInflater factory = LayoutInflater.from(this);
37280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        final View v = factory
37290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .inflate(R.layout.http_authentication, null);
37300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (name != null) {
37310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((EditText) v.findViewById(R.id.username_edit)).setText(name);
37320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
37330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (password != null) {
37340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ((EditText) v.findViewById(R.id.password_edit)).setText(password);
37350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
37360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String titleText = title;
37380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (titleText == null) {
37390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            titleText = getText(R.string.sign_in_to).toString().replace(
37400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    "%s1", host).replace("%s2", realm);
37410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
37420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mHttpAuthHandler = handler;
37440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        AlertDialog dialog = new AlertDialog.Builder(this)
37450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setTitle(titleText)
37460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setIcon(android.R.drawable.ic_dialog_alert)
37470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setView(v)
37480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setPositiveButton(R.string.action,
37490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnClickListener() {
37500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                             public void onClick(DialogInterface dialog,
37510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                     int whichButton) {
37520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                String nm = ((EditText) v
37530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        .findViewById(R.id.username_edit))
37540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        .getText().toString();
37550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                String pw = ((EditText) v
37560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        .findViewById(R.id.password_edit))
37570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        .getText().toString();
37580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                BrowserActivity.this.setHttpAuthUsernamePassword
37590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                        (host, realm, nm, pw);
37600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                handler.proceed(nm, pw);
37610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mHttpAuthenticationDialog = null;
37620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mHttpAuthHandler = null;
37630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }})
37640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setNegativeButton(R.string.cancel,
37650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        new DialogInterface.OnClickListener() {
37660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            public void onClick(DialogInterface dialog,
37670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    int whichButton) {
37680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                handler.cancel();
37690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                BrowserActivity.this.resetTitleAndRevertLockIcon();
37700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mHttpAuthenticationDialog = null;
37710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mHttpAuthHandler = null;
37720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            }})
37730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .setOnCancelListener(new DialogInterface.OnCancelListener() {
37740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        public void onCancel(DialogInterface dialog) {
37750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            handler.cancel();
37760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            BrowserActivity.this.resetTitleAndRevertLockIcon();
37770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            mHttpAuthenticationDialog = null;
37780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                            mHttpAuthHandler = null;
37790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }})
37800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                .create();
37810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Make the IME appear when the dialog is displayed if applicable.
37820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        dialog.getWindow().setSoftInputMode(
37830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
37840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        dialog.show();
37850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (focusId != 0) {
37860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            dialog.findViewById(focusId).requestFocus();
37870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
37880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            v.findViewById(R.id.username_edit).requestFocus();
37890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
37900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mHttpAuthenticationDialog = dialog;
37910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
37920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
37930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public int getProgress() {
37940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView w = mTabControl.getCurrentWebView();
37950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (w != null) {
37960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return w.getProgress();
37970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
37980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return 100;
37990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
38000c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
38010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
38020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
38030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Set HTTP authentication password.
38040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
38050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param host The host for the password
38060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param realm The realm for the password
38070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param username The username for the password. If it is null, it means
38080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *            password can't be saved.
38090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param password The password
38100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
38110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void setHttpAuthUsernamePassword(String host, String realm,
38120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            String username,
38130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            String password) {
38140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView w = mTabControl.getCurrentWebView();
38150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (w != null) {
38160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            w.setHttpAuthUsernamePassword(host, realm, username, password);
38170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
38180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
38190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
38200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
38210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * connectivity manager says net has come or gone... inform the user
38220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @param up true if net has come up, false if net has gone down
38230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
38240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    public void onNetworkToggle(boolean up) {
38250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (up == mIsNetworkUp) {
38260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
38270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else if (up) {
38280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mIsNetworkUp = true;
38290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mAlertDialog != null) {
38300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mAlertDialog.cancel();
38310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mAlertDialog = null;
38320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
38330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
38340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mIsNetworkUp = false;
38350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (mInLoad && mAlertDialog == null) {
38360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                mAlertDialog = new AlertDialog.Builder(this)
38370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setTitle(R.string.loadSuspendedTitle)
38380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setMessage(R.string.loadSuspended)
38390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .setPositiveButton(R.string.ok, null)
38400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        .show();
38410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
38420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
38430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView w = mTabControl.getCurrentWebView();
38440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (w != null) {
38450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            w.setNetworkAvailable(up);
38460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
38470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
38480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
38490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    @Override
38500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    protected void onActivityResult(int requestCode, int resultCode,
38510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                    Intent intent) {
38520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        switch (requestCode) {
38530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            case COMBO_PAGE:
38540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                if (resultCode == RESULT_OK && intent != null) {
38550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    String data = intent.getAction();
38560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    Bundle extras = intent.getExtras();
38570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    if (extras != null && extras.getBoolean("new_window", false)) {
3858b0e4fc7b8efc66b20cf83a8d5f8dc9110d67da4fPatrick Scott                        final TabControl.Tab newTab = openTab(data);
3859b0e4fc7b8efc66b20cf83a8d5f8dc9110d67da4fPatrick Scott                        if (mSettings.openInBackground() &&
38601f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                                newTab != null) {
3861b0e4fc7b8efc66b20cf83a8d5f8dc9110d67da4fPatrick Scott                            mTabControl.populatePickerData(newTab);
3862b0e4fc7b8efc66b20cf83a8d5f8dc9110d67da4fPatrick Scott                            mTabControl.setCurrentTab(newTab);
38631f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                            int newIndex = mTabControl.getCurrentIndex();
38641f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                            if (CUSTOM_BROWSER_BAR) {
38651f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                                mTitleBar.setCurrentTab(newIndex);
38661f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                            }
3867b0e4fc7b8efc66b20cf83a8d5f8dc9110d67da4fPatrick Scott                        }
38680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    } else {
38690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        final TabControl.Tab currentTab =
38700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                mTabControl.getCurrentTab();
38711f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        dismissSubWindow(currentTab);
38721f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                        if (data != null && data.length() != 0) {
38731f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins                            getTopWindow().loadUrl(data);
38740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                        }
38750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    }
38763936209d56f258bf718d7167da99c827661f5db6Leon Scroggins/*
38773936209d56f258bf718d7167da99c827661f5db6Leon Scroggins                FIXME: Removing this breaks the behavior of pressing BACK from
38783936209d56f258bf718d7167da99c827661f5db6Leon Scroggins                the Go page resulting in the window being closed.  However, it
38793936209d56f258bf718d7167da99c827661f5db6Leon Scroggins                needs to be removed so that the user can use the Search bar to
38803936209d56f258bf718d7167da99c827661f5db6Leon Scroggins                enter a URL.  Further, the Go behavior is going to change
38813936209d56f258bf718d7167da99c827661f5db6Leon Scroggins                drastically, so this behavior may not last anyway.
3882160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                } else if (resultCode == RESULT_CANCELED
3883160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                        && mCancelGoPageMeansClose) {
3884160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                    if (mTabControl.getTabCount() == 1) {
3885160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                        // finish the Browser.  When the Browser opens up again,
3886160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                        // we will go through onCreate and once again open up
3887160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                        // the Go page.
3888160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                        finish();
3889160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                        return;
3890160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                    }
3891160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins                    closeCurrentWindow();
38923936209d56f258bf718d7167da99c827661f5db6Leon Scroggins*/
38930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
38940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
38950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            default:
38960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                break;
38970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
3898160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        mCancelGoPageMeansClose = false;
3899160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        if (getTopWindow() != null) {
3900160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            getTopWindow().requestFocus();
3901160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        }
39020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
39030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /*
39050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * This method is called as a result of the user selecting the options
39060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * menu to see the download window, or when a download changes state. It
39070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * shows the download window ontop of the current window.
39080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
39090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void viewDownloads(Uri downloadRecord) {
39100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        Intent intent = new Intent(this,
39110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                BrowserDownloadPage.class);
39120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        intent.setData(downloadRecord);
39130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        startActivityForResult(intent, this.DOWNLOAD_PAGE);
39140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
39160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3917160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins    // True if canceling the "Go" screen should result in closing the current
3918160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins    // window/browser.
3919160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins    private boolean mCancelGoPageMeansClose;
3920160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins
3921160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins    /**
3922160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     * Open the Go page.
3923160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     * @param startWithHistory If true, open starting on the history tab.
3924160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     *                         Otherwise, start with the bookmarks tab.
3925160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     * @param cancelGoPageMeansClose Set to true if this came from a new tab, or
3926160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     *                               from the only tab, and canceling means to
3927160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     *                               close the tab (and possibly the browser)
3928160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins     */
3929160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins    /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory,
3930160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins            boolean cancelGoPageMeansClose) {
39310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        WebView current = mTabControl.getCurrentWebView();
39320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (current == null) {
39330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return;
39340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
39350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        Intent intent = new Intent(this,
39360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                CombinedBookmarkHistoryActivity.class);
39370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String title = current.getTitle();
39380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String url = current.getUrl();
39390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // Just in case the user opens bookmarks before a page finishes loading
39400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // so the current history item, and therefore the page, is null.
39410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (null == url) {
39420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            url = mLastEnteredUrl;
39430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // This can happen.
39440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (null == url) {
39450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                url = mSettings.getHomePage();
39460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
39470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
39480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // In case the web page has not yet received its associated title.
39490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (title == null) {
39500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            title = url;
39510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
39520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        intent.putExtra("title", title);
39530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        intent.putExtra("url", url);
3954190095d42ef36347df265f40df190ec8be190d9eLeon Scroggins        // If this is opening in a new window, then disable opening in a
3955190095d42ef36347df265f40df190ec8be190d9eLeon Scroggins        // (different) new window.  Also disable it if we have maxed out the
3956190095d42ef36347df265f40df190ec8be190d9eLeon Scroggins        // windows.
3957190095d42ef36347df265f40df190ec8be190d9eLeon Scroggins        intent.putExtra("disable_new_window", cancelGoPageMeansClose
3958190095d42ef36347df265f40df190ec8be190d9eLeon Scroggins                || mTabControl.getTabCount() >= TabControl.MAX_TABS);
39593918d4443ff38ef1870e02aa51a8b29f8352bb1aPatrick Scott        intent.putExtra("touch_icon_url", current.getTouchIconUrl());
39600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (startWithHistory) {
39610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
39620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    CombinedBookmarkHistoryActivity.HISTORY_TAB);
39630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
3964160a7e7d27755b7a3715ad87735b8070ab64d58aLeon Scroggins        mCancelGoPageMeansClose = cancelGoPageMeansClose;
39650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        startActivityForResult(intent, COMBO_PAGE);
39660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
39670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Called when loading from context menu or LOAD_URL message
39690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private void loadURL(WebView view, String url) {
39700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        // In case the user enters nothing.
39710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (url != null && url.length() != 0 && view != null) {
39720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            url = smartUrlFilter(url);
39730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (!mWebViewClient.shouldOverrideUrlLoading(view, url)) {
39740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                view.loadUrl(url);
39750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
39760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
39770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
39780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private String smartUrlFilter(Uri inUri) {
39800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (inUri != null) {
39810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return smartUrlFilter(inUri.toString());
39820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
39830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return null;
39840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
39850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // get window count
39880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
39890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    int getWindowCount(){
39900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      if(mTabControl != null){
39910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return mTabControl.getTabCount();
39920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      }
39930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      return 0;
39940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
39950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3996b34f87a2e13af13d697130482af01b537ab7c5fdFeng Qian    protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
39970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            "(?i)" + // switch on case insensitive matching
39980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            "(" +    // begin group for schema
39990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            "(?:http|https|file):\\/\\/" +
400025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            "|(?:inline|data|about|content|javascript):" +
40010c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            ")" +
40020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            "(.*)" );
40030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
40040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
40050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Attempts to determine whether user input is a URL or search
40060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * terms.  Anything with a space is passed to search.
40070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
40080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Converts to lowercase any mistakenly uppercased schema (i.e.,
40090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * "Http://" converts to "http://"
40100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
40110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * @return Original or modified URL
40120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
40130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
40140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    String smartUrlFilter(String url) {
40150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
40160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        String inUrl = url.trim();
40170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        boolean hasSpace = inUrl.indexOf(' ') != -1;
40180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
40190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
40200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (matcher.matches()) {
40210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            // force scheme to lowercase
40220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String scheme = matcher.group(1);
40230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            String lcScheme = scheme.toLowerCase();
40240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (!lcScheme.equals(scheme)) {
4025123ecfb21e7ca670e3d04efe58780b3bac54a9a1Mitsuru Oshima                inUrl = lcScheme + matcher.group(2);
4026123ecfb21e7ca670e3d04efe58780b3bac54a9a1Mitsuru Oshima            }
4027123ecfb21e7ca670e3d04efe58780b3bac54a9a1Mitsuru Oshima            if (hasSpace) {
4028123ecfb21e7ca670e3d04efe58780b3bac54a9a1Mitsuru Oshima                inUrl = inUrl.replace(" ", "%20");
40290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
40300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            return inUrl;
40310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
40320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (hasSpace) {
4033565505b1a2c1d3099496af5910760087602e994aSatish Sampath            // FIXME: Is this the correct place to add to searches?
4034565505b1a2c1d3099496af5910760087602e994aSatish Sampath            // what if someone else calls this function?
4035565505b1a2c1d3099496af5910760087602e994aSatish Sampath            int shortcut = parseUrlShortcut(inUrl);
4036565505b1a2c1d3099496af5910760087602e994aSatish Sampath            if (shortcut != SHORTCUT_INVALID) {
4037565505b1a2c1d3099496af5910760087602e994aSatish Sampath                Browser.addSearchUrl(mResolver, inUrl);
4038565505b1a2c1d3099496af5910760087602e994aSatish Sampath                String query = inUrl.substring(2);
4039565505b1a2c1d3099496af5910760087602e994aSatish Sampath                switch (shortcut) {
4040565505b1a2c1d3099496af5910760087602e994aSatish Sampath                case SHORTCUT_GOOGLE_SEARCH:
404147fdfdb989ffb79590a39c40792be995de32e521Grace Kloba                    return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
4042565505b1a2c1d3099496af5910760087602e994aSatish Sampath                case SHORTCUT_WIKIPEDIA_SEARCH:
4043565505b1a2c1d3099496af5910760087602e994aSatish Sampath                    return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
4044565505b1a2c1d3099496af5910760087602e994aSatish Sampath                case SHORTCUT_DICTIONARY_SEARCH:
4045565505b1a2c1d3099496af5910760087602e994aSatish Sampath                    return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
4046565505b1a2c1d3099496af5910760087602e994aSatish Sampath                case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
40470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                    // FIXME: we need location in this case
4048565505b1a2c1d3099496af5910760087602e994aSatish Sampath                    return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
40490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                }
40500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
40510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
40520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            if (Regex.WEB_URL_PATTERN.matcher(inUrl).matches()) {
40530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                return URLUtil.guessUrl(inUrl);
40540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            }
40550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
40560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
40570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        Browser.addSearchUrl(mResolver, inUrl);
405847fdfdb989ffb79590a39c40792be995de32e521Grace Kloba        return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
40590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
40600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
4061bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch    /* package */ void setShouldShowErrorConsole(boolean flag) {
4062bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        if (flag == mShouldShowErrorConsole) {
4063bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            // Nothing to do.
4064bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            return;
4065bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        }
4066bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
4067bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        mShouldShowErrorConsole = flag;
4068bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
4069bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
4070bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
4071bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        if (flag) {
4072bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            // Setting the show state of the console will cause it's the layout to be inflated.
4073bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            if (errorConsole.numberOfErrors() > 0) {
4074bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
4075bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            } else {
4076bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
4077bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            }
4078bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
4079bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            // Now we can add it to the main view.
4080bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            mErrorConsoleContainer.addView(errorConsole,
4081bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
4082bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch                                                  ViewGroup.LayoutParams.WRAP_CONTENT));
4083bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        } else {
4084bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            mErrorConsoleContainer.removeView(errorConsole);
4085bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch        }
4086bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
4087bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch    }
4088bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
40890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final static int LOCK_ICON_UNSECURE = 0;
40900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final static int LOCK_ICON_SECURE   = 1;
40910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final static int LOCK_ICON_MIXED    = 2;
40920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
40930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private int mLockIconType = LOCK_ICON_UNSECURE;
40940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private int mPrevLockType = LOCK_ICON_UNSECURE;
40950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
40960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private BrowserSettings mSettings;
40970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private TabControl      mTabControl;
40980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private ContentResolver mResolver;
40990c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private FrameLayout     mContentView;
4100adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu    private View            mCustomView;
4101adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu    private FrameLayout     mCustomViewContainer;
4102c9b5556351b59caec77b4eec1ad30652fec6a4d9Andrei Popescu    private WebChromeClient.CustomViewCallback mCustomViewCallback;
41030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
41050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // view, we should rewrite this.
41060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private int mCurrentMenuState = 0;
41070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private int mMenuState = R.id.MAIN_MENU;
4108adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu    private int mOldMenuState = EMPTY_MENU;
41090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private static final int EMPTY_MENU = -1;
41100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Menu mMenu;
41110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private FindDialog mFindDialog;
41130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Used to prevent chording to result in firing two shortcuts immediately
41140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // one after another.  Fixes bug 1211714.
41150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    boolean mCanChord;
41160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private boolean mInLoad;
41180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private boolean mIsNetworkUp;
41190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private boolean mPageStarted;
41210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private boolean mActivityInPause = true;
41220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private boolean mMenuIsDown;
41240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final KeyTracker mKeyTracker = new KeyTracker(this);
41260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // As trackball doesn't send repeat down, we have to track it ourselves
41280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private boolean mTrackTrackball;
41290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private static boolean mInTrace;
41310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Performance probe
41330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private static final int[] SYSTEM_CPU_FORMAT = new int[] {
41340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
41350c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
41360c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
41370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
41380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
41390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
41400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
41410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG  // 7: softirq time
41420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    };
41430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mStart;
41450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mProcessStart;
41460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mUserStart;
41470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mSystemStart;
41480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mIdleStart;
41490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mIrqStart;
41500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private long mUiStart;
41520c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Drawable    mMixLockIcon;
41540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Drawable    mSecLockIcon;
41550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Drawable    mGenericFavicon;
41560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* hold a ref so we can auto-cancel if necessary */
41580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private AlertDialog mAlertDialog;
41590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Wait for credentials before loading google.com
41610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private ProgressDialog mCredsDlg;
41620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // The up-to-date URL and title (these can be different from those stored
41640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // in WebView, since it takes some time for the information in WebView to
41650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // get updated)
41660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private String mUrl;
41670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private String mTitle;
41680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // As PageInfo has different style for landscape / portrait, we have
41700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // to re-open it when configuration changed
41710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private AlertDialog mPageInfoDialog;
41720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private TabControl.Tab mPageInfoView;
41730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // If the Page-Info dialog is launched from the SSL-certificate-on-error
41740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // dialog, we should not just dismiss it, but should get back to the
41750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // SSL-certificate-on-error dialog. This flag is used to store this state
41760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Boolean mPageInfoFromShowSSLCertificateOnError;
41770c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // as SSLCertificateOnError has different style for landscape / portrait,
41790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // we have to re-open it when configuration changed
41800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private AlertDialog mSSLCertificateOnErrorDialog;
41810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private WebView mSSLCertificateOnErrorView;
41820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private SslErrorHandler mSSLCertificateOnErrorHandler;
41830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private SslError mSSLCertificateOnErrorError;
41840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // as SSLCertificate has different style for landscape / portrait, we
41860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // have to re-open it when configuration changed
41870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private AlertDialog mSSLCertificateDialog;
41880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private TabControl.Tab mSSLCertificateView;
41890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // as HttpAuthentication has different style for landscape / portrait, we
41910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // have to re-open it when configuration changed
41920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private AlertDialog mHttpAuthenticationDialog;
41930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private HttpAuthHandler mHttpAuthHandler;
41940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
41950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
41960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            new FrameLayout.LayoutParams(
41970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            ViewGroup.LayoutParams.FILL_PARENT,
41980c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project                                            ViewGroup.LayoutParams.FILL_PARENT);
4199adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu    /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4200adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                                            new FrameLayout.LayoutParams(
4201adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                                            ViewGroup.LayoutParams.FILL_PARENT,
4202adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                                            ViewGroup.LayoutParams.FILL_PARENT,
4203adc008d2d87676a2a32e93afba20156a2bf2355bAndrei Popescu                                            Gravity.CENTER);
420447fdfdb989ffb79590a39c40792be995de32e521Grace Kloba    // Google search
420547fdfdb989ffb79590a39c40792be995de32e521Grace Kloba    final static String QuickSearch_G = "http://www.google.com/m?q=%s";
42060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Wikipedia search
42070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
42080c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Dictionary search
42090c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
42100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // Google Mobile Local search
42110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
42120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String QUERY_PLACE_HOLDER = "%s";
42140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // "source" parameter for Google search through search key
42160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
42170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // "source" parameter for Google search through goto menu
42180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
42190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // "source" parameter for Google search through simplily type
42200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
42210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // "source" parameter for Google search suggested by the browser
42220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
42230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // "source" parameter for Google search from unknown source
42240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
42250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42260c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final static String LOGTAG = "browser";
42270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private String mLastEnteredUrl;
42290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private PowerManager.WakeLock mWakeLock;
42310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
42320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42330c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private Toast mStopToast;
42340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42351f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    private TitleBarSet mTitleBar;
423681db366d79bb317de62fed05eb58906abd88a75eLeon Scroggins
4237bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch    private LinearLayout mErrorConsoleContainer = null;
4238bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch    private boolean mShouldShowErrorConsole = false;
4239bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch
42400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // As the ids are dynamically created, we can't guarantee that they will
42410c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // be in sequence, so this static array maps ids to a window number.
42420c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
42430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
42440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
42450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project      R.id.window_seven_menu_id, R.id.window_eight_menu_id };
42460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
42470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // monitor platform changes
42480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private IntentFilter mNetworkStateChangedFilter;
42490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    private BroadcastReceiver mNetworkStateIntentReceiver;
42500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
4251b4da0ad0351996f639b50f9f29b123a9b5c24fd0Grace Kloba    private BroadcastReceiver mPackageInstallationReceiver;
4252b4da0ad0351996f639b50f9f29b123a9b5c24fd0Grace Kloba
42530c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    // activity requestCode
425478a98e459323f938e97789a6131beddbebb459ceNicolas Roard    final static int COMBO_PAGE                 = 1;
425578a98e459323f938e97789a6131beddbebb459ceNicolas Roard    final static int DOWNLOAD_PAGE              = 2;
425678a98e459323f938e97789a6131beddbebb459ceNicolas Roard    final static int PREFERENCES_PAGE           = 3;
42570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
425825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    /**
425925ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima     * A UrlData class to abstract how the content will be set to WebView.
426025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima     * This base class uses loadUrl to show the content.
426125ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima     */
426225ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    private static class UrlData {
426325ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        String mUrl;
426460e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba        byte[] mPostData;
426560e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba
426625ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        UrlData(String url) {
426725ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            this.mUrl = url;
426825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        }
426960e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba
427060e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba        void setPostData(byte[] postData) {
427160e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba            mPostData = postData;
427260e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba        }
427360e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba
427425ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        boolean isEmpty() {
427525ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            return mUrl == null || mUrl.length() == 0;
427625ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        }
427725ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima
42787944b7def8b04c3dc0cb8815a8527aba8c91b219Mitsuru Oshima        public void loadIn(WebView webView) {
427960e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba            if (mPostData != null) {
428060e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba                webView.postUrl(mUrl, mPostData);
428160e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba            } else {
428260e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba                webView.loadUrl(mUrl);
428360e095c190496d42327f8ec2ef990c044d15a76fGrace Kloba            }
428425ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        }
428525ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    };
428625ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima
428725ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    /**
428825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima     * A subclass of UrlData class that can display inlined content using
428925ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima     * {@link WebView#loadDataWithBaseURL(String, String, String, String, String)}.
429025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima     */
429125ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    private static class InlinedUrlData extends UrlData {
429225ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        InlinedUrlData(String inlined, String mimeType, String encoding, String failUrl) {
429325ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            super(failUrl);
429425ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            mInlined = inlined;
429525ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            mMimeType = mimeType;
429625ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            mEncoding = encoding;
429725ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        }
429825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        String mMimeType;
429925ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        String mInlined;
430025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        String mEncoding;
43017944b7def8b04c3dc0cb8815a8527aba8c91b219Mitsuru Oshima        @Override
430225ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        boolean isEmpty() {
4303bff2d603c022691237c31d9a57ad8c217c6e7e11Ben Murdoch            return mInlined == null || mInlined.length() == 0 || super.isEmpty();
430425ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        }
430525ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima
43067944b7def8b04c3dc0cb8815a8527aba8c91b219Mitsuru Oshima        @Override
43077944b7def8b04c3dc0cb8815a8527aba8c91b219Mitsuru Oshima        public void loadIn(WebView webView) {
430825ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima            webView.loadDataWithBaseURL(null, mInlined, mMimeType, mEncoding, mUrl);
430925ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima        }
431025ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima    }
431125ad8ab821d67293a5f53e8d341492be06e187eeMitsuru Oshima
43121f005d370557b6e69d2de6537e5ea21f73a7ec3fLeon Scroggins    /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
43130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project}
4314