michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.Telemetry; michael@0: import org.mozilla.gecko.TelemetryContract; michael@0: michael@0: import android.content.Context; michael@0: import android.text.TextUtils; michael@0: import android.util.AttributeSet; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.widget.ExpandableListView; michael@0: import android.widget.SimpleExpandableListAdapter; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.HashMap; michael@0: import java.util.List; michael@0: michael@0: /** michael@0: * The actual list of synced tabs. This serves as the only child view of {@link RemoteTabsContainer} michael@0: * so it can be refreshed using a swipe-to-refresh gesture. michael@0: */ michael@0: class RemoteTabsList extends ExpandableListView michael@0: implements ExpandableListView.OnGroupClickListener, michael@0: ExpandableListView.OnChildClickListener, michael@0: TabsAccessor.OnQueryTabsCompleteListener { michael@0: private static final String[] CLIENT_KEY = new String[] { "name" }; michael@0: private static final String[] TAB_KEY = new String[] { "title", "url" }; michael@0: private static final int[] CLIENT_RESOURCE = new int[] { R.id.client }; michael@0: private static final int[] TAB_RESOURCE = new int[] { R.id.tab, R.id.url }; michael@0: michael@0: private final Context context; michael@0: private TabsPanel tabsPanel; michael@0: michael@0: private ArrayList >> tabsList; michael@0: michael@0: public RemoteTabsList(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: this.context = context; michael@0: michael@0: setOnGroupClickListener(this); michael@0: setOnChildClickListener(this); michael@0: } michael@0: michael@0: public void setTabsPanel(TabsPanel panel) { michael@0: tabsPanel = panel; michael@0: } michael@0: michael@0: private void autoHidePanel() { michael@0: tabsPanel.autoHidePanel(); michael@0: } michael@0: michael@0: @Override michael@0: public boolean onGroupClick(ExpandableListView parent, View view, int position, long id) { michael@0: // By default, the group collapses/expands. Consume the event. michael@0: return true; michael@0: } michael@0: michael@0: @Override michael@0: public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) { michael@0: HashMap tab = tabsList.get(groupPosition).get(childPosition); michael@0: if (tab == null) { michael@0: autoHidePanel(); michael@0: return true; michael@0: } michael@0: michael@0: Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, "", "remote"); michael@0: michael@0: Tabs.getInstance().loadUrl(tab.get("url"), Tabs.LOADURL_NEW_TAB); michael@0: autoHidePanel(); michael@0: return true; michael@0: } michael@0: michael@0: @Override michael@0: public void onQueryTabsComplete(List remoteTabsList) { michael@0: ArrayList remoteTabs = new ArrayList (remoteTabsList); michael@0: if (remoteTabs == null || remoteTabs.size() == 0) michael@0: return; michael@0: michael@0: ArrayList > clients = new ArrayList >(); michael@0: michael@0: tabsList = new ArrayList >>(); michael@0: michael@0: String oldGuid = null; michael@0: ArrayList > tabsForClient = null; michael@0: HashMap client; michael@0: HashMap tab; michael@0: michael@0: for (TabsAccessor.RemoteTab remoteTab : remoteTabs) { michael@0: if (oldGuid == null || !TextUtils.equals(oldGuid, remoteTab.guid)) { michael@0: client = new HashMap (); michael@0: client.put("name", remoteTab.name); michael@0: clients.add(client); michael@0: michael@0: tabsForClient = new ArrayList >(); michael@0: tabsList.add(tabsForClient); michael@0: michael@0: oldGuid = new String(remoteTab.guid); michael@0: } michael@0: michael@0: tab = new HashMap(); michael@0: tab.put("title", TextUtils.isEmpty(remoteTab.title) ? remoteTab.url : remoteTab.title); michael@0: tab.put("url", remoteTab.url); michael@0: tabsForClient.add(tab); michael@0: } michael@0: michael@0: setAdapter(new SimpleExpandableListAdapter(context, michael@0: clients, michael@0: R.layout.remote_tabs_group, michael@0: CLIENT_KEY, michael@0: CLIENT_RESOURCE, michael@0: tabsList, michael@0: R.layout.remote_tabs_child, michael@0: TAB_KEY, michael@0: TAB_RESOURCE)); michael@0: michael@0: for (int i = 0; i < clients.size(); i++) { michael@0: expandGroup(i); michael@0: } michael@0: } michael@0: }