michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.GeckoEvent; michael@0: import org.mozilla.gecko.home.PanelLayout.DatasetBacked; michael@0: import org.mozilla.gecko.home.PanelLayout.FilterManager; michael@0: import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout; michael@0: import org.mozilla.gecko.widget.GeckoSwipeRefreshLayout.OnRefreshListener; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.util.Log; michael@0: import android.view.View; michael@0: michael@0: /** michael@0: * Used to wrap a {@code DatasetBacked} ListView or GridView to give the child view swipe-to-refresh michael@0: * capabilities. michael@0: * michael@0: * This view acts as a decorator to forward the {@code DatasetBacked} methods to the child view michael@0: * while providing the refresh gesture support on top of it. michael@0: */ michael@0: class PanelRefreshLayout extends GeckoSwipeRefreshLayout implements DatasetBacked { michael@0: private static final String LOGTAG = "GeckoPanelRefreshLayout"; michael@0: michael@0: private static final String JSON_KEY_PANEL_ID = "panelId"; michael@0: private static final String JSON_KEY_VIEW_INDEX = "viewIndex"; michael@0: michael@0: private final String panelId; michael@0: private final int viewIndex; michael@0: private final DatasetBacked datasetBacked; michael@0: michael@0: /** michael@0: * @param context Android context. michael@0: * @param childView ListView or GridView. Must implement {@code DatasetBacked}. michael@0: * @param panelId The ID from the {@code PanelConfig}. michael@0: * @param viewIndex The index from the {@code ViewConfig}. michael@0: */ michael@0: public PanelRefreshLayout(Context context, View childView, String panelId, int viewIndex) { michael@0: super(context); michael@0: michael@0: if (!(childView instanceof DatasetBacked)) { michael@0: throw new IllegalArgumentException("View must implement DatasetBacked to be refreshed"); michael@0: } michael@0: michael@0: this.panelId = panelId; michael@0: this.viewIndex = viewIndex; michael@0: this.datasetBacked = (DatasetBacked) childView; michael@0: michael@0: setOnRefreshListener(new RefreshListener()); michael@0: addView(childView); michael@0: michael@0: // Must be called after the child view has been added. michael@0: setColorScheme(R.color.swipe_refresh_orange, R.color.swipe_refresh_white, michael@0: R.color.swipe_refresh_orange, R.color.swipe_refresh_white); michael@0: } michael@0: michael@0: @Override michael@0: public void setDataset(Cursor cursor) { michael@0: datasetBacked.setDataset(cursor); michael@0: setRefreshing(false); michael@0: } michael@0: michael@0: @Override michael@0: public void setFilterManager(FilterManager manager) { michael@0: datasetBacked.setFilterManager(manager); michael@0: } michael@0: michael@0: private class RefreshListener implements OnRefreshListener { michael@0: @Override michael@0: public void onRefresh() { michael@0: final JSONObject response = new JSONObject(); michael@0: try { michael@0: response.put(JSON_KEY_PANEL_ID, panelId); michael@0: response.put(JSON_KEY_VIEW_INDEX, viewIndex); michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "Could not create refresh message", e); michael@0: return; michael@0: } michael@0: michael@0: final GeckoEvent event = michael@0: GeckoEvent.createBroadcastEvent("HomePanels:RefreshView", response.toString()); michael@0: GeckoAppShell.sendEventToGecko(event); michael@0: } michael@0: } michael@0: }