mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
29ad2b9722
--HG-- extra : rebase_source : d1229eb3621b943382a55930dbce720a5524e887
187 lines
5.9 KiB
Java
187 lines
5.9 KiB
Java
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package org.mozilla.gecko.home;
|
|
|
|
import org.mozilla.gecko.R;
|
|
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
|
|
import org.mozilla.gecko.db.BrowserDB;
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
|
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
|
|
import org.mozilla.gecko.home.TwoLinePageRow;
|
|
import org.mozilla.gecko.ReaderModeUtils;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.os.Bundle;
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
import android.support.v4.content.Loader;
|
|
import android.support.v4.widget.CursorAdapter;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.AdapterView;
|
|
import android.widget.ListView;
|
|
|
|
/**
|
|
* Fragment that displays reading list contents in a ListView.
|
|
*/
|
|
public class ReadingListPage extends HomeFragment {
|
|
// Cursor loader ID for reading list
|
|
private static final int LOADER_ID_READING_LIST = 0;
|
|
|
|
// Adapter for the list of reading list items
|
|
private ReadingListAdapter mAdapter;
|
|
|
|
// The view shown by the fragment
|
|
private ListView mList;
|
|
|
|
// Callbacks used for the reading list and favicon cursor loaders
|
|
private CursorLoaderCallbacks mCursorLoaderCallbacks;
|
|
|
|
// On URL open listener
|
|
private OnUrlOpenListener mUrlOpenListener;
|
|
|
|
@Override
|
|
public void onAttach(Activity activity) {
|
|
super.onAttach(activity);
|
|
|
|
try {
|
|
mUrlOpenListener = (OnUrlOpenListener) activity;
|
|
} catch (ClassCastException e) {
|
|
throw new ClassCastException(activity.toString()
|
|
+ " must implement HomePager.OnUrlOpenListener");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onDetach() {
|
|
super.onDetach();
|
|
|
|
mUrlOpenListener = null;
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
// All list views are styled to look the same with a global activity theme.
|
|
// If the style of the list changes, inflate it from an XML.
|
|
mList = new HomeListView(container.getContext());
|
|
return mList;
|
|
}
|
|
|
|
@Override
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
final Cursor c = mAdapter.getCursor();
|
|
if (c == null || !c.moveToPosition(position)) {
|
|
return;
|
|
}
|
|
|
|
String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
|
|
url = ReaderModeUtils.getAboutReaderForUrl(url, true);
|
|
mUrlOpenListener.onUrlOpen(url);
|
|
}
|
|
});
|
|
|
|
registerForContextMenu(mList);
|
|
}
|
|
|
|
@Override
|
|
public void onDestroyView() {
|
|
super.onDestroyView();
|
|
mList = null;
|
|
}
|
|
|
|
@Override
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
mAdapter = new ReadingListAdapter(getActivity(), null);
|
|
mList.setAdapter(mAdapter);
|
|
|
|
// Create callbacks before the initial loader is started.
|
|
mCursorLoaderCallbacks = new CursorLoaderCallbacks();
|
|
loadIfVisible();
|
|
}
|
|
|
|
@Override
|
|
protected void load() {
|
|
getLoaderManager().initLoader(LOADER_ID_READING_LIST, null, mCursorLoaderCallbacks);
|
|
}
|
|
|
|
/**
|
|
* Cursor loader for the list of reading list items.
|
|
*/
|
|
private static class ReadingListLoader extends SimpleCursorLoader {
|
|
public ReadingListLoader(Context context) {
|
|
super(context);
|
|
}
|
|
|
|
@Override
|
|
public Cursor loadCursor() {
|
|
return BrowserDB.getBookmarksInFolder(getContext().getContentResolver(), Bookmarks.FIXED_READING_LIST_ID);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cursor adapter for the list of reading list items.
|
|
*/
|
|
private class ReadingListAdapter extends CursorAdapter {
|
|
public ReadingListAdapter(Context context, Cursor cursor) {
|
|
super(context, cursor);
|
|
}
|
|
|
|
@Override
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
final TwoLinePageRow row = (TwoLinePageRow) view;
|
|
row.updateFromCursor(cursor);
|
|
}
|
|
|
|
@Override
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
return LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_row, parent, false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* LoaderCallbacks implementation that interacts with the LoaderManager.
|
|
*/
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
|
@Override
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
switch(id) {
|
|
case LOADER_ID_READING_LIST:
|
|
return new ReadingListLoader(getActivity());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
|
final int loaderId = loader.getId();
|
|
switch(loaderId) {
|
|
case LOADER_ID_READING_LIST:
|
|
mAdapter.swapCursor(c);
|
|
break;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
|
final int loaderId = loader.getId();
|
|
switch(loaderId) {
|
|
case LOADER_ID_READING_LIST:
|
|
mAdapter.swapCursor(null);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|