2022-10-02 23:06:56 +02:00
|
|
|
package android.view;
|
|
|
|
|
2023-08-17 10:46:24 +02:00
|
|
|
import android.R;
|
|
|
|
import android.animation.LayoutTransition;
|
2022-10-02 23:06:56 +02:00
|
|
|
import android.content.Context;
|
2023-08-17 10:46:24 +02:00
|
|
|
import android.content.res.TypedArray;
|
2023-06-22 11:45:46 +02:00
|
|
|
import android.util.AttributeSet;
|
2022-10-02 23:06:56 +02:00
|
|
|
import java.util.ArrayList;
|
2023-08-17 12:59:37 +02:00
|
|
|
import java.util.Iterator;
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
public class ViewGroup extends View implements ViewParent, ViewManager {
|
|
|
|
public int id;
|
|
|
|
public ArrayList<View> children;
|
|
|
|
|
|
|
|
public ViewGroup() {
|
|
|
|
children = new ArrayList<View>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ViewGroup(Context context) {
|
|
|
|
super(context);
|
|
|
|
|
|
|
|
children = new ArrayList<View>();
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:04 +02:00
|
|
|
public ViewGroup(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
children = new ArrayList<View>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
children = new ArrayList<View>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ViewGroup(int _id) { // FIXME
|
|
|
|
children = new ArrayList<View>();
|
|
|
|
|
|
|
|
id = _id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addView(View child) {
|
2023-09-01 13:16:01 +02:00
|
|
|
addView(child, -1);
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addView(View child, int index) {
|
2023-09-01 13:16:01 +02:00
|
|
|
LayoutParams params = child.getLayoutParams();
|
|
|
|
if (params == null) {
|
|
|
|
params = generateDefaultLayoutParams();
|
|
|
|
}
|
|
|
|
addView(child, index, params);
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addView(View child, LayoutParams params) {
|
|
|
|
addView(child, -1, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addView(View child, int width, int height) {
|
2023-09-01 13:16:01 +02:00
|
|
|
final LayoutParams params = generateDefaultLayoutParams();
|
|
|
|
params.width = width;
|
|
|
|
params.height = height;
|
|
|
|
addView(child, params);
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 09:16:45 +02:00
|
|
|
private void addViewInternal(View child, int index, LayoutParams params) {
|
2023-08-22 14:41:01 +02:00
|
|
|
if (child.parent == this)
|
|
|
|
return;
|
2023-09-01 13:16:01 +02:00
|
|
|
if (!checkLayoutParams(params)) {
|
|
|
|
params = generateLayoutParams(params);
|
2023-08-17 12:59:37 +02:00
|
|
|
}
|
2023-09-01 13:16:01 +02:00
|
|
|
child.setLayoutParams(params);
|
2023-08-17 12:59:37 +02:00
|
|
|
child.parent = this;
|
2023-08-23 09:30:38 +02:00
|
|
|
if (index < 0)
|
|
|
|
index = children.size();
|
|
|
|
children.add(index, child);
|
2023-08-17 12:59:37 +02:00
|
|
|
native_addView(widget, child.widget, index, params);
|
2023-09-01 13:16:01 +02:00
|
|
|
requestLayout();
|
2023-08-17 12:59:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 09:16:45 +02:00
|
|
|
public void addView(View child, int index, LayoutParams params) {
|
|
|
|
addViewInternal(child, index, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean addViewInLayout(View child, int index, LayoutParams params) {
|
|
|
|
addViewInternal(child, index, params);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:59:37 +02:00
|
|
|
public void removeView(View child) {
|
2023-08-22 14:41:01 +02:00
|
|
|
if (child.parent != this)
|
|
|
|
return;
|
2023-08-17 12:59:37 +02:00
|
|
|
child.parent = null;
|
|
|
|
children.remove(child);
|
|
|
|
native_removeView(widget, child.widget);
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:41:01 +02:00
|
|
|
public void removeViewAt(int index) {
|
|
|
|
removeView(children.get(index));
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:59:37 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2023-08-22 14:41:01 +02:00
|
|
|
public void detachViewFromParent(int index) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void attachViewToParent(View view, int index, LayoutParams params) {
|
2023-09-01 12:55:04 +02:00
|
|
|
addViewInternal(view, index, params);
|
2023-08-22 14:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void removeDetachedView(View child, boolean animate) {
|
|
|
|
removeView(child);
|
|
|
|
}
|
|
|
|
|
2023-10-14 11:06:27 +02:00
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
native_measure(widget, widthMeasureSpec, heightMeasureSpec, true);
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:59:37 +02:00
|
|
|
@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);
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
public View getChildAt(int index) {
|
2023-09-01 12:39:20 +02:00
|
|
|
try {
|
|
|
|
return children.get(index);
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-22 14:41:01 +02:00
|
|
|
public int indexOfChild(View child) {
|
|
|
|
return children.indexOf(child);
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:45:46 +02:00
|
|
|
public void updateViewLayout(View view, ViewGroup.LayoutParams params) {}
|
2022-10-02 23:06:56 +02:00
|
|
|
|
|
|
|
public LayoutParams generateLayoutParams(AttributeSet attrs) {
|
2023-07-14 20:09:10 +02:00
|
|
|
return new LayoutParams(getContext(), attrs);
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
2023-09-01 12:55:04 +02:00
|
|
|
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
|
|
|
|
return p;
|
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2022-11-24 23:10:27 +01:00
|
|
|
public void bringChildToFront(View child) {
|
|
|
|
// TODO: actually implement this (might make sense to implement it in the subclasses instead), when applicable
|
|
|
|
}
|
|
|
|
|
2023-06-18 11:03:43 +02:00
|
|
|
/**
|
2023-06-22 11:45:46 +02:00
|
|
|
* Returns the number of children in the group.
|
|
|
|
*
|
|
|
|
* @return a positive integer representing the number of children in
|
|
|
|
* the group
|
|
|
|
*/
|
|
|
|
public int getChildCount() {
|
|
|
|
return children.size();
|
|
|
|
}
|
2023-06-18 11:03:43 +02:00
|
|
|
|
2023-07-14 20:09:10 +02:00
|
|
|
public void setMotionEventSplittingEnabled(boolean enabled) {}
|
|
|
|
|
|
|
|
public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) {}
|
|
|
|
|
2023-09-01 12:55:04 +02:00
|
|
|
protected boolean checkLayoutParams(LayoutParams params) {
|
2023-07-14 20:09:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-17 10:46:24 +02:00
|
|
|
public LayoutTransition getLayoutTransition() {return null;}
|
|
|
|
|
|
|
|
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
|
|
|
|
int specMode = MeasureSpec.getMode(spec);
|
|
|
|
int specSize = MeasureSpec.getSize(spec);
|
|
|
|
int size = Math.max(0, specSize - padding);
|
|
|
|
int resultSize = 0;
|
|
|
|
int resultMode = 0;
|
|
|
|
switch (specMode) {
|
|
|
|
// Parent has imposed an exact size on us
|
|
|
|
case MeasureSpec.EXACTLY:
|
|
|
|
if (childDimension >= 0) {
|
|
|
|
resultSize = childDimension;
|
|
|
|
resultMode = MeasureSpec.EXACTLY;
|
|
|
|
} else if (childDimension == LayoutParams.MATCH_PARENT) {
|
|
|
|
// Child wants to be our size. So be it.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = MeasureSpec.EXACTLY;
|
|
|
|
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
|
|
|
|
// Child wants to determine its own size. It can't be
|
|
|
|
// bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = MeasureSpec.AT_MOST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Parent has imposed a maximum size on us
|
|
|
|
case MeasureSpec.AT_MOST:
|
|
|
|
if (childDimension >= 0) {
|
|
|
|
// Child wants a specific size... so be it
|
|
|
|
resultSize = childDimension;
|
|
|
|
resultMode = MeasureSpec.EXACTLY;
|
|
|
|
} else if (childDimension == LayoutParams.MATCH_PARENT) {
|
|
|
|
// Child wants to be our size, but our size is not fixed.
|
|
|
|
// Constrain child to not be bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = MeasureSpec.AT_MOST;
|
|
|
|
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
|
|
|
|
// Child wants to determine its own size. It can't be
|
|
|
|
// bigger than us.
|
|
|
|
resultSize = size;
|
|
|
|
resultMode = MeasureSpec.AT_MOST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Parent asked to see how big we want to be
|
|
|
|
case MeasureSpec.UNSPECIFIED:
|
|
|
|
if (childDimension >= 0) {
|
|
|
|
// Child wants a specific size... let them have it
|
|
|
|
resultSize = childDimension;
|
|
|
|
resultMode = MeasureSpec.EXACTLY;
|
|
|
|
} else if (childDimension == LayoutParams.MATCH_PARENT) {
|
|
|
|
// Child wants to be our size... find out how big it should
|
|
|
|
// be
|
|
|
|
resultSize = 0; //View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
|
|
|
|
resultMode = MeasureSpec.UNSPECIFIED;
|
|
|
|
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
|
|
|
|
// Child wants to determine its own size.... find out how
|
|
|
|
// big it should be
|
|
|
|
resultSize = 0; //View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
|
|
|
|
resultMode = MeasureSpec.UNSPECIFIED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//noinspection ResourceType
|
|
|
|
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:18:33 +02:00
|
|
|
protected void measureChildWithMargins(View child,
|
|
|
|
int parentWidthMeasureSpec, int widthUsed,
|
|
|
|
int parentHeightMeasureSpec, int heightUsed) {
|
|
|
|
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
|
|
|
|
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
|
|
|
|
/*mPaddingLeft + mPaddingRight +*/ lp.leftMargin + lp.rightMargin
|
|
|
|
+ widthUsed, lp.width);
|
|
|
|
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
|
|
|
|
/*mPaddingTop + mPaddingBottom +*/ lp.topMargin + lp.bottomMargin
|
|
|
|
+ heightUsed, lp.height);
|
|
|
|
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
|
|
|
|
}
|
|
|
|
|
2023-09-01 12:55:04 +02:00
|
|
|
protected void measureChild(View child, int parentWidthMeasureSpec,
|
|
|
|
int parentHeightMeasureSpec) {
|
|
|
|
final LayoutParams lp = child.getLayoutParams();
|
|
|
|
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
|
|
|
|
/*mPaddingLeft + mPaddingRight*/0, lp.width);
|
|
|
|
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
|
|
|
|
/*mPaddingTop + mPaddingBottom*/0, lp.height);
|
|
|
|
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:41:01 +02:00
|
|
|
public void setAddStatesFromChildren(boolean addsStates) {}
|
|
|
|
|
|
|
|
public View getFocusedChild() {return null;}
|
|
|
|
|
|
|
|
public int getDescendantFocusability() {return 0;}
|
|
|
|
|
2023-09-01 12:55:04 +02:00
|
|
|
public void startViewTransition(View view) {}
|
|
|
|
public void endViewTransition(View view) {}
|
|
|
|
|
|
|
|
protected LayoutParams generateDefaultLayoutParams() {
|
|
|
|
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void focusableViewAvailable(View v) {}
|
2023-09-08 18:32:34 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGravity(int gravity) {
|
|
|
|
super.setGravity(gravity);
|
|
|
|
// update children as necessary
|
|
|
|
for (View child: children) {
|
|
|
|
LayoutParams params = child.getLayoutParams();
|
|
|
|
if (params.gravity == -1)
|
|
|
|
child.setLayoutParams(params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:18:47 +02:00
|
|
|
protected void setChildrenDrawingOrderEnabled(boolean enabled) {}
|
|
|
|
|
2022-10-02 23:06:56 +02:00
|
|
|
public static class LayoutParams {
|
|
|
|
public static final int FILL_PARENT = -1;
|
|
|
|
public static final int MATCH_PARENT = -1;
|
|
|
|
public static final int WRAP_CONTENT = -2;
|
|
|
|
|
|
|
|
public int width = 0;
|
|
|
|
public int height = 0;
|
2023-09-08 18:32:34 +02:00
|
|
|
public float weight = 0;
|
2022-10-02 23:06:56 +02:00
|
|
|
public int gravity = -1;
|
|
|
|
|
|
|
|
public LayoutParams() {
|
2023-06-22 11:45:46 +02:00
|
|
|
// FIXME
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public LayoutParams(int width, int height) {
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LayoutParams(int width, int height, float weight) {
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
this.weight = weight;
|
|
|
|
}
|
|
|
|
|
2023-07-14 20:09:10 +02:00
|
|
|
public LayoutParams(Context context, AttributeSet attrs) {
|
2023-08-17 10:46:24 +02:00
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
|
|
|
|
width = a.getLayoutDimension(R.styleable.ViewGroup_Layout_layout_width, "layout_width");
|
|
|
|
height = a.getLayoutDimension(R.styleable.ViewGroup_Layout_layout_height, "layout_height");
|
2023-07-14 20:09:10 +02:00
|
|
|
this.gravity = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "layout_gravity", -1);
|
2023-08-17 10:46:24 +02:00
|
|
|
a.recycle();
|
2023-07-14 20:09:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 23:06:56 +02:00
|
|
|
public void setMargins(int left, int top, int right, int bottom) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to animate layouts.
|
|
|
|
*/
|
|
|
|
// public LayoutAnimationController.AnimationParameters layoutAnimationParameters;
|
|
|
|
}
|
2023-06-18 11:03:43 +02:00
|
|
|
|
2023-06-22 11:45:46 +02:00
|
|
|
public static class MarginLayoutParams extends ViewGroup.LayoutParams {
|
2023-08-17 10:46:24 +02:00
|
|
|
public int leftMargin;
|
|
|
|
public int topMargin;
|
|
|
|
public int rightMargin;
|
|
|
|
public int bottomMargin;
|
2023-06-18 11:03:43 +02:00
|
|
|
|
|
|
|
public MarginLayoutParams() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:16:01 +02:00
|
|
|
public MarginLayoutParams(LayoutParams params) {
|
|
|
|
super();
|
|
|
|
width = params.width;
|
|
|
|
height = params.height;
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:45:46 +02:00
|
|
|
public MarginLayoutParams(int width, int height) {
|
2023-06-18 11:03:43 +02:00
|
|
|
super(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MarginLayoutParams(int width, int height, float weight) {
|
|
|
|
super(width, height, weight);
|
|
|
|
}
|
2023-07-14 20:09:10 +02:00
|
|
|
|
|
|
|
public MarginLayoutParams(Context context, AttributeSet attributeSet) {
|
|
|
|
super(context, attributeSet);
|
|
|
|
}
|
2023-09-12 23:18:47 +02:00
|
|
|
|
|
|
|
public int getMarginStart() {
|
|
|
|
return leftMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMarginEnd() {
|
|
|
|
return rightMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMarginStart(int marginStart) {}
|
|
|
|
public void setMarginEnd(int marginEnd) {}
|
2023-07-14 20:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnHierarchyChangeListener {
|
|
|
|
|
2023-06-18 11:03:43 +02:00
|
|
|
}
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|