blob: b0d82056e0deb01f5d4e94eeadc1d0bfd8575459 [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
John Reckb977a322011-09-08 18:08:25 -070018import android.animation.Animator;
19import android.animation.Animator.AnimatorListener;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
John Reck2bc80422011-06-30 15:11:49 -070022import android.app.Fragment;
23import android.app.LoaderManager.LoaderCallbacks;
24import android.content.ContentResolver;
25import android.content.ContentUris;
26import android.content.Context;
27import android.content.CursorLoader;
28import android.content.Loader;
29import android.database.Cursor;
30import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
32import android.net.Uri;
33import android.os.Bundle;
34import android.view.ContextMenu;
35import android.view.ContextMenu.ContextMenuInfo;
36import android.view.LayoutInflater;
37import android.view.MenuInflater;
38import android.view.MenuItem;
39import android.view.View;
40import android.view.View.MeasureSpec;
41import android.view.ViewGroup;
42import android.widget.AdapterView;
43import android.widget.AdapterView.AdapterContextMenuInfo;
44import android.widget.AdapterView.OnItemClickListener;
45import android.widget.GridView;
46import android.widget.ImageView;
47import android.widget.ResourceCursorAdapter;
48import android.widget.TextView;
49
John Reck8cc92352011-07-06 17:41:52 -070050import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck2bc80422011-06-30 15:11:49 -070051
52import java.text.DateFormat;
53import java.util.Date;
54
55public class BrowserSnapshotPage extends Fragment implements
56 LoaderCallbacks<Cursor>, OnItemClickListener {
57
58 public static final String EXTRA_ANIMATE_ID = "animate_id";
59
60 private static final int LOADER_SNAPSHOTS = 1;
61 private static final String[] PROJECTION = new String[] {
62 Snapshots._ID,
63 Snapshots.TITLE,
John Reck2b71d6d2012-04-18 17:42:06 -070064 Snapshots.VIEWSTATE_SIZE,
John Reck2bc80422011-06-30 15:11:49 -070065 Snapshots.THUMBNAIL,
66 Snapshots.FAVICON,
67 Snapshots.URL,
John Reck8cc92352011-07-06 17:41:52 -070068 Snapshots.DATE_CREATED,
John Reck2bc80422011-06-30 15:11:49 -070069 };
John Reckb977a322011-09-08 18:08:25 -070070 private static final int SNAPSHOT_ID = 0;
John Reck2bc80422011-06-30 15:11:49 -070071 private static final int SNAPSHOT_TITLE = 1;
John Reck2b71d6d2012-04-18 17:42:06 -070072 private static final int SNAPSHOT_VIEWSTATE_SIZE = 2;
John Reck2bc80422011-06-30 15:11:49 -070073 private static final int SNAPSHOT_THUMBNAIL = 3;
74 private static final int SNAPSHOT_FAVICON = 4;
75 private static final int SNAPSHOT_URL = 5;
John Reck8cc92352011-07-06 17:41:52 -070076 private static final int SNAPSHOT_DATE_CREATED = 6;
John Reck2bc80422011-06-30 15:11:49 -070077
78 GridView mGrid;
79 View mEmpty;
80 SnapshotAdapter mAdapter;
John Reckd3e4d5b2011-07-13 15:48:43 -070081 CombinedBookmarksCallbacks mCallback;
John Reckb977a322011-09-08 18:08:25 -070082 long mAnimateId;
John Reck2bc80422011-06-30 15:11:49 -070083
John Reck2d963a22011-08-10 15:53:07 -070084 @Override
85 public void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
87 mCallback = (CombinedBookmarksCallbacks) getActivity();
John Reckb977a322011-09-08 18:08:25 -070088 mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
John Reck2bc80422011-06-30 15:11:49 -070089 }
90
91 @Override
92 public View onCreateView(LayoutInflater inflater, ViewGroup container,
93 Bundle savedInstanceState) {
94 View view = inflater.inflate(R.layout.snapshots, container, false);
95 mEmpty = view.findViewById(android.R.id.empty);
96 mGrid = (GridView) view.findViewById(R.id.grid);
97 setupGrid(inflater);
98 getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
99 return view;
100 }
101
102 @Override
103 public void onDestroyView() {
104 super.onDestroyView();
105 getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
John Reck24bdcb92011-07-11 16:48:10 -0700106 if (mAdapter != null) {
107 mAdapter.changeCursor(null);
108 mAdapter = null;
109 }
John Reck2bc80422011-06-30 15:11:49 -0700110 }
111
112 void setupGrid(LayoutInflater inflater) {
113 View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
114 int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
115 item.measure(mspec, mspec);
116 int width = item.getMeasuredWidth();
117 mGrid.setColumnWidth(width);
118 mGrid.setOnItemClickListener(this);
119 mGrid.setOnCreateContextMenuListener(this);
120 }
121
122 @Override
123 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
124 if (id == LOADER_SNAPSHOTS) {
John Reck2bc80422011-06-30 15:11:49 -0700125 return new CursorLoader(getActivity(),
126 Snapshots.CONTENT_URI, PROJECTION,
John Reck8cc92352011-07-06 17:41:52 -0700127 null, null, Snapshots.DATE_CREATED + " DESC");
John Reck2bc80422011-06-30 15:11:49 -0700128 }
129 return null;
130 }
131
132 @Override
133 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
134 if (loader.getId() == LOADER_SNAPSHOTS) {
135 if (mAdapter == null) {
136 mAdapter = new SnapshotAdapter(getActivity(), data);
137 mGrid.setAdapter(mAdapter);
138 } else {
139 mAdapter.changeCursor(data);
140 }
John Reckb977a322011-09-08 18:08:25 -0700141 if (mAnimateId > 0) {
142 mAdapter.animateIn(mAnimateId);
143 mAnimateId = 0;
144 getArguments().remove(EXTRA_ANIMATE_ID);
145 }
John Reck2bc80422011-06-30 15:11:49 -0700146 boolean empty = mAdapter.isEmpty();
147 mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
148 mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
149 }
150 }
151
152 @Override
153 public void onLoaderReset(Loader<Cursor> loader) {
154 }
155
156 @Override
157 public void onCreateContextMenu(ContextMenu menu, View v,
158 ContextMenuInfo menuInfo) {
159 MenuInflater inflater = getActivity().getMenuInflater();
160 inflater.inflate(R.menu.snapshots_context, menu);
161 // Create the header, re-use BookmarkItem (has the layout we want)
162 BookmarkItem header = new BookmarkItem(getActivity());
John Reck83c01512011-09-09 11:14:16 -0700163 header.setEnableScrolling(true);
John Reck2bc80422011-06-30 15:11:49 -0700164 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
165 populateBookmarkItem(mAdapter.getItem(info.position), header);
166 menu.setHeaderView(header);
167 }
168
169 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
170 item.setName(cursor.getString(SNAPSHOT_TITLE));
171 item.setUrl(cursor.getString(SNAPSHOT_URL));
172 item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
173 }
174
175 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
176 byte[] data = cursor.getBlob(columnIndex);
177 if (data == null) {
178 return null;
179 }
180 return BitmapFactory.decodeByteArray(data, 0, data.length);
181 }
182
183 @Override
184 public boolean onContextItemSelected(MenuItem item) {
185 if (item.getItemId() == R.id.delete_context_menu_id) {
186 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
187 deleteSnapshot(info.id);
188 return true;
189 }
190 return super.onContextItemSelected(item);
191 }
192
193 void deleteSnapshot(long id) {
194 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
195 final ContentResolver cr = getActivity().getContentResolver();
196 new Thread() {
197 @Override
198 public void run() {
199 cr.delete(uri, null, null);
200 }
201 }.start();
202
203 }
204
205 @Override
206 public void onItemClick(AdapterView<?> parent, View view, int position,
207 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700208 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700209 }
210
211 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700212 private long mAnimateId;
213 private AnimatorSet mAnimation;
214 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700215
216 public SnapshotAdapter(Context context, Cursor c) {
217 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700218 mAnimation = new AnimatorSet();
219 mAnimation.playTogether(
220 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
221 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
222 mAnimation.setStartDelay(100);
223 mAnimation.setDuration(400);
224 mAnimation.addListener(new AnimatorListener() {
225
226 @Override
227 public void onAnimationStart(Animator animation) {
228 }
229
230 @Override
231 public void onAnimationRepeat(Animator animation) {
232 }
233
234 @Override
235 public void onAnimationEnd(Animator animation) {
236 mAnimateId = 0;
237 mAnimationTarget = null;
238 }
239
240 @Override
241 public void onAnimationCancel(Animator animation) {
242 }
243 });
244 }
245
246 public void animateIn(long id) {
247 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700248 }
249
250 @Override
251 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700252 long id = cursor.getLong(SNAPSHOT_ID);
253 if (id == mAnimateId) {
254 if (mAnimationTarget != view) {
255 float scale = 0f;
256 if (mAnimationTarget != null) {
257 scale = mAnimationTarget.getScaleX();
258 mAnimationTarget.setScaleX(1f);
259 mAnimationTarget.setScaleY(1f);
260 }
261 view.setScaleX(scale);
262 view.setScaleY(scale);
263 }
264 mAnimation.setTarget(view);
265 mAnimationTarget = view;
266 if (!mAnimation.isRunning()) {
267 mAnimation.start();
268 }
269
270 }
John Reck2bc80422011-06-30 15:11:49 -0700271 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
272 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
273 if (thumbBlob == null) {
274 thumbnail.setImageResource(R.drawable.browser_thumbnail);
275 } else {
276 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
277 thumbBlob, 0, thumbBlob.length);
278 thumbnail.setImageBitmap(thumbBitmap);
279 }
280 TextView title = (TextView) view.findViewById(R.id.title);
281 title.setText(cursor.getString(SNAPSHOT_TITLE));
282 TextView size = (TextView) view.findViewById(R.id.size);
John Reck86374722011-07-19 16:10:23 -0700283 if (size != null) {
John Reck2b71d6d2012-04-18 17:42:06 -0700284 int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE);
John Reck86374722011-07-19 16:10:23 -0700285 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
286 }
John Reck8cc92352011-07-06 17:41:52 -0700287 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700288 TextView date = (TextView) view.findViewById(R.id.date);
289 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700290 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700291 }
292
293 @Override
294 public Cursor getItem(int position) {
295 return (Cursor) super.getItem(position);
296 }
297 }
298
299}