diff options
author | John Reck <jreck@google.com> | 2011-06-30 15:11:49 -0700 |
---|---|---|
committer | John Reck <jreck@google.com> | 2011-07-01 14:29:22 -0700 |
commit | 2bc8042224be51966d748b870768ec1b376a1621 (patch) | |
tree | aee49131cbc4ba66d004f8ead027d5259ee34c64 /src/com/android/browser/BrowserSnapshotPage.java | |
parent | 36afb9b00d7c1e91331bacbd67724a593b26ac0b (diff) | |
download | packages_apps_Browser-2bc8042224be51966d748b870768ec1b376a1621.zip packages_apps_Browser-2bc8042224be51966d748b870768ec1b376a1621.tar.gz packages_apps_Browser-2bc8042224be51966d748b870768ec1b376a1621.tar.bz2 |
Saved Pages tab
Bug: 4982126
Add saved pages tab
Remove "save page" menu option
Add "save for offline reading" menu option
Smooth animation to combo view
Change-Id: Ia67552a6f6a5474a6dfcff6790a341d4d36d5a77
Diffstat (limited to 'src/com/android/browser/BrowserSnapshotPage.java')
-rw-r--r-- | src/com/android/browser/BrowserSnapshotPage.java | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/src/com/android/browser/BrowserSnapshotPage.java b/src/com/android/browser/BrowserSnapshotPage.java new file mode 100644 index 0000000..06b2e42 --- /dev/null +++ b/src/com/android/browser/BrowserSnapshotPage.java @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.browser; + +import android.app.Fragment; +import android.app.LoaderManager.LoaderCallbacks; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.Context; +import android.content.CursorLoader; +import android.content.Loader; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.Bundle; +import android.view.ContextMenu; +import android.view.ContextMenu.ContextMenuInfo; +import android.view.LayoutInflater; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.View.MeasureSpec; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.AdapterContextMenuInfo; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.GridView; +import android.widget.ImageView; +import android.widget.ResourceCursorAdapter; +import android.widget.TextView; + +import com.android.browser.provider.BrowserProvider2.Snapshots; + +import java.text.DateFormat; +import java.util.Date; + +public class BrowserSnapshotPage extends Fragment implements + LoaderCallbacks<Cursor>, OnItemClickListener { + + public static final String EXTRA_ANIMATE_ID = "animate_id"; + + private static final int LOADER_SNAPSHOTS = 1; + private static final String[] PROJECTION = new String[] { + Snapshots._ID, + Snapshots.TITLE, + "length(" + Snapshots.VIEWSTATE + ")", + Snapshots.THUMBNAIL, + Snapshots.FAVICON, + Snapshots.URL, + }; + private static final int SNAPSHOT_TITLE = 1; + private static final int SNAPSHOT_VIEWSTATE_LENGTH = 2; + private static final int SNAPSHOT_THUMBNAIL = 3; + private static final int SNAPSHOT_FAVICON = 4; + private static final int SNAPSHOT_URL = 5; + + GridView mGrid; + View mEmpty; + SnapshotAdapter mAdapter; + UiController mUiController; + + public static BrowserSnapshotPage newInstance(UiController uiController, + Bundle extras) { + BrowserSnapshotPage instance = new BrowserSnapshotPage(); + instance.mUiController = uiController; + instance.setArguments(extras); + return instance; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.snapshots, container, false); + mEmpty = view.findViewById(android.R.id.empty); + mGrid = (GridView) view.findViewById(R.id.grid); + setupGrid(inflater); + getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this); + return view; + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + getLoaderManager().destroyLoader(LOADER_SNAPSHOTS); + mAdapter.changeCursor(null); + mAdapter = null; + } + + void setupGrid(LayoutInflater inflater) { + View item = inflater.inflate(R.layout.snapshot_item, mGrid, false); + int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); + item.measure(mspec, mspec); + int width = item.getMeasuredWidth(); + mGrid.setColumnWidth(width); + mGrid.setOnItemClickListener(this); + mGrid.setOnCreateContextMenuListener(this); + } + + @Override + public Loader<Cursor> onCreateLoader(int id, Bundle args) { + if (id == LOADER_SNAPSHOTS) { + // TODO: Sort by date created + return new CursorLoader(getActivity(), + Snapshots.CONTENT_URI, PROJECTION, + null, null, null); + } + return null; + } + + @Override + public void onLoadFinished(Loader<Cursor> loader, Cursor data) { + if (loader.getId() == LOADER_SNAPSHOTS) { + if (mAdapter == null) { + mAdapter = new SnapshotAdapter(getActivity(), data); + mGrid.setAdapter(mAdapter); + } else { + mAdapter.changeCursor(data); + } + boolean empty = mAdapter.isEmpty(); + mGrid.setVisibility(empty ? View.GONE : View.VISIBLE); + mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE); + } + } + + @Override + public void onLoaderReset(Loader<Cursor> loader) { + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, + ContextMenuInfo menuInfo) { + MenuInflater inflater = getActivity().getMenuInflater(); + inflater.inflate(R.menu.snapshots_context, menu); + // Create the header, re-use BookmarkItem (has the layout we want) + BookmarkItem header = new BookmarkItem(getActivity()); + AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; + populateBookmarkItem(mAdapter.getItem(info.position), header); + menu.setHeaderView(header); + } + + private void populateBookmarkItem(Cursor cursor, BookmarkItem item) { + item.setName(cursor.getString(SNAPSHOT_TITLE)); + item.setUrl(cursor.getString(SNAPSHOT_URL)); + item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON)); + } + + static Bitmap getBitmap(Cursor cursor, int columnIndex) { + byte[] data = cursor.getBlob(columnIndex); + if (data == null) { + return null; + } + return BitmapFactory.decodeByteArray(data, 0, data.length); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + if (item.getItemId() == R.id.delete_context_menu_id) { + AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + deleteSnapshot(info.id); + return true; + } + return super.onContextItemSelected(item); + } + + void deleteSnapshot(long id) { + final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id); + final ContentResolver cr = getActivity().getContentResolver(); + new Thread() { + @Override + public void run() { + cr.delete(uri, null, null); + } + }.start(); + + } + + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, + long id) { + mUiController.removeComboView(); + mUiController.createNewSnapshotTab(id, true); + } + + private static class SnapshotAdapter extends ResourceCursorAdapter { + + public SnapshotAdapter(Context context, Cursor c) { + super(context, R.layout.snapshot_item, c, 0); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb); + byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL); + if (thumbBlob == null) { + thumbnail.setImageResource(R.drawable.browser_thumbnail); + } else { + Bitmap thumbBitmap = BitmapFactory.decodeByteArray( + thumbBlob, 0, thumbBlob.length); + thumbnail.setImageBitmap(thumbBitmap); + } + TextView title = (TextView) view.findViewById(R.id.title); + title.setText(cursor.getString(SNAPSHOT_TITLE)); + TextView size = (TextView) view.findViewById(R.id.size); + int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_LENGTH); + size.setText(String.format("%.1fMB", stateLen / 1024f / 1024f)); + // We don't actually have the date in the database yet + // Use the current date as a placeholder + TextView date = (TextView) view.findViewById(R.id.date); + DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); + date.setText(dateFormat.format(new Date())); + } + + @Override + public Cursor getItem(int position) { + return (Cursor) super.getItem(position); + } + } + +} |