1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

code simplification

This commit is contained in:
Nikolay Pultsin 2010-11-06 06:02:16 +00:00
parent c74164d1da
commit 4b981deadf
4 changed files with 36 additions and 31 deletions

View file

@ -24,6 +24,9 @@ import org.geometerplus.zlibrary.core.image.ZLFileImage;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidLibrary;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.network.NetworkLibraryItem;
import org.geometerplus.fbreader.network.NetworkTree;
@ -51,7 +54,7 @@ public class AddCustomCatalogItemTree extends NetworkTree {
@Override
protected ZLImage createCover() {
ZLResourceFile file = ZLResourceFile.createResourceFile("R.drawable.ic_list_plus");
ZLResourceFile file = ((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).createDrawableFile(R.drawable.ic_list_plus);
return new ZLFileImage("image/png", file);
}
}

View file

@ -43,6 +43,8 @@ import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageManager;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidLibrary;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.network.NetworkTree;
import org.geometerplus.fbreader.network.NetworkImage;
@ -111,7 +113,10 @@ abstract class NetworkBaseActivity extends ListActivity
// this set is used to track whether this activity will be notified, when specific cover will be synchronized.
private HashSet<String> myAwaitedCovers = new HashSet<String>();
private ZLFileImage myFBReaderIcon = new ZLFileImage("image/auto", ZLResourceFile.createResourceFile("R.drawable.fbreader"));
private ZLFileImage myFBReaderIcon = new ZLFileImage(
"image/auto",
((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).createDrawableFile(R.drawable.fbreader)
);
private void setupCover(final ImageView coverView, NetworkTree tree, int width, int height) {
Bitmap coverBitmap = null;

View file

@ -32,6 +32,8 @@ import org.geometerplus.fbreader.tree.FBTree;
import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.tree.NetworkAuthorTree;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidLibrary;
import org.geometerplus.zlibrary.ui.android.R;
public class SearchItemTree extends NetworkTree {
@ -53,7 +55,7 @@ public class SearchItemTree extends NetworkTree {
@Override
protected ZLImage createCover() {
ZLResourceFile file = ZLResourceFile.createResourceFile("R.drawable.ic_list_searchresult");
ZLResourceFile file = ((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).createDrawableFile(R.drawable.ic_list_searchresult);
return new ZLFileImage("image/png", file);
}

View file

@ -34,7 +34,6 @@ import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.view.ZLAndroidPaintContext;
import org.geometerplus.zlibrary.ui.android.view.ZLAndroidWidget;
import org.geometerplus.zlibrary.ui.android.dialogs.ZLAndroidDialogManager;
@ -98,12 +97,11 @@ public final class ZLAndroidLibrary extends ZLibrary {
@Override
public ZLResourceFile createResourceFile(String path) {
final String drawablePrefix = "R.drawable.";
if (path.startsWith(drawablePrefix)) {
return new AndroidResourceFile(path.substring(drawablePrefix.length()));
} else {
return new AndroidAssetsFile(path);
}
return new AndroidAssetsFile(path);
}
public ZLResourceFile createDrawableFile(int drawableId) {
return new AndroidDrawableFile(drawableId);
}
@Override
@ -132,30 +130,24 @@ public final class ZLAndroidLibrary extends ZLibrary {
return (myActivity != null) ? myActivity.getScreenBrightness() : 0;
}
private final class AndroidResourceFile extends ZLResourceFile {
private boolean myExists;
private int myResourceId;
private final class AndroidDrawableFile extends ZLResourceFile {
private int myId;
AndroidResourceFile(String fieldName) {
super(fieldName);
try {
myResourceId = R.drawable.class.getField(fieldName).getInt(null);
myExists = true;
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
AndroidDrawableFile(int drawableId) {
super("drawable/" + drawableId);
myId = drawableId;
}
@Override
public boolean exists() {
return myExists;
return true;
}
@Override
public long size() {
try {
AssetFileDescriptor descriptor =
myApplication.getResources().openRawResourceFd(myResourceId);
myApplication.getResources().openRawResourceFd(myId);
long length = descriptor.getLength();
descriptor.close();
return length;
@ -168,11 +160,8 @@ public final class ZLAndroidLibrary extends ZLibrary {
@Override
public InputStream getInputStream() throws IOException {
if (!myExists) {
throw new IOException("File not found: " + getPath());
}
try {
return myApplication.getResources().openRawResource(myResourceId);
return myApplication.getResources().openRawResource(myId);
} catch (Resources.NotFoundException e) {
throw new IOException(e.getMessage());
}
@ -182,13 +171,15 @@ public final class ZLAndroidLibrary extends ZLibrary {
private final class AndroidAssetsFile extends ZLResourceFile {
AndroidAssetsFile(String path) {
super(path);
System.err.println("file " + path + " : " + exists());
}
@Override
public boolean exists() {
try {
AssetFileDescriptor descriptor = myActivity.getAssets().openFd(getPath());
AssetFileDescriptor descriptor = myApplication.getAssets().openFd(getPath());
if (descriptor == null) {
return false;
}
descriptor.close();
return true;
} catch (IOException e) {
@ -199,7 +190,10 @@ public final class ZLAndroidLibrary extends ZLibrary {
@Override
public long size() {
try {
AssetFileDescriptor descriptor = myActivity.getAssets().openFd(getPath());
AssetFileDescriptor descriptor = myApplication.getAssets().openFd(getPath());
if (descriptor == null) {
return 0;
}
long length = descriptor.getLength();
descriptor.close();
return length;
@ -210,7 +204,8 @@ public final class ZLAndroidLibrary extends ZLibrary {
@Override
public InputStream getInputStream() throws IOException {
return myActivity.getAssets().open(getPath());
System.err.println("open: " + getPath());
return myApplication.getAssets().open(getPath());
}
}
}