John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 16 | package com.android.browser; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 17 | |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 18 | import android.animation.Animator; |
| 19 | import android.animation.Animator.AnimatorListener; |
| 20 | import android.animation.AnimatorSet; |
| 21 | import android.animation.ObjectAnimator; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 22 | import android.app.Fragment; |
| 23 | import android.app.LoaderManager.LoaderCallbacks; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.ContentUris; |
| 26 | import android.content.Context; |
| 27 | import android.content.CursorLoader; |
| 28 | import android.content.Loader; |
| 29 | import android.database.Cursor; |
| 30 | import android.graphics.Bitmap; |
| 31 | import android.graphics.BitmapFactory; |
| 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
| 34 | import android.view.ContextMenu; |
| 35 | import android.view.ContextMenu.ContextMenuInfo; |
| 36 | import android.view.LayoutInflater; |
| 37 | import android.view.MenuInflater; |
| 38 | import android.view.MenuItem; |
| 39 | import android.view.View; |
| 40 | import android.view.View.MeasureSpec; |
| 41 | import android.view.ViewGroup; |
| 42 | import android.widget.AdapterView; |
| 43 | import android.widget.AdapterView.AdapterContextMenuInfo; |
| 44 | import android.widget.AdapterView.OnItemClickListener; |
| 45 | import android.widget.GridView; |
| 46 | import android.widget.ImageView; |
| 47 | import android.widget.ResourceCursorAdapter; |
| 48 | import android.widget.TextView; |
| 49 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 50 | import com.android.browser.R; |
| 51 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 52 | |
| 53 | import java.text.DateFormat; |
| 54 | import java.util.Date; |
| 55 | |
| 56 | public 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 Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 65 | Snapshots.VIEWSTATE_SIZE, |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 66 | Snapshots.THUMBNAIL, |
| 67 | Snapshots.FAVICON, |
| 68 | Snapshots.URL, |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 69 | Snapshots.DATE_CREATED, |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 70 | }; |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 71 | private static final int SNAPSHOT_ID = 0; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 72 | private static final int SNAPSHOT_TITLE = 1; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 73 | private static final int SNAPSHOT_VIEWSTATE_SIZE = 2; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 74 | private static final int SNAPSHOT_THUMBNAIL = 3; |
| 75 | private static final int SNAPSHOT_FAVICON = 4; |
| 76 | private static final int SNAPSHOT_URL = 5; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 77 | private static final int SNAPSHOT_DATE_CREATED = 6; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 78 | |
| 79 | GridView mGrid; |
| 80 | View mEmpty; |
| 81 | SnapshotAdapter mAdapter; |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 82 | CombinedBookmarksCallbacks mCallback; |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 83 | long mAnimateId; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 84 | |
John Reck | 2d963a2 | 2011-08-10 15:53:07 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public void onCreate(Bundle savedInstanceState) { |
| 87 | super.onCreate(savedInstanceState); |
| 88 | mCallback = (CombinedBookmarksCallbacks) getActivity(); |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 89 | mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 90 | } |
| 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 Reck | 24bdcb9 | 2011-07-11 16:48:10 -0700 | [diff] [blame] | 107 | if (mAdapter != null) { |
| 108 | mAdapter.changeCursor(null); |
| 109 | mAdapter = null; |
| 110 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 111 | } |
| 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 126 | return new CursorLoader(getActivity(), |
| 127 | Snapshots.CONTENT_URI, PROJECTION, |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 128 | null, null, Snapshots.DATE_CREATED + " DESC"); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 129 | } |
| 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 Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 142 | if (mAnimateId > 0) { |
| 143 | mAdapter.animateIn(mAnimateId); |
| 144 | mAnimateId = 0; |
| 145 | getArguments().remove(EXTRA_ANIMATE_ID); |
| 146 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 147 | 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 Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 164 | header.setEnableScrolling(true); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 165 | 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) { |
kaiyiz | d1f75c7 | 2013-09-09 09:46:27 +0800 | [diff] [blame] | 186 | if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) { |
| 187 | return false; |
| 188 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 189 | 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 Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 212 | mCallback.openSnapshot(id); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private static class SnapshotAdapter extends ResourceCursorAdapter { |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 216 | private long mAnimateId; |
| 217 | private AnimatorSet mAnimation; |
| 218 | private View mAnimationTarget; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 219 | |
| 220 | public SnapshotAdapter(Context context, Cursor c) { |
| 221 | super(context, R.layout.snapshot_item, c, 0); |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 222 | 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void bindView(View view, Context context, Cursor cursor) { |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 256 | 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 275 | 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 Reck | 8637472 | 2011-07-19 16:10:23 -0700 | [diff] [blame] | 287 | if (size != null) { |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 288 | int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE); |
John Reck | 8637472 | 2011-07-19 16:10:23 -0700 | [diff] [blame] | 289 | size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f)); |
| 290 | } |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 291 | long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 292 | TextView date = (TextView) view.findViewById(R.id.date); |
| 293 | DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 294 | date.setText(dateFormat.format(new Date(timestamp))); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public Cursor getItem(int position) { |
| 299 | return (Cursor) super.getItem(position); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | } |