blob: 0d36c48f485af12b4d66dc56b3f9afcad88c08d1 [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) {
kaiyizd1f75c72013-09-09 09:46:27 +0800185 if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) {
186 return false;
187 }
John Reck2bc80422011-06-30 15:11:49 -0700188 if (item.getItemId() == R.id.delete_context_menu_id) {
189 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
190 deleteSnapshot(info.id);
191 return true;
192 }
193 return super.onContextItemSelected(item);
194 }
195
196 void deleteSnapshot(long id) {
197 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
198 final ContentResolver cr = getActivity().getContentResolver();
199 new Thread() {
200 @Override
201 public void run() {
202 cr.delete(uri, null, null);
203 }
204 }.start();
205
206 }
207
208 @Override
209 public void onItemClick(AdapterView<?> parent, View view, int position,
210 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700211 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700212 }
213
214 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700215 private long mAnimateId;
216 private AnimatorSet mAnimation;
217 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700218
219 public SnapshotAdapter(Context context, Cursor c) {
220 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700221 mAnimation = new AnimatorSet();
222 mAnimation.playTogether(
223 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
224 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
225 mAnimation.setStartDelay(100);
226 mAnimation.setDuration(400);
227 mAnimation.addListener(new AnimatorListener() {
228
229 @Override
230 public void onAnimationStart(Animator animation) {
231 }
232
233 @Override
234 public void onAnimationRepeat(Animator animation) {
235 }
236
237 @Override
238 public void onAnimationEnd(Animator animation) {
239 mAnimateId = 0;
240 mAnimationTarget = null;
241 }
242
243 @Override
244 public void onAnimationCancel(Animator animation) {
245 }
246 });
247 }
248
249 public void animateIn(long id) {
250 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700251 }
252
253 @Override
254 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700255 long id = cursor.getLong(SNAPSHOT_ID);
256 if (id == mAnimateId) {
257 if (mAnimationTarget != view) {
258 float scale = 0f;
259 if (mAnimationTarget != null) {
260 scale = mAnimationTarget.getScaleX();
261 mAnimationTarget.setScaleX(1f);
262 mAnimationTarget.setScaleY(1f);
263 }
264 view.setScaleX(scale);
265 view.setScaleY(scale);
266 }
267 mAnimation.setTarget(view);
268 mAnimationTarget = view;
269 if (!mAnimation.isRunning()) {
270 mAnimation.start();
271 }
272
273 }
John Reck2bc80422011-06-30 15:11:49 -0700274 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
275 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
276 if (thumbBlob == null) {
277 thumbnail.setImageResource(R.drawable.browser_thumbnail);
278 } else {
279 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
280 thumbBlob, 0, thumbBlob.length);
281 thumbnail.setImageBitmap(thumbBitmap);
282 }
283 TextView title = (TextView) view.findViewById(R.id.title);
284 title.setText(cursor.getString(SNAPSHOT_TITLE));
285 TextView size = (TextView) view.findViewById(R.id.size);
John Reck86374722011-07-19 16:10:23 -0700286 if (size != null) {
John Reck2b71d6d2012-04-18 17:42:06 -0700287 int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE);
John Reck86374722011-07-19 16:10:23 -0700288 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
289 }
John Reck8cc92352011-07-06 17:41:52 -0700290 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700291 TextView date = (TextView) view.findViewById(R.id.date);
292 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700293 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700294 }
295
296 @Override
297 public Cursor getItem(int position) {
298 return (Cursor) super.getItem(position);
299 }
300 }
301
302}