blob: dd997714384b20aef8a2569f9f0881c95df0c85d [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,
64 "length(" + Snapshots.VIEWSTATE + ")",
65 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;
72 private static final int SNAPSHOT_VIEWSTATE_LENGTH = 2;
73 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());
163 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
164 populateBookmarkItem(mAdapter.getItem(info.position), header);
165 menu.setHeaderView(header);
166 }
167
168 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
169 item.setName(cursor.getString(SNAPSHOT_TITLE));
170 item.setUrl(cursor.getString(SNAPSHOT_URL));
171 item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
172 }
173
174 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
175 byte[] data = cursor.getBlob(columnIndex);
176 if (data == null) {
177 return null;
178 }
179 return BitmapFactory.decodeByteArray(data, 0, data.length);
180 }
181
182 @Override
183 public boolean onContextItemSelected(MenuItem item) {
184 if (item.getItemId() == R.id.delete_context_menu_id) {
185 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
186 deleteSnapshot(info.id);
187 return true;
188 }
189 return super.onContextItemSelected(item);
190 }
191
192 void deleteSnapshot(long id) {
193 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
194 final ContentResolver cr = getActivity().getContentResolver();
195 new Thread() {
196 @Override
197 public void run() {
198 cr.delete(uri, null, null);
199 }
200 }.start();
201
202 }
203
204 @Override
205 public void onItemClick(AdapterView<?> parent, View view, int position,
206 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700207 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700208 }
209
210 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700211 private long mAnimateId;
212 private AnimatorSet mAnimation;
213 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700214
215 public SnapshotAdapter(Context context, Cursor c) {
216 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700217 mAnimation = new AnimatorSet();
218 mAnimation.playTogether(
219 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
220 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
221 mAnimation.setStartDelay(100);
222 mAnimation.setDuration(400);
223 mAnimation.addListener(new AnimatorListener() {
224
225 @Override
226 public void onAnimationStart(Animator animation) {
227 }
228
229 @Override
230 public void onAnimationRepeat(Animator animation) {
231 }
232
233 @Override
234 public void onAnimationEnd(Animator animation) {
235 mAnimateId = 0;
236 mAnimationTarget = null;
237 }
238
239 @Override
240 public void onAnimationCancel(Animator animation) {
241 }
242 });
243 }
244
245 public void animateIn(long id) {
246 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700247 }
248
249 @Override
250 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700251 long id = cursor.getLong(SNAPSHOT_ID);
252 if (id == mAnimateId) {
253 if (mAnimationTarget != view) {
254 float scale = 0f;
255 if (mAnimationTarget != null) {
256 scale = mAnimationTarget.getScaleX();
257 mAnimationTarget.setScaleX(1f);
258 mAnimationTarget.setScaleY(1f);
259 }
260 view.setScaleX(scale);
261 view.setScaleY(scale);
262 }
263 mAnimation.setTarget(view);
264 mAnimationTarget = view;
265 if (!mAnimation.isRunning()) {
266 mAnimation.start();
267 }
268
269 }
John Reck2bc80422011-06-30 15:11:49 -0700270 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
271 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
272 if (thumbBlob == null) {
273 thumbnail.setImageResource(R.drawable.browser_thumbnail);
274 } else {
275 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
276 thumbBlob, 0, thumbBlob.length);
277 thumbnail.setImageBitmap(thumbBitmap);
278 }
279 TextView title = (TextView) view.findViewById(R.id.title);
280 title.setText(cursor.getString(SNAPSHOT_TITLE));
281 TextView size = (TextView) view.findViewById(R.id.size);
John Reck86374722011-07-19 16:10:23 -0700282 if (size != null) {
283 int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_LENGTH);
284 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
285 }
John Reck8cc92352011-07-06 17:41:52 -0700286 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700287 TextView date = (TextView) view.findViewById(R.id.date);
288 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700289 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700290 }
291
292 @Override
293 public Cursor getItem(int position) {
294 return (Cursor) super.getItem(position);
295 }
296 }
297
298}