Bug 722413 - Refactor tabs to use bookmarks observer. r=mfinkle

This commit is contained in:
Brian Nicholson 2012-02-14 16:23:06 -08:00
parent 3d0060a1dc
commit ca4841b51c
2 changed files with 32 additions and 50 deletions

View File

@ -38,6 +38,7 @@
package org.mozilla.gecko;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
@ -88,6 +89,8 @@ public final class Tab {
private ArrayList<View> mPluginViews;
private HashMap<Surface, Layer> mPluginLayers;
private boolean mHasLoaded;
private ContentResolver mContentResolver;
private ContentObserver mContentObserver;
public static final class HistoryEntry {
public String mUri; // must never be null
@ -123,6 +126,18 @@ public final class Tab {
mPluginViews = new ArrayList<View>();
mPluginLayers = new HashMap<Surface, Layer>();
mHasLoaded = false;
mContentResolver = Tabs.getInstance().getContentResolver();
mContentObserver = new ContentObserver(GeckoAppShell.getHandler()) {
public void onChange(boolean selfChange) {
updateBookmark();
}
};
BrowserDB.registerBookmarkObserver(mContentResolver, mContentObserver);
}
public void onDestroy() {
mDoorHangers = new HashMap<String, DoorHanger>();
BrowserDB.unregisterBookmarkObserver(mContentResolver, mContentObserver);
}
public int getId() {
@ -313,10 +328,6 @@ public final class Tab {
mLoading = loading;
}
private void setBookmark(boolean bookmark) {
mBookmark = bookmark;
}
public void setHasTouchListeners(boolean aValue) {
mHasTouchListeners = aValue;
}
@ -366,11 +377,19 @@ public final class Tab {
}
public void addBookmark() {
new AddBookmarkTask().execute();
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
BrowserDB.addBookmark(mContentResolver, getTitle(), getURL());
}
});
}
public void removeBookmark() {
new RemoveBookmarkTask().execute();
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
BrowserDB.removeBookmarksWithURL(mContentResolver, getURL());
}
});
}
public boolean doReload() {
@ -417,10 +436,6 @@ public final class Tab {
mDoorHangers.remove(value);
}
public void removeAllDoorHangers() {
mDoorHangers = new HashMap<String, DoorHanger>();
}
public void removeTransientDoorHangers() {
for (String value : mDoorHangers.keySet()) {
DoorHanger dh = mDoorHangers.get(value);
@ -499,8 +514,7 @@ public final class Tab {
@Override
protected Boolean doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
return BrowserDB.isBookmark(resolver, mUrl);
return BrowserDB.isBookmark(mContentResolver, mUrl);
}
@Override
@ -519,51 +533,20 @@ public final class Tab {
if (!mUrl.equals(getURL()))
return;
setBookmark(isBookmark.booleanValue());
mBookmark = isBookmark.booleanValue();
}
});
}
}
private final class AddBookmarkTask extends GeckoAsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
BrowserDB.addBookmark(resolver, getTitle(), getURL());
return null;
}
@Override
protected void onPostExecute(Void unused) {
setBookmark(true);
}
}
private void saveThumbnailToDB(BitmapDrawable thumbnail) {
try {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
BrowserDB.updateThumbnailForUrl(resolver, getURL(), thumbnail);
BrowserDB.updateThumbnailForUrl(mContentResolver, getURL(), thumbnail);
} catch (Exception e) {
// ignore
}
}
private final class RemoveBookmarkTask extends GeckoAsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
// We want to remove all bookmarks with this URL
BrowserDB.removeBookmarksWithURL(resolver, getURL());
return null;
}
@Override
protected void onPostExecute(Void unused) {
setBookmark(false);
}
}
public void addPluginView(View view) {
mPluginViews.add(view);
}

View File

@ -187,7 +187,7 @@ public class Tabs implements GeckoEventListener {
}
/** Close tab and then select nextTab */
public void closeTab(Tab tab, Tab nextTab) {
public void closeTab(final Tab tab, Tab nextTab) {
if (tab == null || nextTab == null)
return;
@ -195,15 +195,14 @@ public class Tabs implements GeckoEventListener {
int tabId = tab.getId();
removeTab(tabId);
tab.removeAllDoorHangers();
final Tab closedTab = tab;
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
public void run() {
GeckoApp.mAppContext.onTabsChanged(closedTab);
GeckoApp.mAppContext.onTabsChanged(tab);
GeckoApp.mBrowserToolbar.updateTabCountAndAnimate(Tabs.getInstance().getCount());
GeckoApp.mDoorHangerPopup.updatePopup();
GeckoApp.mAppContext.hidePlugins(closedTab, true);
GeckoApp.mAppContext.hidePlugins(tab, true);
tab.onDestroy();
}
});