Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.gecko;
7 import org.mozilla.gecko.Telemetry;
8 import org.mozilla.gecko.TelemetryContract;
10 import android.content.Context;
11 import android.text.TextUtils;
12 import android.util.AttributeSet;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.widget.ExpandableListView;
16 import android.widget.SimpleExpandableListAdapter;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
22 /**
23 * The actual list of synced tabs. This serves as the only child view of {@link RemoteTabsContainer}
24 * so it can be refreshed using a swipe-to-refresh gesture.
25 */
26 class RemoteTabsList extends ExpandableListView
27 implements ExpandableListView.OnGroupClickListener,
28 ExpandableListView.OnChildClickListener,
29 TabsAccessor.OnQueryTabsCompleteListener {
30 private static final String[] CLIENT_KEY = new String[] { "name" };
31 private static final String[] TAB_KEY = new String[] { "title", "url" };
32 private static final int[] CLIENT_RESOURCE = new int[] { R.id.client };
33 private static final int[] TAB_RESOURCE = new int[] { R.id.tab, R.id.url };
35 private final Context context;
36 private TabsPanel tabsPanel;
38 private ArrayList <ArrayList <HashMap <String, String>>> tabsList;
40 public RemoteTabsList(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 this.context = context;
44 setOnGroupClickListener(this);
45 setOnChildClickListener(this);
46 }
48 public void setTabsPanel(TabsPanel panel) {
49 tabsPanel = panel;
50 }
52 private void autoHidePanel() {
53 tabsPanel.autoHidePanel();
54 }
56 @Override
57 public boolean onGroupClick(ExpandableListView parent, View view, int position, long id) {
58 // By default, the group collapses/expands. Consume the event.
59 return true;
60 }
62 @Override
63 public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
64 HashMap <String, String> tab = tabsList.get(groupPosition).get(childPosition);
65 if (tab == null) {
66 autoHidePanel();
67 return true;
68 }
70 Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, "", "remote");
72 Tabs.getInstance().loadUrl(tab.get("url"), Tabs.LOADURL_NEW_TAB);
73 autoHidePanel();
74 return true;
75 }
77 @Override
78 public void onQueryTabsComplete(List<TabsAccessor.RemoteTab> remoteTabsList) {
79 ArrayList<TabsAccessor.RemoteTab> remoteTabs = new ArrayList<TabsAccessor.RemoteTab> (remoteTabsList);
80 if (remoteTabs == null || remoteTabs.size() == 0)
81 return;
83 ArrayList <HashMap <String, String>> clients = new ArrayList <HashMap <String, String>>();
85 tabsList = new ArrayList <ArrayList <HashMap <String, String>>>();
87 String oldGuid = null;
88 ArrayList <HashMap <String, String>> tabsForClient = null;
89 HashMap <String, String> client;
90 HashMap <String, String> tab;
92 for (TabsAccessor.RemoteTab remoteTab : remoteTabs) {
93 if (oldGuid == null || !TextUtils.equals(oldGuid, remoteTab.guid)) {
94 client = new HashMap <String, String>();
95 client.put("name", remoteTab.name);
96 clients.add(client);
98 tabsForClient = new ArrayList <HashMap <String, String>>();
99 tabsList.add(tabsForClient);
101 oldGuid = new String(remoteTab.guid);
102 }
104 tab = new HashMap<String, String>();
105 tab.put("title", TextUtils.isEmpty(remoteTab.title) ? remoteTab.url : remoteTab.title);
106 tab.put("url", remoteTab.url);
107 tabsForClient.add(tab);
108 }
110 setAdapter(new SimpleExpandableListAdapter(context,
111 clients,
112 R.layout.remote_tabs_group,
113 CLIENT_KEY,
114 CLIENT_RESOURCE,
115 tabsList,
116 R.layout.remote_tabs_child,
117 TAB_KEY,
118 TAB_RESOURCE));
120 for (int i = 0; i < clients.size(); i++) {
121 expandGroup(i);
122 }
123 }
124 }