start implementing Drawable using underlying GdkPaintable

This commit is contained in:
Julian Winkler
2023-12-29 16:55:11 +01:00
parent 1b03fa6e1a
commit 8c7dbf6ceb
11 changed files with 148 additions and 14 deletions

View File

@@ -19,6 +19,7 @@ package android.content.res;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.icu.text.PluralRules;
import android.graphics.drawable.ColorDrawable;
// import android.graphics.Movie;
import android.graphics.drawable.Drawable;
// import android.graphics.drawable.ColorDrawable;
@@ -48,7 +49,6 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
class Movie {}
class ColorDrawable {}
class ConstantState {}
@@ -2017,7 +2017,7 @@ public class Resources {
final String name = getResourceName(id);
if (name != null) android.util.Log.d("PreloadDrawable", name);
}
}
}*/
boolean isColorDrawable = false;
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
@@ -2027,6 +2027,8 @@ public class Resources {
final long key = isColorDrawable ? value.data :
(((long) value.assetCookie) << 32) | value.data;
Drawable dr = null;
/*
Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);
if (dr != null) {
@@ -2040,7 +2042,7 @@ public class Resources {
}
if (cs != null) {
dr = cs.newDrawable(this);
} else {
} else*/ {
if (isColorDrawable) {
dr = new ColorDrawable(value.data);
}
@@ -2089,8 +2091,7 @@ public class Resources {
InputStream is = mAssets.openNonAsset(
value.assetCookie, file, AssetManager.ACCESS_STREAMING);
// System.out.println("Opened file " + file + ": " + is);
dr = null/*Drawable.createFromResourceStream(this, value, is,
file, null)* /;
dr = Drawable.createFromResourceStream(this, value, is, file, null);
is.close();
// System.out.println("Created stream: " + dr);
} catch (Exception e) {
@@ -2108,7 +2109,7 @@ public class Resources {
if (dr != null) {
dr.setChangingConfigurations(value.changingConfigurations);
cs = dr.getConstantState();
/* cs = dr.getConstantState();
if (cs != null) {
if (mPreloading) {
final int changingConfigs = cs.getChangingConfigurations();
@@ -2145,12 +2146,10 @@ public class Resources {
}
}
}
}
}*/
}
return dr;
*/
return null;
}
private Drawable getCachedDrawable(

View File

@@ -1,17 +1,35 @@
package android.graphics.drawable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.TypedValue;
public abstract class Drawable {
public class Drawable {
public static interface Callback {}
private Rect mBounds = new Rect();
private int[] mStateSet = new int[0];
public long paintable;
public Drawable() {}
public Drawable(long paintable) {
this.paintable = paintable;
}
public int getChangingConfigurations() {
return 0;
@@ -38,7 +56,7 @@ public abstract class Drawable {
return mBounds;
}
public abstract void draw(Canvas canvas);
public void draw(Canvas canvas) {}
public boolean setState(int[] stateSet) {
this.mStateSet = stateSet;
@@ -89,4 +107,36 @@ public abstract class Drawable {
public void setTintMode(PorterDuff.Mode tintMode) {}
public boolean isProjected () {return false;}
public static Drawable createFromXml(Resources resources, XmlResourceParser parser) throws XmlPullParserException, IOException {
int type;
while ((type=parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
if (type != XmlPullParser.START_TAG)
throw new XmlPullParserException("No start tag found");
if ("selector".equals(parser.getName())) {
StateListDrawable drawable = new StateListDrawable();
drawable.inflate(resources, parser, parser, null);
return drawable;
}
return null;
}
public static Drawable createFromResourceStream(Resources resources, TypedValue value, InputStream is, String file,
Object object) {
Path path = Paths.get(android.os.Environment.getExternalStorageDirectory().getPath(), file);
if (!Files.exists(path)) {
try (InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(file)) {
if (inputStream != null) {
Files.createDirectories(path.getParent());
Files.copy(inputStream, path);
}
} catch (IOException e) {
e.printStackTrace();
}
}
long paintable = native_paintable_from_path(path.toString());
return new Drawable(paintable);
}
protected static native long native_paintable_from_path(String path);
}

View File

@@ -1,6 +1,15 @@
package android.graphics.drawable;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Slog;
public class StateListDrawable extends Drawable {
@@ -11,5 +20,16 @@ public class StateListDrawable extends Drawable {
}
public void addState(int[] stateSet, Drawable drawable) {}
public void inflate(Resources resources, XmlPullParser parser, AttributeSet attrs, Theme theme) throws XmlPullParserException, IOException {
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if ("item".equals(parser.getName())) {
int resid = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "drawable", -1);
Drawable drawable = resources.getDrawable(resid, theme);
this.paintable = drawable.paintable;
Slog.i("StateListDrawable", "item resid = " + resid + ", paintable = " + drawable.paintable);
}
}
}
}

View File

@@ -930,6 +930,7 @@ public class View extends Object {
protected native void native_measure(long widget, int widthMeasureSpec, int heightMeasureSpec, boolean isComplex);
protected native void native_layout(long widget, int l, int t, int r, int b);
protected native void native_requestLayout(long widget);
protected native void native_setBackgroundDrawable(long widget, long paintable);
// --- stubs
@@ -1005,8 +1006,8 @@ public class View extends Object {
this.visibility = visibility;
}
public void setPadding(int left, int top, int right, int bottom) {}
public void setBackgroundResource(int resid) {
// Slog.w(TAG, "*** setBackgroundResource: " + getString(resid));
public void setBackgroundResource(int resid) throws Exception {
setBackgroundDrawable(getResources().getDrawable(resid));
}
public void getHitRect(Rect outRect) {}
@@ -1243,7 +1244,9 @@ public class View extends Object {
layout(left + offset, top, right + offset, bottom);
}
public void setBackgroundDrawable(Drawable backgroundDrawable) {}
public void setBackgroundDrawable(Drawable backgroundDrawable) {
native_setBackgroundDrawable(widget, backgroundDrawable != null ? backgroundDrawable.paintable : 0);
}
public int getOverScrollMode() {return 0;}