mobile/android/base/home/PanelViewItemHandler.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/home/PanelViewItemHandler.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     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.db.BrowserContract.HomeItems;
    1.12 +import org.mozilla.gecko.home.HomeConfig.ViewConfig;
    1.13 +import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
    1.14 +import org.mozilla.gecko.home.PanelLayout.FilterManager;
    1.15 +import org.mozilla.gecko.home.PanelLayout.OnItemOpenListener;
    1.16 +
    1.17 +import android.database.Cursor;
    1.18 +
    1.19 +import java.util.EnumSet;
    1.20 +
    1.21 +class PanelViewItemHandler {
    1.22 +    private final ViewConfig mViewConfig;
    1.23 +    private OnItemOpenListener mItemOpenListener;
    1.24 +    private FilterManager mFilterManager;
    1.25 +
    1.26 +    public PanelViewItemHandler(ViewConfig viewConfig) {
    1.27 +        mViewConfig = viewConfig;
    1.28 +    }
    1.29 +
    1.30 +    public void setOnItemOpenListener(OnItemOpenListener listener) {
    1.31 +        mItemOpenListener = listener;
    1.32 +    }
    1.33 +
    1.34 +    public void setFilterManager(FilterManager manager) {
    1.35 +        mFilterManager = manager;
    1.36 +    }
    1.37 +
    1.38 +    /**
    1.39 +     * If item at this position is a back item, perform the go back action via the
    1.40 +     * {@code FilterManager}. Otherwise, prepare the url to be opened by the
    1.41 +     * {@code OnUrlOpenListener}.
    1.42 +     */
    1.43 +    public void openItemAtPosition(Cursor cursor, int position) {
    1.44 +        if (mFilterManager != null && mFilterManager.canGoBack()) {
    1.45 +            if (position == 0) {
    1.46 +                mFilterManager.goBack();
    1.47 +                return;
    1.48 +            }
    1.49 +
    1.50 +            position--;
    1.51 +        }
    1.52 +
    1.53 +        if (cursor == null || !cursor.moveToPosition(position)) {
    1.54 +            throw new IllegalStateException("Couldn't move cursor to position " + position);
    1.55 +        }
    1.56 +
    1.57 +        int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
    1.58 +        final String url = cursor.getString(urlIndex);
    1.59 +
    1.60 +        int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE);
    1.61 +        final String title = cursor.getString(titleIndex);
    1.62 +
    1.63 +        if (mItemOpenListener != null) {
    1.64 +            mItemOpenListener.onItemOpen(url, title);
    1.65 +        }
    1.66 +    }
    1.67 +}

mercurial