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 /** 70 * Same as {@link View#findViewById}, but crashes if there's no view. 71 */ 72 @SuppressWarnings("unchecked") 73 public static <T extends View> T getView(View parent, int viewId) { 74 return (T) checkView(parent.findViewById(viewId)); 75 } 76 77 private static View checkView(View v) { 78 if (v == null) { 79 throw new IllegalArgumentException("View doesn't exist"); 80 } 81 return v; 82 } 83 84 /** 85 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 86 */ 87 public static void setVisibilitySafe(View v, int visibility) { 88 if (v != null) { 89 v.setVisibility(visibility); 90 } 91 } 92 93 /** 94 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 95 */ 96 public static void setVisibilitySafe(Activity parent, int viewId, int visibility) { 97 setVisibilitySafe(parent.findViewById(viewId), visibility); 98 } 99 100 /** 101 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 102 */ 103 public static void setVisibilitySafe(View parent, int viewId, int visibility) { 104 setVisibilitySafe(parent.findViewById(viewId), visibility); 105 } 106 } 107