blob: a5a35195912dfbc43303a87f4be66f25183bbfbb [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;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -070080 private static Bitmap sDefaultFavicon;
John Reck2bc80422011-06-30 15:11:49 -070081
82 GridView mGrid;
83 View mEmpty;
84 SnapshotAdapter mAdapter;
John Reckd3e4d5b2011-07-13 15:48:43 -070085 CombinedBookmarksCallbacks mCallback;
John Reckb977a322011-09-08 18:08:25 -070086 long mAnimateId;
John Reck2bc80422011-06-30 15:11:49 -070087
Pankaj Garg68faf1c2015-06-26 17:07:37 -070088 View mRoot;
89
John Reck2d963a22011-08-10 15:53:07 -070090 @Override
91 public void onCreate(Bundle savedInstanceState) {
92 super.onCreate(savedInstanceState);
Tarun Nainani87a86682015-02-05 11:47:35 -080093 if (mCallback == null && getActivity() instanceof CombinedBookmarksCallbacks) {
94 mCallback = (CombinedBookmarksCallbacks) getActivity();
95 } else {
96 View cb = getActivity().getWindow().getDecorView().findViewById(R.id.combo_view_container);
97 if (cb != null && cb instanceof CombinedBookmarksCallbacks) {
98 mCallback = (CombinedBookmarksCallbacks) cb;
99 }
100 }
John Reckb977a322011-09-08 18:08:25 -0700101 mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700102 if (sDefaultFavicon == null)
103 sDefaultFavicon = BitmapFactory.decodeResource(getResources(),
104 R.drawable.ic_deco_favicon_normal);
John Reck2bc80422011-06-30 15:11:49 -0700105 }
106
107 @Override
108 public View onCreateView(LayoutInflater inflater, ViewGroup container,
109 Bundle savedInstanceState) {
110 View view = inflater.inflate(R.layout.snapshots, container, false);
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700111 mRoot = view;
John Reck2bc80422011-06-30 15:11:49 -0700112 mEmpty = view.findViewById(android.R.id.empty);
113 mGrid = (GridView) view.findViewById(R.id.grid);
114 setupGrid(inflater);
115 getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
116 return view;
117 }
118
119 @Override
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700120 public void onConfigurationChanged(Configuration newConfig) {
121 super.onConfigurationChanged(newConfig);
122 Resources res = getActivity().getResources();
123 int paddingTop = (int) res.getDimension(R.dimen.combo_paddingTop);
124 mRoot.setPadding(0, paddingTop, 0, 0);
125 }
126
127 @Override
John Reck2bc80422011-06-30 15:11:49 -0700128 public void onDestroyView() {
129 super.onDestroyView();
130 getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
John Reck24bdcb92011-07-11 16:48:10 -0700131 if (mAdapter != null) {
132 mAdapter.changeCursor(null);
133 mAdapter = null;
134 }
John Reck2bc80422011-06-30 15:11:49 -0700135 }
136
137 void setupGrid(LayoutInflater inflater) {
138 View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
139 int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
140 item.measure(mspec, mspec);
141 int width = item.getMeasuredWidth();
142 mGrid.setColumnWidth(width);
143 mGrid.setOnItemClickListener(this);
144 mGrid.setOnCreateContextMenuListener(this);
145 }
146
147 @Override
148 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
149 if (id == LOADER_SNAPSHOTS) {
John Reck2bc80422011-06-30 15:11:49 -0700150 return new CursorLoader(getActivity(),
151 Snapshots.CONTENT_URI, PROJECTION,
John Reck8cc92352011-07-06 17:41:52 -0700152 null, null, Snapshots.DATE_CREATED + " DESC");
John Reck2bc80422011-06-30 15:11:49 -0700153 }
154 return null;
155 }
156
157 @Override
158 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
159 if (loader.getId() == LOADER_SNAPSHOTS) {
160 if (mAdapter == null) {
161 mAdapter = new SnapshotAdapter(getActivity(), data);
162 mGrid.setAdapter(mAdapter);
163 } else {
164 mAdapter.changeCursor(data);
165 }
John Reckb977a322011-09-08 18:08:25 -0700166 if (mAnimateId > 0) {
167 mAdapter.animateIn(mAnimateId);
168 mAnimateId = 0;
169 getArguments().remove(EXTRA_ANIMATE_ID);
170 }
John Reck2bc80422011-06-30 15:11:49 -0700171 boolean empty = mAdapter.isEmpty();
172 mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
173 mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
174 }
175 }
176
177 @Override
178 public void onLoaderReset(Loader<Cursor> loader) {
179 }
180
181 @Override
182 public void onCreateContextMenu(ContextMenu menu, View v,
183 ContextMenuInfo menuInfo) {
184 MenuInflater inflater = getActivity().getMenuInflater();
185 inflater.inflate(R.menu.snapshots_context, menu);
186 // Create the header, re-use BookmarkItem (has the layout we want)
187 BookmarkItem header = new BookmarkItem(getActivity());
John Reck83c01512011-09-09 11:14:16 -0700188 header.setEnableScrolling(true);
John Reck2bc80422011-06-30 15:11:49 -0700189 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
190 populateBookmarkItem(mAdapter.getItem(info.position), header);
191 menu.setHeaderView(header);
192 }
193
194 private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
195 item.setName(cursor.getString(SNAPSHOT_TITLE));
196 item.setUrl(cursor.getString(SNAPSHOT_URL));
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700197 Bitmap favicon = getBitmap(cursor, SNAPSHOT_FAVICON);
198 if (favicon != null) {
199 item.setFavicon(favicon);
200 } else {
201 item.setFavicon(sDefaultFavicon);
202 }
203
John Reck2bc80422011-06-30 15:11:49 -0700204 }
205
206 static Bitmap getBitmap(Cursor cursor, int columnIndex) {
207 byte[] data = cursor.getBlob(columnIndex);
208 if (data == null) {
209 return null;
210 }
211 return BitmapFactory.decodeByteArray(data, 0, data.length);
212 }
213
214 @Override
215 public boolean onContextItemSelected(MenuItem item) {
kaiyizd1f75c72013-09-09 09:46:27 +0800216 if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) {
217 return false;
218 }
John Reck2bc80422011-06-30 15:11:49 -0700219 if (item.getItemId() == R.id.delete_context_menu_id) {
220 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
221 deleteSnapshot(info.id);
222 return true;
223 }
224 return super.onContextItemSelected(item);
225 }
226
227 void deleteSnapshot(long id) {
228 final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
229 final ContentResolver cr = getActivity().getContentResolver();
230 new Thread() {
231 @Override
232 public void run() {
233 cr.delete(uri, null, null);
234 }
235 }.start();
236
237 }
238
239 @Override
240 public void onItemClick(AdapterView<?> parent, View view, int position,
241 long id) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700242 mCallback.openSnapshot(id);
John Reck2bc80422011-06-30 15:11:49 -0700243 }
244
245 private static class SnapshotAdapter extends ResourceCursorAdapter {
John Reckb977a322011-09-08 18:08:25 -0700246 private long mAnimateId;
247 private AnimatorSet mAnimation;
248 private View mAnimationTarget;
John Reck2bc80422011-06-30 15:11:49 -0700249
250 public SnapshotAdapter(Context context, Cursor c) {
251 super(context, R.layout.snapshot_item, c, 0);
John Reckb977a322011-09-08 18:08:25 -0700252 mAnimation = new AnimatorSet();
253 mAnimation.playTogether(
254 ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
255 ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
256 mAnimation.setStartDelay(100);
257 mAnimation.setDuration(400);
258 mAnimation.addListener(new AnimatorListener() {
259
260 @Override
261 public void onAnimationStart(Animator animation) {
262 }
263
264 @Override
265 public void onAnimationRepeat(Animator animation) {
266 }
267
268 @Override
269 public void onAnimationEnd(Animator animation) {
270 mAnimateId = 0;
271 mAnimationTarget = null;
272 }
273
274 @Override
275 public void onAnimationCancel(Animator animation) {
276 }
277 });
278 }
279
280 public void animateIn(long id) {
281 mAnimateId = id;
John Reck2bc80422011-06-30 15:11:49 -0700282 }
283
284 @Override
285 public void bindView(View view, Context context, Cursor cursor) {
John Reckb977a322011-09-08 18:08:25 -0700286 long id = cursor.getLong(SNAPSHOT_ID);
287 if (id == mAnimateId) {
288 if (mAnimationTarget != view) {
289 float scale = 0f;
290 if (mAnimationTarget != null) {
291 scale = mAnimationTarget.getScaleX();
292 mAnimationTarget.setScaleX(1f);
293 mAnimationTarget.setScaleY(1f);
294 }
295 view.setScaleX(scale);
296 view.setScaleY(scale);
297 }
298 mAnimation.setTarget(view);
299 mAnimationTarget = view;
300 if (!mAnimation.isRunning()) {
301 mAnimation.start();
302 }
303
304 }
John Reck2bc80422011-06-30 15:11:49 -0700305 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
306 byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
307 if (thumbBlob == null) {
308 thumbnail.setImageResource(R.drawable.browser_thumbnail);
309 } else {
310 Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
311 thumbBlob, 0, thumbBlob.length);
312 thumbnail.setImageBitmap(thumbBitmap);
313 }
314 TextView title = (TextView) view.findViewById(R.id.title);
315 title.setText(cursor.getString(SNAPSHOT_TITLE));
John Reck8cc92352011-07-06 17:41:52 -0700316 long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
John Reck2bc80422011-06-30 15:11:49 -0700317 TextView date = (TextView) view.findViewById(R.id.date);
318 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
John Reck8cc92352011-07-06 17:41:52 -0700319 date.setText(dateFormat.format(new Date(timestamp)));
John Reck2bc80422011-06-30 15:11:49 -0700320 }
321
322 @Override
323 public Cursor getItem(int position) {
324 return (Cursor) super.getItem(position);
325 }
326 }
327
328}