John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.browser; |
| 17 | |
| 18 | import android.app.Fragment; |
| 19 | import android.app.LoaderManager.LoaderCallbacks; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentUris; |
| 22 | import android.content.Context; |
| 23 | import android.content.CursorLoader; |
| 24 | import android.content.Loader; |
| 25 | import android.database.Cursor; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.BitmapFactory; |
| 28 | import android.net.Uri; |
| 29 | import android.os.Bundle; |
| 30 | import android.view.ContextMenu; |
| 31 | import android.view.ContextMenu.ContextMenuInfo; |
| 32 | import android.view.LayoutInflater; |
| 33 | import android.view.MenuInflater; |
| 34 | import android.view.MenuItem; |
| 35 | import android.view.View; |
| 36 | import android.view.View.MeasureSpec; |
| 37 | import android.view.ViewGroup; |
| 38 | import android.widget.AdapterView; |
| 39 | import android.widget.AdapterView.AdapterContextMenuInfo; |
| 40 | import android.widget.AdapterView.OnItemClickListener; |
| 41 | import android.widget.GridView; |
| 42 | import android.widget.ImageView; |
| 43 | import android.widget.ResourceCursorAdapter; |
| 44 | import android.widget.TextView; |
| 45 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 46 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 47 | |
| 48 | import java.text.DateFormat; |
| 49 | import java.util.Date; |
| 50 | |
| 51 | public class BrowserSnapshotPage extends Fragment implements |
| 52 | LoaderCallbacks<Cursor>, OnItemClickListener { |
| 53 | |
| 54 | public static final String EXTRA_ANIMATE_ID = "animate_id"; |
| 55 | |
| 56 | private static final int LOADER_SNAPSHOTS = 1; |
| 57 | private static final String[] PROJECTION = new String[] { |
| 58 | Snapshots._ID, |
| 59 | Snapshots.TITLE, |
| 60 | "length(" + Snapshots.VIEWSTATE + ")", |
| 61 | Snapshots.THUMBNAIL, |
| 62 | Snapshots.FAVICON, |
| 63 | Snapshots.URL, |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 64 | Snapshots.DATE_CREATED, |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 65 | }; |
| 66 | private static final int SNAPSHOT_TITLE = 1; |
| 67 | private static final int SNAPSHOT_VIEWSTATE_LENGTH = 2; |
| 68 | private static final int SNAPSHOT_THUMBNAIL = 3; |
| 69 | private static final int SNAPSHOT_FAVICON = 4; |
| 70 | private static final int SNAPSHOT_URL = 5; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 71 | private static final int SNAPSHOT_DATE_CREATED = 6; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 72 | |
| 73 | GridView mGrid; |
| 74 | View mEmpty; |
| 75 | SnapshotAdapter mAdapter; |
| 76 | UiController mUiController; |
| 77 | |
| 78 | public static BrowserSnapshotPage newInstance(UiController uiController, |
| 79 | Bundle extras) { |
| 80 | BrowserSnapshotPage instance = new BrowserSnapshotPage(); |
| 81 | instance.mUiController = uiController; |
| 82 | instance.setArguments(extras); |
| 83 | return instance; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 88 | Bundle savedInstanceState) { |
| 89 | View view = inflater.inflate(R.layout.snapshots, container, false); |
| 90 | mEmpty = view.findViewById(android.R.id.empty); |
| 91 | mGrid = (GridView) view.findViewById(R.id.grid); |
| 92 | setupGrid(inflater); |
| 93 | getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this); |
| 94 | return view; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onDestroyView() { |
| 99 | super.onDestroyView(); |
| 100 | getLoaderManager().destroyLoader(LOADER_SNAPSHOTS); |
John Reck | 24bdcb9 | 2011-07-11 16:48:10 -0700 | [diff] [blame] | 101 | if (mAdapter != null) { |
| 102 | mAdapter.changeCursor(null); |
| 103 | mAdapter = null; |
| 104 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void setupGrid(LayoutInflater inflater) { |
| 108 | View item = inflater.inflate(R.layout.snapshot_item, mGrid, false); |
| 109 | int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 110 | item.measure(mspec, mspec); |
| 111 | int width = item.getMeasuredWidth(); |
| 112 | mGrid.setColumnWidth(width); |
| 113 | mGrid.setOnItemClickListener(this); |
| 114 | mGrid.setOnCreateContextMenuListener(this); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 119 | if (id == LOADER_SNAPSHOTS) { |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 120 | return new CursorLoader(getActivity(), |
| 121 | Snapshots.CONTENT_URI, PROJECTION, |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 122 | null, null, Snapshots.DATE_CREATED + " DESC"); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 123 | } |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 129 | if (loader.getId() == LOADER_SNAPSHOTS) { |
| 130 | if (mAdapter == null) { |
| 131 | mAdapter = new SnapshotAdapter(getActivity(), data); |
| 132 | mGrid.setAdapter(mAdapter); |
| 133 | } else { |
| 134 | mAdapter.changeCursor(data); |
| 135 | } |
| 136 | boolean empty = mAdapter.isEmpty(); |
| 137 | mGrid.setVisibility(empty ? View.GONE : View.VISIBLE); |
| 138 | mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onLoaderReset(Loader<Cursor> loader) { |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onCreateContextMenu(ContextMenu menu, View v, |
| 148 | ContextMenuInfo menuInfo) { |
| 149 | MenuInflater inflater = getActivity().getMenuInflater(); |
| 150 | inflater.inflate(R.menu.snapshots_context, menu); |
| 151 | // Create the header, re-use BookmarkItem (has the layout we want) |
| 152 | BookmarkItem header = new BookmarkItem(getActivity()); |
| 153 | AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; |
| 154 | populateBookmarkItem(mAdapter.getItem(info.position), header); |
| 155 | menu.setHeaderView(header); |
| 156 | } |
| 157 | |
| 158 | private void populateBookmarkItem(Cursor cursor, BookmarkItem item) { |
| 159 | item.setName(cursor.getString(SNAPSHOT_TITLE)); |
| 160 | item.setUrl(cursor.getString(SNAPSHOT_URL)); |
| 161 | item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON)); |
| 162 | } |
| 163 | |
| 164 | static Bitmap getBitmap(Cursor cursor, int columnIndex) { |
| 165 | byte[] data = cursor.getBlob(columnIndex); |
| 166 | if (data == null) { |
| 167 | return null; |
| 168 | } |
| 169 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public boolean onContextItemSelected(MenuItem item) { |
| 174 | if (item.getItemId() == R.id.delete_context_menu_id) { |
| 175 | AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); |
| 176 | deleteSnapshot(info.id); |
| 177 | return true; |
| 178 | } |
| 179 | return super.onContextItemSelected(item); |
| 180 | } |
| 181 | |
| 182 | void deleteSnapshot(long id) { |
| 183 | final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id); |
| 184 | final ContentResolver cr = getActivity().getContentResolver(); |
| 185 | new Thread() { |
| 186 | @Override |
| 187 | public void run() { |
| 188 | cr.delete(uri, null, null); |
| 189 | } |
| 190 | }.start(); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 196 | long id) { |
| 197 | mUiController.removeComboView(); |
| 198 | mUiController.createNewSnapshotTab(id, true); |
| 199 | } |
| 200 | |
| 201 | private static class SnapshotAdapter extends ResourceCursorAdapter { |
| 202 | |
| 203 | public SnapshotAdapter(Context context, Cursor c) { |
| 204 | super(context, R.layout.snapshot_item, c, 0); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void bindView(View view, Context context, Cursor cursor) { |
| 209 | ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb); |
| 210 | byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL); |
| 211 | if (thumbBlob == null) { |
| 212 | thumbnail.setImageResource(R.drawable.browser_thumbnail); |
| 213 | } else { |
| 214 | Bitmap thumbBitmap = BitmapFactory.decodeByteArray( |
| 215 | thumbBlob, 0, thumbBlob.length); |
| 216 | thumbnail.setImageBitmap(thumbBitmap); |
| 217 | } |
| 218 | TextView title = (TextView) view.findViewById(R.id.title); |
| 219 | title.setText(cursor.getString(SNAPSHOT_TITLE)); |
| 220 | TextView size = (TextView) view.findViewById(R.id.size); |
| 221 | int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_LENGTH); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 222 | size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f)); |
| 223 | long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 224 | TextView date = (TextView) view.findViewById(R.id.date); |
| 225 | DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 226 | date.setText(dateFormat.format(new Date(timestamp))); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Cursor getItem(int position) { |
| 231 | return (Cursor) super.getItem(position); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } |