Home | History | Annotate | Download | only in widget
      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.internal.widget;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.content.res.TypedArray;
     22 import android.graphics.Canvas;
     23 import android.util.AttributeSet;
     24 import android.view.RemotableViewMethod;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.RemoteViews;
     28 
     29 import com.android.internal.R;
     30 
     31 /**
     32  * A custom-built layout for the Notification.MessagingStyle.
     33  *
     34  * Evicts children until they all fit.
     35  */
     36 @RemoteViews.RemoteView
     37 public class MessagingLinearLayout extends ViewGroup {
     38 
     39     /**
     40      * Spacing to be applied between views.
     41      */
     42     private int mSpacing;
     43 
     44     private int mMaxDisplayedLines = Integer.MAX_VALUE;
     45 
     46     private MessagingLayout mMessagingLayout;
     47 
     48     public MessagingLinearLayout(Context context, @Nullable AttributeSet attrs) {
     49         super(context, attrs);
     50 
     51         final TypedArray a = context.obtainStyledAttributes(attrs,
     52                 R.styleable.MessagingLinearLayout, 0,
     53                 0);
     54 
     55         final int N = a.getIndexCount();
     56         for (int i = 0; i < N; i++) {
     57             int attr = a.getIndex(i);
     58             switch (attr) {
     59                 case R.styleable.MessagingLinearLayout_spacing:
     60                     mSpacing = a.getDimensionPixelSize(i, 0);
     61                     break;
     62             }
     63         }
     64 
     65         a.recycle();
     66     }
     67 
     68     @Override
     69     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     70         // This is essentially a bottom-up linear layout that only adds children that fit entirely
     71         // up to a maximum height.
     72         int targetHeight = MeasureSpec.getSize(heightMeasureSpec);
     73         switch (MeasureSpec.getMode(heightMeasureSpec)) {
     74             case MeasureSpec.UNSPECIFIED:
     75                 targetHeight = Integer.MAX_VALUE;
     76                 break;
     77         }
     78 
     79         // Now that we know which views to take, fix up the indents and see what width we get.
     80         int measuredWidth = mPaddingLeft + mPaddingRight;
     81         final int count = getChildCount();
     82         int totalHeight;
     83         for (int i = 0; i < count; ++i) {
     84             final View child = getChildAt(i);
     85             final LayoutParams lp = (LayoutParams) child.getLayoutParams();
     86             lp.hide = true;
     87         }
     88 
     89         totalHeight = mPaddingTop + mPaddingBottom;
     90         boolean first = true;
     91         int linesRemaining = mMaxDisplayedLines;
     92         // Starting from the bottom: we measure every view as if it were the only one. If it still
     93         // fits, we take it, otherwise we stop there.
     94         for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) {
     95             if (getChildAt(i).getVisibility() == GONE) {
     96                 continue;
     97             }
     98             final View child = getChildAt(i);
     99             LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
    100             MessagingChild messagingChild = null;
    101             int spacing = mSpacing;
    102             if (child instanceof MessagingChild) {
    103                 messagingChild = (MessagingChild) child;
    104                 messagingChild.setMaxDisplayedLines(linesRemaining);
    105                 spacing += messagingChild.getExtraSpacing();
    106             }
    107             spacing = first ? 0 : spacing;
    108             measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, totalHeight
    109                     - mPaddingTop - mPaddingBottom + spacing);
    110 
    111             final int childHeight = child.getMeasuredHeight();
    112             int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin +
    113                     lp.bottomMargin + spacing);
    114             first = false;
    115             int measureType = MessagingChild.MEASURED_NORMAL;
    116             if (messagingChild != null) {
    117                 measureType = messagingChild.getMeasuredType();
    118                 linesRemaining -= messagingChild.getConsumedLines();
    119             }
    120             boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED;
    121             boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL;
    122             if (newHeight <= targetHeight && !isTooSmall) {
    123                 totalHeight = newHeight;
    124                 measuredWidth = Math.max(measuredWidth,
    125                         child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin
    126                                 + mPaddingLeft + mPaddingRight);
    127                 lp.hide = false;
    128                 if (isShortened || linesRemaining <= 0) {
    129                     break;
    130                 }
    131             } else {
    132                 break;
    133             }
    134         }
    135 
    136         setMeasuredDimension(
    137                 resolveSize(Math.max(getSuggestedMinimumWidth(), measuredWidth),
    138                         widthMeasureSpec),
    139                 Math.max(getSuggestedMinimumHeight(), totalHeight));
    140     }
    141 
    142     @Override
    143     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    144         final int paddingLeft = mPaddingLeft;
    145 
    146         int childTop;
    147 
    148         // Where right end of child should go
    149         final int width = right - left;
    150         final int childRight = width - mPaddingRight;
    151 
    152         final int layoutDirection = getLayoutDirection();
    153         final int count = getChildCount();
    154 
    155         childTop = mPaddingTop;
    156 
    157         boolean first = true;
    158         final boolean shown = isShown();
    159         for (int i = 0; i < count; i++) {
    160             final View child = getChildAt(i);
    161             if (child.getVisibility() == GONE) {
    162                 continue;
    163             }
    164             final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    165             MessagingChild messagingChild = (MessagingChild) child;
    166 
    167             final int childWidth = child.getMeasuredWidth();
    168             final int childHeight = child.getMeasuredHeight();
    169 
    170             int childLeft;
    171             if (layoutDirection == LAYOUT_DIRECTION_RTL) {
    172                 childLeft = childRight - childWidth - lp.rightMargin;
    173             } else {
    174                 childLeft = paddingLeft + lp.leftMargin;
    175             }
    176             if (lp.hide) {
    177                 if (shown && lp.visibleBefore) {
    178                     // We still want to lay out the child to have great animations
    179                     child.layout(childLeft, childTop, childLeft + childWidth,
    180                             childTop + lp.lastVisibleHeight);
    181                     messagingChild.hideAnimated();
    182                 }
    183                 lp.visibleBefore = false;
    184                 continue;
    185             } else {
    186                 lp.visibleBefore = true;
    187                 lp.lastVisibleHeight = childHeight;
    188             }
    189 
    190             if (!first) {
    191                 childTop += mSpacing;
    192             }
    193 
    194             childTop += lp.topMargin;
    195             child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
    196 
    197             childTop += childHeight + lp.bottomMargin;
    198 
    199             first = false;
    200         }
    201     }
    202 
    203     @Override
    204     protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    205         final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    206         if (lp.hide) {
    207             MessagingChild messagingChild = (MessagingChild) child;
    208             if (!messagingChild.isHidingAnimated()) {
    209                 return true;
    210             }
    211         }
    212         return super.drawChild(canvas, child, drawingTime);
    213     }
    214 
    215     @Override
    216     public LayoutParams generateLayoutParams(AttributeSet attrs) {
    217         return new LayoutParams(mContext, attrs);
    218     }
    219 
    220     @Override
    221     protected LayoutParams generateDefaultLayoutParams() {
    222         return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    223 
    224     }
    225 
    226     @Override
    227     protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
    228         LayoutParams copy = new LayoutParams(lp.width, lp.height);
    229         if (lp instanceof MarginLayoutParams) {
    230             copy.copyMarginsFrom((MarginLayoutParams) lp);
    231         }
    232         return copy;
    233     }
    234 
    235     public static boolean isGone(View view) {
    236         if (view.getVisibility() == View.GONE) {
    237             return true;
    238         }
    239         final ViewGroup.LayoutParams lp = view.getLayoutParams();
    240         if (lp instanceof MessagingLinearLayout.LayoutParams
    241                 && ((MessagingLinearLayout.LayoutParams) lp).hide) {
    242             return true;
    243         }
    244         return false;
    245     }
    246 
    247     /**
    248      * Sets how many lines should be displayed at most
    249      */
    250     @RemotableViewMethod
    251     public void setMaxDisplayedLines(int numberLines) {
    252         mMaxDisplayedLines = numberLines;
    253     }
    254 
    255     public void setMessagingLayout(MessagingLayout layout) {
    256         mMessagingLayout = layout;
    257     }
    258 
    259     public MessagingLayout getMessagingLayout() {
    260         return mMessagingLayout;
    261     }
    262 
    263     public interface MessagingChild {
    264         int MEASURED_NORMAL = 0;
    265         int MEASURED_SHORTENED = 1;
    266         int MEASURED_TOO_SMALL = 2;
    267 
    268         int getMeasuredType();
    269         int getConsumedLines();
    270         void setMaxDisplayedLines(int lines);
    271         void hideAnimated();
    272         boolean isHidingAnimated();
    273         default int getExtraSpacing() {
    274             return 0;
    275         }
    276     }
    277 
    278     public static class LayoutParams extends MarginLayoutParams {
    279 
    280         public boolean hide = false;
    281         public boolean visibleBefore = false;
    282         public int lastVisibleHeight;
    283 
    284         public LayoutParams(Context c, AttributeSet attrs) {
    285             super(c, attrs);
    286         }
    287 
    288         public LayoutParams(int width, int height) {
    289             super(width, height);
    290         }
    291     }
    292 }
    293