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 com.android.email.R; 20 21 import android.app.Activity; 22 import android.app.Fragment; 23 import android.content.Context; 24 import android.content.res.Resources; 25 import android.view.View; 26 27 public class UiUtilities { UiUtilities()28 private UiUtilities() { 29 } 30 31 /** 32 * Formats the given size as a String in bytes, kB, MB or GB. Ex: 12,315,000 = 11 MB 33 */ formatSize(Context context, long size)34 public static String formatSize(Context context, long size) { 35 final Resources res = context.getResources(); 36 final long KB = 1024; 37 final long MB = (KB * 1024); 38 final long GB = (MB * 1024); 39 40 int resId; 41 int value; 42 43 if (size < KB) { 44 resId = R.plurals.message_view_attachment_bytes; 45 value = (int) size; 46 } else if (size < MB) { 47 resId = R.plurals.message_view_attachment_kilobytes; 48 value = (int) (size / KB); 49 } else if (size < GB) { 50 resId = R.plurals.message_view_attachment_megabytes; 51 value = (int) (size / MB); 52 } else { 53 resId = R.plurals.message_view_attachment_gigabytes; 54 value = (int) (size / GB); 55 } 56 return res.getQuantityString(resId, value, value); 57 } 58 getMessageCountForUi(Context context, int count, boolean replaceZeroWithBlank)59 public static String getMessageCountForUi(Context context, int count, 60 boolean replaceZeroWithBlank) { 61 if (replaceZeroWithBlank && (count == 0)) { 62 return ""; 63 } else if (count > 999) { 64 return context.getString(R.string.more_than_999); 65 } else { 66 return Integer.toString(count); 67 } 68 } 69 70 /** Generics version of {@link Activity#findViewById} */ 71 @SuppressWarnings("unchecked") getViewOrNull(Activity parent, int viewId)72 public static <T extends View> T getViewOrNull(Activity parent, int viewId) { 73 return (T) parent.findViewById(viewId); 74 } 75 76 /** Generics version of {@link View#findViewById} */ 77 @SuppressWarnings("unchecked") getViewOrNull(View parent, int viewId)78 public static <T extends View> T getViewOrNull(View parent, int viewId) { 79 return (T) parent.findViewById(viewId); 80 } 81 82 /** 83 * Same as {@link Activity#findViewById}, but crashes if there's no view. 84 */ 85 @SuppressWarnings("unchecked") getView(Activity parent, int viewId)86 public static <T extends View> T getView(Activity parent, int viewId) { 87 return (T) checkView(parent.findViewById(viewId)); 88 } 89 90 /** 91 * Same as {@link View#findViewById}, but crashes if there's no view. 92 */ 93 @SuppressWarnings("unchecked") getView(View parent, int viewId)94 public static <T extends View> T getView(View parent, int viewId) { 95 return (T) checkView(parent.findViewById(viewId)); 96 } 97 checkView(View v)98 private static View checkView(View v) { 99 if (v == null) { 100 throw new IllegalArgumentException("View doesn't exist"); 101 } 102 return v; 103 } 104 105 /** 106 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 107 */ setVisibilitySafe(View v, int visibility)108 public static void setVisibilitySafe(View v, int visibility) { 109 if (v != null) { 110 v.setVisibility(visibility); 111 } 112 } 113 114 /** 115 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 116 */ setVisibilitySafe(Activity parent, int viewId, int visibility)117 public static void setVisibilitySafe(Activity parent, int viewId, int visibility) { 118 setVisibilitySafe(parent.findViewById(viewId), visibility); 119 } 120 121 /** 122 * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null. 123 */ setVisibilitySafe(View parent, int viewId, int visibility)124 public static void setVisibilitySafe(View parent, int viewId, int visibility) { 125 setVisibilitySafe(parent.findViewById(viewId), visibility); 126 } 127 128 /** 129 * Used by an {@link Fragment} to install itself to the host activity. 130 * 131 * @see FragmentInstallable 132 */ installFragment(Fragment fragment)133 public static void installFragment(Fragment fragment) { 134 final Activity a = fragment.getActivity(); 135 if (a instanceof FragmentInstallable) { 136 ((FragmentInstallable) a).onInstallFragment(fragment); 137 } 138 } 139 140 /** 141 * Used by an {@link Fragment} to uninstall itself from the host activity. 142 * 143 * @see FragmentInstallable 144 */ uninstallFragment(Fragment fragment)145 public static void uninstallFragment(Fragment fragment) { 146 final Activity a = fragment.getActivity(); 147 if (a instanceof FragmentInstallable) { 148 ((FragmentInstallable) a).onUninstallFragment(fragment); 149 } 150 } 151 152 private static int sDebugForcedPaneMode = 0; 153 154 /** 155 * Force 1-pane UI or 2-pane UI. 156 * 157 * @param paneMode Set 1 if 1-pane UI should be used. Set 2 if 2-pane UI should be used. 158 * Set 0 to use the default UI. 159 */ setDebugPaneMode(int paneMode)160 static void setDebugPaneMode(int paneMode) { 161 sDebugForcedPaneMode = paneMode; 162 } 163 164 /** 165 * @return {@code true} if 2-pane UI should be used. {@code false} otherwise. 166 */ useTwoPane(Context context)167 public static boolean useTwoPane(Context context) { 168 if (sDebugForcedPaneMode == 1) { 169 return false; 170 } 171 if (sDebugForcedPaneMode == 2) { 172 return true; 173 } 174 return context.getResources().getBoolean(R.bool.use_two_pane); 175 } 176 177 /** 178 * Return whether to show search results in a split pane. 179 */ showTwoPaneSearchResults(Context context)180 public static boolean showTwoPaneSearchResults(Context context) { 181 return context.getResources().getBoolean(R.bool.show_two_pane_search_result); 182 } 183 } 184