Home | History | Annotate | Download | only in text
      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.text;
     19 
     20 import android.graphics.Canvas;
     21 import android.graphics.Paint;
     22 import android.support.v4.text.BidiFormatter;
     23 import android.text.TextPaint;
     24 import android.text.TextUtils;
     25 import android.text.style.ReplacementSpan;
     26 
     27 import com.android.mail.ui.FolderDisplayer;
     28 
     29 /**
     30  * A replacement span to use when displaying folders in conversation view. Prevents a folder name
     31  * from wrapping mid-name, and ellipsizes very long folder names that can't fit on a single line.
     32  */
     33 public class FolderSpan extends ReplacementSpan {
     34     private final TextPaint mWorkPaint = new TextPaint();
     35     private final String mName;
     36     private final int mFgColor;
     37     private final int mBgColor;
     38     private final FolderDisplayer.FolderDrawableResources mRes;
     39     private final BidiFormatter mFormatter;
     40     private final FolderSpanDimensions mDim;
     41 
     42     public FolderSpan(String name, int fgColor, int bgColor,
     43             FolderDisplayer.FolderDrawableResources res, BidiFormatter formatter,
     44             FolderSpanDimensions dim) {
     45         super();
     46 
     47         mName = name;
     48         mFgColor = fgColor;
     49         mBgColor = bgColor;
     50         mRes = res;
     51         mFormatter = formatter;
     52         mDim = dim;
     53     }
     54 
     55     private int getWidth(Paint p) {
     56         p.setTextSize(mRes.folderFontSize);
     57         return Math.min((int) p.measureText(mName) + 2 * mRes.folderHorizontalPadding,
     58                 mDim.getMaxChipWidth());
     59     }
     60 
     61     private int getHeight(Paint p) {
     62         p.setTextSize(mRes.folderFontSize);
     63         final Paint.FontMetricsInt fm = p.getFontMetricsInt();
     64         return fm.bottom - fm.top;
     65     }
     66 
     67     @Override
     68     public int getSize(Paint p, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
     69         mWorkPaint.set(p);
     70         return getWidth(mWorkPaint);
     71     }
     72 
     73     @Override
     74     public void draw(Canvas canvas, CharSequence charSequence, int start, int end, float x, int top,
     75             int baseline, int bottom, Paint paint) {
     76         mWorkPaint.set(paint);
     77         mWorkPaint.setTextSize(mRes.folderFontSize);
     78         final int width = getWidth(mWorkPaint);
     79         final int height = getHeight(mWorkPaint);
     80         String name = mName;
     81         if (width == mDim.getMaxChipWidth()) {
     82             name = TextUtils.ellipsize(mName, mWorkPaint, width - 2 * mRes.folderHorizontalPadding,
     83                     TextUtils.TruncateAt.MIDDLE).toString();
     84         }
     85         FolderDisplayer.drawFolder(canvas, x, baseline - height, width, height, name, mFgColor,
     86                 mBgColor, mRes, mFormatter, mWorkPaint);
     87     }
     88 
     89     public static interface FolderSpanDimensions {
     90         int getMaxChipWidth();
     91     }
     92 }
     93