blob: afa02693077301a610340930c9835892345f61c4 [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;
Pankaj Garg68faf1c2015-06-26 17:07:37 -070029import android.content.res.Configuration;
30import android.content.res.Resources;
John Reck2bc80422011-06-30 15:11:49 -070031import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
35import android.os.Bundle;
36import android.view.ContextMenu;
37import android.view.ContextMenu.ContextMenuInfo;
38import android.view.LayoutInflater;
39import android.view.MenuInflater;
40import android.view.MenuItem;
41import android.view.View;
42import android.view.View.MeasureSpec;
43import android.view.ViewGroup;
44import android.widget.AdapterView;
45import android.widget.AdapterView.AdapterContextMenuInfo;
46import android.widget.AdapterView.OnItemClickListener;
47import android.widget.GridView;
48import android.widget.ImageView;
49import android.widget.ResourceCursorAdapter;
50import android.widget.TextView;
51
Bijan Amirzada41242f22014-03-21 12:12:18 -070052import com.android.browser.R;
53import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck2bc80422011-06-30 15:11:49 -070054
55import java.text.DateFormat;
56import java.util.Date;
57
58public class BrowserSnapshotPage extends Fragment implements
59 LoaderCallbacks<Cursor>, OnItemClickListener {
60
61 public static final String EXTRA_ANIMATE_ID = "animate_id";
62
63 private static final int LOADER_SNAPSHOTS = 1;
64 private static final String[] PROJECTION = new String[] {
65 Snapshots._ID,
66 Snapshots.TITLE,
John Reck2b71d6d2012-04-18 17:42:06 -070067 Snapshots.VIEWSTATE_SIZE,
John Reck2bc80422011-06-30 15:11:49 -070068 Snapshots.THUMBNAIL,
69 Snapshots.FAVICON,
70 Snapshots.URL,
John Reck8cc92352011-07-06 17:41:52 -070071 Snapshots.DATE_CREATED,
John Reck2bc80422011-06-30 15:11:49 -070072 };
John Reckb977a322011-09-08 18:08:25 -070073 private static final int SNAPSHOT_ID = 0;
John Reck2bc80422011-06-30 15:11:49 -070074 private static final int SNAPSHOT_TITLE = 1;
John Reck2b71d6d2012-04-18 17:42:06 -070075 private static final int SNAPSHOT_VIEWSTATE_SIZE = 2;
John Reck2bc80422011-06-30 15:11:49 -070076 private static final int SNAPSHOT_THUMBNAIL = 3;
77 private static final int SNAPSHOT_FAVICON = 4;
78 private static final int SNAPSHOT_URL = 5;
John Reck8cc92352011-07-06 17:41:52 -070079 private static final int SNAPSHOT_DATE_CREATED = 6;
John Reck2bc80422011-06-30 15:11:49 -070080
81 GridView mGrid;
82 View mEmpty;
83 SnapshotAdapter mAdapter;
John Reckd3e4d5b2011-07-13 15:48:43 -070084 CombinedBookmarksCallbacks mCallback;
John Reckb977a322011-09-08 18:08:25 -070085 long mAnimateId;
John Reck2bc80422011-06-30 15:11:49 -070086
Pankaj Garg68faf1c2015-06-26 17:07:37 -070087 View mRoot;
88
John Reck2d963a22011-08-10 15:53:07 -070089 @Override
90 public void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
Tarun Nainani87a86682015-02-05 11:47:35 -080092 if (mCallback == null && getActivity() instanceof CombinedBookmarksCallbacks) {
93 mCallback = (CombinedBookmarksCallbacks) getActivity();
94 } else {
95 View cb = getActivity().getWindow().getDecorView().findViewById(R.id.combo_view_container);
96 if (cb != null && cb instanceof CombinedBookmarksCallbacks) {
97 mCallback = (CombinedBookmarksCallbacks) cb;
98 }
99 }
John Reckb977a322011-09-08 18:08:25 -0700100 mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
John Reck2bc80422011-06-30 15:11:49 -0700101 }
102
103 @Override
104 public View onCreateView(LayoutInflater inflater, ViewGroup container,
105 Bundle savedInstanceState) {
106 View view = inflater.inflate(R.layout.snapshots, container, false);
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700107 mRoot = view;
John Reck2bc80422011-06-30 15:11:49 -0700108 mEmpty = view.findViewById(android.R.id.empty);
109 mGrid = (GridView) view.findViewById(R.id.grid);
110 setupGrid(inflater);
111 getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
112 return view;
113 }
114
115 @Override
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700116 public void onConfigurationChanged(Configuration newConfig) {
117 super.onConfigurationChanged(newConfig);
118 Resources res = getActivity().getResources();
119 int paddingTop = (int) res.getDimension(R.dimen.combo_paddingTop);
120 mRoot.setPadding(0, paddingTop, 0, 0);
121 }
122
123 @Override
John Reck2bc80422011-06-30 15:11:49 -0700124 public void onDestroyView() {
125 super.onDestroyView();
126 getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
John Reck24bdcb92011-07-11 16:48:10 -0700127 if (mAdapter != null) {
128 mAdapter.changeCursor(null);
129 mAdapter = null;
130 }
John Reck2bc80422011-06-30 15:11:49 -0700131 }
132
133 void setupGrid(LayoutInflater inflater) {
134 View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
135 int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
136 item.measure(mspec, mspec);
137 int width = item.getMeasuredWidth();
138 mGrid.setColumnWidth(width);
139 mGrid.setOnItemClickListener(this);
140 mGrid.setOnCreateContextMenuListener(this);
141 }
142
143 @Override
144 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
145 if (id == LOADER_SNAPSHOTS) {
John Reck2bc80422011-06-30 15:11:49 -0700146 return new CursorLoader(getActivity(),
147 Snapshots.CONTENT_URI, PROJECTION,
John Reck8cc92352011-07-06 17:41:52 -0700148 null, null, Snapshots.DATE_CREATED + " DESC");
John Reck2bc80422011-06-30 15:11:49 -0700149 }
150 return null;
151 }
152
153 @Override
154 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
155 if (loader.getId() == LOADER_SNAPSHOTS) {
156 if (mAdapter == null) {
157 mAdapter = new SnapshotAdapter(getActivity(), data);
158 mGrid.setAdapter(mAdapter);
159 } else {
160 mAdapter.changeCursor(data);
161 }
John Reckb977a322011-09-08 18:08:25 -0700162 if (mAnimateId > 0) {
163 mAdapter.animateIn(mAnimateId);
164 mAnimateId = 0;
165 getArguments().remove(EXTRA_ANIMATE_ID);
166 }
John Reck2bc80422011-06-30 15:11:49 -0700167 boolean empty = mAdapter.isEmpty();
168 mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
169 mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
170 }
171 }
172
173 @Override
174 public void onLoaderReset(Loader<Cursor> loader) {
175 }
176
177 @Override
178 public void onCreateContextMenu(ContextMenu menu, View v,
179 ContextMenuInfo menuInfo) {
180 MenuInflater inflater = getActivity().getMenuInflater();
181 inflater.inflate(R.menu.snapshots_context, menu);
182 // Create the header, re-use BookmarkItem (has the layout we want)
183 BookmarkItem header = new BookmarkItem(getActivity());
John Reck83c01512011-09-09 11:14:16 -0700184 header.setEnableScrolling(true);
John Reck2bc80422011-06-30 15:11:49 -0700185 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
186 populateBookmarkItem(mAdapter.getItem(info.position), header);
187 menu.setHeaderView(header);
188 }
189
190 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
191 item.setName(cursor.getString(SNAPSHOT_TITLE));
192 item.setUrl(cursor.getString(SNAPSHOT_URL));
193 item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
194 }
195
196 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
197 byte[] data = cursor.getBlob(columnIndex);
198 if (data == null) {
199 return null;
200 }
201 return BitmapFactory.decodeByteArray(data, 0, data.length);
202 }
203
204 @Override
205 public boolean onContextItemSelected(MenuItem item) {
kaiyizd1f75c72013-09-09 09:46:27 +0800206 if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) {
207 return false;
208 }
John Reck2bc80422011-06-30 15:11:49 -0700209 if (item.getItemId() == R.id.delete_context_menu_id) {
210 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
211 deleteSnapshot(info.id);
212 return true;
213 }
214 return super.onContextItemSelected(item);
215 }
216
217 void deleteSnapshot(long id) {
218 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
219 final ContentResolver cr = getActivity().getContentResolver();
220 new Thread() {
221 @Override
222 public void run() {
223 cr.delete(uri, null, null);
224 }
225 }.start();
226
227 }
228
229 @Override
230 public void onItemClick(AdapterView<?> parent, View view, int position,
231 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700232 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700233 }
234
235 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700236 private long mAnimateId;
237 private AnimatorSet mAnimation;
238 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700239
240 public SnapshotAdapter(Context context, Cursor c) {
241 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700242 mAnimation = new AnimatorSet();
243 mAnimation.playTogether(
244 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
245 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
246 mAnimation.setStartDelay(100);
247 mAnimation.setDuration(400);
248 mAnimation.addListener(new AnimatorListener() {
249
250 @Override
251 public void onAnimationStart(Animator animation) {
252 }
253
254 @Override
255 public void onAnimationRepeat(Animator animation) {
256 }
257
258 @Override
259 public void onAnimationEnd(Animator animation) {
260 mAnimateId = 0;
261 mAnimationTarget = null;
262 }
263
264 @Override
265 public void onAnimationCancel(Animator animation) {
266 }
267 });
268 }
269
270 public void animateIn(long id) {
271 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700272 }
273
274 @Override
275 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700276 long id = cursor.getLong(SNAPSHOT_ID);
277 if (id == mAnimateId) {
278 if (mAnimationTarget != view) {
279 float scale = 0f;
280 if (mAnimationTarget != null) {
281 scale = mAnimationTarget.getScaleX();
282 mAnimationTarget.setScaleX(1f);
283 mAnimationTarget.setScaleY(1f);
284 }
285 view.setScaleX(scale);
286 view.setScaleY(scale);
287 }
288 mAnimation.setTarget(view);
289 mAnimationTarget = view;
290 if (!mAnimation.isRunning()) {
291 mAnimation.start();
292 }
293
294 }
John Reck2bc80422011-06-30 15:11:49 -0700295 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
296 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
297 if (thumbBlob == null) {
298 thumbnail.setImageResource(R.drawable.browser_thumbnail);
299 } else {
300 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
301 thumbBlob, 0, thumbBlob.length);
302 thumbnail.setImageBitmap(thumbBitmap);
303 }
304 TextView title = (TextView) view.findViewById(R.id.title);
305 title.setText(cursor.getString(SNAPSHOT_TITLE));
306 TextView size = (TextView) view.findViewById(R.id.size);
John Reck86374722011-07-19 16:10:23 -0700307 if (size != null) {
John Reck2b71d6d2012-04-18 17:42:06 -0700308 int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE);
John Reck86374722011-07-19 16:10:23 -0700309 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
310 }
John Reck8cc92352011-07-06 17:41:52 -0700311 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700312 TextView date = (TextView) view.findViewById(R.id.date);
313 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700314 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700315 }
316
317 @Override
318 public Cursor getItem(int position) {
319 return (Cursor) super.getItem(position);
320 }
321 }
322
323}