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-13 07:18:33 -07:00
|
|
|
import android.os.AsyncTask;
|
2011-10-26 14:33:16 -07:00
|
|
|
import android.provider.Browser;
|
2011-10-27 06:18:34 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.util.Stack;
|
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;
|
|
|
|
private Drawable mThumbnail;
|
2011-10-27 06:18:34 -07:00
|
|
|
private Stack<HistoryEntry> mHistory;
|
|
|
|
private boolean mLoading;
|
|
|
|
private boolean mBookmark;
|
2011-10-14 22:35:15 -07:00
|
|
|
|
|
|
|
static class HistoryEntry {
|
|
|
|
public final String mUri;
|
|
|
|
public final String mTitle;
|
|
|
|
|
|
|
|
public HistoryEntry(String uri, String title) {
|
|
|
|
mUri = uri;
|
|
|
|
mTitle = title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tab() {
|
2011-10-27 06:18:34 -07:00
|
|
|
this.mId = -1;
|
|
|
|
this.mUrl = new String();
|
|
|
|
this.mTitle = new String();
|
|
|
|
this.mFavicon = null;
|
|
|
|
this.mThumbnail = null;
|
|
|
|
this.mHistory = new Stack<HistoryEntry>();
|
|
|
|
this.mBookmark = false;
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tab(int id, String url) {
|
2011-10-27 06:18:34 -07:00
|
|
|
this.mId = id;
|
|
|
|
this.mUrl = new String(url);
|
|
|
|
this.mTitle = new String();
|
|
|
|
this.mFavicon = null;
|
|
|
|
this.mThumbnail = null;
|
|
|
|
this.mHistory = new Stack<HistoryEntry>();
|
|
|
|
this.mBookmark = false;
|
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-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-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 Stack<HistoryEntry> getHistory() {
|
2011-10-27 06:18:34 -07:00
|
|
|
return mHistory;
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateURL(String url) {
|
2011-10-14 23:20:23 -07:00
|
|
|
|
|
|
|
if(url != null && url.length() > 0) {
|
2011-10-27 06:18:34 -07:00
|
|
|
this.mUrl = new String(url);
|
|
|
|
Log.i(LOG_NAME, "Updated url: " + url + " for tab with id: " + this.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-14 23:20:23 -07:00
|
|
|
if(title != null && title.length() > 0) {
|
2011-10-27 06:18:34 -07:00
|
|
|
this.mTitle = new String(title);
|
|
|
|
Log.i(LOG_NAME, "Updated title: " + title + " for tab with id: " + this.mId);
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
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
|
|
|
this.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
|
|
|
this.mBookmark = bookmark;
|
2011-10-26 14:33:16 -07:00
|
|
|
}
|
|
|
|
|
2011-10-14 22:35:15 -07:00
|
|
|
public void addHistory(HistoryEntry entry) {
|
2011-10-27 06:18:34 -07:00
|
|
|
if (mHistory.empty() || !mHistory.peek().mUri.equals(entry.mUri)) {
|
|
|
|
mHistory.push(entry);
|
2011-10-14 22:35:15 -07:00
|
|
|
new HistoryEntryTask().execute(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public HistoryEntry getLastHistoryEntry() {
|
2011-10-27 06:18:34 -07:00
|
|
|
if (mHistory.empty())
|
2011-10-14 22:35:15 -07:00
|
|
|
return null;
|
2011-10-27 06:18:34 -07:00
|
|
|
return mHistory.peek();
|
2011-10-14 22:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateFavicon(Drawable favicon) {
|
2011-10-27 06:18:34 -07:00
|
|
|
this.mFavicon = favicon;
|
|
|
|
Log.i(LOG_NAME, "Updated favicon for tab with id: " + this.mId);
|
2011-10-13 07:18:33 -07:00
|
|
|
}
|
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-27 06:18:34 -07:00
|
|
|
if (mHistory.empty())
|
2011-10-13 07:18:33 -07:00
|
|
|
return false;
|
2011-10-13 11:13:41 -07:00
|
|
|
GeckoEvent e = new GeckoEvent("session-reload", "");
|
|
|
|
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-27 06:18:34 -07:00
|
|
|
if (mHistory.size() <= 1) {
|
2011-10-13 07:18:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
2011-10-27 06:18:34 -07:00
|
|
|
mHistory.pop();
|
2011-10-13 11:13:41 -07:00
|
|
|
GeckoEvent e = new GeckoEvent("session-back", "");
|
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
2011-10-13 07:18:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class HistoryEntryTask extends AsyncTask<HistoryEntry, Void, Void> {
|
|
|
|
protected Void doInBackground(HistoryEntry... entries) {
|
|
|
|
HistoryEntry entry = entries[0];
|
2011-10-25 08:39:32 -07:00
|
|
|
GlobalHistory.getInstance().add(entry.mUri);
|
2011-10-13 07:18:33 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2011-10-26 14:33:16 -07:00
|
|
|
|
|
|
|
private class CheckBookmarkTask extends AsyncTask<Void, Void, Boolean> {
|
|
|
|
@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);
|
|
|
|
if (cursor.getCount() == 1)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean isBookmark) {
|
|
|
|
setBookmark(isBookmark.booleanValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AddBookmarkTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void unused) {
|
|
|
|
setBookmark(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class RemoveBookmarkTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
@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-10-14 22:35:15 -07:00
|
|
|
}
|