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