Home | History | Annotate | Download | only in ui
      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.TypedArray;
     21 import android.support.v7.widget.CardView;
     22 import android.util.AttributeSet;
     23 import android.util.Log;
     24 import android.view.ViewGroup;
     25 
     26 /**
     27  * A {@link CardView} whose width can be specified by the number of columns that it will span.
     28  *
     29  * @see {@link ColumnCalculator}
     30  */
     31 public final class StreamCardView extends CardView {
     32     private static final String TAG = "Em.StreamCardView";
     33 
     34     private ColumnCalculator mColumnCalculator;
     35     private int mColumnSpan;
     36 
     37     /**
     38      * The default number of columns that this {@link StreamCardView} spans. This number is used
     39      * if {@link #setColumnSpan(int)} is not called or {@code columnSpan} is not defined in an
     40      * XML layout.
     41      */
     42     private int mDefaultColumnSpan;
     43 
     44     public StreamCardView(Context context) {
     45         super(context);
     46         init(context, null, 0 /* defStyleAttrs */);
     47     }
     48 
     49     public StreamCardView(Context context, AttributeSet attrs) {
     50         super(context, attrs);
     51         init(context, attrs, 0 /* defStyleAttrs */);
     52     }
     53 
     54     public StreamCardView(Context context, AttributeSet attrs, int defStyleAttr) {
     55         super(context, attrs, defStyleAttr);
     56         init(context, attrs, defStyleAttr);
     57     }
     58 
     59     private void init(Context context, AttributeSet attrs, int defStyleAttrs) {
     60         mColumnCalculator = ColumnCalculator.getInstance(context);
     61 
     62         mDefaultColumnSpan = getResources().getInteger(R.integer.stream_card_default_column_span);
     63         mColumnSpan = mDefaultColumnSpan;
     64 
     65         TypedArray ta = null;
     66 
     67         try {
     68             ta = context.obtainStyledAttributes(attrs, R.styleable.StreamCardView, defStyleAttrs,
     69                     0 /* defStyleRes */);
     70             mColumnSpan = ta.getInteger(R.styleable.StreamCardView_columnSpan, mDefaultColumnSpan);
     71 
     72             if (Log.isLoggable(TAG, Log.DEBUG)) {
     73                 Log.d(TAG, "mColumnSpan: " + mColumnSpan);
     74             }
     75         } finally {
     76             if (ta != null) {
     77                 ta.recycle();
     78             }
     79         }
     80     }
     81 
     82     @Override
     83     public void setLayoutParams(ViewGroup.LayoutParams params) {
     84         // Override any given layoutParams so that the width is one that is calculated based on
     85         // column and gutter span.
     86         params.width = mColumnCalculator.getSizeForColumnSpan(mColumnSpan);
     87 
     88         // Then, set the LayoutParams normally.
     89         super.setLayoutParams(params);
     90     }
     91 
     92     /**
     93      * Sets the number of columns that this {@link StreamCardView} will span. The given span is
     94      * ignored if it is less than 0 or greater than the number of columns that fit on screen.
     95      */
     96     public void setColumnSpan(int columnSpan) {
     97         if (columnSpan <= 0 || columnSpan > mColumnCalculator.getNumOfColumns()) {
     98             return;
     99         }
    100 
    101         mColumnSpan = columnSpan;
    102 
    103         // Re-initialize the LayoutParams so that the width of this card is updated.
    104         if (getLayoutParams() != null) {
    105             setLayoutParams(getLayoutParams());
    106         }
    107     }
    108 
    109     /**
    110      * Returns the currently number of columns that this StreamCardView spans.
    111      */
    112     public int getColumnSpan() {
    113         return mColumnSpan;
    114     }
    115 }
    116