|
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 java.util.EnumSet; |
|
9 |
|
10 import org.mozilla.gecko.R; |
|
11 import org.mozilla.gecko.Telemetry; |
|
12 import org.mozilla.gecko.TelemetryContract; |
|
13 import org.mozilla.gecko.db.BrowserContract.Bookmarks; |
|
14 import org.mozilla.gecko.db.BrowserDB.URLColumns; |
|
15 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; |
|
16 |
|
17 import android.content.Context; |
|
18 import android.database.Cursor; |
|
19 import android.util.AttributeSet; |
|
20 import android.view.KeyEvent; |
|
21 import android.view.View; |
|
22 import android.widget.AdapterView; |
|
23 import android.widget.HeaderViewListAdapter; |
|
24 import android.widget.ListAdapter; |
|
25 |
|
26 /** |
|
27 * A ListView of bookmarks. |
|
28 */ |
|
29 public class BookmarksListView extends HomeListView |
|
30 implements AdapterView.OnItemClickListener{ |
|
31 public static final String LOGTAG = "GeckoBookmarksListView"; |
|
32 |
|
33 public BookmarksListView(Context context) { |
|
34 this(context, null); |
|
35 } |
|
36 |
|
37 public BookmarksListView(Context context, AttributeSet attrs) { |
|
38 this(context, attrs, R.attr.bookmarksListViewStyle); |
|
39 } |
|
40 |
|
41 public BookmarksListView(Context context, AttributeSet attrs, int defStyle) { |
|
42 super(context, attrs, defStyle); |
|
43 } |
|
44 |
|
45 @Override |
|
46 public void onAttachedToWindow() { |
|
47 super.onAttachedToWindow(); |
|
48 |
|
49 setOnItemClickListener(this); |
|
50 |
|
51 setOnKeyListener(new View.OnKeyListener() { |
|
52 @Override |
|
53 public boolean onKey(View v, int keyCode, KeyEvent event) { |
|
54 final int action = event.getAction(); |
|
55 |
|
56 // If the user hit the BACK key, try to move to the parent folder. |
|
57 if (action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { |
|
58 return getBookmarksListAdapter().moveToParentFolder(); |
|
59 } |
|
60 return false; |
|
61 } |
|
62 }); |
|
63 } |
|
64 |
|
65 @Override |
|
66 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
|
67 final BookmarksListAdapter adapter = getBookmarksListAdapter(); |
|
68 if (adapter.isShowingChildFolder()) { |
|
69 if (position == 0) { |
|
70 // If we tap on an opened folder, move back to parent folder. |
|
71 adapter.moveToParentFolder(); |
|
72 return; |
|
73 } |
|
74 |
|
75 // Accounting for the folder view. |
|
76 position--; |
|
77 } |
|
78 |
|
79 final Cursor cursor = adapter.getCursor(); |
|
80 if (cursor == null) { |
|
81 return; |
|
82 } |
|
83 |
|
84 cursor.moveToPosition(position); |
|
85 |
|
86 int type = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE)); |
|
87 if (type == Bookmarks.TYPE_FOLDER) { |
|
88 // If we're clicking on a folder, update adapter to move to that folder |
|
89 final int folderId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID)); |
|
90 final String folderTitle = adapter.getFolderTitle(parent.getContext(), cursor); |
|
91 adapter.moveToChildFolder(folderId, folderTitle); |
|
92 } else { |
|
93 // Otherwise, just open the URL |
|
94 final String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)); |
|
95 |
|
96 Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL); |
|
97 |
|
98 // This item is a TwoLinePageRow, so we allow switch-to-tab. |
|
99 getOnUrlOpenListener().onUrlOpen(url, EnumSet.of(OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB)); |
|
100 } |
|
101 } |
|
102 |
|
103 @Override |
|
104 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
|
105 // Adjust the item position to account for the parent folder row that is inserted |
|
106 // at the top of the list when viewing the contents of a folder. |
|
107 final BookmarksListAdapter adapter = getBookmarksListAdapter(); |
|
108 if (adapter.isShowingChildFolder()) { |
|
109 position--; |
|
110 } |
|
111 |
|
112 return super.onItemLongClick(parent, view, position, id); |
|
113 } |
|
114 |
|
115 private BookmarksListAdapter getBookmarksListAdapter() { |
|
116 BookmarksListAdapter adapter; |
|
117 ListAdapter listAdapter = getAdapter(); |
|
118 if (listAdapter instanceof HeaderViewListAdapter) { |
|
119 adapter = (BookmarksListAdapter) ((HeaderViewListAdapter) listAdapter).getWrappedAdapter(); |
|
120 } else { |
|
121 adapter = (BookmarksListAdapter) listAdapter; |
|
122 } |
|
123 return adapter; |
|
124 } |
|
125 } |