BrowserSnapshotPage.java revision b977a32608e08bee7d165ce1447e9db111d27fc8
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 */
16package com.android.browser;
17
18import android.animation.Animator;
19import android.animation.Animator.AnimatorListener;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
22import 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
50import com.android.browser.provider.SnapshotProvider.Snapshots;
51
52import java.text.DateFormat;
53import java.util.Date;
54
55public class BrowserSnapshotPage extends Fragment implements
56        LoaderCallbacks<Cursor>, OnItemClickListener {
57
58    public static final String EXTRA_ANIMATE_ID = "animate_id";
59
60    private static final int LOADER_SNAPSHOTS = 1;
61    private static final String[] PROJECTION = new String[] {
62        Snapshots._ID,
63        Snapshots.TITLE,
64        "length(" + Snapshots.VIEWSTATE + ")",
65        Snapshots.THUMBNAIL,
66        Snapshots.FAVICON,
67        Snapshots.URL,
68        Snapshots.DATE_CREATED,
69    };
70    private static final int SNAPSHOT_ID = 0;
71    private static final int SNAPSHOT_TITLE = 1;
72    private static final int SNAPSHOT_VIEWSTATE_LENGTH = 2;
73    private static final int SNAPSHOT_THUMBNAIL = 3;
74    private static final int SNAPSHOT_FAVICON = 4;
75    private static final int SNAPSHOT_URL = 5;
76    private static final int SNAPSHOT_DATE_CREATED = 6;
77
78    GridView mGrid;
79    View mEmpty;
80    SnapshotAdapter mAdapter;
81    CombinedBookmarksCallbacks mCallback;
82    long mAnimateId;
83
84    @Override
85    public void onCreate(Bundle savedInstanceState) {
86        super.onCreate(savedInstanceState);
87        mCallback = (CombinedBookmarksCallbacks) getActivity();
88        mAnimateId = getArguments().getLong(EXTRA_ANIMATE_ID);
89    }
90
91    @Override
92    public View onCreateView(LayoutInflater inflater, ViewGroup container,
93            Bundle savedInstanceState) {
94        View view = inflater.inflate(R.layout.snapshots, container, false);
95        mEmpty = view.findViewById(android.R.id.empty);
96        mGrid = (GridView) view.findViewById(R.id.grid);
97        setupGrid(inflater);
98        getLoaderManager().initLoader(LOADER_SNAPSHOTS, null, this);
99        return view;
100    }
101
102    @Override
103    public void onDestroyView() {
104        super.onDestroyView();
105        getLoaderManager().destroyLoader(LOADER_SNAPSHOTS);
106        if (mAdapter != null) {
107            mAdapter.changeCursor(null);
108            mAdapter = null;
109        }
110    }
111
112    void setupGrid(LayoutInflater inflater) {
113        View item = inflater.inflate(R.layout.snapshot_item, mGrid, false);
114        int mspec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
115        item.measure(mspec, mspec);
116        int width = item.getMeasuredWidth();
117        mGrid.setColumnWidth(width);
118        mGrid.setOnItemClickListener(this);
119        mGrid.setOnCreateContextMenuListener(this);
120    }
121
122    @Override
123    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
124        if (id == LOADER_SNAPSHOTS) {
125            return new CursorLoader(getActivity(),
126                    Snapshots.CONTENT_URI, PROJECTION,
127                    null, null, Snapshots.DATE_CREATED + " DESC");
128        }
129        return null;
130    }
131
132    @Override
133    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
134        if (loader.getId() == LOADER_SNAPSHOTS) {
135            if (mAdapter == null) {
136                mAdapter = new SnapshotAdapter(getActivity(), data);
137                mGrid.setAdapter(mAdapter);
138            } else {
139                mAdapter.changeCursor(data);
140            }
141            if (mAnimateId > 0) {
142                mAdapter.animateIn(mAnimateId);
143                mAnimateId = 0;
144                getArguments().remove(EXTRA_ANIMATE_ID);
145            }
146            boolean empty = mAdapter.isEmpty();
147            mGrid.setVisibility(empty ? View.GONE : View.VISIBLE);
148            mEmpty.setVisibility(empty ? View.VISIBLE : View.GONE);
149        }
150    }
151
152    @Override
153    public void onLoaderReset(Loader<Cursor> loader) {
154    }
155
156    @Override
157    public void onCreateContextMenu(ContextMenu menu, View v,
158            ContextMenuInfo menuInfo) {
159        MenuInflater inflater = getActivity().getMenuInflater();
160        inflater.inflate(R.menu.snapshots_context, menu);
161        // Create the header, re-use BookmarkItem (has the layout we want)
162        BookmarkItem header = new BookmarkItem(getActivity());
163        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
164        populateBookmarkItem(mAdapter.getItem(info.position), header);
165        menu.setHeaderView(header);
166    }
167
168    private void populateBookmarkItem(Cursor cursor, BookmarkItem item) {
169        item.setName(cursor.getString(SNAPSHOT_TITLE));
170        item.setUrl(cursor.getString(SNAPSHOT_URL));
171        item.setFavicon(getBitmap(cursor, SNAPSHOT_FAVICON));
172    }
173
174    static Bitmap getBitmap(Cursor cursor, int columnIndex) {
175        byte[] data = cursor.getBlob(columnIndex);
176        if (data == null) {
177            return null;
178        }
179        return BitmapFactory.decodeByteArray(data, 0, data.length);
180    }
181
182    @Override
183    public boolean onContextItemSelected(MenuItem item) {
184        if (item.getItemId() == R.id.delete_context_menu_id) {
185            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
186            deleteSnapshot(info.id);
187            return true;
188        }
189        return super.onContextItemSelected(item);
190    }
191
192    void deleteSnapshot(long id) {
193        final Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
194        final ContentResolver cr = getActivity().getContentResolver();
195        new Thread() {
196            @Override
197            public void run() {
198                cr.delete(uri, null, null);
199            }
200        }.start();
201
202    }
203
204    @Override
205    public void onItemClick(AdapterView<?> parent, View view, int position,
206            long id) {
207        mCallback.openSnapshot(id);
208    }
209
210    private static class SnapshotAdapter extends ResourceCursorAdapter {
211        private long mAnimateId;
212        private AnimatorSet mAnimation;
213        private View mAnimationTarget;
214
215        public SnapshotAdapter(Context context, Cursor c) {
216            super(context, R.layout.snapshot_item, c, 0);
217            mAnimation = new AnimatorSet();
218            mAnimation.playTogether(
219                    ObjectAnimator.ofFloat(null, View.SCALE_X, 0f, 1f),
220                    ObjectAnimator.ofFloat(null, View.SCALE_Y, 0f, 1f));
221            mAnimation.setStartDelay(100);
222            mAnimation.setDuration(400);
223            mAnimation.addListener(new AnimatorListener() {
224
225                @Override
226                public void onAnimationStart(Animator animation) {
227                }
228
229                @Override
230                public void onAnimationRepeat(Animator animation) {
231                }
232
233                @Override
234                public void onAnimationEnd(Animator animation) {
235                    mAnimateId = 0;
236                    mAnimationTarget = null;
237                }
238
239                @Override
240                public void onAnimationCancel(Animator animation) {
241                }
242            });
243        }
244
245        public void animateIn(long id) {
246            mAnimateId = id;
247        }
248
249        @Override
250        public void bindView(View view, Context context, Cursor cursor) {
251            long id = cursor.getLong(SNAPSHOT_ID);
252            if (id == mAnimateId) {
253                if (mAnimationTarget != view) {
254                    float scale = 0f;
255                    if (mAnimationTarget != null) {
256                        scale = mAnimationTarget.getScaleX();
257                        mAnimationTarget.setScaleX(1f);
258                        mAnimationTarget.setScaleY(1f);
259                    }
260                    view.setScaleX(scale);
261                    view.setScaleY(scale);
262                }
263                mAnimation.setTarget(view);
264                mAnimationTarget = view;
265                if (!mAnimation.isRunning()) {
266                    mAnimation.start();
267                }
268
269            }
270            ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
271            byte[] thumbBlob = cursor.getBlob(SNAPSHOT_THUMBNAIL);
272            if (thumbBlob == null) {
273                thumbnail.setImageResource(R.drawable.browser_thumbnail);
274            } else {
275                Bitmap thumbBitmap = BitmapFactory.decodeByteArray(
276                        thumbBlob, 0, thumbBlob.length);
277                thumbnail.setImageBitmap(thumbBitmap);
278            }
279            TextView title = (TextView) view.findViewById(R.id.title);
280            title.setText(cursor.getString(SNAPSHOT_TITLE));
281            TextView size = (TextView) view.findViewById(R.id.size);
282            if (size != null) {
283                int stateLen = cursor.getInt(SNAPSHOT_VIEWSTATE_LENGTH);
284                size.setText(String.format("%.2fMB", stateLen / 1024f / 1024f));
285            }
286            long timestamp = cursor.getLong(SNAPSHOT_DATE_CREATED);
287            TextView date = (TextView) view.findViewById(R.id.date);
288            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
289            date.setText(dateFormat.format(new Date(timestamp)));
290        }
291
292        @Override
293        public Cursor getItem(int position) {
294            return (Cursor) super.getItem(position);
295        }
296    }
297
298}
299