Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
| 16 | |
| 17 | package com.android.gallery3d.ui; |
| 18 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
| 20 | |
Owen Lin | 504dd40 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 21 | import com.android.gallery3d.data.MediaItem; |
| 22 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 23 | public abstract class AbstractDisplayItem extends DisplayItem { |
| 24 | |
| 25 | private static final String TAG = "AbstractDisplayItem"; |
| 26 | |
| 27 | private static final int STATE_INVALID = 0x01; |
| 28 | private static final int STATE_VALID = 0x02; |
| 29 | private static final int STATE_UPDATING = 0x04; |
| 30 | private static final int STATE_CANCELING = 0x08; |
| 31 | private static final int STATE_ERROR = 0x10; |
| 32 | |
| 33 | private int mState = STATE_INVALID; |
| 34 | private boolean mImageRequested = false; |
| 35 | private boolean mRecycling = false; |
| 36 | private Bitmap mBitmap; |
| 37 | |
| 38 | protected final MediaItem mMediaItem; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 39 | |
| 40 | public AbstractDisplayItem(MediaItem item) { |
| 41 | mMediaItem = item; |
| 42 | if (item == null) mState = STATE_ERROR; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | protected void updateImage(Bitmap bitmap, boolean isCancelled) { |
| 46 | if (mRecycling) { |
Owen Lin | 504dd40 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 47 | recycleBitmap(bitmap); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (isCancelled && bitmap == null) { |
| 52 | mState = STATE_INVALID; |
| 53 | if (mImageRequested) { |
| 54 | // request image again. |
| 55 | requestImage(); |
| 56 | } |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | mBitmap = bitmap; |
| 61 | mState = bitmap == null ? STATE_ERROR : STATE_VALID ; |
| 62 | onBitmapAvailable(mBitmap); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int getRotation() { |
Chih-Chung Chang | 89e853d | 2011-10-24 19:46:07 +0800 | [diff] [blame] | 67 | if (mMediaItem != null) return mMediaItem.getRotation(); |
| 68 | return 0; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Chih-Chung Chang | ad21fbe | 2012-02-15 08:19:50 +0800 | [diff] [blame] | 72 | public int getIdentity() { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 73 | return mMediaItem != null |
| 74 | ? System.identityHashCode(mMediaItem.getPath()) |
| 75 | : System.identityHashCode(this); |
| 76 | } |
| 77 | |
| 78 | public void requestImage() { |
| 79 | mImageRequested = true; |
| 80 | if (mState == STATE_INVALID) { |
| 81 | mState = STATE_UPDATING; |
| 82 | startLoadBitmap(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public void cancelImageRequest() { |
| 87 | mImageRequested = false; |
| 88 | if (mState == STATE_UPDATING) { |
| 89 | mState = STATE_CANCELING; |
| 90 | cancelLoadBitmap(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private boolean inState(int states) { |
| 95 | return (mState & states) != 0; |
| 96 | } |
| 97 | |
| 98 | public void recycle() { |
| 99 | if (!inState(STATE_UPDATING | STATE_CANCELING)) { |
Owen Lin | 504dd40 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 100 | if (mBitmap != null) { |
| 101 | recycleBitmap(mBitmap); |
| 102 | mBitmap = null; |
| 103 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 104 | } else { |
| 105 | mRecycling = true; |
| 106 | cancelImageRequest(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public boolean isRequestInProgress() { |
| 111 | return mImageRequested && inState(STATE_UPDATING | STATE_CANCELING); |
| 112 | } |
| 113 | |
| 114 | abstract protected void startLoadBitmap(); |
| 115 | abstract protected void cancelLoadBitmap(); |
| 116 | abstract protected void onBitmapAvailable(Bitmap bitmap); |
Owen Lin | 504dd40 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 117 | abstract protected void recycleBitmap(Bitmap bitmap); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 118 | } |