12bc8042224be51966d748b870768ec1b376a1621John Reck/*
22bc8042224be51966d748b870768ec1b376a1621John Reck * Copyright (C) 2011 The Android Open Source Project
32bc8042224be51966d748b870768ec1b376a1621John Reck *
42bc8042224be51966d748b870768ec1b376a1621John Reck * Licensed under the Apache License, Version 2.0 (the "License");
52bc8042224be51966d748b870768ec1b376a1621John Reck * you may not use this file except in compliance with the License.
62bc8042224be51966d748b870768ec1b376a1621John Reck * You may obtain a copy of the License at
72bc8042224be51966d748b870768ec1b376a1621John Reck *
82bc8042224be51966d748b870768ec1b376a1621John Reck *      http://www.apache.org/licenses/LICENSE-2.0
92bc8042224be51966d748b870768ec1b376a1621John Reck *
102bc8042224be51966d748b870768ec1b376a1621John Reck * Unless required by applicable law or agreed to in writing, software
112bc8042224be51966d748b870768ec1b376a1621John Reck * distributed under the License is distributed on an "AS IS" BASIS,
122bc8042224be51966d748b870768ec1b376a1621John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132bc8042224be51966d748b870768ec1b376a1621John Reck * See the License for the specific language governing permissions and
142bc8042224be51966d748b870768ec1b376a1621John Reck * limitations under the License.
152bc8042224be51966d748b870768ec1b376a1621John Reck */
162bc8042224be51966d748b870768ec1b376a1621John Reckpackage com.android.browser;
172bc8042224be51966d748b870768ec1b376a1621John Reck
18b977a32608e08bee7d165ce1447e9db111d27fc8John Reckimport android.animation.Animator;
19b977a32608e08bee7d165ce1447e9db111d27fc8John Reckimport android.animation.Animator.AnimatorListener;
20b977a32608e08bee7d165ce1447e9db111d27fc8John Reckimport android.animation.AnimatorSet;
21b977a32608e08bee7d165ce1447e9db111d27fc8John Reckimport android.animation.ObjectAnimator;
222bc8042224be51966d748b870768ec1b376a1621John Reckimport android.app.Fragment;
232bc8042224be51966d748b870768ec1b376a1621John Reckimport android.app.LoaderManager.LoaderCallbacks;
242bc8042224be51966d748b870768ec1b376a1621John Reckimport android.content.ContentResolver;
252bc8042224be51966d748b870768ec1b376a1621John Reckimport android.content.ContentUris;
262bc8042224be51966d748b870768ec1b376a1621John Reckimport android.content.Context;
272bc8042224be51966d748b870768ec1b376a1621John Reckimport android.content.CursorLoader;
282bc8042224be51966d748b870768ec1b376a1621John Reckimport android.content.Loader;
292bc8042224be51966d748b870768ec1b376a1621John Reckimport android.database.Cursor;
302bc8042224be51966d748b870768ec1b376a1621John Reckimport android.graphics.Bitmap;
312bc8042224be51966d748b870768ec1b376a1621John Reckimport android.graphics.BitmapFactory;
322bc8042224be51966d748b870768ec1b376a1621John Reckimport android.net.Uri;
332bc8042224be51966d748b870768ec1b376a1621John Reckimport android.os.Bundle;
342bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.ContextMenu;
352bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.ContextMenu.ContextMenuInfo;
362bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.LayoutInflater;
372bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.MenuInflater;
382bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.MenuItem;
392bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.View;
402bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.View.MeasureSpec;
412bc8042224be51966d748b870768ec1b376a1621John Reckimport android.view.ViewGroup;
422bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.AdapterView;
432bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.AdapterView.AdapterContextMenuInfo;
442bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.AdapterView.OnItemClickListener;
452bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.GridView;
462bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.ImageView;
472bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.ResourceCursorAdapter;
482bc8042224be51966d748b870768ec1b376a1621John Reckimport android.widget.TextView;
492bc8042224be51966d748b870768ec1b376a1621John Reck
508cc9235816ac9b3f1b3551d6234684f0455746dcJohn Reckimport com.android.browser.provider.SnapshotProvider.Snapshots;
512bc8042224be51966d748b870768ec1b376a1621John Reck
522bc8042224be51966d748b870768ec1b376a1621John Reckimport java.text.DateFormat;
532bc8042224be51966d748b870768ec1b376a1621John Reckimport java.util.Date;
542bc8042224be51966d748b870768ec1b376a1621John Reck
552bc8042224be51966d748b870768ec1b376a1621John Reckpublic class BrowserSnapshotPage extends Fragment implements
562bc8042224be51966d748b870768ec1b376a1621John Reck        LoaderCallbacks<Cursor>, OnItemClickListener {
572bc8042224be51966d748b870768ec1b376a1621John Reck
582bc8042224be51966d748b870768ec1b376a1621John Reck    public static final String EXTRA_ANIMATE_ID = "animate_id";
592bc8042224be51966d748b870768ec1b376a1621John Reck
602bc8042224be51966d748b870768ec1b376a1621John Reck    private static final int LOADER_SNAPSHOTS = 1;
612bc8042224be51966d748b870768ec1b376a1621John Reck    private static final String[] PROJECTION = new String[] {
622bc8042224be51966d748b870768ec1b376a1621John Reck        Snapshots._ID,
632bc8042224be51966d748b870768ec1b376a1621John Reck        Snapshots.TITLE,
642b71d6dad1cbdc84da3eed140429a102971a1106John Reck        Snapshots.VIEWSTATE_SIZE,
652bc8042224be51966d748b870768ec1b376a1621John Reck        Snapshots.THUMBNAIL,
662bc8042224be51966d748b870768ec1b376a1621John Reck        Snapshots.FAVICON,
672bc8042224be51966d748b870768ec1b376a1621John Reck        Snapshots.URL,
688cc9235816ac9b3f1b3551d6234684f0455746dcJohn Reck        Snapshots.DATE_CREATED,
692bc8042224be51966d748b870768ec1b376a1621John Reck    };
70b977a32608e08bee7d165ce1447e9db111d27fc8John Reck    private static final int SNAPSHOT_ID = 0;
712bc8042224be51966d748b870768ec1b376a1621John Reck    private static final int SNAPSHOT_TITLE = 1;
722b71d6dad1cbdc84da3eed140429a102971a1106John Reck    private static final int SNAPSHOT_VIEWSTATE_SIZE = 2;
732bc8042224be51966d748b870768ec1b376a1621John Reck    private static final int SNAPSHOT_THUMBNAIL = 3;
742bc8042224be51966d748b870768ec1b376a1621John Reck    private static final int SNAPSHOT_FAVICON = 4;
752bc8042224be51966d748b870768ec1b376a1621John Reck    private static final int SNAPSHOT_URL = 5;
768cc9235816ac9b3f1b3551d6234684f0455746dcJohn Reck    private static final int SNAPSHOT_DATE_CREATED = 6;
772bc8042224be51966d748b870768ec1b376a1621John Reck
782bc8042224be51966d748b870768ec1b376a1621John Reck    GridView mGrid;
792bc8042224be51966d748b870768ec1b376a1621John Reck    View mEmpty;
802bc8042224be51966d748b870768ec1b376a1621John Reck    SnapshotAdapter mAdapter;
81d3e4d5b4ffdf374b836ec9d4d3e315040c8c3779John Reck    CombinedBookmarksCallbacks mCallback;
82b977a32608e08bee7d165ce1447e9db111d27fc8John Reck    long mAnimateId;
832bc8042224be51966d748b870768ec1b376a1621John Reck
842d963a2899afbe000d1ab498f94de87f25e33184John Reck    @Override
852d963a2899afbe000d1ab498f94de87f25e33184John Reck    public void onCreate(Bundle savedInstanceState) {
862d963a2899afbe000d1ab498f94de87f25e33184John Reck        super.onCreate(savedInstanceState);
872d963a2899afbe000d1ab498f94de87f25e33184John Reck        mCallback = (CombinedBookmarksCallbacks) getActivity();
88b977a32608e08bee7d165ce1447e9db111d27fc8John Reck        mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
892bc8042224be51966d748b870768ec1b376a1621John Reck    }
902bc8042224be51966d748b870768ec1b376a1621John Reck
912bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
922bc8042224be51966d748b870768ec1b376a1621John Reck    public View onCreateView(LayoutInflater inflater, ViewGroup container,
932bc8042224be51966d748b870768ec1b376a1621John Reck            Bundle savedInstanceState) {
942bc8042224be51966d748b870768ec1b376a1621John Reck        View view = inflater.inflate(R.layout.snapshots, container, false);
952bc8042224be51966d748b870768ec1b376a1621John Reck        mEmpty = view.findViewById(android.R.id.empty);
962bc8042224be51966d748b870768ec1b376a1621John Reck        mGrid = (GridView) view.findViewById(R.id.grid);
972bc8042224be51966d748b870768ec1b376a1621John Reck        setupGrid(inflater);
982bc8042224be51966d748b870768ec1b376a1621John Reck        getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
992bc8042224be51966d748b870768ec1b376a1621John Reck        return view;
1002bc8042224be51966d748b870768ec1b376a1621John Reck    }
1012bc8042224be51966d748b870768ec1b376a1621John Reck
1022bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
1032bc8042224be51966d748b870768ec1b376a1621John Reck    public void onDestroyView() {
1042bc8042224be51966d748b870768ec1b376a1621John Reck        super.onDestroyView();
1052bc8042224be51966d748b870768ec1b376a1621John Reck        getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
10624bdcb98d296f1b31e83379db27d978b90534fefJohn Reck        if (mAdapter != null) {
10724bdcb98d296f1b31e83379db27d978b90534fefJohn Reck            mAdapter.changeCursor(null);
10824bdcb98d296f1b31e83379db27d978b90534fefJohn Reck            mAdapter = null;
10924bdcb98d296f1b31e83379db27d978b90534fefJohn Reck        }
1102bc8042224be51966d748b870768ec1b376a1621John Reck    }
1112bc8042224be51966d748b870768ec1b376a1621John Reck
1122bc8042224be51966d748b870768ec1b376a1621John Reck    void setupGrid(LayoutInflater inflater) {
1132bc8042224be51966d748b870768ec1b376a1621John Reck        View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
1142bc8042224be51966d748b870768ec1b376a1621John Reck        int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
1152bc8042224be51966d748b870768ec1b376a1621John Reck        item.measure(mspec, mspec);
1162bc8042224be51966d748b870768ec1b376a1621John Reck        int width = item.getMeasuredWidth();
1172bc8042224be51966d748b870768ec1b376a1621John Reck        mGrid.setColumnWidth(width);
1182bc8042224be51966d748b870768ec1b376a1621John Reck        mGrid.setOnItemClickListener(this);
1192bc8042224be51966d748b870768ec1b376a1621John Reck        mGrid.setOnCreateContextMenuListener(this);
1202bc8042224be51966d748b870768ec1b376a1621John Reck    }
1212bc8042224be51966d748b870768ec1b376a1621John Reck
1222bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
1232bc8042224be51966d748b870768ec1b376a1621John Reck    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
1242bc8042224be51966d748b870768ec1b376a1621John Reck        if (id == LOADER_SNAPSHOTS) {
1252bc8042224be51966d748b870768ec1b376a1621John Reck            return new CursorLoader(getActivity(),
1262bc8042224be51966d748b870768ec1b376a1621John Reck                    Snapshots.CONTENT_URI, PROJECTION,
1278cc9235816ac9b3f1b3551d6234684f0455746dcJohn Reck                    null, null, Snapshots.DATE_CREATED + " DESC");
1282bc8042224be51966d748b870768ec1b376a1621John Reck        }
1292bc8042224be51966d748b870768ec1b376a1621John Reck        return null;
1302bc8042224be51966d748b870768ec1b376a1621John Reck    }
1312bc8042224be51966d748b870768ec1b376a1621John Reck
1322bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
1332bc8042224be51966d748b870768ec1b376a1621John Reck    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
1342bc8042224be51966d748b870768ec1b376a1621John Reck        if (loader.getId() == LOADER_SNAPSHOTS) {
1352bc8042224be51966d748b870768ec1b376a1621John Reck            if (mAdapter == null) {
1362bc8042224be51966d748b870768ec1b376a1621John Reck                mAdapter = new SnapshotAdapter(getActivity(), data);
1372bc8042224be51966d748b870768ec1b376a1621John Reck                mGrid.setAdapter(mAdapter);
1382bc8042224be51966d748b870768ec1b376a1621John Reck            } else {
1392bc8042224be51966d748b870768ec1b376a1621John Reck                mAdapter.changeCursor(data);
1402bc8042224be51966d748b870768ec1b376a1621John Reck            }
141b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            if (mAnimateId > 0) {
142b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                mAdapter.animateIn(mAnimateId);
143b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                mAnimateId = 0;
144b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                getArguments().remove(EXTRA_ANIMATE_ID);
145b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            }
1462bc8042224be51966d748b870768ec1b376a1621John Reck            boolean empty = mAdapter.isEmpty();
1472bc8042224be51966d748b870768ec1b376a1621John Reck            mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
1482bc8042224be51966d748b870768ec1b376a1621John Reck            mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
1492bc8042224be51966d748b870768ec1b376a1621John Reck        }
1502bc8042224be51966d748b870768ec1b376a1621John Reck    }
1512bc8042224be51966d748b870768ec1b376a1621John Reck
1522bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
1532bc8042224be51966d748b870768ec1b376a1621John Reck    public void onLoaderReset(Loader<Cursor> loader) {
1542bc8042224be51966d748b870768ec1b376a1621John Reck    }
1552bc8042224be51966d748b870768ec1b376a1621John Reck
1562bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
1572bc8042224be51966d748b870768ec1b376a1621John Reck    public void onCreateContextMenu(ContextMenu menu, View v,
1582bc8042224be51966d748b870768ec1b376a1621John Reck            ContextMenuInfo menuInfo) {
1592bc8042224be51966d748b870768ec1b376a1621John Reck        MenuInflater inflater = getActivity().getMenuInflater();
1602bc8042224be51966d748b870768ec1b376a1621John Reck        inflater.inflate(R.menu.snapshots_context, menu);
1612bc8042224be51966d748b870768ec1b376a1621John Reck        // Create the header, re-use BookmarkItem (has the layout we want)
1622bc8042224be51966d748b870768ec1b376a1621John Reck        BookmarkItem header = new BookmarkItem(getActivity());
16383c0151b266f4789cb1728294eac98e3dba0ed53John Reck        header.setEnableScrolling(true);
1642bc8042224be51966d748b870768ec1b376a1621John Reck        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
1652bc8042224be51966d748b870768ec1b376a1621John Reck        populateBookmarkItem(mAdapter.getItem(info.position), header);
1662bc8042224be51966d748b870768ec1b376a1621John Reck        menu.setHeaderView(header);
1672bc8042224be51966d748b870768ec1b376a1621John Reck    }
1682bc8042224be51966d748b870768ec1b376a1621John Reck
1692bc8042224be51966d748b870768ec1b376a1621John Reck    private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
1702bc8042224be51966d748b870768ec1b376a1621John Reck        item.setName(cursor.getString(SNAPSHOT_TITLE));
1712bc8042224be51966d748b870768ec1b376a1621John Reck        item.setUrl(cursor.getString(SNAPSHOT_URL));
1722bc8042224be51966d748b870768ec1b376a1621John Reck        item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
1732bc8042224be51966d748b870768ec1b376a1621John Reck    }
1742bc8042224be51966d748b870768ec1b376a1621John Reck
1752bc8042224be51966d748b870768ec1b376a1621John Reck    static Bitmap getBitmap(Cursor cursor, int columnIndex) {
1762bc8042224be51966d748b870768ec1b376a1621John Reck        byte[] data = cursor.getBlob(columnIndex);
1772bc8042224be51966d748b870768ec1b376a1621John Reck        if (data == null) {
1782bc8042224be51966d748b870768ec1b376a1621John Reck            return null;
1792bc8042224be51966d748b870768ec1b376a1621John Reck        }
1802bc8042224be51966d748b870768ec1b376a1621John Reck        return BitmapFactory.decodeByteArray(data, 0, data.length);
1812bc8042224be51966d748b870768ec1b376a1621John Reck    }
1822bc8042224be51966d748b870768ec1b376a1621John Reck
1832bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
1842bc8042224be51966d748b870768ec1b376a1621John Reck    public boolean onContextItemSelected(MenuItem item) {
1852bc8042224be51966d748b870768ec1b376a1621John Reck        if (item.getItemId() == R.id.delete_context_menu_id) {
1862bc8042224be51966d748b870768ec1b376a1621John Reck            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
1872bc8042224be51966d748b870768ec1b376a1621John Reck            deleteSnapshot(info.id);
1882bc8042224be51966d748b870768ec1b376a1621John Reck            return true;
1892bc8042224be51966d748b870768ec1b376a1621John Reck        }
1902bc8042224be51966d748b870768ec1b376a1621John Reck        return super.onContextItemSelected(item);
1912bc8042224be51966d748b870768ec1b376a1621John Reck    }
1922bc8042224be51966d748b870768ec1b376a1621John Reck
1932bc8042224be51966d748b870768ec1b376a1621John Reck    void deleteSnapshot(long id) {
1942bc8042224be51966d748b870768ec1b376a1621John Reck        final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
1952bc8042224be51966d748b870768ec1b376a1621John Reck        final ContentResolver cr = getActivity().getContentResolver();
1962bc8042224be51966d748b870768ec1b376a1621John Reck        new Thread() {
1972bc8042224be51966d748b870768ec1b376a1621John Reck            @Override
1982bc8042224be51966d748b870768ec1b376a1621John Reck            public void run() {
1992bc8042224be51966d748b870768ec1b376a1621John Reck                cr.delete(uri, null, null);
2002bc8042224be51966d748b870768ec1b376a1621John Reck            }
2012bc8042224be51966d748b870768ec1b376a1621John Reck        }.start();
2022bc8042224be51966d748b870768ec1b376a1621John Reck
2032bc8042224be51966d748b870768ec1b376a1621John Reck    }
2042bc8042224be51966d748b870768ec1b376a1621John Reck
2052bc8042224be51966d748b870768ec1b376a1621John Reck    @Override
2062bc8042224be51966d748b870768ec1b376a1621John Reck    public void onItemClick(AdapterView<?> parent, View view, int position,
2072bc8042224be51966d748b870768ec1b376a1621John Reck            long id) {
208d3e4d5b4ffdf374b836ec9d4d3e315040c8c3779John Reck        mCallback.openSnapshot(id);
2092bc8042224be51966d748b870768ec1b376a1621John Reck    }
2102bc8042224be51966d748b870768ec1b376a1621John Reck
2112bc8042224be51966d748b870768ec1b376a1621John Reck    private static class SnapshotAdapter extends ResourceCursorAdapter {
212b977a32608e08bee7d165ce1447e9db111d27fc8John Reck        private long mAnimateId;
213b977a32608e08bee7d165ce1447e9db111d27fc8John Reck        private AnimatorSet mAnimation;
214b977a32608e08bee7d165ce1447e9db111d27fc8John Reck        private View mAnimationTarget;
2152bc8042224be51966d748b870768ec1b376a1621John Reck
2162bc8042224be51966d748b870768ec1b376a1621John Reck        public SnapshotAdapter(Context context, Cursor c) {
2172bc8042224be51966d748b870768ec1b376a1621John Reck            super(context, R.layout.snapshot_item, c, 0);
218b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            mAnimation = new AnimatorSet();
219b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            mAnimation.playTogether(
220b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
221b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
222b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            mAnimation.setStartDelay(100);
223b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            mAnimation.setDuration(400);
224b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            mAnimation.addListener(new AnimatorListener() {
225b977a32608e08bee7d165ce1447e9db111d27fc8John Reck
226b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                @Override
227b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                public void onAnimationStart(Animator animation) {
228b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                }
229b977a32608e08bee7d165ce1447e9db111d27fc8John Reck
230b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                @Override
231b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                public void onAnimationRepeat(Animator animation) {
232b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                }
233b977a32608e08bee7d165ce1447e9db111d27fc8John Reck
234b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                @Override
235b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                public void onAnimationEnd(Animator animation) {
236b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    mAnimateId = 0;
237b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    mAnimationTarget = null;
238b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                }
239b977a32608e08bee7d165ce1447e9db111d27fc8John Reck
240b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                @Override
241b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                public void onAnimationCancel(Animator animation) {
242b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                }
243b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            });
244b977a32608e08bee7d165ce1447e9db111d27fc8John Reck        }
245b977a32608e08bee7d165ce1447e9db111d27fc8John Reck
246b977a32608e08bee7d165ce1447e9db111d27fc8John Reck        public void animateIn(long id) {
247b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            mAnimateId = id;
2482bc8042224be51966d748b870768ec1b376a1621John Reck        }
2492bc8042224be51966d748b870768ec1b376a1621John Reck
2502bc8042224be51966d748b870768ec1b376a1621John Reck        @Override
2512bc8042224be51966d748b870768ec1b376a1621John Reck        public void bindView(View view, Context context, Cursor cursor) {
252b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            long id = cursor.getLong(SNAPSHOT_ID);
253b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            if (id == mAnimateId) {
254b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                if (mAnimationTarget != view) {
255b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    float scale = 0f;
256b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    if (mAnimationTarget != null) {
257b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                        scale = mAnimationTarget.getScaleX();
258b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                        mAnimationTarget.setScaleX(1f);
259b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                        mAnimationTarget.setScaleY(1f);
260b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    }
261b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    view.setScaleX(scale);
262b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    view.setScaleY(scale);
263b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                }
264b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                mAnimation.setTarget(view);
265b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                mAnimationTarget = view;
266b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                if (!mAnimation.isRunning()) {
267b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                    mAnimation.start();
268b977a32608e08bee7d165ce1447e9db111d27fc8John Reck                }
269b977a32608e08bee7d165ce1447e9db111d27fc8John Reck
270b977a32608e08bee7d165ce1447e9db111d27fc8John Reck            }
2712bc8042224be51966d748b870768ec1b376a1621John Reck            ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
2722bc8042224be51966d748b870768ec1b376a1621John Reck            byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
2732bc8042224be51966d748b870768ec1b376a1621John Reck            if (thumbBlob == null) {
2742bc8042224be51966d748b870768ec1b376a1621John Reck                thumbnail.setImageResource(R.drawable.browser_thumbnail);
2752bc8042224be51966d748b870768ec1b376a1621John Reck            } else {
2762bc8042224be51966d748b870768ec1b376a1621John Reck                Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
2772bc8042224be51966d748b870768ec1b376a1621John Reck                        thumbBlob, 0, thumbBlob.length);
2782bc8042224be51966d748b870768ec1b376a1621John Reck                thumbnail.setImageBitmap(thumbBitmap);
2792bc8042224be51966d748b870768ec1b376a1621John Reck            }
2802bc8042224be51966d748b870768ec1b376a1621John Reck            TextView title = (TextView) view.findViewById(R.id.title);
2812bc8042224be51966d748b870768ec1b376a1621John Reck            title.setText(cursor.getString(SNAPSHOT_TITLE));
2822bc8042224be51966d748b870768ec1b376a1621John Reck            TextView size = (TextView) view.findViewById(R.id.size);
28386374729c42dae85147d207d03903a949b27ef43John Reck            if (size != null) {
2842b71d6dad1cbdc84da3eed140429a102971a1106John Reck                int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_SIZE);
28586374729c42dae85147d207d03903a949b27ef43John Reck                size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
28686374729c42dae85147d207d03903a949b27ef43John Reck            }
2878cc9235816ac9b3f1b3551d6234684f0455746dcJohn Reck            long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
2882bc8042224be51966d748b870768ec1b376a1621John Reck            TextView date = (TextView) view.findViewById(R.id.date);
2892bc8042224be51966d748b870768ec1b376a1621John Reck            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
2908cc9235816ac9b3f1b3551d6234684f0455746dcJohn Reck            date.setText(dateFormat.format(new Date(timestamp)));
2912bc8042224be51966d748b870768ec1b376a1621John Reck        }
2922bc8042224be51966d748b870768ec1b376a1621John Reck
2932bc8042224be51966d748b870768ec1b376a1621John Reck        @Override
2942bc8042224be51966d748b870768ec1b376a1621John Reck        public Cursor getItem(int position) {
2952bc8042224be51966d748b870768ec1b376a1621John Reck            return (Cursor) super.getItem(position);
2962bc8042224be51966d748b870768ec1b376a1621John Reck        }
2972bc8042224be51966d748b870768ec1b376a1621John Reck    }
2982bc8042224be51966d748b870768ec1b376a1621John Reck
2992bc8042224be51966d748b870768ec1b376a1621John Reck}
300