2011-11-18 18:28:17 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 11:12:37 +00: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 18:28:17 +00:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-06-12 14:03:06 +00:00
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
|
|
|
|
2012-01-24 17:15:41 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
2012-06-20 17:12:13 +00:00
|
|
|
import android.content.Intent;
|
2012-01-24 17:16:26 +00:00
|
|
|
import android.os.SystemClock;
|
2011-11-18 18:28:17 +00:00
|
|
|
import android.util.Log;
|
2012-06-12 14:03:06 +00:00
|
|
|
import android.widget.Toast;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
public class Tabs implements GeckoEventListener {
|
|
|
|
private static final String LOGTAG = "GeckoTabs";
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
private Tab mSelectedTab;
|
|
|
|
private HashMap<Integer, Tab> mTabs;
|
|
|
|
private ArrayList<Tab> mOrder;
|
|
|
|
private ContentResolver mResolver;
|
|
|
|
private boolean mRestoringSession;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
|
|
|
private Tabs() {
|
2012-07-13 18:58:42 +00:00
|
|
|
mTabs = new HashMap<Integer, Tab>();
|
|
|
|
mOrder = new ArrayList<Tab>();
|
2011-11-18 18:28:17 +00:00
|
|
|
GeckoAppShell.registerGeckoEventListener("SessionHistory:New", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("SessionHistory:Back", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("SessionHistory:Forward", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("SessionHistory:Goto", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("SessionHistory:Purge", this);
|
2012-01-24 17:16:26 +00:00
|
|
|
GeckoAppShell.registerGeckoEventListener("Tab:Added", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("Tab:Close", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("Tab:Select", this);
|
2012-02-01 22:25:50 +00:00
|
|
|
GeckoAppShell.registerGeckoEventListener("Session:RestoreBegin", this);
|
|
|
|
GeckoAppShell.registerGeckoEventListener("Session:RestoreEnd", this);
|
2012-06-12 14:03:06 +00:00
|
|
|
GeckoAppShell.registerGeckoEventListener("Reader:Added", this);
|
2012-06-20 17:12:13 +00:00
|
|
|
GeckoAppShell.registerGeckoEventListener("Reader:Share", this);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getCount() {
|
2012-07-13 18:58:42 +00:00
|
|
|
return mTabs.size();
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 18:44:52 +00:00
|
|
|
public Tab addTab(JSONObject params) throws JSONException {
|
|
|
|
int id = params.getInt("tabID");
|
2012-07-13 18:58:42 +00:00
|
|
|
if (mTabs.containsKey(id))
|
|
|
|
return mTabs.get(id);
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2012-03-05 21:20:04 +00:00
|
|
|
// null strings return "null" (http://code.google.com/p/android/issues/detail?id=13830)
|
|
|
|
String url = params.isNull("uri") ? null : params.getString("uri");
|
2011-12-19 18:44:52 +00:00
|
|
|
Boolean external = params.getBoolean("external");
|
|
|
|
int parentId = params.getInt("parentId");
|
2011-12-21 01:41:45 +00:00
|
|
|
String title = params.getString("title");
|
2011-12-19 18:44:52 +00:00
|
|
|
|
2012-06-11 22:18:40 +00:00
|
|
|
final Tab tab = new Tab(id, url, external, parentId, title);
|
2012-07-13 18:58:42 +00:00
|
|
|
mTabs.put(id, tab);
|
|
|
|
mOrder.add(tab);
|
2012-01-24 17:16:18 +00:00
|
|
|
|
2012-02-01 22:25:50 +00:00
|
|
|
if (!mRestoringSession) {
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
2012-06-11 22:18:40 +00:00
|
|
|
notifyListeners(tab, TabEvents.ADDED);
|
2012-02-01 22:25:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-01-24 17:16:18 +00:00
|
|
|
|
2012-03-05 21:20:04 +00:00
|
|
|
Log.i(LOGTAG, "Added a tab with id: " + id);
|
2011-11-18 18:28:17 +00:00
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeTab(int id) {
|
2012-07-13 18:58:42 +00:00
|
|
|
if (mTabs.containsKey(id)) {
|
2012-06-14 16:08:51 +00:00
|
|
|
Tab tab = getTab(id);
|
2012-07-13 18:58:42 +00:00
|
|
|
mOrder.remove(tab);
|
|
|
|
mTabs.remove(id);
|
2012-06-14 16:08:51 +00:00
|
|
|
tab.freeBuffer();
|
2011-11-18 18:28:17 +00:00
|
|
|
Log.i(LOGTAG, "Removed a tab with id: " + id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tab selectTab(int id) {
|
2012-07-13 18:58:42 +00:00
|
|
|
if (!mTabs.containsKey(id))
|
2011-11-18 18:28:17 +00:00
|
|
|
return null;
|
2012-01-24 17:15:52 +00:00
|
|
|
|
2012-01-31 14:30:47 +00:00
|
|
|
final Tab oldTab = getSelectedTab();
|
2012-07-13 18:58:42 +00:00
|
|
|
final Tab tab = mTabs.get(id);
|
2012-01-24 17:15:52 +00:00
|
|
|
// This avoids a NPE below, but callers need to be careful to
|
|
|
|
// handle this case
|
|
|
|
if (tab == null)
|
|
|
|
return null;
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
mSelectedTab = tab;
|
2012-01-24 17:15:52 +00:00
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
2012-07-16 02:56:31 +00:00
|
|
|
if (GeckoApp.mAppContext.mFormAssistPopup != null)
|
|
|
|
GeckoApp.mAppContext.mFormAssistPopup.hide();
|
2012-01-24 17:15:52 +00:00
|
|
|
if (isSelectedTab(tab)) {
|
2012-03-05 21:20:04 +00:00
|
|
|
String url = tab.getURL();
|
2012-02-29 17:09:43 +00:00
|
|
|
notifyListeners(tab, TabEvents.SELECTED);
|
2012-01-31 14:30:47 +00:00
|
|
|
|
|
|
|
if (oldTab != null)
|
2012-06-11 22:18:40 +00:00
|
|
|
notifyListeners(oldTab, TabEvents.UNSELECTED);
|
2012-01-24 17:15:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass a message to Gecko to update tab state in BrowserApp
|
2012-02-09 07:18:27 +00:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Selected", String.valueOf(tab.getId())));
|
2012-05-10 17:41:00 +00:00
|
|
|
return tab;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getIndexOf(Tab tab) {
|
2012-07-13 18:58:42 +00:00
|
|
|
return mOrder.lastIndexOf(tab);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tab getTabAt(int index) {
|
2012-07-13 18:58:42 +00:00
|
|
|
if (index >= 0 && index < mOrder.size())
|
|
|
|
return mOrder.get(index);
|
2011-11-18 18:28:17 +00:00
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tab getSelectedTab() {
|
2012-07-13 18:58:42 +00:00
|
|
|
return mSelectedTab;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSelectedTab(Tab tab) {
|
2012-07-13 18:58:42 +00:00
|
|
|
if (mSelectedTab == null)
|
2012-01-24 17:15:52 +00:00
|
|
|
return false;
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
return tab == mSelectedTab;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tab getTab(int id) {
|
|
|
|
if (getCount() == 0)
|
|
|
|
return null;
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
if (!mTabs.containsKey(id))
|
2011-11-18 18:28:17 +00:00
|
|
|
return null;
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
return mTabs.get(id);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 18:44:52 +00:00
|
|
|
/** Close tab and then select the default next tab */
|
|
|
|
public void closeTab(Tab tab) {
|
|
|
|
closeTab(tab, getNextTab(tab));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Close tab and then select nextTab */
|
2012-02-15 00:23:06 +00:00
|
|
|
public void closeTab(final Tab tab, Tab nextTab) {
|
2011-12-27 06:07:41 +00:00
|
|
|
if (tab == null || nextTab == null)
|
2011-12-19 18:44:52 +00:00
|
|
|
return;
|
|
|
|
|
2012-06-15 03:12:06 +00:00
|
|
|
selectTab(nextTab.getId());
|
|
|
|
|
2012-01-24 17:15:41 +00:00
|
|
|
int tabId = tab.getId();
|
|
|
|
removeTab(tabId);
|
|
|
|
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
2012-02-29 17:09:43 +00:00
|
|
|
notifyListeners(tab, TabEvents.CLOSED);
|
2012-06-15 03:12:06 +00:00
|
|
|
tab.onDestroy();
|
2012-01-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass a message to Gecko to update tab state in BrowserApp
|
2012-02-09 07:18:27 +00:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Closed", String.valueOf(tabId)));
|
2011-12-19 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the tab that will be selected by default after this one is closed */
|
|
|
|
public Tab getNextTab(Tab tab) {
|
|
|
|
Tab selectedTab = getSelectedTab();
|
|
|
|
if (selectedTab != tab)
|
|
|
|
return selectedTab;
|
|
|
|
|
|
|
|
int index = getIndexOf(tab);
|
|
|
|
Tab nextTab = getTabAt(index + 1);
|
|
|
|
if (nextTab == null)
|
|
|
|
nextTab = getTabAt(index - 1);
|
|
|
|
|
|
|
|
Tab parent = getTab(tab.getParentId());
|
|
|
|
if (parent != null) {
|
|
|
|
// If the next tab is a sibling, switch to it. Otherwise go back to the parent.
|
|
|
|
if (nextTab != null && nextTab.getParentId() == tab.getParentId())
|
|
|
|
return nextTab;
|
|
|
|
else
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
return nextTab;
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
public HashMap<Integer, Tab> getTabs() {
|
|
|
|
if (getCount() == 0)
|
|
|
|
return null;
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
return mTabs;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Tab> getTabsInOrder() {
|
|
|
|
if (getCount() == 0)
|
|
|
|
return null;
|
|
|
|
|
2012-07-13 18:58:42 +00:00
|
|
|
return mOrder;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setContentResolver(ContentResolver resolver) {
|
2012-07-13 18:58:42 +00:00
|
|
|
mResolver = resolver;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ContentResolver getContentResolver() {
|
2012-07-13 18:58:42 +00:00
|
|
|
return mResolver;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Making Tabs a singleton class
|
|
|
|
private static class TabsInstanceHolder {
|
|
|
|
private static final Tabs INSTANCE = new Tabs();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Tabs getInstance() {
|
|
|
|
return Tabs.TabsInstanceHolder.INSTANCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeckoEventListener implementation
|
|
|
|
|
|
|
|
public void handleMessage(String event, JSONObject message) {
|
2012-01-24 17:16:26 +00:00
|
|
|
Log.i(LOGTAG, "Got message: " + event);
|
2011-11-18 18:28:17 +00:00
|
|
|
try {
|
|
|
|
if (event.startsWith("SessionHistory:")) {
|
|
|
|
Tab tab = getTab(message.getInt("tabID"));
|
|
|
|
if (tab != null) {
|
|
|
|
event = event.substring("SessionHistory:".length());
|
|
|
|
tab.handleSessionHistoryMessage(event, message);
|
|
|
|
}
|
2012-01-24 17:16:26 +00:00
|
|
|
} else if (event.equals("Tab:Added")) {
|
|
|
|
Log.i(LOGTAG, "Received message from Gecko: " + SystemClock.uptimeMillis() + " - Tab:Added");
|
|
|
|
Tab tab = addTab(message);
|
|
|
|
if (message.getBoolean("selected"))
|
|
|
|
selectTab(tab.getId());
|
2012-01-31 22:13:33 +00:00
|
|
|
if (message.getBoolean("delayLoad"))
|
2012-03-07 21:58:31 +00:00
|
|
|
tab.setState(Tab.STATE_DELAYED);
|
2012-07-02 19:42:11 +00:00
|
|
|
if (message.getBoolean("desktopMode"))
|
|
|
|
tab.setDesktopMode(true);
|
2012-01-24 17:16:26 +00:00
|
|
|
} else if (event.equals("Tab:Close")) {
|
|
|
|
Tab tab = getTab(message.getInt("tabID"));
|
|
|
|
closeTab(tab);
|
|
|
|
} else if (event.equals("Tab:Select")) {
|
|
|
|
selectTab(message.getInt("tabID"));
|
2012-02-01 22:25:50 +00:00
|
|
|
} else if (event.equals("Session:RestoreBegin")) {
|
|
|
|
mRestoringSession = true;
|
|
|
|
} else if (event.equals("Session:RestoreEnd")) {
|
|
|
|
mRestoringSession = false;
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
2012-06-11 22:18:40 +00:00
|
|
|
notifyListeners(null, TabEvents.RESTORED);
|
2012-02-01 22:25:50 +00:00
|
|
|
}
|
|
|
|
});
|
2012-06-12 14:03:06 +00:00
|
|
|
} else if (event.equals("Reader:Added")) {
|
|
|
|
final boolean success = message.getBoolean("success");
|
|
|
|
final String title = message.getString("title");
|
|
|
|
final String url = message.getString("url");
|
|
|
|
handleReaderAdded(success, title, url);
|
2012-06-20 17:12:13 +00:00
|
|
|
} else if (event.equals("Reader:Share")) {
|
|
|
|
final String title = message.getString("title");
|
|
|
|
final String url = message.getString("url");
|
|
|
|
|
|
|
|
GeckoAppShell.openUriExternal(url, "text/plain", "", "",
|
|
|
|
Intent.ACTION_SEND, title);
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.i(LOGTAG, "handleMessage throws " + e + " for message: " + event);
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 03:23:04 +00:00
|
|
|
|
2012-06-12 14:03:06 +00:00
|
|
|
void handleReaderAdded(boolean success, final String title, final String url) {
|
|
|
|
if (!success) {
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
Toast.makeText(GeckoApp.mAppContext,
|
|
|
|
R.string.reading_list_failed, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
BrowserDB.addReadingListItem(GeckoApp.mAppContext.getContentResolver(), title, url);
|
|
|
|
|
|
|
|
GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
Toast.makeText(GeckoApp.mAppContext,
|
|
|
|
R.string.reading_list_added, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-01-17 03:23:04 +00:00
|
|
|
public void refreshThumbnails() {
|
2012-07-13 18:58:42 +00:00
|
|
|
Iterator<Tab> iterator = mTabs.values().iterator();
|
2012-01-26 22:57:15 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
final Tab tab = iterator.next();
|
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
2012-04-01 19:17:32 +00:00
|
|
|
GeckoApp.mAppContext.getAndProcessThumbnailForTab(tab);
|
2012-01-26 22:57:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-01-17 03:23:04 +00:00
|
|
|
}
|
2012-02-29 17:09:43 +00:00
|
|
|
|
|
|
|
public interface OnTabsChangedListener {
|
2012-06-11 22:18:40 +00:00
|
|
|
public void onTabChanged(Tab tab, TabEvents msg, Object data);
|
2012-02-29 17:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ArrayList<OnTabsChangedListener> mTabsChangedListeners;
|
|
|
|
|
|
|
|
public static void registerOnTabsChangedListener(OnTabsChangedListener listener) {
|
|
|
|
if (mTabsChangedListeners == null)
|
|
|
|
mTabsChangedListeners = new ArrayList<OnTabsChangedListener>();
|
|
|
|
|
|
|
|
mTabsChangedListeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void unregisterOnTabsChangedListener(OnTabsChangedListener listener) {
|
|
|
|
if (mTabsChangedListeners == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mTabsChangedListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum TabEvents {
|
|
|
|
CLOSED,
|
|
|
|
START,
|
|
|
|
LOADED,
|
2012-06-11 22:18:40 +00:00
|
|
|
LOAD_ERROR,
|
2012-02-29 17:09:43 +00:00
|
|
|
STOP,
|
|
|
|
FAVICON,
|
|
|
|
THUMBNAIL,
|
|
|
|
TITLE,
|
2012-06-11 22:18:40 +00:00
|
|
|
SELECTED,
|
|
|
|
UNSELECTED,
|
|
|
|
ADDED,
|
|
|
|
RESTORED,
|
2012-07-24 23:12:12 +00:00
|
|
|
LOCATION_CHANGE,
|
|
|
|
MENU_UPDATED
|
2012-02-29 17:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyListeners(Tab tab, TabEvents msg) {
|
2012-06-11 22:18:40 +00:00
|
|
|
notifyListeners(tab, msg, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyListeners(Tab tab, TabEvents msg, Object data) {
|
2012-02-29 17:09:43 +00:00
|
|
|
if (mTabsChangedListeners == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Iterator<OnTabsChangedListener> items = mTabsChangedListeners.iterator();
|
|
|
|
while (items.hasNext()) {
|
2012-06-11 22:18:40 +00:00
|
|
|
items.next().onTabChanged(tab, msg, data);
|
2012-02-29 17:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-11 22:18:40 +00:00
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|