Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2011 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.email.activity;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.view.View;
     23 
     24 import com.android.email.R;
     25 
     26 public class UiUtilities {
     27     private UiUtilities() {
     28     }
     29 
     30     /**
     31      * Formats the given size as a String in bytes, kB, MB or GB.  Ex: 12,315,000 = 11 MB
     32      */
     33     public static String formatSize(Context context, long size) {
     34         final Resources res = context.getResources();
     35         final long KB = 1024;
     36         final long MB = (KB * 1024);
     37         final long GB  = (MB * 1024);
     38 
     39         int resId;
     40         int value;
     41 
     42         if (size < KB) {
     43             resId = R.plurals.message_view_attachment_bytes;
     44             value = (int) size;
     45         } else if (size < MB) {
     46             resId = R.plurals.message_view_attachment_kilobytes;
     47             value = (int) (size / KB);
     48         } else if (size < GB) {
     49             resId = R.plurals.message_view_attachment_megabytes;
     50             value = (int) (size / MB);
     51         } else {
     52             resId = R.plurals.message_view_attachment_gigabytes;
     53             value = (int) (size / GB);
     54         }
     55         return res.getQuantityString(resId, value, value);
     56     }
     57 
     58     public static String getMessageCountForUi(Context context, int count,
     59             boolean replaceZeroWithBlank) {
     60         if (replaceZeroWithBlank && (count == 0)) {
     61             return "";
     62         } else if (count > 999) {
     63             return context.getString(R.string.more_than_999);
     64         } else {
     65             return Integer.toString(count);
     66         }
     67     }
     68 
     69     /** Generics version of {@link Activity#findViewById} */
     70     @SuppressWarnings("unchecked")
     71     public static <T extends View> T getViewOrNull(Activity parent, int viewId) {
     72         return (T) parent.findViewById(viewId);
     73     }
     74 
     75     /** Generics version of {@link View#findViewById} */
     76     @SuppressWarnings("unchecked")
     77     public static <T extends View> T getViewOrNull(View parent, int viewId) {
     78         return (T) parent.findViewById(viewId);
     79     }
     80 
     81     /**
     82      * Same as {@link Activity#findViewById}, but crashes if there's no view.
     83      */
     84     @SuppressWarnings("unchecked")
     85     public static <T extends View> T getView(Activity parent, int viewId) {
     86         return (T) checkView(parent.findViewById(viewId));
     87     }
     88 
     89     /**
     90      * Same as {@link View#findViewById}, but crashes if there's no view.
     91      */
     92     @SuppressWarnings("unchecked")
     93     public static <T extends View> T getView(View parent, int viewId) {
     94         return (T) checkView(parent.findViewById(viewId));
     95     }
     96 
     97     private static View checkView(View v) {
     98         if (v == null) {
     99             throw new IllegalArgumentException("View doesn't exist");
    100         }
    101         return v;
    102     }
    103 
    104     /**
    105      * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
    106      */
    107     public static void setVisibilitySafe(View v, int visibility) {
    108         if (v != null) {
    109             v.setVisibility(visibility);
    110         }
    111     }
    112 
    113     /**
    114      * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
    115      */
    116     public static void setVisibilitySafe(Activity parent, int viewId, int visibility) {
    117         setVisibilitySafe(parent.findViewById(viewId), visibility);
    118     }
    119 
    120     /**
    121      * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
    122      */
    123     public static void setVisibilitySafe(View parent, int viewId, int visibility) {
    124         setVisibilitySafe(parent.findViewById(viewId), visibility);
    125     }
    126 }
    127