2013-07-08 15:02:02 -07:00
|
|
|
/* -*- 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.GeckoProfile;
|
|
|
|
import org.mozilla.gecko.R;
|
|
|
|
import org.mozilla.gecko.SessionParser;
|
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
|
|
|
import org.mozilla.gecko.db.BrowserContract.Combined;
|
2013-07-12 14:51:29 -07:00
|
|
|
import org.mozilla.gecko.home.HomePager.OnNewTabsListener;
|
2013-07-08 15:02:02 -07:00
|
|
|
import org.mozilla.gecko.home.TwoLinePageRow;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.MatrixCursor;
|
|
|
|
import android.database.MatrixCursor.RowBuilder;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.Loader;
|
2013-07-09 15:26:05 -07:00
|
|
|
import android.support.v4.widget.SimpleCursorAdapter;
|
2013-07-08 15:02:02 -07:00
|
|
|
import android.util.SparseArray;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment that displays tabs from last session in a ListView.
|
|
|
|
*/
|
|
|
|
public class LastTabsPage extends HomeFragment {
|
|
|
|
// Logging tag name
|
|
|
|
private static final String LOGTAG = "GeckoLastTabsPage";
|
|
|
|
|
|
|
|
// Cursor loader ID for the session parser
|
|
|
|
private static final int LAST_TABS_LOADER_ID = 0;
|
|
|
|
|
|
|
|
// Adapter for the list of search results
|
|
|
|
private LastTabsAdapter mAdapter;
|
|
|
|
|
|
|
|
// The view shown by the fragment.
|
|
|
|
private ListView mList;
|
|
|
|
|
|
|
|
// Callbacks used for the search and favicon cursor loaders
|
|
|
|
private CursorLoaderCallbacks mCursorLoaderCallbacks;
|
|
|
|
|
|
|
|
// Inflater used by the adapter
|
|
|
|
private LayoutInflater mInflater;
|
|
|
|
|
2013-07-12 14:51:29 -07:00
|
|
|
// On new tabs listener
|
|
|
|
private OnNewTabsListener mNewTabsListener;
|
2013-07-08 15:02:02 -07:00
|
|
|
|
|
|
|
public static LastTabsPage newInstance() {
|
|
|
|
return new LastTabsPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public LastTabsPage() {
|
2013-07-12 14:51:29 -07:00
|
|
|
mNewTabsListener = null;
|
2013-07-08 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
|
|
|
try {
|
2013-07-12 14:51:29 -07:00
|
|
|
mNewTabsListener = (OnNewTabsListener) activity;
|
2013-07-08 15:02:02 -07:00
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(activity.toString()
|
2013-07-12 14:51:29 -07:00
|
|
|
+ " must implement HomePager.OnNewTabsListener");
|
2013-07-08 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
|
|
|
|
mInflater = null;
|
2013-07-12 14:51:29 -07:00
|
|
|
mNewTabsListener = null;
|
2013-07-08 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2013-07-23 16:33:40 -07:00
|
|
|
return inflater.inflate(R.layout.home_last_tabs_page, container, false);
|
2013-07-08 15:02:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
final TextView title = (TextView) view.findViewById(R.id.title);
|
2013-07-18 14:54:14 -07:00
|
|
|
title.setText(R.string.home_last_tabs_title);
|
2013-07-08 15:02:02 -07:00
|
|
|
|
|
|
|
mList = (ListView) view.findViewById(R.id.list);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String url = c.getString(c.getColumnIndexOrThrow(Combined.URL));
|
2013-07-12 14:51:29 -07:00
|
|
|
mNewTabsListener.onNewTabs(new String[] { url });
|
2013-07-08 15:02:02 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
registerForContextMenu(mList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
mList = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
// Intialize adapter
|
|
|
|
mAdapter = new LastTabsAdapter(getActivity());
|
|
|
|
mList.setAdapter(mAdapter);
|
|
|
|
|
|
|
|
// Create callbacks before the initial loader is started
|
|
|
|
mCursorLoaderCallbacks = new CursorLoaderCallbacks();
|
|
|
|
|
|
|
|
// Reconnect to the loader only if present
|
|
|
|
getLoaderManager().initLoader(LAST_TABS_LOADER_ID, null, mCursorLoaderCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class LastTabsCursorLoader extends SimpleCursorLoader {
|
|
|
|
public LastTabsCursorLoader(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor loadCursor() {
|
|
|
|
final Context context = getContext();
|
|
|
|
|
|
|
|
final String jsonString = GeckoProfile.get(context).readSessionFile(true);
|
|
|
|
if (jsonString == null) {
|
|
|
|
// No previous session data
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final MatrixCursor c = new MatrixCursor(new String[] { Combined._ID,
|
|
|
|
Combined.URL,
|
|
|
|
Combined.TITLE,
|
|
|
|
Combined.FAVICON });
|
|
|
|
|
|
|
|
new SessionParser() {
|
|
|
|
@Override
|
|
|
|
public void onTabRead(SessionTab tab) {
|
|
|
|
final String url = tab.getUrl();
|
|
|
|
|
|
|
|
// Don't show last tabs for about:home
|
|
|
|
if (url.equals("about:home")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final RowBuilder row = c.newRow();
|
|
|
|
row.add(-1);
|
|
|
|
row.add(url);
|
|
|
|
|
|
|
|
final String title = tab.getTitle();
|
|
|
|
row.add(title);
|
|
|
|
|
|
|
|
final ContentResolver cr = context.getContentResolver();
|
|
|
|
final byte[] favicon = BrowserDB.getFaviconBytesForUrl(cr, url);
|
|
|
|
row.add(favicon);
|
|
|
|
}
|
|
|
|
}.parse(jsonString);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class LastTabsAdapter extends SimpleCursorAdapter {
|
|
|
|
public LastTabsAdapter(Context context) {
|
|
|
|
super(context, -1, null, new String[] {}, new int[] {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
final TwoLinePageRow row;
|
|
|
|
if (convertView == null) {
|
|
|
|
row = (TwoLinePageRow) mInflater.inflate(R.layout.home_item_row, mList, false);
|
|
|
|
} else {
|
|
|
|
row = (TwoLinePageRow) convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Cursor c = getCursor();
|
|
|
|
if (!c.moveToPosition(position)) {
|
|
|
|
throw new IllegalStateException("Couldn't move cursor to position " + position);
|
|
|
|
}
|
|
|
|
|
|
|
|
row.updateFromCursor(c);
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
return new LastTabsCursorLoader(getActivity());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
|
|
|
mAdapter.swapCursor(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
|
|
|
mAdapter.swapCursor(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|