mobile/android/base/home/PanelRefreshLayout.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial