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-08-03 00:13:40 +00:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2012-06-12 14:03:06 +00:00
|
|
|
|
2012-01-24 17:15:41 +00:00
|
|
|
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
|
|
|
|
2012-07-28 00:53:54 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2012-08-09 20:52:58 +00:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2012-10-05 21:53:44 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
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;
|
2012-08-09 20:52:58 +00:00
|
|
|
private final HashMap<Integer, Tab> mTabs = new HashMap<Integer, Tab>();
|
|
|
|
private final CopyOnWriteArrayList<Tab> mOrder = new CopyOnWriteArrayList<Tab>();
|
2012-07-13 18:58:42 +00:00
|
|
|
private boolean mRestoringSession;
|
2011-11-18 18:28:17 +00:00
|
|
|
|
2012-08-09 20:52:58 +00:00
|
|
|
// Keeps track of how much has happened since we last updated our persistent tab store.
|
|
|
|
private volatile int mScore = 0;
|
|
|
|
|
2012-10-05 21:51:18 +00:00
|
|
|
public static final int LOADURL_NONE = 0;
|
|
|
|
public static final int LOADURL_NEW_TAB = 1;
|
|
|
|
public static final int LOADURL_USER_ENTERED = 2;
|
2012-10-09 18:26:33 +00:00
|
|
|
public static final int LOADURL_PRIVATE = 4;
|
2012-10-05 21:51:18 +00:00
|
|
|
|
2012-08-09 20:52:58 +00:00
|
|
|
private static final int SCORE_INCREMENT_TAB_LOCATION_CHANGE = 5;
|
|
|
|
private static final int SCORE_INCREMENT_TAB_SELECTED = 10;
|
|
|
|
private static final int SCORE_THRESHOLD = 30;
|
|
|
|
|
2012-10-05 21:53:44 +00:00
|
|
|
private static AtomicInteger sTabId = new AtomicInteger(0);
|
|
|
|
|
2012-07-28 06:31:54 +00:00
|
|
|
private GeckoApp mActivity;
|
|
|
|
|
2011-11-18 18:28:17 +00:00
|
|
|
private Tabs() {
|
2012-08-03 01:38:45 +00:00
|
|
|
registerEventListener("SessionHistory:New");
|
|
|
|
registerEventListener("SessionHistory:Back");
|
|
|
|
registerEventListener("SessionHistory:Forward");
|
|
|
|
registerEventListener("SessionHistory:Goto");
|
|
|
|
registerEventListener("SessionHistory:Purge");
|
|
|
|
registerEventListener("Tab:Added");
|
|
|
|
registerEventListener("Tab:Close");
|
|
|
|
registerEventListener("Tab:Select");
|
2012-08-27 20:44:51 +00:00
|
|
|
registerEventListener("Content:LocationChange");
|
2012-08-03 01:38:45 +00:00
|
|
|
registerEventListener("Session:RestoreBegin");
|
|
|
|
registerEventListener("Session:RestoreEnd");
|
|
|
|
registerEventListener("Reader:Added");
|
|
|
|
registerEventListener("Reader:Removed");
|
|
|
|
registerEventListener("Reader:Share");
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 06:31:54 +00:00
|
|
|
public void attachToActivity(GeckoApp activity) {
|
|
|
|
mActivity = activity;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-09 18:26:33 +00:00
|
|
|
private Tab addTab(int id, String url, boolean external, int parentId, String title, boolean isPrivate) {
|
|
|
|
final Tab tab = isPrivate ? new PrivateTab(id, url, external, parentId, title) :
|
|
|
|
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) {
|
2012-07-28 06:31:54 +00:00
|
|
|
mActivity.runOnUiThread(new Runnable() {
|
2012-02-01 22:25:50 +00:00
|
|
|
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-07-28 06:31:54 +00:00
|
|
|
mActivity.runOnUiThread(new Runnable() {
|
2012-01-24 17:15:52 +00:00
|
|
|
public void run() {
|
2012-07-28 06:31:54 +00:00
|
|
|
mActivity.hideFormAssistPopup();
|
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);
|
|
|
|
|
2012-07-28 06:31:54 +00:00
|
|
|
mActivity.runOnUiThread(new Runnable() {
|
2012-01-24 17:15:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-09 20:52:58 +00:00
|
|
|
public Iterable<Tab> getTabsInOrder() {
|
2012-07-13 18:58:42 +00:00
|
|
|
return mOrder;
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ContentResolver getContentResolver() {
|
2012-08-09 20:52:58 +00:00
|
|
|
return mActivity.getContentResolver();
|
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");
|
2012-10-05 21:53:44 +00:00
|
|
|
|
|
|
|
int id = message.getInt("tabID");
|
|
|
|
Tab tab = null;
|
|
|
|
|
|
|
|
if (mTabs.containsKey(id)) {
|
|
|
|
tab = mTabs.get(id);
|
|
|
|
} else {
|
|
|
|
tab = addTab(id,
|
|
|
|
message.isNull("uri") ? null : message.getString("uri"),
|
|
|
|
message.getBoolean("external"),
|
|
|
|
message.getInt("parentId"),
|
2012-10-09 18:26:33 +00:00
|
|
|
message.getString("title"),
|
|
|
|
message.getBoolean("isPrivate"));
|
2012-10-05 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 17:16:26 +00:00
|
|
|
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-08-27 20:44:51 +00:00
|
|
|
} else if (event.equals("Content:LocationChange")) {
|
|
|
|
Tab tab = getTab(message.getInt("tabID"));
|
|
|
|
if (tab != null) {
|
|
|
|
tab.handleLocationChange(message);
|
|
|
|
}
|
2012-02-01 22:25:50 +00:00
|
|
|
} else if (event.equals("Session:RestoreBegin")) {
|
|
|
|
mRestoringSession = true;
|
|
|
|
} else if (event.equals("Session:RestoreEnd")) {
|
|
|
|
mRestoringSession = false;
|
2012-07-28 06:31:54 +00:00
|
|
|
mActivity.runOnUiThread(new Runnable() {
|
2012-02-01 22:25:50 +00:00
|
|
|
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-08-07 14:23:20 +00:00
|
|
|
} else if (event.equals("Reader:Removed")) {
|
|
|
|
final String url = message.getString("url");
|
|
|
|
handleReaderRemoved(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) {
|
2012-07-28 06:31:54 +00:00
|
|
|
mActivity.showToast(R.string.reading_list_failed, Toast.LENGTH_SHORT);
|
2012-06-12 14:03:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
2012-08-09 20:52:58 +00:00
|
|
|
BrowserDB.addReadingListItem(getContentResolver(), title, url);
|
2012-07-28 06:31:54 +00:00
|
|
|
mActivity.showToast(R.string.reading_list_added, Toast.LENGTH_SHORT);
|
2012-06-12 14:03:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-08-07 14:23:20 +00:00
|
|
|
void handleReaderRemoved(final String url) {
|
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
2012-08-09 20:52:58 +00:00
|
|
|
BrowserDB.removeReadingListItemWithURL(getContentResolver(), url);
|
2012-08-07 14:23:20 +00:00
|
|
|
mActivity.showToast(R.string.reading_list_removed, Toast.LENGTH_SHORT);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-07-28 06:31:54 +00:00
|
|
|
mActivity.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-08-09 20:52:58 +00:00
|
|
|
onTabChanged(tab, msg, 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
|
|
|
|
2012-08-09 20:52:58 +00:00
|
|
|
private void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) {
|
|
|
|
switch(msg) {
|
|
|
|
case LOCATION_CHANGE:
|
|
|
|
mScore += SCORE_INCREMENT_TAB_LOCATION_CHANGE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// When one tab is deselected, another one is always selected, so only
|
|
|
|
// increment the score once. When tabs are added/closed, they are also
|
|
|
|
// selected/unselected, so it would be redundant to also listen
|
|
|
|
// for ADDED/CLOSED events.
|
|
|
|
case SELECTED:
|
|
|
|
mScore += SCORE_INCREMENT_TAB_SELECTED;
|
|
|
|
case UNSELECTED:
|
|
|
|
tab.onChange();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mScore > SCORE_THRESHOLD) {
|
|
|
|
persistAllTabs();
|
|
|
|
mScore = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method persists the current ordered list of tabs in our tabs content provider.
|
|
|
|
public void persistAllTabs() {
|
|
|
|
final Iterable<Tab> tabs = getTabsInOrder();
|
|
|
|
GeckoAppShell.getHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
TabsAccessor.persistLocalTabs(getContentResolver(), tabs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-08-03 01:38:45 +00:00
|
|
|
|
|
|
|
private void registerEventListener(String event) {
|
|
|
|
GeckoAppShell.getEventDispatcher().registerEventListener(event, this);
|
|
|
|
}
|
2012-10-05 21:51:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a tab with the given URL in the currently selected tab.
|
|
|
|
*
|
|
|
|
* @param url URL of page to load, or search term used if searchEngine is given
|
|
|
|
*/
|
|
|
|
public void loadUrl(String url) {
|
|
|
|
loadUrl(url, LOADURL_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a tab with the given URL.
|
|
|
|
*
|
|
|
|
* @param url URL of page to load, or search term used if searchEngine is given
|
|
|
|
* @param flags flags used to load tab
|
|
|
|
*/
|
|
|
|
public void loadUrl(String url, int flags) {
|
|
|
|
loadUrl(url, null, -1, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a tab with the given URL.
|
|
|
|
*
|
|
|
|
* @param url URL of page to load, or search term used if searchEngine is given
|
|
|
|
* @param searchEngine if given, the search engine with this name is used
|
|
|
|
* to search for the url string; if null, the URL is loaded directly
|
|
|
|
* @param parentId ID of this tab's parent, or -1 if it has no parent
|
|
|
|
* @param flags flags used to load tab
|
|
|
|
*/
|
|
|
|
public void loadUrl(String url, String searchEngine, int parentId, int flags) {
|
|
|
|
JSONObject args = new JSONObject();
|
2012-10-05 21:53:44 +00:00
|
|
|
int tabId = -1;
|
|
|
|
|
2012-10-05 21:51:18 +00:00
|
|
|
try {
|
2012-10-09 18:26:33 +00:00
|
|
|
boolean isPrivate = (flags & LOADURL_PRIVATE) != 0;
|
|
|
|
|
2012-10-05 21:51:18 +00:00
|
|
|
args.put("url", url);
|
|
|
|
args.put("engine", searchEngine);
|
|
|
|
args.put("parentId", parentId);
|
|
|
|
args.put("userEntered", (flags & LOADURL_USER_ENTERED) != 0);
|
|
|
|
args.put("newTab", (flags & LOADURL_NEW_TAB) != 0);
|
2012-10-09 18:26:33 +00:00
|
|
|
args.put("isPrivate", isPrivate);
|
2012-10-05 21:53:44 +00:00
|
|
|
|
|
|
|
if ((flags & LOADURL_NEW_TAB) != 0) {
|
|
|
|
tabId = getNextTabId();
|
|
|
|
args.put("tabID", tabId);
|
2012-10-09 18:26:33 +00:00
|
|
|
addTab(tabId, null, false, parentId, url, isPrivate);
|
2012-10-05 21:53:44 +00:00
|
|
|
}
|
2012-10-05 21:51:18 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "error building JSON arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.d(LOGTAG, "Sending message to Gecko: " + SystemClock.uptimeMillis() + " - Tab:Load");
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Load", args.toString()));
|
2012-10-05 21:53:44 +00:00
|
|
|
|
|
|
|
if (tabId != -1) {
|
|
|
|
selectTab(tabId);
|
|
|
|
}
|
2012-10-05 21:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the url as a new tab, and mark the selected tab as its "parent".
|
|
|
|
*
|
|
|
|
* If the url is already open in a tab, the existing tab is selected.
|
|
|
|
* Use this for tabs opened by the browser chrome, so users can press the
|
|
|
|
* "Back" button to return to the previous tab.
|
|
|
|
*
|
|
|
|
* @param url URL of page to load
|
|
|
|
*/
|
|
|
|
public void loadUrlInTab(String url) {
|
|
|
|
Iterable<Tab> tabs = getTabsInOrder();
|
|
|
|
for (Tab tab : tabs) {
|
|
|
|
if (url.equals(tab.getURL())) {
|
|
|
|
selectTab(tab.getId());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadUrl(url, null, getSelectedTab().getId(), LOADURL_NEW_TAB);
|
|
|
|
}
|
2012-10-05 21:53:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the next tab ID.
|
|
|
|
*
|
|
|
|
* This method is invoked via JNI.
|
|
|
|
*/
|
|
|
|
public static int getNextTabId() {
|
|
|
|
return sTabId.getAndIncrement();
|
|
|
|
}
|
2011-11-18 18:28:17 +00:00
|
|
|
}
|