|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import org.mozilla.gecko.R; |
|
9 |
|
10 import org.mozilla.gecko.GeckoAppShell; |
|
11 import org.mozilla.gecko.GeckoEvent; |
|
12 import org.mozilla.gecko.home.PanelLayout.DatasetBacked; |
|
13 import org.mozilla.gecko.home.PanelLayout.FilterManager; |
|
14 import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout; |
|
15 import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout.OnRefreshListener; |
|
16 |
|
17 import org.json.JSONException; |
|
18 import org.json.JSONObject; |
|
19 |
|
20 import android.content.Context; |
|
21 import android.database.Cursor; |
|
22 import android.util.Log; |
|
23 import android.view.View; |
|
24 |
|
25 /** |
|
26 * Used to wrap a {@code DatasetBacked} ListView or GridView to give the child view swipe-to-refresh |
|
27 * capabilities. |
|
28 * |
|
29 * This view acts as a decorator to forward the {@code DatasetBacked} methods to the child view |
|
30 * while providing the refresh gesture support on top of it. |
|
31 */ |
|
32 class PanelRefreshLayout extends GeckoSwipeRefreshLayout implements DatasetBacked { |
|
33 private static final String LOGTAG = "GeckoPanelRefreshLayout"; |
|
34 |
|
35 private static final String JSON_KEY_PANEL_ID = "panelId"; |
|
36 private static final String JSON_KEY_VIEW_INDEX = "viewIndex"; |
|
37 |
|
38 private final String panelId; |
|
39 private final int viewIndex; |
|
40 private final DatasetBacked datasetBacked; |
|
41 |
|
42 /** |
|
43 * @param context Android context. |
|
44 * @param childView ListView or GridView. Must implement {@code DatasetBacked}. |
|
45 * @param panelId The ID from the {@code PanelConfig}. |
|
46 * @param viewIndex The index from the {@code ViewConfig}. |
|
47 */ |
|
48 public PanelRefreshLayout(Context context, View childView, String panelId, int viewIndex) { |
|
49 super(context); |
|
50 |
|
51 if (!(childView instanceof DatasetBacked)) { |
|
52 throw new IllegalArgumentException("View must implement DatasetBacked to be refreshed"); |
|
53 } |
|
54 |
|
55 this.panelId = panelId; |
|
56 this.viewIndex = viewIndex; |
|
57 this.datasetBacked = (DatasetBacked) childView; |
|
58 |
|
59 setOnRefreshListener(new RefreshListener()); |
|
60 addView(childView); |
|
61 |
|
62 // Must be called after the child view has been added. |
|
63 setColorScheme(R.color.swipe_refresh_orange, R.color.swipe_refresh_white, |
|
64 R.color.swipe_refresh_orange, R.color.swipe_refresh_white); |
|
65 } |
|
66 |
|
67 @Override |
|
68 public void setDataset(Cursor cursor) { |
|
69 datasetBacked.setDataset(cursor); |
|
70 setRefreshing(false); |
|
71 } |
|
72 |
|
73 @Override |
|
74 public void setFilterManager(FilterManager manager) { |
|
75 datasetBacked.setFilterManager(manager); |
|
76 } |
|
77 |
|
78 private class RefreshListener implements OnRefreshListener { |
|
79 @Override |
|
80 public void onRefresh() { |
|
81 final JSONObject response = new JSONObject(); |
|
82 try { |
|
83 response.put(JSON_KEY_PANEL_ID, panelId); |
|
84 response.put(JSON_KEY_VIEW_INDEX, viewIndex); |
|
85 } catch (JSONException e) { |
|
86 Log.e(LOGTAG, "Could not create refresh message", e); |
|
87 return; |
|
88 } |
|
89 |
|
90 final GeckoEvent event = |
|
91 GeckoEvent.createBroadcastEvent("HomePanels:RefreshView", response.toString()); |
|
92 GeckoAppShell.sendEventToGecko(event); |
|
93 } |
|
94 } |
|
95 } |