1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/RemoteTabsList.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko; 1.9 + 1.10 +import org.mozilla.gecko.Telemetry; 1.11 +import org.mozilla.gecko.TelemetryContract; 1.12 + 1.13 +import android.content.Context; 1.14 +import android.text.TextUtils; 1.15 +import android.util.AttributeSet; 1.16 +import android.view.View; 1.17 +import android.view.ViewGroup; 1.18 +import android.widget.ExpandableListView; 1.19 +import android.widget.SimpleExpandableListAdapter; 1.20 + 1.21 +import java.util.ArrayList; 1.22 +import java.util.HashMap; 1.23 +import java.util.List; 1.24 + 1.25 +/** 1.26 + * The actual list of synced tabs. This serves as the only child view of {@link RemoteTabsContainer} 1.27 + * so it can be refreshed using a swipe-to-refresh gesture. 1.28 + */ 1.29 +class RemoteTabsList extends ExpandableListView 1.30 + implements ExpandableListView.OnGroupClickListener, 1.31 + ExpandableListView.OnChildClickListener, 1.32 + TabsAccessor.OnQueryTabsCompleteListener { 1.33 + private static final String[] CLIENT_KEY = new String[] { "name" }; 1.34 + private static final String[] TAB_KEY = new String[] { "title", "url" }; 1.35 + private static final int[] CLIENT_RESOURCE = new int[] { R.id.client }; 1.36 + private static final int[] TAB_RESOURCE = new int[] { R.id.tab, R.id.url }; 1.37 + 1.38 + private final Context context; 1.39 + private TabsPanel tabsPanel; 1.40 + 1.41 + private ArrayList <ArrayList <HashMap <String, String>>> tabsList; 1.42 + 1.43 + public RemoteTabsList(Context context, AttributeSet attrs) { 1.44 + super(context, attrs); 1.45 + this.context = context; 1.46 + 1.47 + setOnGroupClickListener(this); 1.48 + setOnChildClickListener(this); 1.49 + } 1.50 + 1.51 + public void setTabsPanel(TabsPanel panel) { 1.52 + tabsPanel = panel; 1.53 + } 1.54 + 1.55 + private void autoHidePanel() { 1.56 + tabsPanel.autoHidePanel(); 1.57 + } 1.58 + 1.59 + @Override 1.60 + public boolean onGroupClick(ExpandableListView parent, View view, int position, long id) { 1.61 + // By default, the group collapses/expands. Consume the event. 1.62 + return true; 1.63 + } 1.64 + 1.65 + @Override 1.66 + public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) { 1.67 + HashMap <String, String> tab = tabsList.get(groupPosition).get(childPosition); 1.68 + if (tab == null) { 1.69 + autoHidePanel(); 1.70 + return true; 1.71 + } 1.72 + 1.73 + Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, "", "remote"); 1.74 + 1.75 + Tabs.getInstance().loadUrl(tab.get("url"), Tabs.LOADURL_NEW_TAB); 1.76 + autoHidePanel(); 1.77 + return true; 1.78 + } 1.79 + 1.80 + @Override 1.81 + public void onQueryTabsComplete(List<TabsAccessor.RemoteTab> remoteTabsList) { 1.82 + ArrayList<TabsAccessor.RemoteTab> remoteTabs = new ArrayList<TabsAccessor.RemoteTab> (remoteTabsList); 1.83 + if (remoteTabs == null || remoteTabs.size() == 0) 1.84 + return; 1.85 + 1.86 + ArrayList <HashMap <String, String>> clients = new ArrayList <HashMap <String, String>>(); 1.87 + 1.88 + tabsList = new ArrayList <ArrayList <HashMap <String, String>>>(); 1.89 + 1.90 + String oldGuid = null; 1.91 + ArrayList <HashMap <String, String>> tabsForClient = null; 1.92 + HashMap <String, String> client; 1.93 + HashMap <String, String> tab; 1.94 + 1.95 + for (TabsAccessor.RemoteTab remoteTab : remoteTabs) { 1.96 + if (oldGuid == null || !TextUtils.equals(oldGuid, remoteTab.guid)) { 1.97 + client = new HashMap <String, String>(); 1.98 + client.put("name", remoteTab.name); 1.99 + clients.add(client); 1.100 + 1.101 + tabsForClient = new ArrayList <HashMap <String, String>>(); 1.102 + tabsList.add(tabsForClient); 1.103 + 1.104 + oldGuid = new String(remoteTab.guid); 1.105 + } 1.106 + 1.107 + tab = new HashMap<String, String>(); 1.108 + tab.put("title", TextUtils.isEmpty(remoteTab.title) ? remoteTab.url : remoteTab.title); 1.109 + tab.put("url", remoteTab.url); 1.110 + tabsForClient.add(tab); 1.111 + } 1.112 + 1.113 + setAdapter(new SimpleExpandableListAdapter(context, 1.114 + clients, 1.115 + R.layout.remote_tabs_group, 1.116 + CLIENT_KEY, 1.117 + CLIENT_RESOURCE, 1.118 + tabsList, 1.119 + R.layout.remote_tabs_child, 1.120 + TAB_KEY, 1.121 + TAB_RESOURCE)); 1.122 + 1.123 + for (int i = 0; i < clients.size(); i++) { 1.124 + expandGroup(i); 1.125 + } 1.126 + } 1.127 +}