blob: 2547ebf31c40f2deec3de88c9d7769da1bf1496e [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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
17package com.android.gallery3d.ui;
18
Owen Lina2fba682011-08-17 22:07:43 +080019import android.graphics.Bitmap;
20
Owen Lin504dd402012-03-07 17:39:56 +080021import com.android.gallery3d.data.MediaItem;
22
Owen Lina2fba682011-08-17 22:07:43 +080023public 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 Lina2fba682011-08-17 22:07:43 +080039
40 public AbstractDisplayItem(MediaItem item) {
41 mMediaItem = item;
42 if (item == null) mState = STATE_ERROR;
Owen Lina2fba682011-08-17 22:07:43 +080043 }
44
45 protected void updateImage(Bitmap bitmap, boolean isCancelled) {
46 if (mRecycling) {
Owen Lin504dd402012-03-07 17:39:56 +080047 recycleBitmap(bitmap);
Owen Lina2fba682011-08-17 22:07:43 +080048 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 Chang89e853d2011-10-24 19:46:07 +080067 if (mMediaItem != null) return mMediaItem.getRotation();
68 return 0;
Owen Lina2fba682011-08-17 22:07:43 +080069 }
70
71 @Override
Chih-Chung Changad21fbe2012-02-15 08:19:50 +080072 public int getIdentity() {
Owen Lina2fba682011-08-17 22:07:43 +080073 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 Lin504dd402012-03-07 17:39:56 +0800100 if (mBitmap != null) {
101 recycleBitmap(mBitmap);
102 mBitmap = null;
103 }
Owen Lina2fba682011-08-17 22:07:43 +0800104 } 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 Lin504dd402012-03-07 17:39:56 +0800117 abstract protected void recycleBitmap(Bitmap bitmap);
Owen Lina2fba682011-08-17 22:07:43 +0800118}