2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-02-06 19:53:01 -08:00
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
|
|
|
import org.mozilla.gecko.gfx.Layer;
|
2013-03-15 03:52:53 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2013-04-08 00:51:56 -07:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.content.ContentResolver;
|
2013-04-16 14:34:46 -07:00
|
|
|
import android.content.Context;
|
2011-11-29 19:54:07 -08:00
|
|
|
import android.graphics.Bitmap;
|
2012-04-23 10:29:14 -07:00
|
|
|
import android.graphics.Color;
|
2012-01-17 10:26:58 -08:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2012-10-31 08:44:53 -07:00
|
|
|
import android.os.Build;
|
2013-02-24 20:51:05 -08:00
|
|
|
import android.text.TextUtils;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.util.Log;
|
2012-01-31 06:30:47 -08:00
|
|
|
import android.view.View;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-04-08 00:51:56 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2012-10-09 11:26:33 -07:00
|
|
|
public class Tab {
|
2011-11-18 10:28:17 -08:00
|
|
|
private static final String LOGTAG = "GeckoTab";
|
|
|
|
|
2012-04-23 10:29:14 -07:00
|
|
|
private static Pattern sColorPattern;
|
2012-08-09 13:52:58 -07:00
|
|
|
private final int mId;
|
|
|
|
private long mLastUsed;
|
2011-11-18 10:28:17 -08:00
|
|
|
private String mUrl;
|
2013-04-26 16:17:37 -07:00
|
|
|
private String mBaseDomain;
|
2013-04-10 11:02:05 -07:00
|
|
|
private String mUserSearch;
|
2011-11-18 10:28:17 -08:00
|
|
|
private String mTitle;
|
2012-11-05 03:42:25 -08:00
|
|
|
private Bitmap mFavicon;
|
2011-11-18 10:28:17 -08:00
|
|
|
private String mFaviconUrl;
|
2012-05-29 14:10:48 -07:00
|
|
|
private int mFaviconSize;
|
2013-04-03 12:34:51 -07:00
|
|
|
private boolean mFeedsEnabled;
|
2012-05-02 09:07:58 -07:00
|
|
|
private JSONObject mIdentityData;
|
2012-06-02 11:23:45 -07:00
|
|
|
private boolean mReaderEnabled;
|
2012-07-24 10:31:51 -07:00
|
|
|
private BitmapDrawable mThumbnail;
|
2011-11-18 10:28:17 -08:00
|
|
|
private int mHistoryIndex;
|
2012-05-16 23:07:55 -07:00
|
|
|
private int mHistorySize;
|
2011-12-19 10:44:52 -08:00
|
|
|
private int mParentId;
|
|
|
|
private boolean mExternal;
|
2011-11-18 10:28:17 -08:00
|
|
|
private boolean mBookmark;
|
2012-06-20 11:10:08 -07:00
|
|
|
private boolean mReadingListItem;
|
2011-11-18 10:28:17 -08:00
|
|
|
private long mFaviconLoadId;
|
2011-12-13 16:23:35 -08:00
|
|
|
private String mDocumentURI;
|
|
|
|
private String mContentType;
|
2012-01-24 16:31:33 -08:00
|
|
|
private boolean mHasTouchListeners;
|
2012-08-07 07:39:03 -07:00
|
|
|
private ZoomConstraints mZoomConstraints;
|
2013-04-25 10:47:08 -07:00
|
|
|
private boolean mIsRTL;
|
2012-01-31 06:30:47 -08:00
|
|
|
private ArrayList<View> mPluginViews;
|
2012-04-27 13:04:47 -07:00
|
|
|
private HashMap<Object, Layer> mPluginLayers;
|
2013-04-16 14:34:46 -07:00
|
|
|
private int mBackgroundColor;
|
2012-03-07 13:58:31 -08:00
|
|
|
private int mState;
|
2012-06-14 09:08:51 -07:00
|
|
|
private Bitmap mThumbnailBitmap;
|
2012-06-22 12:53:40 -07:00
|
|
|
private boolean mDesktopMode;
|
2012-09-11 10:51:44 -07:00
|
|
|
private boolean mEnteringReaderMode;
|
2013-07-15 12:12:15 -07:00
|
|
|
private Context mAppContext;
|
2013-02-06 19:53:01 -08:00
|
|
|
private static final int MAX_HISTORY_LIST_SIZE = 50;
|
2012-03-07 13:58:31 -08:00
|
|
|
|
|
|
|
public static final int STATE_DELAYED = 0;
|
|
|
|
public static final int STATE_LOADING = 1;
|
2012-03-07 13:58:31 -08:00
|
|
|
public static final int STATE_SUCCESS = 2;
|
|
|
|
public static final int STATE_ERROR = 3;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-04-16 14:34:46 -07:00
|
|
|
public Tab(Context context, int id, String url, boolean external, int parentId, String title) {
|
2013-07-15 12:12:15 -07:00
|
|
|
mAppContext = context.getApplicationContext();
|
2011-11-18 10:28:17 -08:00
|
|
|
mId = id;
|
2012-08-09 13:52:58 -07:00
|
|
|
mLastUsed = 0;
|
2011-11-18 10:28:17 -08:00
|
|
|
mUrl = url;
|
2013-04-26 16:17:37 -07:00
|
|
|
mBaseDomain = "";
|
2013-04-10 11:02:05 -07:00
|
|
|
mUserSearch = "";
|
2011-12-19 10:44:52 -08:00
|
|
|
mExternal = external;
|
|
|
|
mParentId = parentId;
|
2012-08-09 13:52:58 -07:00
|
|
|
mTitle = title == null ? "" : title;
|
2011-11-18 10:28:17 -08:00
|
|
|
mFavicon = null;
|
|
|
|
mFaviconUrl = null;
|
2012-05-29 14:10:48 -07:00
|
|
|
mFaviconSize = 0;
|
2013-04-03 12:34:51 -07:00
|
|
|
mFeedsEnabled = false;
|
2012-05-02 09:07:58 -07:00
|
|
|
mIdentityData = null;
|
2012-06-02 11:23:45 -07:00
|
|
|
mReaderEnabled = false;
|
2012-09-11 10:51:44 -07:00
|
|
|
mEnteringReaderMode = false;
|
2011-11-18 10:28:17 -08:00
|
|
|
mThumbnail = null;
|
|
|
|
mHistoryIndex = -1;
|
2012-05-16 23:07:55 -07:00
|
|
|
mHistorySize = 0;
|
2011-11-18 10:28:17 -08:00
|
|
|
mBookmark = false;
|
2012-06-20 11:10:08 -07:00
|
|
|
mReadingListItem = false;
|
2011-11-18 10:28:17 -08:00
|
|
|
mFaviconLoadId = 0;
|
2011-12-13 16:23:35 -08:00
|
|
|
mDocumentURI = "";
|
|
|
|
mContentType = "";
|
2012-08-07 07:39:03 -07:00
|
|
|
mZoomConstraints = new ZoomConstraints(false);
|
2012-01-31 06:30:47 -08:00
|
|
|
mPluginViews = new ArrayList<View>();
|
2012-04-27 13:04:47 -07:00
|
|
|
mPluginLayers = new HashMap<Object, Layer>();
|
2013-02-24 20:51:05 -08:00
|
|
|
mState = shouldShowProgress(url) ? STATE_SUCCESS : STATE_LOADING;
|
2013-04-16 14:34:46 -07:00
|
|
|
|
|
|
|
// At startup, the background is set to a color specified by LayerView
|
|
|
|
// when the LayerView is created. Shortly after, this background color
|
|
|
|
// will be used before the tab's content is shown.
|
|
|
|
mBackgroundColor = getBackgroundColorForUrl(url);
|
2013-01-25 10:51:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private ContentResolver getContentResolver() {
|
2013-07-15 12:12:15 -07:00
|
|
|
return mAppContext.getContentResolver();
|
2012-02-14 16:23:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onDestroy() {
|
2012-10-25 09:57:06 -07:00
|
|
|
Tabs.getInstance().notifyListeners(this, Tabs.TabEvents.CLOSED);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getId() {
|
|
|
|
return mId;
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized void onChange() {
|
|
|
|
mLastUsed = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized long getLastUsed() {
|
|
|
|
return mLastUsed;
|
|
|
|
}
|
|
|
|
|
2011-12-19 10:44:52 -08:00
|
|
|
public int getParentId() {
|
|
|
|
return mParentId;
|
|
|
|
}
|
|
|
|
|
2012-03-05 13:20:04 -08:00
|
|
|
// may be null if user-entered query hasn't yet been resolved to a URI
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized String getURL() {
|
2011-11-18 10:28:17 -08:00
|
|
|
return mUrl;
|
|
|
|
}
|
|
|
|
|
2013-04-10 11:02:05 -07:00
|
|
|
// mUserSearch should never be null, but it may be an empty string
|
|
|
|
public synchronized String getUserSearch() {
|
|
|
|
return mUserSearch;
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
// mTitle should never be null, but it may be an empty string
|
|
|
|
public synchronized String getTitle() {
|
|
|
|
return mTitle;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public String getDisplayTitle() {
|
|
|
|
if (mTitle != null && mTitle.length() > 0) {
|
|
|
|
return mTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mUrl;
|
|
|
|
}
|
|
|
|
|
2013-04-26 16:17:37 -07:00
|
|
|
public String getBaseDomain() {
|
|
|
|
return mBaseDomain;
|
|
|
|
}
|
|
|
|
|
2012-11-05 03:42:25 -08:00
|
|
|
public Bitmap getFavicon() {
|
2011-11-18 10:28:17 -08:00
|
|
|
return mFavicon;
|
|
|
|
}
|
|
|
|
|
2011-11-29 19:54:07 -08:00
|
|
|
public Drawable getThumbnail() {
|
|
|
|
return mThumbnail;
|
|
|
|
}
|
|
|
|
|
2012-12-03 11:27:34 -08:00
|
|
|
public Bitmap getThumbnailBitmap(int width, int height) {
|
|
|
|
if (mThumbnailBitmap != null) {
|
|
|
|
// Bug 787318 - Honeycomb has a bug with bitmap caching, we can't
|
|
|
|
// reuse the bitmap there.
|
|
|
|
boolean honeycomb = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
|
|
|
|
&& Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2);
|
|
|
|
boolean sizeChange = mThumbnailBitmap.getWidth() != width
|
|
|
|
|| mThumbnailBitmap.getHeight() != height;
|
|
|
|
if (honeycomb || sizeChange) {
|
|
|
|
mThumbnailBitmap = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mThumbnailBitmap == null) {
|
2013-07-04 06:53:25 -07:00
|
|
|
Bitmap.Config config = (GeckoAppShell.getScreenDepth() == 24) ?
|
|
|
|
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
|
|
|
|
mThumbnailBitmap = Bitmap.createBitmap(width, height, config);
|
2012-10-31 08:44:53 -07:00
|
|
|
}
|
2012-06-14 09:08:51 -07:00
|
|
|
|
2012-12-03 11:27:34 -08:00
|
|
|
return mThumbnailBitmap;
|
2012-06-14 09:08:51 -07:00
|
|
|
}
|
|
|
|
|
2011-11-29 19:54:07 -08:00
|
|
|
public void updateThumbnail(final Bitmap b) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-11-29 19:54:07 -08:00
|
|
|
public void run() {
|
2011-12-07 17:10:21 -08:00
|
|
|
if (b != null) {
|
2011-12-12 12:04:59 -08:00
|
|
|
try {
|
2012-06-14 09:08:51 -07:00
|
|
|
mThumbnail = new BitmapDrawable(b);
|
2012-07-24 10:31:51 -07:00
|
|
|
if (mState == Tab.STATE_SUCCESS)
|
|
|
|
saveThumbnailToDB();
|
2011-12-12 12:04:59 -08:00
|
|
|
} catch (OutOfMemoryError oom) {
|
2012-10-26 15:47:35 -07:00
|
|
|
Log.w(LOGTAG, "Unable to create/scale bitmap.", oom);
|
2011-12-12 12:04:59 -08:00
|
|
|
mThumbnail = null;
|
|
|
|
}
|
2011-12-07 17:10:21 -08:00
|
|
|
} else {
|
|
|
|
mThumbnail = null;
|
|
|
|
}
|
2012-10-25 09:57:06 -07:00
|
|
|
|
|
|
|
Tabs.getInstance().notifyListeners(Tab.this, Tabs.TabEvents.THUMBNAIL);
|
2011-11-29 19:54:07 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized String getFaviconURL() {
|
2011-11-18 10:28:17 -08:00
|
|
|
return mFaviconUrl;
|
|
|
|
}
|
|
|
|
|
2013-04-03 12:34:51 -07:00
|
|
|
public boolean getFeedsEnabled() {
|
|
|
|
return mFeedsEnabled;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public String getSecurityMode() {
|
2012-05-02 09:07:58 -07:00
|
|
|
try {
|
|
|
|
return mIdentityData.getString("mode");
|
|
|
|
} catch (Exception e) {
|
|
|
|
// If mIdentityData is null, or we get a JSONException
|
|
|
|
return SiteIdentityPopup.UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONObject getIdentityData() {
|
|
|
|
return mIdentityData;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-06-02 11:23:45 -07:00
|
|
|
public boolean getReaderEnabled() {
|
|
|
|
return mReaderEnabled;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public boolean isBookmark() {
|
|
|
|
return mBookmark;
|
|
|
|
}
|
|
|
|
|
2012-06-20 11:10:08 -07:00
|
|
|
public boolean isReadingListItem() {
|
|
|
|
return mReadingListItem;
|
|
|
|
}
|
|
|
|
|
2011-12-19 10:44:52 -08:00
|
|
|
public boolean isExternal() {
|
|
|
|
return mExternal;
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized void updateURL(String url) {
|
2011-11-18 10:28:17 -08:00
|
|
|
if (url != null && url.length() > 0) {
|
2011-11-21 11:08:34 -08:00
|
|
|
mUrl = url;
|
2011-11-18 10:28:17 -08:00
|
|
|
updateBookmark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 11:02:05 -07:00
|
|
|
private synchronized void updateUserSearch(String userSearch) {
|
|
|
|
mUserSearch = userSearch;
|
|
|
|
}
|
|
|
|
|
2011-12-13 16:23:35 -08:00
|
|
|
public void setDocumentURI(String documentURI) {
|
|
|
|
mDocumentURI = documentURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDocumentURI() {
|
|
|
|
return mDocumentURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setContentType(String contentType) {
|
2012-09-24 16:42:53 -07:00
|
|
|
mContentType = (contentType == null) ? "" : contentType;
|
2011-12-13 16:23:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getContentType() {
|
|
|
|
return mContentType;
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized void updateTitle(String title) {
|
2012-09-11 10:51:44 -07:00
|
|
|
// Keep the title unchanged while entering reader mode
|
|
|
|
if (mEnteringReaderMode)
|
|
|
|
return;
|
|
|
|
|
2011-11-21 11:08:34 -08:00
|
|
|
mTitle = (title == null ? "" : title);
|
2012-10-25 09:57:06 -07:00
|
|
|
Tabs.getInstance().notifyListeners(this, Tabs.TabEvents.TITLE);
|
2012-01-12 12:22:48 -08:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-03-07 13:58:31 -08:00
|
|
|
public void setState(int state) {
|
2012-04-27 10:52:21 -07:00
|
|
|
mState = state;
|
2012-09-11 10:51:44 -07:00
|
|
|
|
|
|
|
if (mState != Tab.STATE_LOADING)
|
|
|
|
mEnteringReaderMode = false;
|
2012-10-24 09:04:44 -07:00
|
|
|
}
|
2012-03-07 13:58:31 -08:00
|
|
|
|
|
|
|
public int getState() {
|
|
|
|
return mState;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
public void setZoomConstraints(ZoomConstraints constraints) {
|
|
|
|
mZoomConstraints = constraints;
|
2012-05-18 08:24:27 -07:00
|
|
|
}
|
|
|
|
|
2012-08-07 07:39:03 -07:00
|
|
|
public ZoomConstraints getZoomConstraints() {
|
|
|
|
return mZoomConstraints;
|
2012-05-18 08:24:27 -07:00
|
|
|
}
|
|
|
|
|
2013-04-25 10:47:08 -07:00
|
|
|
public void setIsRTL(boolean aIsRTL) {
|
|
|
|
mIsRTL = aIsRTL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getIsRTL() {
|
|
|
|
return mIsRTL;
|
|
|
|
}
|
|
|
|
|
2012-01-24 16:31:33 -08:00
|
|
|
public void setHasTouchListeners(boolean aValue) {
|
|
|
|
mHasTouchListeners = aValue;
|
|
|
|
}
|
|
|
|
|
2012-02-29 09:09:43 -08:00
|
|
|
public boolean getHasTouchListeners() {
|
2012-01-24 16:31:33 -08:00
|
|
|
return mHasTouchListeners;
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public void setFaviconLoadId(long faviconLoadId) {
|
|
|
|
mFaviconLoadId = faviconLoadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getFaviconLoadId() {
|
|
|
|
return mFaviconLoadId;
|
|
|
|
}
|
|
|
|
|
2012-11-05 03:42:25 -08:00
|
|
|
public void updateFavicon(Bitmap favicon) {
|
2011-11-18 10:28:17 -08:00
|
|
|
mFavicon = favicon;
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized void updateFaviconURL(String faviconUrl, int size) {
|
2012-05-29 14:10:48 -07:00
|
|
|
// If we already have an "any" sized icon, don't update the icon.
|
|
|
|
if (mFaviconSize == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Only update the favicon if it's bigger than the current favicon.
|
|
|
|
// We use -1 to represent icons with sizes="any".
|
2012-06-07 17:19:34 -07:00
|
|
|
if (size == -1 || size >= mFaviconSize) {
|
2012-05-29 14:10:48 -07:00
|
|
|
mFaviconUrl = faviconUrl;
|
|
|
|
mFaviconSize = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
public synchronized void clearFavicon() {
|
2012-09-11 10:51:44 -07:00
|
|
|
// Keep the favicon unchanged while entering reader mode
|
|
|
|
if (mEnteringReaderMode)
|
|
|
|
return;
|
|
|
|
|
2012-05-29 14:10:48 -07:00
|
|
|
mFavicon = null;
|
|
|
|
mFaviconUrl = null;
|
|
|
|
mFaviconSize = 0;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2013-04-03 12:34:51 -07:00
|
|
|
public void setFeedsEnabled(boolean feedsEnabled) {
|
|
|
|
mFeedsEnabled = feedsEnabled;
|
|
|
|
}
|
|
|
|
|
2012-05-02 09:07:58 -07:00
|
|
|
public void updateIdentityData(JSONObject identityData) {
|
|
|
|
mIdentityData = identityData;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-06-02 11:23:45 -07:00
|
|
|
public void setReaderEnabled(boolean readerEnabled) {
|
|
|
|
mReaderEnabled = readerEnabled;
|
2012-10-25 09:57:06 -07:00
|
|
|
Tabs.getInstance().notifyListeners(this, Tabs.TabEvents.MENU_UPDATED);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2013-01-25 10:51:41 -08:00
|
|
|
void updateBookmark() {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-10-25 09:57:06 -07:00
|
|
|
public void run() {
|
|
|
|
final String url = getURL();
|
|
|
|
if (url == null)
|
|
|
|
return;
|
2012-06-20 11:10:08 -07:00
|
|
|
|
|
|
|
if (url.equals(getURL())) {
|
2013-01-25 10:51:41 -08:00
|
|
|
mBookmark = BrowserDB.isBookmark(getContentResolver(), url);
|
|
|
|
mReadingListItem = BrowserDB.isReadingListItem(getContentResolver(), url);
|
2012-06-20 11:10:08 -07:00
|
|
|
}
|
2012-07-24 16:12:12 -07:00
|
|
|
|
|
|
|
Tabs.getInstance().notifyListeners(Tab.this, Tabs.TabEvents.MENU_UPDATED);
|
|
|
|
}
|
2012-10-25 09:57:06 -07:00
|
|
|
});
|
2012-06-20 11:10:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public void addBookmark() {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-02-14 16:23:06 -08:00
|
|
|
public void run() {
|
2012-03-05 13:20:04 -08:00
|
|
|
String url = getURL();
|
|
|
|
if (url == null)
|
|
|
|
return;
|
|
|
|
|
2013-01-25 10:51:41 -08:00
|
|
|
BrowserDB.addBookmark(getContentResolver(), mTitle, url);
|
2012-02-14 16:23:06 -08:00
|
|
|
}
|
|
|
|
});
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeBookmark() {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-02-14 16:23:06 -08:00
|
|
|
public void run() {
|
2012-03-05 13:20:04 -08:00
|
|
|
String url = getURL();
|
|
|
|
if (url == null)
|
|
|
|
return;
|
|
|
|
|
2013-01-25 10:51:41 -08:00
|
|
|
BrowserDB.removeBookmarksWithURL(getContentResolver(), url);
|
2012-02-14 16:23:06 -08:00
|
|
|
}
|
|
|
|
});
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-06-02 11:23:45 -07:00
|
|
|
public void addToReadingList() {
|
|
|
|
if (!mReaderEnabled)
|
|
|
|
return;
|
|
|
|
|
2013-03-15 13:10:06 -07:00
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
try {
|
|
|
|
json.put("tabID", String.valueOf(getId()));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "JSON error - failing to add to reading list", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Add", json.toString());
|
2012-06-12 07:03:06 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
2012-06-02 11:23:45 -07:00
|
|
|
}
|
|
|
|
|
2013-05-20 10:35:03 -07:00
|
|
|
public void toggleReaderMode() {
|
|
|
|
if (ReaderModeUtils.isAboutReader(mUrl)) {
|
2013-05-30 16:26:38 -07:00
|
|
|
Tabs.getInstance().loadUrl(ReaderModeUtils.getUrlFromAboutReader(mUrl));
|
2013-05-20 10:35:03 -07:00
|
|
|
} else if (mReaderEnabled) {
|
|
|
|
mEnteringReaderMode = true;
|
|
|
|
Tabs.getInstance().loadUrl(ReaderModeUtils.getAboutReaderForUrl(mUrl, mId, mReadingListItem));
|
|
|
|
}
|
2012-06-02 11:23:45 -07:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:51:44 -07:00
|
|
|
public boolean isEnteringReaderMode() {
|
|
|
|
return mEnteringReaderMode;
|
|
|
|
}
|
|
|
|
|
2012-08-02 14:15:17 -07:00
|
|
|
public void doReload() {
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:Reload", "");
|
2011-11-18 10:28:17 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
}
|
|
|
|
|
2012-08-02 14:15:17 -07:00
|
|
|
// Our version of nsSHistory::GetCanGoBack
|
2012-05-24 15:47:49 -07:00
|
|
|
public boolean canDoBack() {
|
2012-08-02 14:15:17 -07:00
|
|
|
return mHistoryIndex > 0;
|
2012-05-24 15:47:49 -07:00
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public boolean doBack() {
|
2012-08-02 14:15:17 -07:00
|
|
|
if (!canDoBack())
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
2012-08-02 14:15:17 -07:00
|
|
|
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:Back", "");
|
2011-11-18 10:28:17 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-06 19:53:01 -08:00
|
|
|
public boolean showBackHistory() {
|
|
|
|
if (!canDoBack())
|
|
|
|
return false;
|
|
|
|
return this.showHistory(Math.max(mHistoryIndex - MAX_HISTORY_LIST_SIZE, 0), mHistoryIndex, mHistoryIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean showForwardHistory() {
|
|
|
|
if (!canDoForward())
|
|
|
|
return false;
|
|
|
|
return this.showHistory(mHistoryIndex, Math.min(mHistorySize - 1, mHistoryIndex + MAX_HISTORY_LIST_SIZE), mHistoryIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean showAllHistory() {
|
|
|
|
if (!canDoForward() && !canDoBack())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int min = mHistoryIndex - MAX_HISTORY_LIST_SIZE / 2;
|
|
|
|
int max = mHistoryIndex + MAX_HISTORY_LIST_SIZE / 2;
|
|
|
|
if (min < 0) {
|
|
|
|
max -= min;
|
|
|
|
}
|
|
|
|
if (max > mHistorySize - 1) {
|
|
|
|
min -= max - (mHistorySize - 1);
|
|
|
|
max = mHistorySize - 1;
|
|
|
|
}
|
|
|
|
min = Math.max(min, 0);
|
|
|
|
|
|
|
|
return this.showHistory(min, max, mHistoryIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will show the history starting on fromIndex until toIndex of the history.
|
|
|
|
*/
|
|
|
|
public boolean showHistory(int fromIndex, int toIndex, int selIndex) {
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
try {
|
|
|
|
json.put("fromIndex", fromIndex);
|
|
|
|
json.put("toIndex", toIndex);
|
|
|
|
json.put("selIndex", selIndex);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "JSON error", e);
|
|
|
|
}
|
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:ShowHistory", json.toString());
|
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-02 14:15:17 -07:00
|
|
|
public void doStop() {
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:Stop", "");
|
2011-11-18 10:28:17 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
}
|
|
|
|
|
2012-08-02 14:15:17 -07:00
|
|
|
// Our version of nsSHistory::GetCanGoForward
|
2011-11-18 10:28:17 -08:00
|
|
|
public boolean canDoForward() {
|
2012-08-02 14:15:17 -07:00
|
|
|
return mHistoryIndex < mHistorySize - 1;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean doForward() {
|
2012-08-02 14:15:17 -07:00
|
|
|
if (!canDoForward())
|
2011-11-18 10:28:17 -08:00
|
|
|
return false;
|
2012-08-02 14:15:17 -07:00
|
|
|
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:Forward", "");
|
2011-11-18 10:28:17 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleSessionHistoryMessage(String event, JSONObject message) throws JSONException {
|
|
|
|
if (event.equals("New")) {
|
2012-08-02 14:15:17 -07:00
|
|
|
final String url = message.getString("url");
|
2011-11-18 10:28:17 -08:00
|
|
|
mHistoryIndex++;
|
2012-05-16 23:07:55 -07:00
|
|
|
mHistorySize = mHistoryIndex + 1;
|
2011-11-18 10:28:17 -08:00
|
|
|
} else if (event.equals("Back")) {
|
2012-08-02 14:15:17 -07:00
|
|
|
if (!canDoBack()) {
|
2012-10-26 15:47:35 -07:00
|
|
|
Log.w(LOGTAG, "Received unexpected back notification");
|
2011-11-18 10:28:17 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHistoryIndex--;
|
|
|
|
} else if (event.equals("Forward")) {
|
2012-08-02 14:15:17 -07:00
|
|
|
if (!canDoForward()) {
|
2012-10-26 15:47:35 -07:00
|
|
|
Log.w(LOGTAG, "Received unexpected forward notification");
|
2011-11-18 10:28:17 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHistoryIndex++;
|
|
|
|
} else if (event.equals("Goto")) {
|
|
|
|
int index = message.getInt("index");
|
2012-05-16 23:07:55 -07:00
|
|
|
if (index < 0 || index >= mHistorySize) {
|
2012-10-26 15:47:35 -07:00
|
|
|
Log.w(LOGTAG, "Received unexpected history-goto notification");
|
2011-11-18 10:28:17 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHistoryIndex = index;
|
|
|
|
} else if (event.equals("Purge")) {
|
2012-08-02 14:15:17 -07:00
|
|
|
int numEntries = message.getInt("numEntries");
|
|
|
|
if (numEntries > mHistorySize) {
|
2012-10-26 15:47:35 -07:00
|
|
|
Log.w(LOGTAG, "Received unexpectedly large number of history entries to purge");
|
2012-05-16 23:07:55 -07:00
|
|
|
mHistoryIndex = -1;
|
2012-08-02 14:15:17 -07:00
|
|
|
mHistorySize = 0;
|
|
|
|
return;
|
2012-05-16 23:07:55 -07:00
|
|
|
}
|
2012-08-02 14:15:17 -07:00
|
|
|
|
|
|
|
mHistorySize -= numEntries;
|
|
|
|
mHistoryIndex -= numEntries;
|
|
|
|
|
|
|
|
// If we weren't at the last history entry, mHistoryIndex may have become too small
|
|
|
|
if (mHistoryIndex < -1)
|
2013-02-06 19:53:01 -08:00
|
|
|
mHistoryIndex = -1;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 13:44:51 -07:00
|
|
|
void handleLocationChange(JSONObject message) throws JSONException {
|
|
|
|
final String uri = message.getString("uri");
|
2012-09-11 10:51:44 -07:00
|
|
|
mEnteringReaderMode = ReaderModeUtils.isEnteringReaderMode(mUrl, uri);
|
2012-08-27 13:44:51 -07:00
|
|
|
updateURL(uri);
|
2013-04-10 11:02:05 -07:00
|
|
|
updateUserSearch(message.getString("userSearch"));
|
2012-08-27 13:44:51 -07:00
|
|
|
|
|
|
|
setDocumentURI(message.getString("documentURI"));
|
2013-04-26 16:17:37 -07:00
|
|
|
mBaseDomain = message.optString("baseDomain");
|
2012-08-27 13:44:51 -07:00
|
|
|
if (message.getBoolean("sameDocument")) {
|
|
|
|
// We can get a location change event for the same document with an anchor tag
|
2013-04-16 15:07:11 -07:00
|
|
|
// Notify listeners so that buttons like back or forward will update themselves
|
|
|
|
Tabs.getInstance().notifyListeners(this, Tabs.TabEvents.LOCATION_CHANGE, uri);
|
2012-08-27 13:44:51 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setContentType(message.getString("contentType"));
|
|
|
|
clearFavicon();
|
2013-04-03 12:34:51 -07:00
|
|
|
setFeedsEnabled(false);
|
2012-08-27 13:44:51 -07:00
|
|
|
updateTitle(null);
|
|
|
|
updateIdentityData(null);
|
|
|
|
setReaderEnabled(false);
|
|
|
|
setZoomConstraints(new ZoomConstraints(true));
|
|
|
|
setHasTouchListeners(false);
|
2013-04-16 14:34:46 -07:00
|
|
|
setBackgroundColor(getBackgroundColorForUrl(uri));
|
2012-08-27 13:44:51 -07:00
|
|
|
|
2012-10-25 09:57:06 -07:00
|
|
|
Tabs.getInstance().notifyListeners(this, Tabs.TabEvents.LOCATION_CHANGE, uri);
|
2012-08-27 13:44:51 -07:00
|
|
|
}
|
|
|
|
|
2013-02-24 20:51:05 -08:00
|
|
|
private boolean shouldShowProgress(String url) {
|
|
|
|
return "about:home".equals(url) || ReaderModeUtils.isAboutReader(url);
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:34:46 -07:00
|
|
|
private int getBackgroundColorForUrl(String url) {
|
|
|
|
if ("about:home".equals(url)) {
|
2013-07-15 12:12:15 -07:00
|
|
|
return mAppContext.getResources().getColor(R.color.background_normal);
|
2013-04-16 14:34:46 -07:00
|
|
|
}
|
|
|
|
return Color.WHITE;
|
|
|
|
}
|
|
|
|
|
2013-02-24 20:51:05 -08:00
|
|
|
void handleDocumentStart(boolean showProgress, String url) {
|
|
|
|
setState(shouldShowProgress(url) ? STATE_SUCCESS : STATE_LOADING);
|
|
|
|
updateIdentityData(null);
|
|
|
|
setReaderEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDocumentStop(boolean success) {
|
|
|
|
setState(success ? STATE_SUCCESS : STATE_ERROR);
|
|
|
|
|
|
|
|
final String oldURL = getURL();
|
|
|
|
final Tab tab = this;
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.getBackgroundHandler().postDelayed(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2013-02-24 20:51:05 -08:00
|
|
|
public void run() {
|
|
|
|
// tab.getURL() may return null
|
|
|
|
if (!TextUtils.equals(oldURL, getURL()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ThumbnailHelper.getInstance().getAndProcessThumbnailFor(tab);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:33 -07:00
|
|
|
protected void saveThumbnailToDB() {
|
2011-11-29 22:02:55 -08:00
|
|
|
try {
|
2012-03-05 13:20:04 -08:00
|
|
|
String url = getURL();
|
|
|
|
if (url == null)
|
|
|
|
return;
|
|
|
|
|
2013-01-25 10:51:41 -08:00
|
|
|
BrowserDB.updateThumbnailForUrl(getContentResolver(), url, mThumbnail);
|
2011-11-30 00:13:36 -08:00
|
|
|
} catch (Exception e) {
|
2011-11-29 22:02:55 -08:00
|
|
|
// ignore
|
|
|
|
}
|
2011-11-29 19:54:07 -08:00
|
|
|
}
|
|
|
|
|
2012-01-31 06:30:47 -08:00
|
|
|
public void addPluginView(View view) {
|
|
|
|
mPluginViews.add(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removePluginView(View view) {
|
|
|
|
mPluginViews.remove(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public View[] getPluginViews() {
|
|
|
|
return mPluginViews.toArray(new View[mPluginViews.size()]);
|
|
|
|
}
|
2012-01-31 06:40:58 -08:00
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
public void addPluginLayer(Object surfaceOrView, Layer layer) {
|
2012-06-15 08:27:40 -07:00
|
|
|
synchronized(mPluginLayers) {
|
|
|
|
mPluginLayers.put(surfaceOrView, layer);
|
|
|
|
}
|
2012-01-31 06:40:58 -08:00
|
|
|
}
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
public Layer getPluginLayer(Object surfaceOrView) {
|
2012-06-15 08:27:40 -07:00
|
|
|
synchronized(mPluginLayers) {
|
|
|
|
return mPluginLayers.get(surfaceOrView);
|
|
|
|
}
|
2012-01-31 06:40:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Layer> getPluginLayers() {
|
2012-06-15 08:27:40 -07:00
|
|
|
synchronized(mPluginLayers) {
|
|
|
|
return new ArrayList<Layer>(mPluginLayers.values());
|
|
|
|
}
|
2012-01-31 06:40:58 -08:00
|
|
|
}
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
public Layer removePluginLayer(Object surfaceOrView) {
|
2012-06-15 08:27:40 -07:00
|
|
|
synchronized(mPluginLayers) {
|
|
|
|
return mPluginLayers.remove(surfaceOrView);
|
|
|
|
}
|
2012-01-31 06:40:58 -08:00
|
|
|
}
|
2012-04-23 10:29:14 -07:00
|
|
|
|
2012-12-15 07:43:57 -08:00
|
|
|
public int getBackgroundColor() {
|
|
|
|
return mBackgroundColor;
|
2012-04-23 10:29:14 -07:00
|
|
|
}
|
|
|
|
|
2012-12-15 07:43:57 -08:00
|
|
|
/** Sets a new color for the background. */
|
|
|
|
public void setBackgroundColor(int color) {
|
|
|
|
mBackgroundColor = color;
|
2012-04-23 10:29:14 -07:00
|
|
|
}
|
|
|
|
|
2012-12-15 07:43:57 -08:00
|
|
|
/** Parses and sets a new color for the background. */
|
|
|
|
public void setBackgroundColor(String newColor) {
|
|
|
|
setBackgroundColor(parseColorFromGecko(newColor));
|
2012-04-23 10:29:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses a color from an RGB triple of the form "rgb([0-9]+, [0-9]+, [0-9]+)". If the color
|
|
|
|
// cannot be parsed, returns white.
|
|
|
|
private static int parseColorFromGecko(String string) {
|
|
|
|
if (sColorPattern == null) {
|
|
|
|
sColorPattern = Pattern.compile("rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)");
|
|
|
|
}
|
|
|
|
|
|
|
|
Matcher matcher = sColorPattern.matcher(string);
|
|
|
|
if (!matcher.matches()) {
|
|
|
|
return Color.WHITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = Integer.parseInt(matcher.group(1));
|
|
|
|
int g = Integer.parseInt(matcher.group(2));
|
|
|
|
int b = Integer.parseInt(matcher.group(3));
|
|
|
|
return Color.rgb(r, g, b);
|
|
|
|
}
|
2012-06-22 12:53:40 -07:00
|
|
|
|
|
|
|
public void setDesktopMode(boolean enabled) {
|
|
|
|
mDesktopMode = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getDesktopMode() {
|
|
|
|
return mDesktopMode;
|
|
|
|
}
|
2012-10-09 11:26:33 -07:00
|
|
|
|
|
|
|
public boolean isPrivate() {
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|