Home | History | Annotate | Download | only in browse
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mail.browse;
     19 
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.text.Layout;
     23 import android.util.AttributeSet;
     24 import android.widget.TextView;
     25 
     26 import com.android.mail.R;
     27 
     28 /**
     29  * A TextView that knows the widest that any of its containing
     30  * {@link FolderSpan}s can be. They cannot exceed the TextView line width, or
     31  * else {@link Layout} will split up the spans in strange places.
     32  */
     33 public class FolderSpanTextView extends TextView implements FolderSpan.FolderSpanDimensions {
     34 
     35     private final int mFolderPadding;
     36     private final int mFolderPaddingExtraWidth;
     37     private final int mFolderPaddingBefore;
     38     private final int mFolderPaddingAbove;
     39 
     40     private int mMaxSpanWidth;
     41 
     42     public FolderSpanTextView(Context context) {
     43         this(context, null);
     44     }
     45 
     46     public FolderSpanTextView(Context context, AttributeSet attrs) {
     47         super(context, attrs);
     48 
     49         Resources r = getResources();
     50         mFolderPadding = r.getDimensionPixelOffset(R.dimen.conversation_folder_padding);
     51         mFolderPaddingExtraWidth = r.getDimensionPixelOffset(
     52                 R.dimen.conversation_folder_padding_extra_width);
     53         mFolderPaddingBefore = r.getDimensionPixelOffset(
     54                 R.dimen.conversation_folder_padding_before);
     55         mFolderPaddingAbove = r.getDimensionPixelOffset(R.dimen.conversation_folder_padding_above);
     56     }
     57 
     58     @Override
     59     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     60         mMaxSpanWidth = MeasureSpec.getSize(widthMeasureSpec) - getTotalPaddingLeft()
     61                 - getTotalPaddingRight();
     62 
     63         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     64     }
     65 
     66     @Override
     67     public int getPadding() {
     68         return mFolderPadding;
     69     }
     70 
     71     @Override
     72     public int getPaddingExtraWidth() {
     73         return mFolderPaddingExtraWidth;
     74     }
     75 
     76     @Override
     77     public int getPaddingBefore() {
     78         return mFolderPaddingBefore;
     79     }
     80 
     81     @Override
     82     public int getPaddingAbove() {
     83         return mFolderPaddingAbove;
     84     }
     85 
     86     @Override
     87     public int getMaxWidth() {
     88         return mMaxSpanWidth;
     89     }
     90 }
     91