summaryrefslogtreecommitdiffstats
path: root/packages/PrintSpooler/src/com/android/printspooler/widget/PrintOptionsLayout.java
blob: 7a80a8bd426e8e1324c590127a438d121a188b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2014 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 com.android.printspooler.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.android.printspooler.R;

/**
 * This class is a layout manager for the print options. The options are
 * arranged in a configurable number of columns and enough rows to fit all
 * the options given the column count.
 */
@SuppressWarnings("unused")
public final class PrintOptionsLayout extends ViewGroup {

    private int mColumnCount;

    public PrintOptionsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.PrintOptionsLayout);
        mColumnCount = typedArray.getInteger(R.styleable.PrintOptionsLayout_columnCount, 0);
        typedArray.recycle();
    }

    public void setColumnCount(int columnCount) {
        if (mColumnCount != columnCount) {
            mColumnCount = columnCount;
            requestLayout();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        final int columnWidth = (widthSize != 0)
                ? (widthSize - mPaddingLeft - mPaddingRight) / mColumnCount : 0;

        int width = 0;
        int height = 0;
        int childState = 0;

        final int childCount = getChildCount();
        final int rowCount = childCount / mColumnCount + childCount % mColumnCount;

        for (int row = 0; row < rowCount; row++) {
            int rowWidth = 0;
            int rowHeight = 0;

            for (int col = 0; col < mColumnCount; col++) {
                final int childIndex = row * mColumnCount + col;

                if (childIndex >= childCount) {
                    break;
                }

                View child = getChildAt(childIndex);

                if (child.getVisibility() == GONE) {
                    continue;
                }

                MarginLayoutParams childParams = (MarginLayoutParams) child.getLayoutParams();

                final int childWidthMeasureSpec;
                if (columnWidth > 0) {
                    childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                            columnWidth - childParams.getMarginStart() - childParams.getMarginEnd(),
                            MeasureSpec.EXACTLY);
                } else {
                    childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                            getPaddingStart() + getPaddingEnd() + width, childParams.width);
                }

                final int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                        getPaddingTop() + getPaddingBottom() + height, childParams.height);

                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

                childState = combineMeasuredStates(childState, child.getMeasuredState());

                rowWidth += child.getMeasuredWidth() + childParams.getMarginStart()
                        + childParams.getMarginEnd();

                rowHeight = Math.max(rowHeight, child.getMeasuredHeight() + childParams.topMargin
                        + childParams.bottomMargin);
            }

            width = Math.max(width, rowWidth);
            height += rowHeight;
        }

        width += getPaddingStart() + getPaddingEnd();
        width = Math.max(width, getMinimumWidth());

        height += getPaddingTop() + getPaddingBottom();
        height = Math.max(height, getMinimumHeight());

        setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, childState),
                resolveSizeAndState(height, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int childCount = getChildCount();
        final int rowCount = childCount / mColumnCount + childCount % mColumnCount;

        int cellStart = getPaddingStart();
        int cellTop = getPaddingTop();

        for (int row = 0; row < rowCount; row++) {
            int rowHeight = 0;

            for (int col = 0; col < mColumnCount; col++) {
                final int childIndex = row * mColumnCount + col;

                if (childIndex >= childCount) {
                    break;
                }

                View child = getChildAt(childIndex);

                if (child.getVisibility() == GONE) {
                    continue;
                }

                MarginLayoutParams childParams = (MarginLayoutParams) child.getLayoutParams();

                final int childLeft = cellStart + childParams.getMarginStart();
                final int childTop = cellTop + childParams.topMargin;
                final int childRight = childLeft + child.getMeasuredWidth();
                final int childBottom = childTop + child.getMeasuredHeight();

                child.layout(childLeft, childTop, childRight, childBottom);

                cellStart = childRight + childParams.getMarginEnd();

                rowHeight = Math.max(rowHeight, child.getMeasuredHeight()
                        + childParams.topMargin + childParams.bottomMargin);
            }

            cellStart = getPaddingStart();
            cellTop += rowHeight;
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new ViewGroup.MarginLayoutParams(getContext(), attrs);
    }
}