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

@@ -18,8 +18,8 @@ import javax.microedition.khronos.opengles.GL10;
public class GLSurfaceView extends View implements SurfaceHolder.Callback { // TODO: have this extend SurfaceView?
EGLContextFactory context_factory = new default_ContextFactory();
EGLConfigChooser config_chooser = new boolean_ConfigChooser(true);
EGL10 java_egl_wrapper;
GL10 java_gl_wrapper;
EGL10 java_egl_wrapper = (EGL10)EGLContext.getEGL();
GL10 java_gl_wrapper = (GL10)EGLContext.getGL();
int opengl_version = 1;
// from SurfaceHolder.Callback
@@ -29,22 +29,14 @@ public class GLSurfaceView extends View implements SurfaceHolder.Callback { // T
public GLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
java_egl_wrapper = (EGL10)EGLContext.getEGL();
java_gl_wrapper = (GL10)EGLContext.getGL();
native_constructor(attrs);
}
public GLSurfaceView(Context context) {
super(context);
java_egl_wrapper = (EGL10)EGLContext.getEGL();
java_gl_wrapper = (GL10)EGLContext.getGL();
native_constructor(context);
}
private native void native_constructor(AttributeSet attrs);
private native void native_constructor(Context context);
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
// Resumes the rendering thread, re-creating the OpenGL context if necessary. It is the counterpart to onPause(). This method should typically be called in Activity.onStart. Must not be called before a renderer has been set.
// TODO: make use of this

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);

View File

@@ -10,27 +10,22 @@ public class FrameLayout extends ViewGroup {
public FrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
}
public FrameLayout(Context context) {
super(context);
native_constructor(context);
}
public FrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
native_constructor(attrs);
}
public native void native_constructor(AttributeSet attrs);
public native void native_constructor(Context context);
@Override
public native void addView(View child, int index, ViewGroup.LayoutParams params);
protected native long native_constructor(Context context, AttributeSet attrs);
@Override
protected native void native_addView(long widget, long child, int index, ViewGroup.LayoutParams params);
@Override
protected native void native_removeView(long widget, long child);
public void addView(View child, int index) {
addView(child, index, null);

View File

@@ -15,7 +15,6 @@ public class ImageView extends View {
public ImageView(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
if (attrs != null) {
int resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
if (resource != 0)
@@ -25,18 +24,14 @@ public class ImageView extends View {
public ImageView(Context context) {
super(context);
native_constructor(context);
}
public ImageView(Context context, AttributeSet attrs, int xxx) {
super(context);
native_constructor(context);
public ImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private native void native_constructor(AttributeSet attrs);
private native void native_constructor(Context context);
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
private native void native_setPixbuf(long pixbuf);
public /*native*/ void setImageResource(final int resid) {

View File

@@ -11,25 +11,18 @@ public class LinearLayout extends ViewGroup {
public LinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
}
public LinearLayout(Context context) {
super(context);
native_constructor(context);
}
private native void native_constructor(AttributeSet attrs);
private native void native_constructor(Context context);
@Override
public native void addView(View child, int index, ViewGroup.LayoutParams params);
protected native long native_constructor(Context context, AttributeSet attrs);
@Override
public native void removeView(View view);
protected native void native_addView(long widget, long child, int index, ViewGroup.LayoutParams params);
@Override
public native void removeAllViews();
protected native void native_removeView(long widget, long child);
public native void setOrientation(int orientation);
public void setWeightSum(float weightSum) {}

View File

@@ -7,18 +7,11 @@ import android.view.View;
public class ProgressBar extends View {
public ProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
}
public ProgressBar(Context context) {
super(context);
// native_constructor(context);
}
private native void native_constructor(AttributeSet attrs);
private native void native_constructor(Context context);
public synchronized void setIndeterminate(boolean indeterminate) {}
}

View File

@@ -12,21 +12,18 @@ public class RelativeLayout extends ViewGroup {
public RelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
}
public RelativeLayout(Context context) {
super(context);
native_constructor(context);
}
private native void native_constructor(AttributeSet attrs);
private native void native_constructor(Context context);
@Override
public native void addView(View child, int index, ViewGroup.LayoutParams params);
protected native long native_constructor(Context context, AttributeSet attrs);
@Override
protected native void native_addView(long widget, long child, int index, ViewGroup.LayoutParams params);
@Override
protected native void native_removeView(long widget, long child);
public static class LayoutParams extends ViewGroup.LayoutParams {
public LayoutParams(int width, int height) {

View File

@@ -9,25 +9,18 @@ import android.view.ViewGroup.LayoutParams;
public class ScrollView extends ViewGroup {
public ScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
}
public ScrollView(Context context) {
super(context);
native_constructor(context);
}
private native void native_constructor(AttributeSet attrs);
private native void native_constructor(Context context);
@Override
public native void addView(View child, int index, LayoutParams params);
protected native long native_constructor(Context context, AttributeSet attrs);
@Override
public native void removeView(View view);
protected native void native_addView(long widget, long child, int index, ViewGroup.LayoutParams params);
@Override
public native void removeAllViews();
protected native void native_removeView(long widget, long child);
protected void onScrollChanged(int x, int y, int oldx, int oldy) {}
public void setFillViewport(boolean fillViewport) {}

View File

@@ -21,22 +21,18 @@ public class TextView extends View {
public TextView(Context context, AttributeSet attrs) {
super(context, attrs);
native_constructor(attrs);
}
public TextView(Context context) {
super(context);
native_constructor(context);
}
public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs);
}
native void native_constructor(AttributeSet attrs);
native void native_constructor(Context context);
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
public final void setText(CharSequence text) {
if (text == null) {