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