refactor: hide backend Adapter handles
- static methods for backend adapters
This commit is contained in:
@@ -27,20 +27,21 @@ import java.io.InputStreamReader;
|
||||
public abstract class AssetAdapter {
|
||||
|
||||
/** The instance provided by backend */
|
||||
public static AssetAdapter g;
|
||||
static AssetAdapter g;
|
||||
|
||||
/**
|
||||
* Open file from asset path as stream.
|
||||
*
|
||||
* @param name the name
|
||||
* @return the input stream
|
||||
*/
|
||||
public abstract InputStream openFileAsStream(String name);
|
||||
protected abstract InputStream openFileAsStream(String file);
|
||||
|
||||
public String openTextFile(String name) {
|
||||
public static InputStream readFileAsStream(String file) {
|
||||
return g.openFileAsStream(file);
|
||||
}
|
||||
|
||||
public static String readTextFile(String file) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
InputStream is = g.openFileAsStream(name);
|
||||
InputStream is = g.openFileAsStream(file);
|
||||
if (is == null)
|
||||
return null;
|
||||
|
||||
@@ -55,5 +56,10 @@ public abstract class AssetAdapter {
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
public static void init(AssetAdapter adapter) {
|
||||
g = adapter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.oscim.backend.canvas.Paint;
|
||||
public abstract class CanvasAdapter {
|
||||
|
||||
/** The instance provided by backend */
|
||||
public static CanvasAdapter g;
|
||||
static CanvasAdapter g;
|
||||
|
||||
/** The dpi. */
|
||||
public static float dpi = 240;
|
||||
@@ -42,14 +42,22 @@ public abstract class CanvasAdapter {
|
||||
*
|
||||
* @return the canvas
|
||||
*/
|
||||
public abstract Canvas getCanvas();
|
||||
protected abstract Canvas newCanvasImpl();
|
||||
|
||||
public static Canvas newCanvas() {
|
||||
return g.newCanvasImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Paint.
|
||||
*
|
||||
* @return the paint
|
||||
*/
|
||||
public abstract Paint getPaint();
|
||||
protected abstract Paint newPaintImpl();
|
||||
|
||||
public static Paint newPaint() {
|
||||
return g.newPaintImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Bitmap} with given dimensions.
|
||||
@@ -59,7 +67,11 @@ public abstract class CanvasAdapter {
|
||||
* @param format the format
|
||||
* @return the bitmap
|
||||
*/
|
||||
public abstract Bitmap getBitmap(int width, int height, int format);
|
||||
protected abstract Bitmap newBitmapImpl(int width, int height, int format);
|
||||
|
||||
public static Bitmap newBitmap(int width, int height, int format) {
|
||||
return g.newBitmapImpl(width, height, format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Bitmap} from InputStream.
|
||||
@@ -67,7 +79,11 @@ public abstract class CanvasAdapter {
|
||||
* @param inputStream the input stream
|
||||
* @return the bitmap
|
||||
*/
|
||||
public abstract Bitmap decodeBitmap(InputStream inputStream);
|
||||
protected abstract Bitmap decodeBitmapImpl(InputStream inputStream);
|
||||
|
||||
public static Bitmap decodeBitmap(InputStream inputStream) {
|
||||
return g.decodeBitmapImpl(inputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Bitmap} from bundled assets.
|
||||
@@ -75,7 +91,11 @@ public abstract class CanvasAdapter {
|
||||
* @param fileName the file name
|
||||
* @return the bitmap
|
||||
*/
|
||||
public abstract Bitmap loadBitmapAsset(String fileName);
|
||||
protected abstract Bitmap loadBitmapAssetImpl(String fileName);
|
||||
|
||||
public static Bitmap getBitmapAsset(String fileName) {
|
||||
return g.loadBitmapAssetImpl(fileName);
|
||||
}
|
||||
|
||||
protected static Bitmap createBitmap(String src) throws IOException {
|
||||
if (src == null || src.length() == 0) {
|
||||
@@ -89,8 +109,12 @@ public abstract class CanvasAdapter {
|
||||
return null;
|
||||
}
|
||||
|
||||
Bitmap bitmap = CanvasAdapter.g.decodeBitmap(inputStream);
|
||||
Bitmap bitmap = decodeBitmap(inputStream);
|
||||
inputStream.close();
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
protected static void init(CanvasAdapter adapter) {
|
||||
g = adapter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public abstract class GLShader {
|
||||
|
||||
public static int loadShader(String file) {
|
||||
String path = "shaders/" + file + ".glsl";
|
||||
String vs = AssetAdapter.g.openTextFile(path);
|
||||
String vs = AssetAdapter.readTextFile(path);
|
||||
|
||||
if (vs == null)
|
||||
throw new IllegalArgumentException("shader file not found: " + path);
|
||||
|
||||
@@ -42,7 +42,7 @@ public class TextLayer extends TextureLayer {
|
||||
|
||||
public TextLayer() {
|
||||
super(RenderElement.SYMBOL);
|
||||
mCanvas = CanvasAdapter.g.getCanvas();
|
||||
mCanvas = CanvasAdapter.newCanvas();
|
||||
fixed = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -186,7 +186,7 @@ public class TextureItem extends Inlist<TextureItem> {
|
||||
synchronized (mBitmaps) {
|
||||
int size = mBitmaps.size();
|
||||
if (size == 0)
|
||||
t.bitmap = CanvasAdapter.g.getBitmap(mWidth, mHeight, 0);
|
||||
t.bitmap = CanvasAdapter.newBitmap(mWidth, mHeight, 0);
|
||||
else {
|
||||
t.bitmap = mBitmaps.remove(size - 1);
|
||||
t.bitmap.eraseColor(Color.TRANSPARENT);
|
||||
|
||||
@@ -531,7 +531,7 @@ public class XmlThemeBuilder extends DefaultHandler {
|
||||
|
||||
if (src != null) {
|
||||
try {
|
||||
Bitmap bitmap = CanvasAdapter.g.loadBitmapAsset(src);
|
||||
Bitmap bitmap = CanvasAdapter.getBitmapAsset(src);
|
||||
if (bitmap != null)
|
||||
b.texture = new TextureItem(bitmap, true);
|
||||
} catch (Exception e) {
|
||||
@@ -566,7 +566,7 @@ public class XmlThemeBuilder extends DefaultHandler {
|
||||
}
|
||||
validateExists("img", img, elementName);
|
||||
|
||||
Bitmap bitmap = CanvasAdapter.g.loadBitmapAsset(IMG_PATH + img);
|
||||
Bitmap bitmap = CanvasAdapter.getBitmapAsset(IMG_PATH + img);
|
||||
mTextureAtlas = new TextureAtlas(bitmap);
|
||||
}
|
||||
|
||||
|
||||
@@ -165,7 +165,7 @@ public final class TextStyle extends RenderStyle {
|
||||
this.priority = tb.priority;
|
||||
this.texture = tb.texture;
|
||||
|
||||
paint = CanvasAdapter.g.getPaint();
|
||||
paint = CanvasAdapter.newPaint();
|
||||
paint.setTextAlign(Align.CENTER);
|
||||
paint.setTypeface(tb.fontFamily, tb.fontStyle);
|
||||
|
||||
@@ -173,7 +173,7 @@ public final class TextStyle extends RenderStyle {
|
||||
paint.setTextSize(tb.fontSize);
|
||||
|
||||
if (tb.strokeWidth > 0) {
|
||||
stroke = CanvasAdapter.g.getPaint();
|
||||
stroke = CanvasAdapter.newPaint();
|
||||
stroke.setStyle(Paint.Style.STROKE);
|
||||
stroke.setTextAlign(Align.CENTER);
|
||||
stroke.setTypeface(tb.fontFamily, tb.fontStyle);
|
||||
|
||||
@@ -72,7 +72,7 @@ public class BitmapTileSource extends UrlTileSource {
|
||||
public boolean decode(Tile tile, ITileDataSink sink, InputStream is)
|
||||
throws IOException {
|
||||
|
||||
Bitmap bitmap = CanvasAdapter.g.decodeBitmap(is);
|
||||
Bitmap bitmap = CanvasAdapter.decodeBitmap(is);
|
||||
if (!bitmap.isValid()) {
|
||||
log.debug("{} invalid bitmap", tile);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user