Simplify native interface of widget implementations

Makes it easier to overwrite behavior in subclasses. Have a fallback
implementation for ViewGroup.
Save some _GET_LONG_FIELD / _SET_LONG_FIELD calls by directly passing
the native pointers to and from native methods.
This commit is contained in:
Julian Winkler
2023-08-17 12:59:37 +02:00
parent 82744e9e5e
commit 4d90002ec6
33 changed files with 403 additions and 553 deletions

View File

@@ -3,6 +3,8 @@ package android.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import java.util.ArrayList;
public class SurfaceView extends View {
@@ -12,8 +14,6 @@ public class SurfaceView extends View {
public SurfaceView(Context context) {
super(context);
native_constructor(context);
mSurface.widget = this.widget;
}
@@ -23,7 +23,8 @@ public class SurfaceView extends View {
}
}
private native void native_constructor(Context context);
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
public SurfaceHolder getHolder() {
return mSurfaceHolder;

View File

@@ -778,7 +778,7 @@ public class View extends Object {
public View() {} // FIXME
public View(Context context, AttributeSet attrs) {
this(context);
this(context, attrs, 0);
if (attrs != null) {
id = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "id", 0);
@@ -788,7 +788,9 @@ public class View extends Object {
}
public View(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
this.context = context;
widget = native_constructor(context, attrs);
}
public View findViewById(int id) {
@@ -799,21 +801,7 @@ public class View extends Object {
public void onDraw(Canvas canvas) {}
public View(Context context) {
boolean custom_drawing;
this.context = context;
try {
Class[] cArg = new Class[1];
cArg[0] = Canvas.class;
custom_drawing = !(this.getClass().getMethod("onDraw", cArg).getDeclaringClass() == View.class);
} catch (NoSuchMethodException e) {
custom_drawing = false;
}
if (custom_drawing) {
native_constructor(context);
}
this(context, null);
}
public final ViewParent getParent() {
@@ -853,7 +841,7 @@ public class View extends Object {
public native final int getWidth();
public native final int getHeight();
private native void native_constructor(Context context); // will create a custom GtkWidget with a custom drawing function
protected native long native_constructor(Context context, AttributeSet attrs); // will create a custom GtkWidget with a custom drawing function
private native void native_set_size_request(int width, int height);
// --- stubs

View File

@@ -6,6 +6,7 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.Iterator;
public class ViewGroup extends View implements ViewParent, ViewManager {
public int id;
@@ -55,10 +56,34 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
addView(child, new LayoutParams(width, height));
}
public native void addView(View child, int index, LayoutParams params);
public void addView(View child, int index, LayoutParams params) {
if (params != null) {
child.setLayoutParams(params);
}
child.parent = this;
children.add(child);
native_addView(widget, child.widget, index, params);
}
public native void removeView(View view);
public native void removeAllViews();
public void removeView(View child) {
child.parent = null;
children.remove(child);
native_removeView(widget, child.widget);
}
public void removeAllViews() {
for (Iterator<View> it = children.iterator(); it.hasNext();) {
View child = it.next();
child.parent = null;
it.remove();
native_removeView(widget, child.widget);
}
}
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native void native_addView(long widget, long child, int index, LayoutParams params);
protected native void native_removeView(long widget, long child);
public View getChildAt(int index) {
return children.get(index);