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; |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 29 | import android.content.res.Configuration; |
| 30 | import android.content.res.Resources; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 31 | import android.database.Cursor; |
| 32 | import android.graphics.Bitmap; |
| 33 | import android.graphics.BitmapFactory; |
| 34 | import android.net.Uri; |
| 35 | import android.os.Bundle; |
| 36 | import android.view.ContextMenu; |
| 37 | import android.view.ContextMenu.ContextMenuInfo; |
| 38 | import android.view.LayoutInflater; |
| 39 | import android.view.MenuInflater; |
| 40 | import android.view.MenuItem; |
| 41 | import android.view.View; |
| 42 | import android.view.View.MeasureSpec; |
| 43 | import android.view.ViewGroup; |
| 44 | import android.widget.AdapterView; |
| 45 | import android.widget.AdapterView.AdapterContextMenuInfo; |
| 46 | import android.widget.AdapterView.OnItemClickListener; |
| 47 | import android.widget.GridView; |
| 48 | import android.widget.ImageView; |
| 49 | import android.widget.ResourceCursorAdapter; |
| 50 | import android.widget.TextView; |
| 51 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 52 | import com.android.browser.R; |
| 53 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 54 | |
| 55 | import java.text.DateFormat; |
| 56 | import java.util.Date; |
| 57 | |
| 58 | public 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 Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 67 | Snapshots.VIEWSTATE_SIZE, |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 68 | Snapshots.THUMBNAIL, |
| 69 | Snapshots.FAVICON, |
| 70 | Snapshots.URL, |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 71 | Snapshots.DATE_CREATED, |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 72 | }; |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 73 | private static final int SNAPSHOT_ID = 0; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 74 | private static final int SNAPSHOT_TITLE = 1; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 75 | private static final int SNAPSHOT_VIEWSTATE_SIZE = 2; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 76 | private static final int SNAPSHOT_THUMBNAIL = 3; |
| 77 | private static final int SNAPSHOT_FAVICON = 4; |
| 78 | private static final int SNAPSHOT_URL = 5; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 79 | private static final int SNAPSHOT_DATE_CREATED = 6; |
Sagar Dhawan | ca9ecfb | 2015-08-10 17:27:58 -0700 | [diff] [blame] | 80 | private static Bitmap sDefaultFavicon; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 81 | |
| 82 | GridView mGrid; |
| 83 | View mEmpty; |
| 84 | SnapshotAdapter mAdapter; |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 85 | CombinedBookmarksCallbacks mCallback; |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 86 | long mAnimateId; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 87 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 88 | View mRoot; |
| 89 | |
John Reck | 2d963a2 | 2011-08-10 15:53:07 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void onCreate(Bundle savedInstanceState) { |
| 92 | super.onCreate(savedInstanceState); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 93 | 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 Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 101 | mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID); |
Sagar Dhawan | ca9ecfb | 2015-08-10 17:27:58 -0700 | [diff] [blame] | 102 | if (sDefaultFavicon == null) |
| 103 | sDefaultFavicon = BitmapFactory.decodeResource(getResources(), |
| 104 | R.drawable.ic_deco_favicon_normal); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 105 | } |
| 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 Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 111 | mRoot = view; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 112 | 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 Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 120 | 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 128 | public void onDestroyView() { |
| 129 | super.onDestroyView(); |
| 130 | getLoaderManager().destroyLoader(LOADER_SNAPSHOTS); |
John Reck | 24bdcb9 | 2011-07-11 16:48:10 -0700 | [diff] [blame] | 131 | if (mAdapter != null) { |
| 132 | mAdapter.changeCursor(null); |
| 133 | mAdapter = null; |
| 134 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 135 | } |
| 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 150 | return new CursorLoader(getActivity(), |
| 151 | Snapshots.CONTENT_URI, PROJECTION, |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 152 | null, null, Snapshots.DATE_CREATED + " DESC"); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 153 | } |
| 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 Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 166 | if (mAnimateId > 0) { |
| 167 | mAdapter.animateIn(mAnimateId); |
| 168 | mAnimateId = 0; |
| 169 | getArguments().remove(EXTRA_ANIMATE_ID); |
| 170 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 171 | 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 Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 188 | header.setEnableScrolling(true); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 189 | 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 Dhawan | ca9ecfb | 2015-08-10 17:27:58 -0700 | [diff] [blame] | 197 | Bitmap favicon = getBitmap(cursor, SNAPSHOT_FAVICON); |
| 198 | if (favicon != null) { |
| 199 | item.setFavicon(favicon); |
| 200 | } else { |
| 201 | item.setFavicon(sDefaultFavicon); |
| 202 | } |
| 203 | |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 204 | } |
| 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) { |
kaiyiz | d1f75c7 | 2013-09-09 09:46:27 +0800 | [diff] [blame] | 216 | if (!(item.getMenuInfo() instanceof AdapterContextMenuInfo)) { |
| 217 | return false; |
| 218 | } |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 219 | 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 Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 242 | mCallback.openSnapshot(id); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | private static class SnapshotAdapter extends ResourceCursorAdapter { |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 246 | private long mAnimateId; |
| 247 | private AnimatorSet mAnimation; |
| 248 | private View mAnimationTarget; |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 249 | |
| 250 | public SnapshotAdapter(Context context, Cursor c) { |
| 251 | super(context, R.layout.snapshot_item, c, 0); |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 252 | 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void bindView(View view, Context context, Cursor cursor) { |
John Reck | b977a32 | 2011-09-08 18:08:25 -0700 | [diff] [blame] | 286 | 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 Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 305 | 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 Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 316 | long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 317 | TextView date = (TextView) view.findViewById(R.id.date); |
| 318 | DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 319 | date.setText(dateFormat.format(new Date(timestamp))); |
John Reck | 2bc8042 | 2011-06-30 15:11:49 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public Cursor getItem(int position) { |
| 324 | return (Cursor) super.getItem(position); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | } |