UiUtilities.java revision 2fbb3db5d86210d03175ce77ff08c989a96c5864
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 17package com.android.email.activity; 18 19import com.android.email.R; 20 21import android.app.Activity; 22import android.content.Context; 23import android.content.res.Resources; 24import android.view.View; 25 26public 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 Activity#findViewById}, but crashes if there's no view. 71 */ 72 public static View getView(Activity parent, int viewId) { 73 return checkView(parent.findViewById(viewId)); 74 } 75 76 /** 77 * Same as {@link View#findViewById}, but crashes if there's no view. 78 */ 79 public static View getView(View parent, int viewId) { 80 return checkView(parent.findViewById(viewId)); 81 } 82 83 private static View checkView(View v) { 84 if (v == null) { 85 throw new IllegalArgumentException("View doesn't exist"); 86 } 87 return v; 88 } 89 90 /** 91 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 92 */ 93 public static void setVisibilitySafe(View v, int visibility) { 94 if (v != null) { 95 v.setVisibility(visibility); 96 } 97 } 98 99 /** 100 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 101 */ 102 public static void setVisibilitySafe(Activity parent, int viewId, int visibility) { 103 setVisibilitySafe(parent.findViewById(viewId), visibility); 104 } 105 106 /** 107 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 108 */ 109 public static void setVisibilitySafe(View parent, int viewId, int visibility) { 110 setVisibilitySafe(parent.findViewById(viewId), visibility); 111 } 112} 113