|
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.db.BrowserContract.HomeItems; |
|
9 import org.mozilla.gecko.home.HomeConfig.ViewConfig; |
|
10 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; |
|
11 import org.mozilla.gecko.home.PanelLayout.FilterManager; |
|
12 import org.mozilla.gecko.home.PanelLayout.OnItemOpenListener; |
|
13 |
|
14 import android.database.Cursor; |
|
15 |
|
16 import java.util.EnumSet; |
|
17 |
|
18 class PanelViewItemHandler { |
|
19 private final ViewConfig mViewConfig; |
|
20 private OnItemOpenListener mItemOpenListener; |
|
21 private FilterManager mFilterManager; |
|
22 |
|
23 public PanelViewItemHandler(ViewConfig viewConfig) { |
|
24 mViewConfig = viewConfig; |
|
25 } |
|
26 |
|
27 public void setOnItemOpenListener(OnItemOpenListener listener) { |
|
28 mItemOpenListener = listener; |
|
29 } |
|
30 |
|
31 public void setFilterManager(FilterManager manager) { |
|
32 mFilterManager = manager; |
|
33 } |
|
34 |
|
35 /** |
|
36 * If item at this position is a back item, perform the go back action via the |
|
37 * {@code FilterManager}. Otherwise, prepare the url to be opened by the |
|
38 * {@code OnUrlOpenListener}. |
|
39 */ |
|
40 public void openItemAtPosition(Cursor cursor, int position) { |
|
41 if (mFilterManager != null && mFilterManager.canGoBack()) { |
|
42 if (position == 0) { |
|
43 mFilterManager.goBack(); |
|
44 return; |
|
45 } |
|
46 |
|
47 position--; |
|
48 } |
|
49 |
|
50 if (cursor == null || !cursor.moveToPosition(position)) { |
|
51 throw new IllegalStateException("Couldn't move cursor to position " + position); |
|
52 } |
|
53 |
|
54 int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL); |
|
55 final String url = cursor.getString(urlIndex); |
|
56 |
|
57 int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE); |
|
58 final String title = cursor.getString(titleIndex); |
|
59 |
|
60 if (mItemOpenListener != null) { |
|
61 mItemOpenListener.onItemOpen(url, title); |
|
62 } |
|
63 } |
|
64 } |