Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 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.phone.common.util;
     18 
     19 import android.content.res.Resources;
     20 import android.graphics.Outline;
     21 import android.graphics.Paint;
     22 import android.util.TypedValue;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.view.ViewOutlineProvider;
     26 import android.widget.ListView;
     27 import android.widget.TextView;
     28 
     29 import com.android.phone.common.R;
     30 
     31 /**
     32  * Provides static functions to work with views
     33  */
     34 public class ViewUtil {
     35     private ViewUtil() {}
     36 
     37     /**
     38      * Returns the width as specified in the LayoutParams
     39      * @throws IllegalStateException Thrown if the view's width is unknown before a layout pass
     40      * s
     41      */
     42     public static int getConstantPreLayoutWidth(View view) {
     43         // We haven't been layed out yet, so get the size from the LayoutParams
     44         final ViewGroup.LayoutParams p = view.getLayoutParams();
     45         if (p.width < 0) {
     46             throw new IllegalStateException("Expecting view's width to be a constant rather " +
     47                     "than a result of the layout pass");
     48         }
     49         return p.width;
     50     }
     51 
     52     /**
     53      * Returns a boolean indicating whether or not the view's layout direction is RTL
     54      *
     55      * @param view - A valid view
     56      * @return True if the view's layout direction is RTL
     57      */
     58     public static boolean isViewLayoutRtl(View view) {
     59         return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
     60     }
     61 
     62     private static final ViewOutlineProvider OVAL_OUTLINE_PROVIDER = new ViewOutlineProvider() {
     63         @Override
     64         public void getOutline(View view, Outline outline) {
     65             outline.setOval(0, 0, view.getWidth(), view.getHeight());
     66         }
     67     };
     68 
     69     /**
     70      * Configures the floating action button, clipping it to a circle and setting its translation z.
     71      * @param view The float action button's view.
     72      * @param res The resources file.
     73      */
     74     public static void setupFloatingActionButton(View view, Resources res) {
     75         view.setOutlineProvider(OVAL_OUTLINE_PROVIDER);
     76         view.setTranslationZ(
     77                 res.getDimensionPixelSize(R.dimen.floating_action_button_translation_z));
     78     }
     79 
     80     /**
     81      * Adds padding to the bottom of the given {@link ListView} so that the floating action button
     82      * does not obscure any content.
     83      *
     84      * @param listView to add the padding to
     85      * @param res valid resources object
     86      */
     87     public static void addBottomPaddingToListViewForFab(ListView listView, Resources res) {
     88         final int fabPadding = res.getDimensionPixelSize(
     89                 R.dimen.floating_action_button_list_bottom_padding);
     90         listView.setPaddingRelative(listView.getPaddingStart(), listView.getPaddingTop(),
     91                 listView.getPaddingEnd(), listView.getPaddingBottom() + fabPadding);
     92         listView.setClipToPadding(false);
     93     }
     94 
     95     public static void resizeText(TextView textView, int originalTextSize, int minTextSize) {
     96         final Paint paint = textView.getPaint();
     97         final int width = textView.getWidth();
     98         if (width == 0) return;
     99         textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, originalTextSize);
    100         float ratio = width / paint.measureText(textView.getText().toString());
    101         if (ratio <= 1.0f) {
    102             textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
    103                     Math.max(minTextSize, originalTextSize * ratio));
    104         }
    105     }
    106 }
    107