Skip to content

Commit

Permalink
add json dump getSceneString() to ConstraintLayout (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
jafu888 committed Jan 10, 2022
1 parent a380cb3 commit 86ae0a7
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 3 deletions.
Expand Up @@ -3631,4 +3631,49 @@ private void markHierarchyDirty() {
public boolean shouldDelayChildPressedState() {
return false;
}

/**
* Returns a JSON5 string useful for debugging the constraints actually applied.
* In situations where a complex set of code dynamically constructs constraints
* it is useful to be able to query the layout for what are the constraints.
* @return json5 string representing the constraint in effect now.
*/
public String getSceneString() {
StringBuilder ret = new StringBuilder();

if (mLayoutWidget.stringId == null) {
int id = this.getId();
if (id != -1) {
String str = getContext().getResources().getResourceEntryName(id);
mLayoutWidget.stringId = str;
} else {
mLayoutWidget.stringId = "parent";
}
}
if (mLayoutWidget.getDebugName() == null) {
mLayoutWidget.setDebugName(mLayoutWidget.stringId);
Log.v(TAG, " setDebugName " + mLayoutWidget.getDebugName());
}

ArrayList<ConstraintWidget> children = mLayoutWidget.getChildren();
for (ConstraintWidget child : children) {
View v = (View) child.getCompanionWidget();
if (v != null) {
if (child.stringId == null) {
int id = v.getId();
if (id != -1) {
String str = getContext().getResources().getResourceEntryName(id);
child.stringId = str;
}
}
if (child.getDebugName() == null) {
child.setDebugName(child.stringId);
Log.v(TAG, " setDebugName " + child.getDebugName());
}

}
}
mLayoutWidget.getSceneString(ret);
return ret.toString();
}
}
Expand Up @@ -615,7 +615,17 @@ private void serializeAttribute(StringBuilder ret, String type, float value, flo
}
ret.append(type);
ret.append(" : ");
ret.append(def);
ret.append(value);
ret.append(",\n");
}

private void serializeAttribute(StringBuilder ret, String type, int value, int def){
if (value == def) {
return;
}
ret.append(type);
ret.append(" : ");
ret.append(value);
ret.append(",\n");
}

Expand Down Expand Up @@ -3553,4 +3563,87 @@ public void addChildrenToSolverByDependency(ConstraintWidgetContainer container,
// horizontal
}

public void getSceneString(StringBuilder ret ) {

ret.append(" "+stringId+":{\n");
ret.append(" actualWidth:" + mWidth);
ret.append("\n");
ret.append(" actualHeight:" + mHeight);
ret.append("\n");
ret.append(" actualLeft:" + mX);
ret.append("\n");
ret.append(" actualTop:" + mY);
ret.append("\n");
getSceneString(ret,"left", mLeft);
getSceneString(ret,"top", mTop );
getSceneString(ret,"right", mRight);
getSceneString(ret,"bottom", mBottom);
getSceneString(ret,"baseline", mBaseline);
getSceneString(ret,"centerX", mCenterX);
getSceneString(ret,"centerY", mCenterY);
getSceneString(ret," width",
mWidth,
mMinWidth,
mMaxDimension[HORIZONTAL],
mWidthOverride,
mMatchConstraintMinWidth,
mMatchConstraintDefaultWidth,
mMatchConstraintPercentWidth,
mWeight[DIMENSION_HORIZONTAL]
);

getSceneString(ret," height",
mHeight,
mMinHeight,
mMaxDimension[VERTICAL],
mHeightOverride,
mMatchConstraintMinHeight,
mMatchConstraintDefaultHeight,
mMatchConstraintPercentHeight,
mWeight[DIMENSION_VERTICAL]);
serializeDimensionRatio(ret," dimensionRatio",mDimensionRatio, mDimensionRatioSide);
serializeAttribute(ret," horizontalBias",mHorizontalBiasPercent,DEFAULT_BIAS );
serializeAttribute(ret," verticalBias",mVerticalBiasPercent,DEFAULT_BIAS );
serializeAttribute(ret," horizontalChainStyle",mHorizontalChainStyle,CHAIN_SPREAD );
serializeAttribute(ret," verticalChainStyle",mVerticalChainStyle,CHAIN_SPREAD );

ret.append(" }");

}

private void getSceneString(StringBuilder ret, String type, int size,
int min, int max, int override,
int matchConstraintMin, int matchConstraintDefault,
float MatchConstraintPercent,
float weight){
ret.append(type);
ret.append(" : {\n");
serializeAttribute(ret," size",size,0);
serializeAttribute(ret," min",min,0);
serializeAttribute(ret," max",max, Integer.MAX_VALUE);
serializeAttribute(ret," matchMin",matchConstraintMin, 0);
serializeAttribute(ret," matchDef",matchConstraintDefault, MATCH_CONSTRAINT_SPREAD);
serializeAttribute(ret," matchPercent",MatchConstraintPercent, 1);
ret.append(" },\n");
}
private void getSceneString(StringBuilder ret, String side, ConstraintAnchor a) {
if (a.mTarget == null) {
return;
}
ret.append(" ");
ret.append(side);
ret.append(" : [ '");
ret.append(a.mTarget);
ret.append("'");
if (a.mGoneMargin != Integer.MIN_VALUE || a.mMargin != 0) {
ret.append(",");
ret.append(a.mMargin);
if (a.mGoneMargin != Integer.MIN_VALUE) {
ret.append(",");
ret.append(a.mGoneMargin);
ret.append(",");
}
}
ret.append(" ] ,\n");
}
}
Expand Up @@ -1101,4 +1101,21 @@ private void addVerticalChain(ConstraintWidget widget) {
public void setPass(int pass) {
this.pass = pass;
}

public void getSceneString(StringBuilder ret ) {

ret.append(stringId+":{\n");
ret.append(" actualWidth:" + mWidth);
ret.append("\n");
ret.append(" actualHeight:" + mHeight);
ret.append("\n");

ArrayList<ConstraintWidget> children = getChildren();
for (ConstraintWidget child : children) {
child.getSceneString(ret);
ret.append(",\n");
}
ret.append("}");

}
}
Expand Up @@ -43,6 +43,7 @@
<activity android:name=".Bug010" />
<activity android:name=".FullScreenActivity" />
<activity android:name=".CheckSetProgress" />
<activity android:name=".CheckDumpJson" />
</application>

</manifest>
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.support.constraint.app;

import android.content.Context;
import android.os.Bundle;

import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.motion.widget.Debug;
import androidx.constraintlayout.widget.ConstraintLayout;

// used with verification_057.xml
public class CheckDumpJson extends AppCompatActivity {
private static final String TAG = "CheckDumpJson";
String layout_name;
ConstraintLayout mLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int id = R.layout.basic_cl_001;
Bundle extra = getIntent().getExtras();
if (extra != null) {
String prelayout = extra.getString(Utils.KEY);
setTitle(layout_name = prelayout);
Context ctx = getApplicationContext();
id = ctx.getResources().getIdentifier(prelayout, "layout", ctx.getPackageName());

}
setContentView(id);
mLayout = Utils.findConstraintLayout(this);


}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
mLayout.postDelayed(()-> dumpJson(), 2000);
}

private void dumpJson() {
Log.v(TAG, Debug.getLoc() +"\n"+mLayout.getSceneString() );
}
}
Expand Up @@ -97,6 +97,8 @@ public class VerificationActivity extends AppCompatActivity implements View.OnCl
activity_map.put("verification_503", FullScreenActivity.class);
activity_map.put("v_000", ParseLayouts.class);
activity_map.put("verification_800", CheckSetProgress.class);
activity_map.put("basic_cl_001", CheckDumpJson.class);
activity_map.put("basic_cl_002", CheckDumpJson.class);


// activity_map.put("verification_037", RotationToolbar.class);
Expand All @@ -108,8 +110,8 @@ public class VerificationActivity extends AppCompatActivity implements View.OnCl
private static boolean REVERSE = false;


private static final String RUN_FIRST = (true) ? "verification_801" : "bug_005";
private final String LAYOUTS_MATCHES = "v.*_.*";
private static final String RUN_FIRST = "basic_cl_001";// (true) ? "verification_801" : "bug_005";
private final String LAYOUTS_MATCHES = "[bv].*_.*";

private static String SHOW_FIRST = "";
MotionLayout mMotionLayout;
Expand Down
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
android:id="@+id/simple"
android:background="#718"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<View
android:id="@+id/simple0"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintBottom_toTopOf="@+id/simple3"
app:layout_constraintEnd_toStartOf="@+id/simple1"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/simple1"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/simple0"
app:layout_constraintTop_toTopOf="@+id/simple0" />

<View
android:id="@+id/simple3"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintBottom_toTopOf="@+id/simple4"
app:layout_constraintEnd_toStartOf="@+id/simple2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/simple0" />

<View
android:id="@+id/simple2"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/simple3"
app:layout_constraintTop_toTopOf="@+id/simple3" />

<View
android:id="@+id/simple4"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/simple6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/simple3" />

<View
android:id="@+id/simple6"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintEnd_toStartOf="@+id/simple5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/simple4"
app:layout_constraintTop_toTopOf="@+id/simple4" />

<View
android:id="@+id/simple5"
android:layout_width="128dp"
android:layout_height="105dp"
android:background="#718"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/simple6"
app:layout_constraintTop_toTopOf="@+id/simple6" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 86ae0a7

Please sign in to comment.