blob: e2194d165d952d2f7fdcb99258f58d5a85872833 [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);
Tarun Nainani87a86682015-02-05 11:47:35 -080088 if (mCallback == null && getActivity() instanceof CombinedBookmarksCallbacks) {
89 mCallback = (CombinedBookmarksCallbacks) getActivity();
90 } else {
91 View cb = getActivity().getWindow().getDecorView().findViewById(R.id.combo_view_container);
92 if (cb != null && cb instanceof CombinedBookmarksCallbacks) {
93 mCallback = (CombinedBookmarksCallbacks) cb;
94 }
95 }
John Reckb977a322011-09-08 18:08:25 -070096 mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
John Reck2bc80422011-06-30 15:11:49 -070097 }
98
99 @Override
100 public View onCreateView(LayoutInflater inflater, ViewGroup container,
101 Bundle savedInstanceState) {
102 View view = inflater.inflate(R.layout.snapshots, container, false);
103 mEmpty = view.findViewById(android.R.id.empty);
104 mGrid = (GridView) view.findViewById(R.id.grid);
105 setupGrid(inflater);
106 getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
107 return view;
108 }
109
110 @Override
111 public void onDestroyView() {
112 super.onDestroyView();
113 getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
John Reck24bdcb92011-07-11 16:48:10 -0700114 if (mAdapter != null) {
115 mAdapter.changeCursor(null);
116 mAdapter = null;
117 }
John Reck2bc80422011-06-30 15:11:49 -0700118 }
119
120 void setupGrid(LayoutInflater inflater) {
121 View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
122 int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
123 item.measure(mspec, mspec);
124 int width = item.getMeasuredWidth();
125 mGrid.setColumnWidth(width);
126 mGrid.setOnItemClickListener(this);
127 mGrid.setOnCreateContextMenuListener(this);
128 }
129
130 @Override
131 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
132 if (id == LOADER_SNAPSHOTS) {
John Reck2bc80422011-06-30 15:11:49 -0700133 return new CursorLoader(getActivity(),
134 Snapshots.CONTENT_URI, PROJECTION,
John Reck8cc92352011-07-06 17:41:52 -0700135 null, null, Snapshots.DATE_CREATED + " DESC");
John Reck2bc80422011-06-30 15:11:49 -0700136 }
137 return null;
138 }
139
140 @Override
141 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
142 if (loader.getId() == LOADER_SNAPSHOTS) {
143 if (mAdapter == null) {
144 mAdapter = new SnapshotAdapter(getActivity(), data);
145 mGrid.setAdapter(mAdapter);
146 } else {
147 mAdapter.changeCursor(data);
148 }
John Reckb977a322011-09-08 18:08:25 -0700149 if (mAnimateId > 0) {
150 mAdapter.animateIn(mAnimateId);
151 mAnimateId = 0;
152 getArguments().remove(EXTRA_ANIMATE_ID);
153 }
John Reck2bc80422011-06-30 15:11:49 -0700154 boolean empty = mAdapter.isEmpty();
155 mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
156 mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
157 }
158 }
159
160 @Override
161 public void onLoaderReset(Loader<Cursor> loader) {
162 }
163
164 @Override
165 public void onCreateContextMenu(ContextMenu menu, View v,
166 ContextMenuInfo menuInfo) {
167 MenuInflater inflater = getActivity().getMenuInflater();
168 inflater.inflate(R.menu.snapshots_context, menu);
169 // Create the header, re-use BookmarkItem (has the layout we want)
170 BookmarkItem header = new BookmarkItem(getActivity());
John Reck83c01512011-09-09 11:14:16 -0700171 header.setEnableScrolling(true);
John Reck2bc80422011-06-30 15:11:49 -0700172 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
173 populateBookmarkItem(mAdapter.getItem(info.position), header);
174 menu.setHeaderView(header);
175 }
176
177 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
178 item.setName(cursor.getString(SNAPSHOT_TITLE));
179 item.setUrl(cursor.getString(SNAPSHOT_URL));
180 item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
181 }
182
183 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
184 byte[] data = cursor.getBlob(columnIndex);
185 if (data == null) {
186 return null;
187 }
188 return BitmapFactory.decodeByteArray(data, 0, data.length);
189 }
190
191 @Override
192 public boolean onContextItemSelected(MenuItem item) {
kaiyizd1f75c72013-09-09 09:46:27 +0800193 if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) {
194 return false;
195 }
John Reck2bc80422011-06-30 15:11:49 -0700196 if (item.getItemId() == R.id.delete_context_menu_id) {
197 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
198 deleteSnapshot(info.id);
199 return true;
200 }
201 return super.onContextItemSelected(item);
202 }
203
204 void deleteSnapshot(long id) {
205 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
206 final ContentResolver cr = getActivity().getContentResolver();
207 new Thread() {
208 @Override
209 public void run() {
210 cr.delete(uri, null, null);
211 }
212 }.start();
213
214 }
215
216 @Override
217 public void onItemClick(AdapterView<?> parent, View view, int position,
218 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700219 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700220 }
221
222 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700223 private long mAnimateId;
224 private AnimatorSet mAnimation;
225 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700226
227 public SnapshotAdapter(Context context, Cursor c) {
228 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700229 mAnimation = new AnimatorSet();
230 mAnimation.playTogether(
231 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
232 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
233 mAnimation.setStartDelay(100);
234 mAnimation.setDuration(400);
235 mAnimation.addListener(new AnimatorListener() {
236
237 @Override
238 public void onAnimationStart(Animator animation) {
239 }
240
241 @Override
242 public void onAnimationRepeat(Animator animation) {
243 }
244
245 @Override
246 public void onAnimationEnd(Animator animation) {
247 mAnimateId = 0;
248 mAnimationTarget = null;
249 }
250
251 @Override
252 public void onAnimationCancel(Animator animation) {
253 }
254 });
255 }
256
257 public void animateIn(long id) {
258 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700259 }
260
261 @Override
262 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700263 long id = cursor.getLong(SNAPSHOT_ID);
264 if (id == mAnimateId) {
265 if (mAnimationTarget != view) {
266 float scale = 0f;
267 if (mAnimationTarget != null) {
268 scale = mAnimationTarget.getScaleX();
269 mAnimationTarget.setScaleX(1f);
270 mAnimationTarget.setScaleY(1f);
271 }
272 view.setScaleX(scale);
273 view.setScaleY(scale);
274 }
275 mAnimation.setTarget(view);
276 mAnimationTarget = view;
277 if (!mAnimation.isRunning()) {
278 mAnimation.start();
279 }
280
281 }
John Reck2bc80422011-06-30 15:11:49 -0700282 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
283 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
284 if (thumbBlob == null) {
285 thumbnail.setImageResource(R.drawable.browser_thumbnail);
286 } else {
287 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
288 thumbBlob, 0, thumbBlob.length);
289 thumbnail.setImageBitmap(thumbBitmap);
290 }
291 TextView title = (TextView) view.findViewById(R.id.title);
292 title.setText(cursor.getString(SNAPSHOT_TITLE));
293 TextView size = (TextView) view.findViewById(R.id.size);
John Reck86374722011-07-19 16:10:23 -0700294 if (size != null) {
John Reck2b71d6d2012-04-18 17:42:06 -0700295 int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE);
John Reck86374722011-07-19 16:10:23 -0700296 size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
297 }
John Reck8cc92352011-07-06 17:41:52 -0700298 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700299 TextView date = (TextView) view.findViewById(R.id.date);
300 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700301 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700302 }
303
304 @Override
305 public Cursor getItem(int position) {
306 return (Cursor) super.getItem(position);
307 }
308 }
309
310}