1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/PanelRefreshLayout.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.mozilla.gecko.R; 1.12 + 1.13 +import org.mozilla.gecko.GeckoAppShell; 1.14 +import org.mozilla.gecko.GeckoEvent; 1.15 +import org.mozilla.gecko.home.PanelLayout.DatasetBacked; 1.16 +import org.mozilla.gecko.home.PanelLayout.FilterManager; 1.17 +import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout; 1.18 +import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout.OnRefreshListener; 1.19 + 1.20 +import org.json.JSONException; 1.21 +import org.json.JSONObject; 1.22 + 1.23 +import android.content.Context; 1.24 +import android.database.Cursor; 1.25 +import android.util.Log; 1.26 +import android.view.View; 1.27 + 1.28 +/** 1.29 + * Used to wrap a {@code DatasetBacked} ListView or GridView to give the child view swipe-to-refresh 1.30 + * capabilities. 1.31 + * 1.32 + * This view acts as a decorator to forward the {@code DatasetBacked} methods to the child view 1.33 + * while providing the refresh gesture support on top of it. 1.34 + */ 1.35 +class PanelRefreshLayout extends GeckoSwipeRefreshLayout implements DatasetBacked { 1.36 + private static final String LOGTAG = "GeckoPanelRefreshLayout"; 1.37 + 1.38 + private static final String JSON_KEY_PANEL_ID = "panelId"; 1.39 + private static final String JSON_KEY_VIEW_INDEX = "viewIndex"; 1.40 + 1.41 + private final String panelId; 1.42 + private final int viewIndex; 1.43 + private final DatasetBacked datasetBacked; 1.44 + 1.45 + /** 1.46 + * @param context Android context. 1.47 + * @param childView ListView or GridView. Must implement {@code DatasetBacked}. 1.48 + * @param panelId The ID from the {@code PanelConfig}. 1.49 + * @param viewIndex The index from the {@code ViewConfig}. 1.50 + */ 1.51 + public PanelRefreshLayout(Context context, View childView, String panelId, int viewIndex) { 1.52 + super(context); 1.53 + 1.54 + if (!(childView instanceof DatasetBacked)) { 1.55 + throw new IllegalArgumentException("View must implement DatasetBacked to be refreshed"); 1.56 + } 1.57 + 1.58 + this.panelId = panelId; 1.59 + this.viewIndex = viewIndex; 1.60 + this.datasetBacked = (DatasetBacked) childView; 1.61 + 1.62 + setOnRefreshListener(new RefreshListener()); 1.63 + addView(childView); 1.64 + 1.65 + // Must be called after the child view has been added. 1.66 + setColorScheme(R.color.swipe_refresh_orange, R.color.swipe_refresh_white, 1.67 + R.color.swipe_refresh_orange, R.color.swipe_refresh_white); 1.68 + } 1.69 + 1.70 + @Override 1.71 + public void setDataset(Cursor cursor) { 1.72 + datasetBacked.setDataset(cursor); 1.73 + setRefreshing(false); 1.74 + } 1.75 + 1.76 + @Override 1.77 + public void setFilterManager(FilterManager manager) { 1.78 + datasetBacked.setFilterManager(manager); 1.79 + } 1.80 + 1.81 + private class RefreshListener implements OnRefreshListener { 1.82 + @Override 1.83 + public void onRefresh() { 1.84 + final JSONObject response = new JSONObject(); 1.85 + try { 1.86 + response.put(JSON_KEY_PANEL_ID, panelId); 1.87 + response.put(JSON_KEY_VIEW_INDEX, viewIndex); 1.88 + } catch (JSONException e) { 1.89 + Log.e(LOGTAG, "Could not create refresh message", e); 1.90 + return; 1.91 + } 1.92 + 1.93 + final GeckoEvent event = 1.94 + GeckoEvent.createBroadcastEvent("HomePanels:RefreshView", response.toString()); 1.95 + GeckoAppShell.sendEventToGecko(event); 1.96 + } 1.97 + } 1.98 +}