Add GalleryProvider
This commit is contained in:
1 parent
091ef383eb
commit
7ad8a3afd1
19 files changed
+1045
-292
No files matched your search
@@ -16,26 +16,42 @@
|
||||
|
||||
package com.hippo.ehviewer.ui;
|
||||
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.SparseIntArray;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.ui.gl.GalleryPageView;
|
||||
import com.hippo.ehviewer.ui.gl.GalleryView;
|
||||
import com.hippo.ehviewer.gallery.DirGalleryProvider;
|
||||
import com.hippo.ehviewer.gallery.GalleryProvider;
|
||||
import com.hippo.ehviewer.gallery.GalleryProviderListener;
|
||||
import com.hippo.ehviewer.gallery.gl.GalleryPageView;
|
||||
import com.hippo.ehviewer.gallery.gl.GalleryView;
|
||||
import com.hippo.gl.glrenderer.ImageTexture;
|
||||
import com.hippo.gl.glrenderer.MovableTextTexture;
|
||||
import com.hippo.gl.view.GLRootView;
|
||||
import com.hippo.gl.view.GLView;
|
||||
import com.hippo.image.Image;
|
||||
import com.hippo.yorozuya.IntIdGenerator;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.File;
|
||||
|
||||
public class GalleryActivity extends AppCompatActivity {
|
||||
public class GalleryActivity extends AppCompatActivity implements GalleryProviderListener {
|
||||
|
||||
@Nullable
|
||||
private GalleryView mGalleryView;
|
||||
|
||||
@Nullable
|
||||
private MovableTextTexture mPageTextTexture;
|
||||
@Nullable
|
||||
private ImageTexture.Uploader mUploader;
|
||||
@Nullable
|
||||
private GalleryProvider mGalleryProvider;
|
||||
@Nullable
|
||||
private SparseIntArray mIndexIdMap;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@@ -43,16 +59,41 @@ public class GalleryActivity extends AppCompatActivity {
|
||||
|
||||
setContentView(R.layout.activity_gallery);
|
||||
GLRootView glRootView = (GLRootView) findViewById(R.id.gl_root_view);
|
||||
mGalleryView = new GalleryView(this, new InvalidPageIterator(), null);
|
||||
glRootView.setContentPane(mGalleryView);
|
||||
|
||||
mUploader = new ImageTexture.Uploader(glRootView);
|
||||
mIndexIdMap = new SparseIntArray();
|
||||
mGalleryProvider = new DirGalleryProvider(new File(Environment.getExternalStorageDirectory(), "nmb/image"));
|
||||
mGalleryProvider.addGalleryProviderListener(this);
|
||||
mGalleryProvider.start();
|
||||
|
||||
mPageTextTexture = MovableTextTexture.create(Typeface.DEFAULT,
|
||||
getResources().getDimensionPixelSize(R.dimen.gallery_page_text),
|
||||
getResources().getColor(R.color.secondary_text_dark),
|
||||
new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
|
||||
|
||||
mGalleryView = new GalleryView(this, new GalleryPageIterator(),
|
||||
mPageTextTexture, null, GalleryView.LAYOUT_MODE_LEFT_TO_RIGHT);
|
||||
glRootView.setContentPane(mGalleryView);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mGalleryView = null;
|
||||
mUploader = null;
|
||||
if (mUploader != null) {
|
||||
mUploader.clear();
|
||||
mUploader = null;
|
||||
}
|
||||
if (mPageTextTexture != null) {
|
||||
mPageTextTexture.recycle();
|
||||
mPageTextTexture = null;
|
||||
}
|
||||
if (mGalleryProvider != null) {
|
||||
mGalleryProvider.removeGalleryProviderListener(this);
|
||||
mGalleryProvider.stop();
|
||||
mGalleryProvider = null;
|
||||
}
|
||||
mIndexIdMap = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -128,24 +169,61 @@ public class GalleryActivity extends AppCompatActivity {
|
||||
return super.onKeyUp(keyCode, event);
|
||||
}
|
||||
|
||||
private GalleryPageView findPageByIndex(int index) {
|
||||
if (mIndexIdMap == null || mGalleryView == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final int MAX_SIZE = 2;
|
||||
int id = mIndexIdMap.get(index, GLView.NO_ID);
|
||||
if (id == GLView.NO_ID) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private class InvalidPageIterator implements GalleryView.PageIterator {
|
||||
return mGalleryView.findPageById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPagePercent(int index, float percent) {
|
||||
GalleryPageView page = findPageByIndex(index);
|
||||
if (page != null) {
|
||||
page.showInfo();
|
||||
page.setImage(null);
|
||||
page.setPage(index + 1);
|
||||
page.setProgress(percent);
|
||||
page.setError(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageSucceed(int index, Image image) {
|
||||
GalleryPageView page = findPageByIndex(index);
|
||||
if (page != null) {
|
||||
page.showImage();
|
||||
page.setImage(image);
|
||||
page.setPage(index + 1);
|
||||
page.setProgress(GalleryPageView.PROGRESS_GONE);
|
||||
page.setError(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFailed(int index, Exception e) {
|
||||
GalleryPageView page = findPageByIndex(index);
|
||||
if (page != null && mGalleryView != null) {
|
||||
page.showInfo();
|
||||
page.setImage(null);
|
||||
page.setPage(index + 1);
|
||||
page.setProgress(GalleryPageView.PROGRESS_GONE);
|
||||
page.setError("Error", mGalleryView);
|
||||
}
|
||||
}
|
||||
|
||||
private class GalleryPageIterator extends GalleryView.PageIterator {
|
||||
|
||||
private IntIdGenerator mIdGenerator = new IntIdGenerator();
|
||||
private int mIndex = 0;
|
||||
private int mBackup = mIndex;
|
||||
|
||||
public ImageTexture mImageTexture;
|
||||
|
||||
public InvalidPageIterator() {
|
||||
try {
|
||||
mImageTexture = new ImageTexture(Image.decode(new FileInputStream("/sdcard/1.jpg"), false));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mark() {
|
||||
mBackup = mIndex;
|
||||
@@ -156,9 +234,28 @@ public class GalleryActivity extends AppCompatActivity {
|
||||
mIndex = mBackup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBusy() {
|
||||
return mGalleryProvider == null ||
|
||||
mGalleryProvider.size() == GalleryProvider.SIZE_WAIT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String isError() {
|
||||
if (mGalleryProvider != null) {
|
||||
int size = mGalleryProvider.size();
|
||||
if (size == 0) {
|
||||
return "Empty Gallery"; // TODO hardcode
|
||||
} else if (size < 0 && size != GalleryProvider.SIZE_WAIT) {
|
||||
return "Weird"; // TODO hardcode
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return mIndex < MAX_SIZE - 1;
|
||||
return mGalleryProvider != null && mIndex < mGalleryProvider.size() - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -166,14 +263,9 @@ public class GalleryActivity extends AppCompatActivity {
|
||||
return mIndex > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return mIndex >= 0 && mIndex < MAX_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void next() {
|
||||
if (mIndex >= MAX_SIZE - 1) {
|
||||
if (mGalleryProvider != null && mIndex >= mGalleryProvider.size() - 1) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
mIndex++;
|
||||
@@ -188,14 +280,51 @@ public class GalleryActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(GalleryPageView view) {
|
||||
view.showImage();
|
||||
view.getImageView().setTexture(mImageTexture);
|
||||
public int onBind(GalleryPageView view) {
|
||||
if (mGalleryProvider != null && mGalleryView != null) {
|
||||
switch (mGalleryProvider.request(mIndex)) {
|
||||
case GalleryProvider.RESULT_WAIT:
|
||||
view.showInfo();
|
||||
view.setImage(null);
|
||||
view.setPage(mIndex + 1);
|
||||
view.setProgress(GalleryPageView.PROGRESS_INDETERMINATE);
|
||||
view.setError(null, null);
|
||||
break;
|
||||
case GalleryProvider.RESULT_FAILED:
|
||||
view.showInfo();
|
||||
view.setImage(null);
|
||||
view.setPage(mIndex + 1);
|
||||
view.setProgress(GalleryPageView.PROGRESS_GONE);
|
||||
view.setError("Failed", mGalleryView);
|
||||
break;
|
||||
case GalleryProvider.RESULT_ERROR:
|
||||
view.showInfo();
|
||||
view.setImage(null);
|
||||
view.setPage(mIndex + 1);
|
||||
view.setProgress(GalleryPageView.PROGRESS_GONE);
|
||||
view.setError("Error", mGalleryView);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int id = mIdGenerator.nextId();
|
||||
if (mIndexIdMap != null) {
|
||||
mIndexIdMap.put(mIndex, id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unbind(GalleryPageView view) {
|
||||
view.getImageView().setTexture(null);
|
||||
public void onUnbind(GalleryPageView view) {
|
||||
if (mIndexIdMap != null) {
|
||||
int index = mIndexIdMap.indexOfKey(view.getId());
|
||||
if (index >= 0) {
|
||||
mIndexIdMap.removeAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
view.setImage(null);
|
||||
view.setError(null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user