Home | History | Annotate | Download | only in browse
      1 /**
      2  * Copyright (c) 2013, Google Inc.
      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.mail.browse;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.LinearLayout;
     25 
     26 import com.android.mail.R;
     27 import com.android.mail.browse.ConversationViewAdapter.BorderItem;
     28 import com.android.mail.utils.LogUtils;
     29 
     30 /**
     31  * View displaying the border between messages.
     32  * Contains two nine-patches and a {@link android.widget.Space}.
     33  * The nine patches are the bottom of the preceding message
     34  * and the top of the following message.
     35  */
     36 public class BorderView extends LinearLayout {
     37 
     38     private static final String LOG_TAG = "BorderView";
     39 
     40     private static int sMessageBorderSpaceHeight = -1;
     41     private static int sMessageBorderHeightCollapsed = -1;
     42     private static int sExpandedHeight = -1;
     43 
     44     private View mCardBottom;
     45     private View mBorderSpace;
     46     private View mCardTop;
     47 
     48     public BorderView(Context context) {
     49         this(context, null);
     50     }
     51 
     52     public BorderView(Context context, AttributeSet attrs) {
     53         this(context, attrs, -1);
     54     }
     55 
     56     public BorderView(Context context, AttributeSet attrs, int defStyle) {
     57         super(context, attrs, defStyle);
     58 
     59         // In order to update the height appropriately based on
     60         // whether the border is expanded or collapsed,
     61         // we want to stash the height values for for the
     62         // space in both its expanded and collapsed values.
     63         // Additionally, we stash the total height of the view
     64         // when both nine patches are visible.
     65         if (sMessageBorderSpaceHeight == -1) {
     66             final Resources res = context.getResources();
     67             sMessageBorderSpaceHeight =
     68                     res.getDimensionPixelSize(R.dimen.message_border_height);
     69             sMessageBorderHeightCollapsed = res.getDimensionPixelSize(
     70                     R.dimen.message_border_height_collapsed);
     71         }
     72     }
     73 
     74     @Override
     75     protected void onFinishInflate() {
     76         super.onFinishInflate();
     77 
     78         mCardBottom = findViewById(R.id.card_bottom);
     79         mBorderSpace = findViewById(R.id.border_space);
     80         mCardTop = findViewById(R.id.card_top);
     81     }
     82 
     83     public void bind(BorderItem borderItem, boolean measureOnly) {
     84         final boolean isExpanded = borderItem.isExpanded();
     85         if (sExpandedHeight == -1 && isExpanded &&
     86                 !borderItem.isFirstBorder() && !borderItem.isLastBorder() &&
     87                 borderItem.getHeight() > 0) {
     88             sExpandedHeight = borderItem.getHeight();
     89             LogUtils.d(LOG_TAG, "Full Border Height: %s", sExpandedHeight);
     90         }
     91 
     92 
     93 
     94         // Selectively show/hide the card nine-patches if the border is expanded or collapsed.
     95         // Additionally this will occur if this is the first or last border.
     96         mCardBottom.setVisibility(!isExpanded || borderItem.isFirstBorder() ? GONE : VISIBLE);
     97         mCardTop.setVisibility(!isExpanded || borderItem.isLastBorder() ? GONE : VISIBLE);
     98 
     99         // Adjust space height based on expanded state.
    100         final ViewGroup.LayoutParams params = mBorderSpace.getLayoutParams();
    101         params.height = isExpanded ? sMessageBorderSpaceHeight : sMessageBorderHeightCollapsed;
    102         mBorderSpace.setLayoutParams(params);
    103     }
    104 
    105     public void disableCardBottomBorder() {
    106         mCardBottom.setVisibility(GONE);
    107     }
    108 
    109     public void disableCardTopBorder() {
    110         mCardTop.setVisibility(GONE);
    111     }
    112 
    113     /**
    114      * Returns the full expanded height value of the border view.
    115      * This height should never change.
    116      */
    117     public static int getExpandedHeight() {
    118         if (sExpandedHeight == -1) {
    119             LogUtils.wtf(LOG_TAG, "full height not initialized");
    120         }
    121 
    122         return sExpandedHeight;
    123     }
    124 
    125     /**
    126      * Returns the collapsed height value of the border view.
    127      * This height should never change.
    128      */
    129     public static int getCollapsedHeight() {
    130         return sMessageBorderHeightCollapsed;
    131     }
    132 }
    133