blob: a0ec5e980c8fa3bd6923b4e23757e5ea9550dbea [file] [log] [blame]
John Reck2bc80422011-06-30 15:11:49 -07001/*
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 */
16package com.android.browser;
17
18import android.app.Fragment;
19import android.app.LoaderManager.LoaderCallbacks;
20import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.Context;
23import android.content.CursorLoader;
24import android.content.Loader;
25import android.database.Cursor;
26import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
28import android.net.Uri;
29import android.os.Bundle;
30import android.view.ContextMenu;
31import android.view.ContextMenu.ContextMenuInfo;
32import android.view.LayoutInflater;
33import android.view.MenuInflater;
34import android.view.MenuItem;
35import android.view.View;
36import android.view.View.MeasureSpec;
37import android.view.ViewGroup;
38import android.widget.AdapterView;
39import android.widget.AdapterView.AdapterContextMenuInfo;
40import android.widget.AdapterView.OnItemClickListener;
41import android.widget.GridView;
42import android.widget.ImageView;
43import android.widget.ResourceCursorAdapter;
44import android.widget.TextView;
45
John Reckd3e4d5b2011-07-13 15:48:43 -070046import com.android.browser.CombinedBookmarkHistoryView.CombinedBookmarksCallbacks;
John Reck8cc92352011-07-06 17:41:52 -070047import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck2bc80422011-06-30 15:11:49 -070048
49import java.text.DateFormat;
50import java.util.Date;
51
52public class BrowserSnapshotPage extends Fragment implements
53 LoaderCallbacks<Cursor>, OnItemClickListener {
54
55 public static final String EXTRA_ANIMATE_ID = "animate_id";
56
57 private static final int LOADER_SNAPSHOTS = 1;
58 private static final String[] PROJECTION = new String[] {
59 Snapshots._ID,
60 Snapshots.TITLE,
61 "length(" + Snapshots.VIEWSTATE + ")",
62 Snapshots.THUMBNAIL,
63 Snapshots.FAVICON,
64 Snapshots.URL,
John Reck8cc92352011-07-06 17:41:52 -070065 Snapshots.DATE_CREATED,
John Reck2bc80422011-06-30 15:11:49 -070066 };
67 private static final int SNAPSHOT_TITLE = 1;
68 private static final int SNAPSHOT_VIEWSTATE_LENGTH = 2;
69 private static final int SNAPSHOT_THUMBNAIL = 3;
70 private static final int SNAPSHOT_FAVICON = 4;
71 private static final int SNAPSHOT_URL = 5;
John Reck8cc92352011-07-06 17:41:52 -070072 private static final int SNAPSHOT_DATE_CREATED = 6;
John Reck2bc80422011-06-30 15:11:49 -070073
74 GridView mGrid;
75 View mEmpty;
76 SnapshotAdapter mAdapter;
John Reckd3e4d5b2011-07-13 15:48:43 -070077 CombinedBookmarksCallbacks mCallback;
John Reck2bc80422011-06-30 15:11:49 -070078
John Reckd3e4d5b2011-07-13 15:48:43 -070079 public static BrowserSnapshotPage newInstance(CombinedBookmarksCallbacks cb,
John Reck2bc80422011-06-30 15:11:49 -070080 Bundle extras) {
81 BrowserSnapshotPage instance = new BrowserSnapshotPage();
John Reckd3e4d5b2011-07-13 15:48:43 -070082 instance.mCallback = cb;
John Reck2bc80422011-06-30 15:11:49 -070083 instance.setArguments(extras);
84 return instance;
85 }
86
87 @Override
88 public View onCreateView(LayoutInflater inflater, ViewGroup container,
89 Bundle savedInstanceState) {
90 View view = inflater.inflate(R.layout.snapshots, container, false);
91 mEmpty = view.findViewById(android.R.id.empty);
92 mGrid = (GridView) view.findViewById(R.id.grid);
93 setupGrid(inflater);
94 getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
95 return view;
96 }
97
98 @Override
99 public void onDestroyView() {
100 super.onDestroyView();
101 getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
John Reck24bdcb92011-07-11 16:48:10 -0700102 if (mAdapter != null) {
103 mAdapter.changeCursor(null);
104 mAdapter = null;
105 }
John Reck2bc80422011-06-30 15:11:49 -0700106 }
107
108 void setupGrid(LayoutInflater inflater) {
109 View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
110 int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
111 item.measure(mspec, mspec);
112 int width = item.getMeasuredWidth();
113 mGrid.setColumnWidth(width);
114 mGrid.setOnItemClickListener(this);
115 mGrid.setOnCreateContextMenuListener(this);
116 }
117
118 @Override
119 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
120 if (id == LOADER_SNAPSHOTS) {
John Reck2bc80422011-06-30 15:11:49 -0700121 return new CursorLoader(getActivity(),
122 Snapshots.CONTENT_URI, PROJECTION,
John Reck8cc92352011-07-06 17:41:52 -0700123 null, null, Snapshots.DATE_CREATED + " DESC");
John Reck2bc80422011-06-30 15:11:49 -0700124 }
125 return null;
126 }
127
128 @Override
129 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
130 if (loader.getId() == LOADER_SNAPSHOTS) {
131 if (mAdapter == null) {
132 mAdapter = new SnapshotAdapter(getActivity(), data);
133 mGrid.setAdapter(mAdapter);
134 } else {
135 mAdapter.changeCursor(data);
136 }
137 boolean empty = mAdapter.isEmpty();
138 mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
139 mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
140 }
141 }
142
143 @Override
144 public void onLoaderReset(Loader<Cursor> loader) {
145 }
146
147 @Override
148 public void onCreateContextMenu(ContextMenu menu, View v,
149 ContextMenuInfo menuInfo) {
150 MenuInflater inflater = getActivity().getMenuInflater();
151 inflater.inflate(R.menu.snapshots_context, menu);
152 // Create the header, re-use BookmarkItem (has the layout we want)
153 BookmarkItem header = new BookmarkItem(getActivity());
154 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
155 populateBookmarkItem(mAdapter.getItem(info.position), header);
156 menu.setHeaderView(header);
157 }
158
159 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
160 item.setName(cursor.getString(SNAPSHOT_TITLE));
161 item.setUrl(cursor.getString(SNAPSHOT_URL));
162 item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
163 }
164
165 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
166 byte[] data = cursor.getBlob(columnIndex);
167 if (data == null) {
168 return null;
169 }
170 return BitmapFactory.decodeByteArray(data, 0, data.length);
171 }
172
173 @Override
174 public boolean onContextItemSelected(MenuItem item) {
175 if (item.getItemId() == R.id.delete_context_menu_id) {
176 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
177 deleteSnapshot(info.id);
178 return true;
179 }
180 return super.onContextItemSelected(item);
181 }
182
183 void deleteSnapshot(long id) {
184 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
185 final ContentResolver cr = getActivity().getContentResolver();
186 new Thread() {
187 @Override
188 public void run() {
189 cr.delete(uri, null, null);
190 }
191 }.start();
192
193 }
194
195 @Override
196 public void onItemClick(AdapterView<?> parent, View view, int position,
197 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700198 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700199 }
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 Reck8cc92352011-07-06 17:41:52 -0700222 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
223 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700224 TextView date = (TextView) view.findViewById(R.id.date);
225 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700226 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700227 }
228
229 @Override
230 public Cursor getItem(int position) {
231 return (Cursor) super.getItem(position);
232 }
233 }
234
235}