2012-03-09 10:42:43 -08: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/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.text.TextUtils;
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
2012-06-07 21:47:22 -07:00
|
|
|
import android.view.ViewGroup;
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.widget.ExpandableListView;
|
|
|
|
import android.widget.SimpleExpandableListAdapter;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
public class RemoteTabs extends ExpandableListView
|
2012-06-07 21:47:22 -07:00
|
|
|
implements TabsPanel.PanelView,
|
|
|
|
ExpandableListView.OnGroupClickListener,
|
|
|
|
ExpandableListView.OnChildClickListener,
|
|
|
|
TabsAccessor.OnQueryTabsCompleteListener {
|
2012-03-09 10:42:43 -08:00
|
|
|
private static final String LOGTAG = "GeckoRemoteTabs";
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
private Context mContext;
|
2012-07-27 23:31:40 -07:00
|
|
|
private TabsPanel mTabsPanel;
|
2012-06-07 21:47:22 -07:00
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
private static ArrayList <ArrayList <HashMap <String, String>>> mTabsList;
|
|
|
|
|
|
|
|
private static final String[] CLIENT_KEY = new String[] { "name" };
|
2013-01-10 13:06:32 -08:00
|
|
|
private static final String[] TAB_KEY = new String[] { "title", "url" };
|
2012-03-09 10:42:43 -08:00
|
|
|
private static final int[] CLIENT_RESOURCE = new int[] { R.id.client };
|
2013-01-10 13:06:32 -08:00
|
|
|
private static final int[] TAB_RESOURCE = new int[] { R.id.tab, R.id.url };
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
public RemoteTabs(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
mContext = context;
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
setOnGroupClickListener(this);
|
|
|
|
setOnChildClickListener(this);
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
@Override
|
|
|
|
public ViewGroup getLayout() {
|
|
|
|
return this;
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-07-27 23:31:40 -07:00
|
|
|
@Override
|
|
|
|
public void setTabsPanel(TabsPanel panel) {
|
|
|
|
mTabsPanel = panel;
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
@Override
|
|
|
|
public void show() {
|
2013-01-18 11:58:45 -08:00
|
|
|
setVisibility(View.VISIBLE);
|
2013-02-25 15:15:49 -08:00
|
|
|
TabsAccessor.getTabs(mContext, this);
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-07 21:47:22 -07:00
|
|
|
public void hide() {
|
2013-01-18 11:58:45 -08:00
|
|
|
setVisibility(View.GONE);
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
|
|
|
|
2012-11-28 16:29:08 -08:00
|
|
|
private void autoHidePanel() {
|
2012-07-27 23:31:40 -07:00
|
|
|
mTabsPanel.autoHidePanel();
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
|
|
|
|
2013-02-21 23:22:37 -08:00
|
|
|
@Override
|
|
|
|
public boolean shouldExpand() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
@Override
|
|
|
|
public boolean onGroupClick(ExpandableListView parent, View view, int position, long id) {
|
|
|
|
// By default, the group collapses/expands. Consume the event.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
|
|
|
|
HashMap <String, String> tab = mTabsList.get(groupPosition).get(childPosition);
|
|
|
|
if (tab == null) {
|
2012-07-27 23:31:40 -07:00
|
|
|
autoHidePanel();
|
2012-03-09 10:42:43 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-15 12:51:54 -07:00
|
|
|
Tabs.getInstance().loadUrl(tab.get("url"), Tabs.LOADURL_NEW_TAB);
|
2012-07-27 23:31:40 -07:00
|
|
|
autoHidePanel();
|
2012-03-09 10:42:43 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-12 12:48:55 -07:00
|
|
|
@Override
|
|
|
|
public void onQueryTabsComplete(List<TabsAccessor.RemoteTab> remoteTabsList) {
|
|
|
|
ArrayList<TabsAccessor.RemoteTab> remoteTabs = new ArrayList<TabsAccessor.RemoteTab> (remoteTabsList);
|
2013-02-04 15:26:50 -08:00
|
|
|
if (remoteTabs == null || remoteTabs.size() == 0)
|
2012-03-12 12:48:55 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
ArrayList <HashMap <String, String>> clients = new ArrayList <HashMap <String, String>>();
|
|
|
|
|
|
|
|
mTabsList = new ArrayList <ArrayList <HashMap <String, String>>>();
|
|
|
|
|
|
|
|
String oldGuid = null;
|
|
|
|
ArrayList <HashMap <String, String>> tabsForClient = null;
|
|
|
|
HashMap <String, String> client;
|
|
|
|
HashMap <String, String> tab;
|
|
|
|
|
|
|
|
for (TabsAccessor.RemoteTab remoteTab : remoteTabs) {
|
|
|
|
if (oldGuid == null || !TextUtils.equals(oldGuid, remoteTab.guid)) {
|
|
|
|
client = new HashMap <String, String>();
|
|
|
|
client.put("name", remoteTab.name);
|
|
|
|
clients.add(client);
|
|
|
|
|
|
|
|
tabsForClient = new ArrayList <HashMap <String, String>>();
|
|
|
|
mTabsList.add(tabsForClient);
|
|
|
|
|
|
|
|
oldGuid = new String(remoteTab.guid);
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
2012-03-12 12:48:55 -07:00
|
|
|
|
|
|
|
tab = new HashMap<String, String>();
|
|
|
|
tab.put("title", TextUtils.isEmpty(remoteTab.title) ? remoteTab.url : remoteTab.title);
|
|
|
|
tab.put("url", remoteTab.url);
|
|
|
|
tabsForClient.add(tab);
|
|
|
|
}
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
setAdapter(new SimpleExpandableListAdapter(mContext,
|
|
|
|
clients,
|
|
|
|
R.layout.remote_tabs_group,
|
|
|
|
CLIENT_KEY,
|
|
|
|
CLIENT_RESOURCE,
|
|
|
|
mTabsList,
|
|
|
|
R.layout.remote_tabs_child,
|
|
|
|
TAB_KEY,
|
|
|
|
TAB_RESOURCE));
|
2012-03-12 12:48:55 -07:00
|
|
|
|
|
|
|
for (int i = 0; i < clients.size(); i++) {
|
2012-12-20 14:35:03 -08:00
|
|
|
expandGroup(i);
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|