• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 
17 package com.android.car.stream.ui;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.util.DisplayMetrics;
22 import android.util.Log;
23 import android.view.WindowManager;
24 
25 /**
26  * Utility class that calculates the number of columns that will fit on the screen. A column's width
27  * is determined by the size of the margins and gutters (space between the columns) that fit
28  * on-screen. Refer to go/aae-por for a table of margin, gutter and number of columns per screen
29  * size.
30  */
31 public class ColumnCalculator {
32     private static final String TAG = "Em.ColumnCalculator";
33 
34     private static ColumnCalculator sInstance;
35     private static int sScreenWidth;
36 
37     private int mNumOfColumns;
38     private int mNumOfGutters;
39     private int mColumnWidth;
40     private int mGutterSize;
41 
getInstance(Context context)42     public static ColumnCalculator getInstance(Context context) {
43         if (sInstance == null) {
44             WindowManager windowManager = (WindowManager) context.getSystemService(
45                     Context.WINDOW_SERVICE);
46             DisplayMetrics displayMetrics = new DisplayMetrics();
47             windowManager.getDefaultDisplay().getMetrics(displayMetrics);
48             sScreenWidth = displayMetrics.widthPixels;
49 
50             sInstance = new ColumnCalculator(context);
51         }
52 
53         return sInstance;
54     }
55 
ColumnCalculator(Context context)56     private ColumnCalculator(Context context) {
57         Resources res = context.getResources();
58         int marginSize = res.getDimensionPixelSize(R.dimen.stream_margin_size);
59         mGutterSize = res.getDimensionPixelSize(R.dimen.stream_gutter_size);
60         mNumOfColumns = res.getInteger(R.integer.stream_num_of_columns);
61 
62         if (Log.isLoggable(TAG, Log.DEBUG)) {
63             Log.d(TAG, "marginSize: " + marginSize + "; numOfColumns: " + mNumOfColumns
64                     + "; gutterSize: " + mGutterSize);
65         }
66 
67         // The gutters appear between each column. As a result, the number of gutters is one less
68         // than the number of columns.
69         mNumOfGutters = mNumOfColumns - 1;
70 
71         // Determine the spacing that is allowed to be filled by the columns by subtracting margins
72         // on both size of the screen and the space taken up by the gutters.
73         int spaceForColumns = sScreenWidth - (2 * marginSize) - (mNumOfGutters * mGutterSize);
74 
75         mColumnWidth = spaceForColumns / mNumOfColumns;
76 
77         if (Log.isLoggable(TAG, Log.DEBUG)) {
78             Log.d(TAG, "mColumnWidth: " + mColumnWidth);
79         }
80     }
81 
82     /**
83      * Returns the total number of columns that fit on screen.
84      */
getNumOfColumns()85     public int getNumOfColumns() {
86         return mNumOfColumns;
87     }
88 
89     /**
90      * Returns the size in pixels of each column.
91      */
getColumnWidth()92     public int getColumnWidth() {
93         return mColumnWidth;
94     }
95 
96     /**
97      * Returns the total number of gutters that fit on screen. A gutter is the space between each
98      * column. This value is always one less than the number of columns.
99      */
getNumOfGutters()100     public int getNumOfGutters() {
101         return mNumOfGutters;
102     }
103 
104     /**
105      * Returns the size of each gutter.
106      */
getGutterSize()107     public int getGutterSize() {
108         return mGutterSize;
109     }
110 
111     /**
112      * Returns the size in pixels for a View that will span the given number of columns.
113      */
getSizeForColumnSpan(int columnSpan)114     public int getSizeForColumnSpan(int columnSpan) {
115         int gutterSpan = columnSpan - 1;
116         return columnSpan * mColumnWidth + gutterSpan * mGutterSize;
117     }
118 }
119