michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import java.util.EnumSet; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.Telemetry; michael@0: import org.mozilla.gecko.TelemetryContract; michael@0: import org.mozilla.gecko.db.BrowserContract.Bookmarks; michael@0: import org.mozilla.gecko.db.BrowserDB.URLColumns; michael@0: import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.util.AttributeSet; michael@0: import android.view.KeyEvent; michael@0: import android.view.View; michael@0: import android.widget.AdapterView; michael@0: import android.widget.HeaderViewListAdapter; michael@0: import android.widget.ListAdapter; michael@0: michael@0: /** michael@0: * A ListView of bookmarks. michael@0: */ michael@0: public class BookmarksListView extends HomeListView michael@0: implements AdapterView.OnItemClickListener{ michael@0: public static final String LOGTAG = "GeckoBookmarksListView"; michael@0: michael@0: public BookmarksListView(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public BookmarksListView(Context context, AttributeSet attrs) { michael@0: this(context, attrs, R.attr.bookmarksListViewStyle); michael@0: } michael@0: michael@0: public BookmarksListView(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: } michael@0: michael@0: @Override michael@0: public void onAttachedToWindow() { michael@0: super.onAttachedToWindow(); michael@0: michael@0: setOnItemClickListener(this); michael@0: michael@0: setOnKeyListener(new View.OnKeyListener() { michael@0: @Override michael@0: public boolean onKey(View v, int keyCode, KeyEvent event) { michael@0: final int action = event.getAction(); michael@0: michael@0: // If the user hit the BACK key, try to move to the parent folder. michael@0: if (action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { michael@0: return getBookmarksListAdapter().moveToParentFolder(); michael@0: } michael@0: return false; michael@0: } michael@0: }); michael@0: } michael@0: michael@0: @Override michael@0: public void onItemClick(AdapterView parent, View view, int position, long id) { michael@0: final BookmarksListAdapter adapter = getBookmarksListAdapter(); michael@0: if (adapter.isShowingChildFolder()) { michael@0: if (position == 0) { michael@0: // If we tap on an opened folder, move back to parent folder. michael@0: adapter.moveToParentFolder(); michael@0: return; michael@0: } michael@0: michael@0: // Accounting for the folder view. michael@0: position--; michael@0: } michael@0: michael@0: final Cursor cursor = adapter.getCursor(); michael@0: if (cursor == null) { michael@0: return; michael@0: } michael@0: michael@0: cursor.moveToPosition(position); michael@0: michael@0: int type = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE)); michael@0: if (type == Bookmarks.TYPE_FOLDER) { michael@0: // If we're clicking on a folder, update adapter to move to that folder michael@0: final int folderId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID)); michael@0: final String folderTitle = adapter.getFolderTitle(parent.getContext(), cursor); michael@0: adapter.moveToChildFolder(folderId, folderTitle); michael@0: } else { michael@0: // Otherwise, just open the URL michael@0: final String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)); michael@0: michael@0: Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL); michael@0: michael@0: // This item is a TwoLinePageRow, so we allow switch-to-tab. michael@0: getOnUrlOpenListener().onUrlOpen(url, EnumSet.of(OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB)); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { michael@0: // Adjust the item position to account for the parent folder row that is inserted michael@0: // at the top of the list when viewing the contents of a folder. michael@0: final BookmarksListAdapter adapter = getBookmarksListAdapter(); michael@0: if (adapter.isShowingChildFolder()) { michael@0: position--; michael@0: } michael@0: michael@0: return super.onItemLongClick(parent, view, position, id); michael@0: } michael@0: michael@0: private BookmarksListAdapter getBookmarksListAdapter() { michael@0: BookmarksListAdapter adapter; michael@0: ListAdapter listAdapter = getAdapter(); michael@0: if (listAdapter instanceof HeaderViewListAdapter) { michael@0: adapter = (BookmarksListAdapter) ((HeaderViewListAdapter) listAdapter).getWrappedAdapter(); michael@0: } else { michael@0: adapter = (BookmarksListAdapter) listAdapter; michael@0: } michael@0: return adapter; michael@0: } michael@0: }