2011-10-13 07:18:33 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
2011-10-14 22:35:15 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009-2010
|
2011-10-13 07:18:33 -07:00
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-10-14 22:35:15 -07:00
|
|
|
* Sriram Ramasubramanian <sriram@mozilla.com>
|
2011-10-13 07:18:33 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2011-10-27 06:18:34 -07:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentValues;
|
2011-10-26 14:33:16 -07:00
|
|
|
import android.database.Cursor;
|
2011-10-13 07:18:33 -07:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2011-10-27 06:18:34 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2011-10-26 14:33:16 -07:00
|
|
|
import android.provider.Browser;
|
2011-10-27 06:18:34 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
2011-10-31 08:02:20 -07:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2011-11-01 10:15:08 -07:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2011-10-31 08:02:20 -07:00
|
|
|
import java.util.ArrayList;
|
2011-11-07 10:27:02 -08:00
|
|
|
import java.util.HashMap;
|
2011-10-31 08:02:20 -07:00
|
|
|
import java.util.List;
|
2011-10-13 07:18:33 -07:00
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public class Tab {
|
2011-10-13 07:18:33 -07:00
|
|
|
|
2011-10-27 06:18:34 -07:00
|
|
|
private static final String LOG_NAME = "Tab";
|
2011-10-27 06:18:34 -07:00
|
|
|
private int mId;
|
2011-10-27 06:18:34 -07:00
|
|
|
private String mUrl;
|
|
|
|
private String mTitle;
|
|
|
|
private Drawable mFavicon;
|
2011-11-07 05:55:25 -08:00
|
|
|
private String mFaviconUrl;
|
2011-11-03 15:53:34 -07:00
|
|
|
private String mSecurityMode;
|
2011-10-27 06:18:34 -07:00
|
|
|
private Drawable mThumbnail;
|
2011-10-31 08:02:20 -07:00
|
|
|
private List<HistoryEntry> mHistory;
|
|
|
|
private int mHistoryIndex;
|
2011-10-27 06:18:34 -07:00
|
|
|
private boolean mLoading;
|
|
|
|
private boolean mBookmark;
|
2011-11-07 10:27:02 -08:00
|
|
|
private HashMap<String, DoorHanger> mDoorHangers;
|
2011-10-14 22:35:15 -07:00
|
|
|
|
|
|
|
static class HistoryEntry {
|
|
|
|
public final String mUri;
|
2011-10-31 08:02:20 -07:00
|
|
|
public String mTitle;
|
2011-10-14 22:35:15 -07:00
|
|
|
|
|
|
|
public HistoryEntry(String uri, String title) {
|
|
|
|
mUri = uri;
|
|
|
|
mTitle = title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tab() {
|
2011-10-31 07:49:13 -07:00
|
|
|
this(-1, "");
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tab(int id, String url) {
|
2011-10-27 06:18:34 -07:00
|
|
|
mId = id;
|
2011-10-31 07:49:13 -07:00
|
|
|
mUrl = url;
|
2011-10-27 06:18:34 -07:00
|
|
|
mTitle = new String();
|
|
|
|
mFavicon = null;
|
2011-11-07 05:55:25 -08:00
|
|
|
mFaviconUrl = null;
|
2011-11-03 15:53:34 -07:00
|
|
|
mSecurityMode = "unknown";
|
2011-10-27 06:18:34 -07:00
|
|
|
mThumbnail = null;
|
2011-10-31 08:02:20 -07:00
|
|
|
mHistory = new ArrayList<HistoryEntry>();
|
|
|
|
mHistoryIndex = -1;
|
2011-10-27 06:18:34 -07:00
|
|
|
mBookmark = false;
|
2011-11-07 10:27:02 -08:00
|
|
|
mDoorHangers = new HashMap<String, DoorHanger>();
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getId() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mId;
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getURL() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mUrl;
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getTitle() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mTitle;
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
2011-10-31 13:36:50 -07:00
|
|
|
public String getDisplayTitle() {
|
|
|
|
if (mTitle != null && mTitle.length() > 0) {
|
|
|
|
return mTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mUrl;
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:20:27 -07:00
|
|
|
public Drawable getFavicon() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mFavicon;
|
2011-10-24 12:20:27 -07:00
|
|
|
}
|
|
|
|
|
2011-11-07 05:55:25 -08:00
|
|
|
public String getFaviconURL() {
|
|
|
|
return mFaviconUrl;
|
|
|
|
}
|
|
|
|
|
2011-11-03 15:53:34 -07:00
|
|
|
public String getSecurityMode() {
|
|
|
|
return mSecurityMode;
|
|
|
|
}
|
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public boolean isLoading() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mLoading;
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 14:33:16 -07:00
|
|
|
public boolean isBookmark() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mBookmark;
|
2011-10-26 14:33:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public void updateURL(String url) {
|
2011-10-31 07:49:13 -07:00
|
|
|
if (url != null && url.length() > 0) {
|
2011-10-27 06:18:34 -07:00
|
|
|
mUrl = new String(url);
|
|
|
|
Log.i(LOG_NAME, "Updated url: " + url + " for tab with id: " + mId);
|
2011-10-26 14:33:16 -07:00
|
|
|
updateBookmark();
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateTitle(String title) {
|
2011-10-31 07:49:13 -07:00
|
|
|
if (title != null && title.length() > 0) {
|
2011-10-27 06:18:34 -07:00
|
|
|
mTitle = new String(title);
|
2011-10-31 13:36:49 -07:00
|
|
|
} else {
|
|
|
|
mTitle = "";
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
2011-10-31 13:36:49 -07:00
|
|
|
|
|
|
|
Log.i(LOG_NAME, "Updated title: " + mTitle + " for tab with id: " + mId);
|
|
|
|
|
2011-10-31 12:29:49 -07:00
|
|
|
final HistoryEntry he = getLastHistoryEntry();
|
2011-10-31 08:02:20 -07:00
|
|
|
if (he != null) {
|
2011-10-31 13:36:49 -07:00
|
|
|
he.mTitle = mTitle;
|
2011-10-31 12:29:49 -07:00
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
2011-11-10 18:34:38 -08:00
|
|
|
public void run() {
|
|
|
|
GlobalHistory.getInstance().update(he.mUri, he.mTitle);
|
|
|
|
}
|
|
|
|
});
|
2011-10-31 08:02:20 -07:00
|
|
|
} else {
|
|
|
|
Log.e(LOG_NAME, "Requested title update on empty history stack");
|
|
|
|
}
|
2011-10-13 07:18:33 -07:00
|
|
|
}
|
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public void setLoading(boolean loading) {
|
2011-10-27 06:18:34 -07:00
|
|
|
mLoading = loading;
|
2011-10-13 07:18:33 -07:00
|
|
|
}
|
|
|
|
|
2011-10-26 14:33:16 -07:00
|
|
|
private void setBookmark(boolean bookmark) {
|
2011-10-27 06:18:34 -07:00
|
|
|
mBookmark = bookmark;
|
2011-10-26 14:33:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public HistoryEntry getLastHistoryEntry() {
|
2011-10-31 08:02:20 -07:00
|
|
|
if (mHistory.isEmpty())
|
2011-10-31 07:49:13 -07:00
|
|
|
return null;
|
2011-10-31 08:02:20 -07:00
|
|
|
return mHistory.get(mHistoryIndex);
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateFavicon(Drawable favicon) {
|
2011-10-27 06:18:34 -07:00
|
|
|
mFavicon = favicon;
|
|
|
|
Log.i(LOG_NAME, "Updated favicon for tab with id: " + mId);
|
2011-10-13 07:18:33 -07:00
|
|
|
}
|
2011-11-10 18:34:38 -08:00
|
|
|
|
2011-11-07 05:55:25 -08:00
|
|
|
public void updateFaviconURL(String faviconUrl) {
|
2011-11-14 14:48:27 -08:00
|
|
|
mFaviconUrl = faviconUrl;
|
2011-11-07 05:55:25 -08:00
|
|
|
Log.i(LOG_NAME, "Updated favicon URL for tab with id: " + mId);
|
|
|
|
}
|
|
|
|
|
2011-11-03 15:53:34 -07:00
|
|
|
public void updateSecurityMode(String mode) {
|
|
|
|
mSecurityMode = mode;
|
|
|
|
}
|
|
|
|
|
2011-10-26 14:33:16 -07:00
|
|
|
private void updateBookmark() {
|
|
|
|
new CheckBookmarkTask().execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addBookmark() {
|
|
|
|
new AddBookmarkTask().execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeBookmark() {
|
|
|
|
new RemoveBookmarkTask().execute();
|
|
|
|
}
|
2011-10-13 07:18:33 -07:00
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public boolean doReload() {
|
2011-10-31 08:02:20 -07:00
|
|
|
if (mHistory.isEmpty())
|
2011-10-13 07:18:33 -07:00
|
|
|
return false;
|
2011-10-31 08:02:34 -07:00
|
|
|
GeckoEvent e = new GeckoEvent("Session:Reload", "");
|
2011-10-13 11:13:41 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
2011-10-13 07:18:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public boolean doBack() {
|
2011-10-31 08:02:20 -07:00
|
|
|
if (mHistoryIndex < 1) {
|
2011-10-13 07:18:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
2011-10-31 08:02:34 -07:00
|
|
|
GeckoEvent e = new GeckoEvent("Session:Back", "");
|
2011-10-13 11:13:41 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
2011-10-13 07:18:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-31 08:02:34 -07:00
|
|
|
public boolean canDoForward() {
|
|
|
|
return (mHistoryIndex + 1 < mHistory.size());
|
|
|
|
}
|
|
|
|
|
2011-10-31 08:02:34 -07:00
|
|
|
public boolean doForward() {
|
|
|
|
if (mHistoryIndex + 1 >= mHistory.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-31 08:02:34 -07:00
|
|
|
GeckoEvent e = new GeckoEvent("Session:Forward", "");
|
2011-10-31 08:02:34 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-07 10:27:02 -08:00
|
|
|
public void addDoorHanger(String value, DoorHanger dh) {
|
|
|
|
mDoorHangers.put(value, dh);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeDoorHanger(String value) {
|
2011-11-15 12:30:54 -08:00
|
|
|
mDoorHangers.remove(value);
|
2011-11-07 10:27:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeAllDoorHangers() {
|
2011-11-15 12:30:54 -08:00
|
|
|
mDoorHangers = new HashMap<String, DoorHanger>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeTransientDoorHangers() {
|
2011-11-11 13:44:09 -08:00
|
|
|
for (String value : mDoorHangers.keySet()) {
|
2011-11-15 12:30:54 -08:00
|
|
|
DoorHanger dh = mDoorHangers.get(value);
|
|
|
|
if (dh.shouldRemove())
|
|
|
|
mDoorHangers.remove(value);
|
|
|
|
}
|
2011-11-07 10:27:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public DoorHanger getDoorHanger(String value) {
|
|
|
|
if (mDoorHangers == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (mDoorHangers.containsKey(value))
|
|
|
|
return mDoorHangers.get(value);
|
|
|
|
|
|
|
|
return null;
|
2011-11-11 13:43:38 -08:00
|
|
|
}
|
2011-11-07 10:27:02 -08:00
|
|
|
|
|
|
|
public HashMap<String, DoorHanger> getDoorHangers() {
|
|
|
|
return mDoorHangers;
|
|
|
|
}
|
|
|
|
|
2011-10-31 08:02:20 -07:00
|
|
|
void handleSessionHistoryMessage(String event, JSONObject message) throws JSONException {
|
|
|
|
if (event.equals("New")) {
|
2011-10-31 12:29:49 -07:00
|
|
|
final String uri = message.getString("uri");
|
2011-10-31 08:02:20 -07:00
|
|
|
mHistoryIndex++;
|
|
|
|
while (mHistory.size() > mHistoryIndex) {
|
|
|
|
mHistory.remove(mHistoryIndex);
|
|
|
|
}
|
|
|
|
HistoryEntry he = new HistoryEntry(uri, null);
|
|
|
|
mHistory.add(he);
|
2011-10-31 12:29:49 -07:00
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
2011-11-10 18:34:38 -08:00
|
|
|
public void run() {
|
|
|
|
GlobalHistory.getInstance().add(uri);
|
|
|
|
}
|
|
|
|
});
|
2011-10-31 08:02:20 -07:00
|
|
|
} else if (event.equals("Back")) {
|
|
|
|
if (mHistoryIndex - 1 < 0) {
|
|
|
|
Log.e(LOG_NAME, "Received unexpected back notification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHistoryIndex--;
|
|
|
|
} else if (event.equals("Forward")) {
|
|
|
|
if (mHistoryIndex + 1 >= mHistory.size()) {
|
|
|
|
Log.e(LOG_NAME, "Received unexpected forward notification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHistoryIndex++;
|
|
|
|
} else if (event.equals("Goto")) {
|
|
|
|
int index = message.getInt("index");
|
|
|
|
if (index < 0 || index >= mHistory.size()) {
|
|
|
|
Log.e(LOG_NAME, "Received unexpected history-goto notification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mHistoryIndex = index;
|
|
|
|
} else if (event.equals("Purge")) {
|
|
|
|
mHistory.clear();
|
|
|
|
mHistoryIndex = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 18:34:38 -08:00
|
|
|
private class CheckBookmarkTask extends GeckoAsyncTask<Void, Void, Boolean> {
|
2011-10-26 14:33:16 -07:00
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... unused) {
|
|
|
|
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
|
|
|
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
|
|
|
|
null,
|
|
|
|
Browser.BookmarkColumns.URL + " = ? and " + Browser.BookmarkColumns.BOOKMARK + " = ?",
|
|
|
|
new String[] { getURL(), "1" },
|
|
|
|
Browser.BookmarkColumns.URL);
|
2011-11-06 21:40:18 -08:00
|
|
|
int count = cursor.getCount();
|
|
|
|
cursor.close();
|
|
|
|
if (count == 1)
|
2011-10-26 14:33:16 -07:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean isBookmark) {
|
|
|
|
setBookmark(isBookmark.booleanValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 18:34:38 -08:00
|
|
|
private class AddBookmarkTask extends GeckoAsyncTask<Void, Void, Void> {
|
2011-10-26 14:33:16 -07:00
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... unused) {
|
|
|
|
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
|
|
|
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
|
|
|
|
null,
|
|
|
|
Browser.BookmarkColumns.URL + " = ?",
|
|
|
|
new String[] { getURL() },
|
|
|
|
Browser.BookmarkColumns.URL);
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Browser.BookmarkColumns.BOOKMARK, "1");
|
|
|
|
values.put(Browser.BookmarkColumns.TITLE, getTitle());
|
|
|
|
|
|
|
|
if (cursor.getCount() == 1) {
|
|
|
|
//entry exists, update the bookmark flag
|
|
|
|
resolver.update(Browser.BOOKMARKS_URI,
|
|
|
|
values,
|
|
|
|
Browser.BookmarkColumns.URL + " = ?",
|
|
|
|
new String[] { getURL() });
|
|
|
|
} else {
|
|
|
|
//add a new entry
|
2011-10-27 06:18:34 -07:00
|
|
|
values.put(Browser.BookmarkColumns.URL, mUrl);
|
2011-10-26 14:33:16 -07:00
|
|
|
resolver.insert(Browser.BOOKMARKS_URI,
|
|
|
|
values);
|
2011-11-06 21:40:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cursor.close();
|
2011-10-26 14:33:16 -07:00
|
|
|
|
2011-11-06 21:40:18 -08:00
|
|
|
return null;
|
2011-10-26 14:33:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void unused) {
|
|
|
|
setBookmark(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 18:34:38 -08:00
|
|
|
private class RemoveBookmarkTask extends GeckoAsyncTask<Void, Void, Void> {
|
2011-10-26 14:33:16 -07:00
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... unused) {
|
|
|
|
ContentResolver resolver = Tabs.getInstance().getContentResolver();
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Browser.BookmarkColumns.BOOKMARK, "0");
|
|
|
|
resolver.update(Browser.BOOKMARKS_URI,
|
|
|
|
values,
|
|
|
|
Browser.BookmarkColumns.URL + " = ?",
|
|
|
|
new String[] { getURL() });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void unused) {
|
|
|
|
setBookmark(false);
|
|
|
|
}
|
|
|
|
}
|
2011-11-10 18:34:38 -08:00
|
|
|
}
|