/
SinglePhotoDataAdapter.java
157 lines (137 loc) · 4.79 KB
/
SinglePhotoDataAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mariotaku.gallery3d.app;
import org.mariotaku.gallery3d.common.BitmapUtils;
import org.mariotaku.gallery3d.data.MediaItem;
import org.mariotaku.gallery3d.ui.BitmapScreenNail;
import org.mariotaku.gallery3d.ui.PhotoView;
import org.mariotaku.gallery3d.ui.SynchronizedHandler;
import org.mariotaku.gallery3d.ui.TileImageViewAdapter;
import org.mariotaku.gallery3d.util.Future;
import org.mariotaku.gallery3d.util.FutureListener;
import org.mariotaku.gallery3d.util.ThreadPool;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class SinglePhotoDataAdapter extends TileImageViewAdapter implements ImageViewerGLActivity.Model {
private static final String TAG = "SinglePhotoDataAdapter";
private static final int SIZE_BACKUP = 1024;
private static final int MSG_IMAGE_LOAD_FINISHED = 2;
private static final int MSG_IMAGE_LOAD_START = 1;
private final MediaItem mItem;
private Future<?> mTask;
private final Handler mHandler;
private final PhotoView mPhotoView;
private final ThreadPool mThreadPool;
private final ImageViewerGLActivity mActivity;
private final int mLoadingState = LOADING_INIT;
private BitmapScreenNail mBitmapScreenNail;
private final FutureListener<BitmapRegionDecoder> mLargeListener = new FutureListener<BitmapRegionDecoder>() {
@Override
public void onFutureDone(final Future<BitmapRegionDecoder> future) {
final BitmapRegionDecoder decoder = future.get();
if (decoder == null) return;
final int width = decoder.getWidth();
final int height = decoder.getHeight();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = BitmapUtils.computeSampleSize((float) SIZE_BACKUP / Math.max(width, height));
final Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, width, height), options);
mHandler.sendMessage(mHandler.obtainMessage(MSG_IMAGE_LOAD_FINISHED, new ImageBundle(decoder, bitmap)));
}
@Override
public void onFutureStart(final Future<BitmapRegionDecoder> future) {
mHandler.sendMessage(mHandler.obtainMessage(MSG_IMAGE_LOAD_START));
}
};
public SinglePhotoDataAdapter(final ImageViewerGLActivity activity, final PhotoView view, final MediaItem item) {
mItem = item;
mPhotoView = view;
mActivity = activity;
mThreadPool = activity.getThreadPool();
mHandler = new SynchronizedHandler(activity.getGLRoot()) {
@Override
public void handleMessage(final Message message) {
switch (message.what) {
case MSG_IMAGE_LOAD_START: {
mActivity.onLoadStart();
break;
}
case MSG_IMAGE_LOAD_FINISHED: {
onDecodeLargeComplete((ImageBundle) message.obj);
mActivity.onLoadFinished();
break;
}
}
}
};
}
@Override
public int getImageRotation() {
return mItem.getFullImageRotation();
}
@Override
public int getLoadingState() {
return mLoadingState;
}
@Override
public MediaItem getMediaItem() {
return mItem;
}
@Override
public void pause() {
final Future<?> task = mTask;
task.cancel();
task.waitDone();
if (task.get() == null) {
mTask = null;
}
if (mBitmapScreenNail != null) {
mBitmapScreenNail.recycle();
mBitmapScreenNail = null;
}
}
@Override
public void resume() {
if (mTask == null) {
mTask = mThreadPool.submit(mItem.requestLargeImage(), mLargeListener);
}
}
private void onDecodeLargeComplete(final ImageBundle bundle) {
try {
setScreenNail(bundle.backupImage, bundle.decoder.getWidth(), bundle.decoder.getHeight());
setRegionDecoder(bundle.decoder);
mPhotoView.notifyImageChange();
} catch (final Throwable t) {
Log.w(TAG, "fail to decode large", t);
}
}
private void setScreenNail(final Bitmap bitmap, final int width, final int height) {
mBitmapScreenNail = new BitmapScreenNail(bitmap);
setScreenNail(mBitmapScreenNail, width, height);
}
private static class ImageBundle {
public final BitmapRegionDecoder decoder;
public final Bitmap backupImage;
public ImageBundle(final BitmapRegionDecoder decoder, final Bitmap backupImage) {
this.decoder = decoder;
this.backupImage = backupImage;
}
}
}