summaryrefslogtreecommitdiff
path: root/android/support/car/utils/ColumnCalculator.java
blob: fa5dd432653968cbe0c1f77d89f36ac4132f4cdf (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
/*
 * Copyright (C) 2017 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.car.utils;

import android.content.Context;
import android.content.res.Resources;
import android.support.car.R;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

/**
 * Utility class that calculates the size of the columns that will fit on the screen. A column's
 * width is determined by the size of the margins and gutters (space between the columns) that fit
 * on-screen.
 *
 * <p>Refer to the appropriate dimens and integers for the size of the margins and number of
 * columns.
 */
public class ColumnCalculator {
    private static final String TAG = "ColumnCalculator";

    private static ColumnCalculator sInstance;
    private static int sScreenWidth;

    private int mNumOfColumns;
    private int mNumOfGutters;
    private int mColumnWidth;
    private int mGutterSize;

    /**
     * Gets an instance of the {@link ColumnCalculator}. If this is the first time that this
     * method has been called, then the given {@link Context} will be used to retrieve resources.
     *
     * @param context The current calling Context.
     * @return An instance of {@link ColumnCalculator}.
     */
    public static ColumnCalculator getInstance(Context context) {
        if (sInstance == null) {
            WindowManager windowManager = (WindowManager) context.getSystemService(
                    Context.WINDOW_SERVICE);
            DisplayMetrics displayMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            sScreenWidth = displayMetrics.widthPixels;

            sInstance = new ColumnCalculator(context);
        }

        return sInstance;
    }

    private ColumnCalculator(Context context) {
        Resources res = context.getResources();
        int marginSize = res.getDimensionPixelSize(R.dimen.car_margin);
        mGutterSize = res.getDimensionPixelSize(R.dimen.car_screen_gutter_size);
        mNumOfColumns = res.getInteger(R.integer.car_screen_num_of_columns);

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, String.format("marginSize: %d; numOfColumns: %d; gutterSize: %d",
                    marginSize, mNumOfColumns, mGutterSize));
        }

        // The gutters appear between each column. As a result, the number of gutters is one less
        // than the number of columns.
        mNumOfGutters = mNumOfColumns - 1;

        // Determine the spacing that is allowed to be filled by the columns by subtracting margins
        // on both size of the screen and the space taken up by the gutters.
        int spaceForColumns = sScreenWidth - (2 * marginSize) - (mNumOfGutters * mGutterSize);

        mColumnWidth = spaceForColumns / mNumOfColumns;

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "mColumnWidth: " + mColumnWidth);
        }
    }

    /**
     * Returns the total number of columns that fit on the current screen.
     *
     * @return The total number of columns that fit on the screen.
     */
    public int getNumOfColumns() {
        return mNumOfColumns;
    }

    /**
     * Returns the size in pixels of each column. The column width is determined by the size of the
     * screen divided by the number of columns, size of gutters and margins.
     *
     * @return The width of a single column in pixels.
     */
    public int getColumnWidth() {
        return mColumnWidth;
    }

    /**
     * Returns the total number of gutters that fit on screen. A gutter is the space between each
     * column. This value is always one less than the number of columns.
     *
     * @return The number of gutters on screen.
     */
    public int getNumOfGutters() {
        return mNumOfGutters;
    }

    /**
     * Returns the size of each gutter in pixels. A gutter is the space between each column.
     *
     * @return The size of a single gutter in pixels.
     */
    public int getGutterSize() {
        return mGutterSize;
    }

    /**
     * Returns the size in pixels for the given number of columns. This value takes into account
     * the size of the gutter between the columns as well. For example, for a column span of four,
     * the size returned is the sum of four columns and three gutters.
     *
     * @return The size in pixels for a given column span.
     */
    public int getSizeForColumnSpan(int columnSpan) {
        int gutterSpan = columnSpan - 1;
        return columnSpan * mColumnWidth + gutterSpan * mGutterSize;
    }
}