1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.constraintlayout.widget;
17 
18 import android.content.Context;
19 import android.content.res.TypedArray;
20 import android.os.Build;
21 import android.util.AttributeSet;
22 import android.util.Log;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 /**
27  *
28  * This defines the internally defined Constraint set
29  * It allows you to have a group of References which point to other views and provide them with
30  * constraint attributes
31  *
32  */
33 public class Constraints extends ViewGroup {
34 
35     public static final String TAG = "Constraints";
36     ConstraintSet mConstraintSet;
37 
Constraints(Context context)38     public Constraints(Context context) {
39         super(context);
40         super.setVisibility(View.GONE);
41     }
42 
Constraints(Context context, AttributeSet attrs)43     public Constraints(Context context, AttributeSet attrs) {
44         super(context, attrs);
45         init();
46         super.setVisibility(View.GONE);
47     }
48 
Constraints(Context context, AttributeSet attrs, int defStyleAttr)49     public Constraints(Context context, AttributeSet attrs, int defStyleAttr) {
50         super(context, attrs, defStyleAttr);
51         init();
52         super.setVisibility(View.GONE);
53     }
54 
55     /**
56      * {@inheritDoc}
57      */
58     @Override
generateLayoutParams(AttributeSet attrs)59     public LayoutParams generateLayoutParams(AttributeSet attrs) {
60         return new LayoutParams(getContext(), attrs);
61     }
62 
63     public static class LayoutParams extends ConstraintLayout.LayoutParams {
64 
65         public float alpha = 1;
66         public boolean applyElevation = false;
67         public float elevation = 0;
68         public float rotation = 0;
69         public float rotationX = 0;
70         public float rotationY = 0;
71         public float scaleX = 1;
72         public float scaleY = 1;
73         public float transformPivotX = 0;
74         public float transformPivotY = 0;
75         public float translationX = 0;
76         public float translationY = 0;
77         public float translationZ = 0;
78 
LayoutParams(int width, int height)79         public LayoutParams(int width, int height) {
80             super(width, height);
81         }
82 
LayoutParams(LayoutParams source)83         public LayoutParams(LayoutParams source) {
84             super(source);
85         }
86 
LayoutParams(Context c, AttributeSet attrs)87         public LayoutParams(Context c, AttributeSet attrs) {
88             super(c, attrs);
89             TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ConstraintSet);
90             final int count = a.getIndexCount();
91             for (int i = 0; i < count; i++) {
92                 int attr = a.getIndex(i);
93                 if (attr == R.styleable.ConstraintSet_android_alpha) {
94                     alpha = a.getFloat(attr, alpha);
95                 } else if (attr == R.styleable.ConstraintSet_android_elevation) {
96                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
97                         elevation = a.getFloat(attr, elevation);
98                         applyElevation = true;
99                     }
100                 } else if (attr == R.styleable.ConstraintSet_android_rotationX) {
101                     rotationX = a.getFloat(attr, rotationX);
102                 } else if (attr == R.styleable.ConstraintSet_android_rotationY) {
103                     rotationY = a.getFloat(attr, rotationY);
104                 } else if (attr == R.styleable.ConstraintSet_android_rotation) {
105                     rotation = a.getFloat(attr, rotation);
106                 } else if (attr == R.styleable.ConstraintSet_android_scaleX) {
107                     scaleX = a.getFloat(attr, scaleX);
108                 } else if (attr == R.styleable.ConstraintSet_android_scaleY) {
109                     scaleY = a.getFloat(attr, scaleY);
110                 } else if (attr == R.styleable.ConstraintSet_android_transformPivotX) {
111                     transformPivotX = a.getFloat(attr, transformPivotX);
112                 } else if (attr == R.styleable.ConstraintSet_android_transformPivotY) {
113                     transformPivotY = a.getFloat(attr, transformPivotY);
114                 } else if (attr == R.styleable.ConstraintSet_android_translationX) {
115                     translationX = a.getFloat(attr, translationX);
116                 } else if (attr == R.styleable.ConstraintSet_android_translationY) {
117                     translationY = a.getFloat(attr, translationY);
118                 } else if (attr == R.styleable.ConstraintSet_android_translationZ) {
119                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
120                         translationZ = a.getFloat(attr, translationZ);
121                     }
122                 }
123             }
124             a.recycle();
125         }
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
generateDefaultLayoutParams()132     protected LayoutParams generateDefaultLayoutParams() {
133         return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
134     }
135 
init()136     private void init() {
137         Log.v(TAG, " ################# init");
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
generateLayoutParams(ViewGroup.LayoutParams p)144     protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
145         return new ConstraintLayout.LayoutParams(p);
146     }
147 
148     /**
149      * get the Constraints associated with this constraint
150      *
151      * @return
152      */
getConstraintSet()153     public ConstraintSet getConstraintSet() {
154         if (mConstraintSet == null) {
155             mConstraintSet = new ConstraintSet();
156         }
157         // TODO -- could be more efficient...
158         mConstraintSet.clone(this);
159         return mConstraintSet;
160     }
161 
162     @Override
onLayout(boolean changed, int l, int t, int r, int b)163     protected void onLayout(boolean changed, int l, int t, int r, int b) {
164     }
165 }
166