blob: 5d2453b8f1f2bc2d248871119d33d72b56e9f51d [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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reck2bc80422011-06-30 15:11:49 -070017
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
Bijan Amirzada41242f22014-03-21 12:12:18 -070050import com.android.browser.R;
51import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck2bc80422011-06-30 15:11:49 -070052
53import java.text.DateFormat;
54import java.util.Date;
55
56public class BrowserSnapshotPage extends Fragment implements
57 LoaderCallbacks<Cursor>, OnItemClickListener {
58
59 public static final String EXTRA_ANIMATE_ID = "animate_id";
60
61 private static final int LOADER_SNAPSHOTS = 1;
62 private static final String[] PROJECTION = new String[] {
63 Snapshots._ID,
64 Snapshots.TITLE,
John Reck2b71d6d2012-04-18 17:42:06 -070065 Snapshots.VIEWSTATE_SIZE,
John Reck2bc80422011-06-30 15:11:49 -070066 Snapshots.THUMBNAIL,
67 Snapshots.FAVICON,
68 Snapshots.URL,
John Reck8cc92352011-07-06 17:41:52 -070069 Snapshots.DATE_CREATED,
John Reck2bc80422011-06-30 15:11:49 -070070 };
John Reckb977a322011-09-08 18:08:25 -070071 private static final int SNAPSHOT_ID = 0;
John Reck2bc80422011-06-30 15:11:49 -070072 private static final int SNAPSHOT_TITLE = 1;
John Reck2b71d6d2012-04-18 17:42:06 -070073 private static final int SNAPSHOT_VIEWSTATE_SIZE = 2;
John Reck2bc80422011-06-30 15:11:49 -070074 private static final int SNAPSHOT_THUMBNAIL = 3;
75 private static final int SNAPSHOT_FAVICON = 4;
76 private static final int SNAPSHOT_URL = 5;
John Reck8cc92352011-07-06 17:41:52 -070077 private static final int SNAPSHOT_DATE_CREATED = 6;
John Reck2bc80422011-06-30 15:11:49 -070078
79 GridView mGrid;
80 View mEmpty;
81 SnapshotAdapter mAdapter;
John Reckd3e4d5b2011-07-13 15:48:43 -070082 CombinedBookmarksCallbacks mCallback;
John Reckb977a322011-09-08 18:08:25 -070083 long mAnimateId;
John Reck2bc80422011-06-30 15:11:49 -070084
John Reck2d963a22011-08-10 15:53:07 -070085 @Override
86 public void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 mCallback = (CombinedBookmarksCallbacks) getActivity();
John Reckb977a322011-09-08 18:08:25 -070089 mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
John Reck2bc80422011-06-30 15:11:49 -070090 }
91
92 @Override
93 public View onCreateView(LayoutInflater inflater, ViewGroup container,
94 Bundle savedInstanceState) {
95 View view = inflater.inflate(R.layout.snapshots, container, false);
96 mEmpty = view.findViewById(android.R.id.empty);
97 mGrid = (GridView) view.findViewById(R.id.grid);
98 setupGrid(inflater);
99 getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
100 return view;
101 }
102
103 @Override
104 public void onDestroyView() {
105 super.onDestroyView();
106 getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
John Reck24bdcb92011-07-11 16:48:10 -0700107 if (mAdapter != null) {
108 mAdapter.changeCursor(null);
109 mAdapter = null;
110 }
John Reck2bc80422011-06-30 15:11:49 -0700111 }
112
113 void setupGrid(LayoutInflater inflater) {
114 View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
115 int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
116 item.measure(mspec, mspec);
117 int width = item.getMeasuredWidth();
118 mGrid.setColumnWidth(width);
119 mGrid.setOnItemClickListener(this);
120 mGrid.setOnCreateContextMenuListener(this);
121 }
122
123 @Override
124 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
125 if (id == LOADER_SNAPSHOTS) {
John Reck2bc80422011-06-30 15:11:49 -0700126 return new CursorLoader(getActivity(),
127 Snapshots.CONTENT_URI, PROJECTION,
John Reck8cc92352011-07-06 17:41:52 -0700128 null, null, Snapshots.DATE_CREATED + " DESC");
John Reck2bc80422011-06-30 15:11:49 -0700129 }
130 return null;
131 }
132
133 @Override
134 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
135 if (loader.getId() == LOADER_SNAPSHOTS) {
136 if (mAdapter == null) {
137 mAdapter = new SnapshotAdapter(getActivity(), data);
138 mGrid.setAdapter(mAdapter);
139 } else {
140 mAdapter.changeCursor(data);
141 }
John Reckb977a322011-09-08 18:08:25 -0700142 if (mAnimateId > 0) {
143 mAdapter.animateIn(mAnimateId);
144 mAnimateId = 0;
145 getArguments().remove(EXTRA_ANIMATE_ID);
146 }
John Reck2bc80422011-06-30 15:11:49 -0700147 boolean empty = mAdapter.isEmpty();
148 mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
149 mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
150 }
151 }
152
153 @Override
154 public void onLoaderReset(Loader<Cursor> loader) {
155 }
156
157 @Override
158 public void onCreateContextMenu(ContextMenu menu, View v,
159 ContextMenuInfo menuInfo) {
160 MenuInflater inflater = getActivity().getMenuInflater();
161 inflater.inflate(R.menu.snapshots_context, menu);
162 // Create the header, re-use BookmarkItem (has the layout we want)
163 BookmarkItem header = new BookmarkItem(getActivity());
John Reck83c01512011-09-09 11:14:16 -0700164 header.setEnableScrolling(true);
John Reck2bc80422011-06-30 15:11:49 -0700165 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
166 populateBookmarkItem(mAdapter.getItem(info.position), header);
167 menu.setHeaderView(header);
168 }
169
170 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
171 item.setName(cursor.getString(SNAPSHOT_TITLE));
172 item.setUrl(cursor.getString(SNAPSHOT_URL));
173 item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
174 }
175
176 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
177 byte[] data = cursor.getBlob(columnIndex);
178 if (data == null) {
179 return null;
180 }
181 return BitmapFactory.decodeByteArray(data, 0, data.length);
182 }
183
184 @Override
185 public boolean onContextItemSelected(MenuItem item) {
kaiyizd1f75c72013-09-09 09:46:27 +0800186 if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) {
187 return false;
188 }
John Reck2bc80422011-06-30 15:11:49 -0700189 if (item.getItemId() == R.id.delete_context_menu_id) {
190 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
191 deleteSnapshot(info.id);
192 return true;
193 }
194 return super.onContextItemSelected(item);
195 }
196
197 void deleteSnapshot(long id) {
198 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
199 final ContentResolver cr = getActivity().getContentResolver();
200 new Thread() {
201 @Override
202 public void run() {
203 cr.delete(uri, null, null);
204 }
205 }.start();
206
207 }
208
209 @Override
210 public void onItemClick(AdapterView<?> parent, View view, int position,
211 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700212 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700213 }
214
215 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700216 private long mAnimateId;
217 private AnimatorSet mAnimation;
218 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700219
220 public SnapshotAdapter(Context context, Cursor c) {
221 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700222 mAnimation = new AnimatorSet();
223 mAnimation.playTogether(
224 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
225 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
226 mAnimation.setStartDelay(100);
227 mAnimation.setDuration(400);
228 mAnimation.addListener(new AnimatorListener() {
229
230 @Override
231 public void onAnimationStart(Animator animation) {
232 }
233
234 @Override
235 public void onAnimationRepeat(Animator animation) {
236 }
237
238 @Override
239 public void onAnimationEnd(Animator animation) {
240 mAnimateId = 0;
241 mAnimationTarget = null;
242 }
243
244 @Override
245 public void onAnimationCancel(Animator animation) {
246 }
247 });
248 }
249
250 public void animateIn(long id) {
251 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700252 }
253
254 @Override
255 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700256 long id = cursor.getLong(SNAPSHOT_ID);
257 if (id == mAnimateId) {
258 if (mAnimationTarget != view) {
259 float scale = 0f;
260 if (mAnimationTarget != null) {
261 scale = mAnimationTarget.getScaleX();
262 mAnimationTarget.setScaleX(1f);
263 mAnimationTarget.setScaleY(1f);
264 }
265 view.setScaleX(scale);
266 view.setScaleY(scale);
267 }
268 mAnimation.setTarget(view);
269 mAnimationTarget = view;
270 if (!mAnimation.isRunning()) {
271 mAnimation.start();
272 }
273
274 }
John Reck2bc80422011-06-30 15:11:49 -0700275 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
276 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
277 if (thumbBlob == null) {
278 thumbnail.setImageResource(R.drawable.browser_thumbnail);
279 } else {
280 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
281 thumbBlob, 0, thumbBlob.length);
282 thumbnail.setImageBitmap(thumbBitmap);
283 }
284 TextView title = (TextView) view.findViewById(R.id.title);
285 title.setText(cursor.getString(SNAPSHOT_TITLE));
286 TextView size = (TextView) view.findViewById(R.id.size);
John Reck86374722011-07-19 16:10:23 -0700287 if (size != null) {
John Reck2b71d6d2012-04-18 17:42:06 -0700288 int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE);
John Reck86374722011-07-19 16:10:23 -0700289 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
290 }
John Reck8cc92352011-07-06 17:41:52 -0700291 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700292 TextView date = (TextView) view.findViewById(R.id.date);
293 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700294 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700295 }
296
297 @Override
298 public Cursor getItem(int position) {
299 return (Cursor) super.getItem(position);
300 }
301 }
302
303}