1 /* 2 * Copyright (C) 2008 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; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.InputStreamReader; 22 import java.io.UnsupportedEncodingException; 23 import java.util.Date; 24 25 import com.android.email.codec.binary.Base64; 26 27 import android.text.Editable; 28 import android.widget.TextView; 29 30 public class Utility { readInputStream(InputStream in, String encoding)31 public final static String readInputStream(InputStream in, String encoding) throws IOException { 32 InputStreamReader reader = new InputStreamReader(in, encoding); 33 StringBuffer sb = new StringBuffer(); 34 int count; 35 char[] buf = new char[512]; 36 while ((count = reader.read(buf)) != -1) { 37 sb.append(buf, 0, count); 38 } 39 return sb.toString(); 40 } 41 arrayContains(Object[] a, Object o)42 public final static boolean arrayContains(Object[] a, Object o) { 43 for (int i = 0, count = a.length; i < count; i++) { 44 if (a[i].equals(o)) { 45 return true; 46 } 47 } 48 return false; 49 } 50 51 /** 52 * Combines the given array of Objects into a single string using the 53 * seperator character and each Object's toString() method. between each 54 * part. 55 * 56 * @param parts 57 * @param seperator 58 * @return 59 */ combine(Object[] parts, char seperator)60 public static String combine(Object[] parts, char seperator) { 61 if (parts == null) { 62 return null; 63 } 64 StringBuffer sb = new StringBuffer(); 65 for (int i = 0; i < parts.length; i++) { 66 sb.append(parts[i].toString()); 67 if (i < parts.length - 1) { 68 sb.append(seperator); 69 } 70 } 71 return sb.toString(); 72 } 73 base64Decode(String encoded)74 public static String base64Decode(String encoded) { 75 if (encoded == null) { 76 return null; 77 } 78 byte[] decoded = new Base64().decode(encoded.getBytes()); 79 return new String(decoded); 80 } 81 base64Encode(String s)82 public static String base64Encode(String s) { 83 if (s == null) { 84 return s; 85 } 86 byte[] encoded = new Base64().encode(s.getBytes()); 87 return new String(encoded); 88 } 89 requiredFieldValid(TextView view)90 public static boolean requiredFieldValid(TextView view) { 91 return view.getText() != null && view.getText().length() > 0; 92 } 93 requiredFieldValid(Editable s)94 public static boolean requiredFieldValid(Editable s) { 95 return s != null && s.length() > 0; 96 } 97 98 /** 99 * Ensures that the given string starts and ends with the double quote character. The string is not modified in any way except to add the 100 * double quote character to start and end if it's not already there. 101 * 102 * TODO: Rename this, because "quoteString()" can mean so many different things. 103 * 104 * sample -> "sample" 105 * "sample" -> "sample" 106 * ""sample"" -> "sample" 107 * "sample"" -> "sample" 108 * sa"mp"le -> "sa"mp"le" 109 * "sa"mp"le" -> "sa"mp"le" 110 * (empty string) -> "" 111 * " -> "" 112 * @param s 113 * @return 114 */ quoteString(String s)115 public static String quoteString(String s) { 116 if (s == null) { 117 return null; 118 } 119 if (!s.matches("^\".*\"$")) { 120 return "\"" + s + "\""; 121 } 122 else { 123 return s; 124 } 125 } 126 127 /** 128 * Apply quoting rules per IMAP RFC, 129 * quoted = DQUOTE *QUOTED-CHAR DQUOTE 130 * QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> / "\" quoted-specials 131 * quoted-specials = DQUOTE / "\" 132 * 133 * This is used primarily for IMAP login, but might be useful elsewhere. 134 * 135 * NOTE: Not very efficient - you may wish to preflight this, or perhaps it should check 136 * for trouble chars before calling the replace functions. 137 * 138 * @param s The string to be quoted. 139 * @return A copy of the string, having undergone quoting as described above 140 */ imapQuoted(String s)141 public static String imapQuoted(String s) { 142 143 // First, quote any backslashes by replacing \ with \\ 144 // regex Pattern: \\ (Java string const = \\\\) 145 // Substitute: \\\\ (Java string const = \\\\\\\\) 146 String result = s.replaceAll("\\\\", "\\\\\\\\"); 147 148 // Then, quote any double-quotes by replacing " with \" 149 // regex Pattern: " (Java string const = \") 150 // Substitute: \\" (Java string const = \\\\\") 151 result = result.replaceAll("\"", "\\\\\""); 152 153 // return string with quotes around it 154 return "\"" + result + "\""; 155 } 156 157 /** 158 * A fast version of URLDecoder.decode() that works only with UTF-8 and does only two 159 * allocations. This version is around 3x as fast as the standard one and I'm using it 160 * hundreds of times in places that slow down the UI, so it helps. 161 */ fastUrlDecode(String s)162 public static String fastUrlDecode(String s) { 163 try { 164 byte[] bytes = s.getBytes("UTF-8"); 165 byte ch; 166 int length = 0; 167 for (int i = 0, count = bytes.length; i < count; i++) { 168 ch = bytes[i]; 169 if (ch == '%') { 170 int h = (bytes[i + 1] - '0'); 171 int l = (bytes[i + 2] - '0'); 172 if (h > 9) { 173 h -= 7; 174 } 175 if (l > 9) { 176 l -= 7; 177 } 178 bytes[length] = (byte) ((h << 4) | l); 179 i += 2; 180 } 181 else if (ch == '+') { 182 bytes[length] = ' '; 183 } 184 else { 185 bytes[length] = bytes[i]; 186 } 187 length++; 188 } 189 return new String(bytes, 0, length, "UTF-8"); 190 } 191 catch (UnsupportedEncodingException uee) { 192 return null; 193 } 194 } 195 196 /** 197 * Returns true if the specified date is within today. Returns false otherwise. 198 * @param date 199 * @return 200 */ isDateToday(Date date)201 public static boolean isDateToday(Date date) { 202 // TODO But Calendar is so slowwwwwww.... 203 Date today = new Date(); 204 if (date.getYear() == today.getYear() && 205 date.getMonth() == today.getMonth() && 206 date.getDate() == today.getDate()) { 207 return true; 208 } 209 return false; 210 } 211 212 /* 213 * TODO disabled this method globally. It is used in all the settings screens but I just 214 * noticed that an unrelated icon was dimmed. Android must share drawables internally. 215 */ setCompoundDrawablesAlpha(TextView view, int alpha)216 public static void setCompoundDrawablesAlpha(TextView view, int alpha) { 217 // Drawable[] drawables = view.getCompoundDrawables(); 218 // for (Drawable drawable : drawables) { 219 // if (drawable != null) { 220 // drawable.setAlpha(alpha); 221 // } 222 // } 223 } 224 } 225