1 /* 2 * Copyright (C) 2009 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.videoeditor.util; 18 19 import java.util.Random; 20 21 import com.android.videoeditor.R; 22 23 import android.content.Context; 24 import android.graphics.Paint; 25 26 /** 27 * String utilities 28 */ 29 public class StringUtils { 30 /** 31 * Pseudo-random number generator object for use with randomString(). The 32 * Random class is not considered to be cryptographically secure, so only 33 * use these random Strings for low to medium security applications. 34 */ 35 private static Random sRandGen = new Random(); 36 37 /** 38 * Array of numbers and letters. Numbers appear in the list 39 * twice so that there is a more equal chance that a number will be picked. 40 * We can use the array to get a random number or letter by picking a random 41 * array index. 42 */ 43 private static char[] sNumbersAndLetters = 44 ("0123456789abcdefghijklmnopqrstuvwxyz0123456789").toCharArray(); 45 46 /** 47 * Array of numbers. 48 */ 49 private static char[] sNumbers = ("0123456789").toCharArray(); 50 51 /** 52 * This class cannot be instantiated 53 */ 54 private StringUtils() { 55 } 56 57 /** 58 * Returns a random String of numbers and letters (lower and upper case) of 59 * the specified length. The method uses the Random class that is built-in 60 * to Java which is suitable for low to medium grade security uses. This 61 * means that the output is only pseudo random, i.e., each number is 62 * mathematically generated so is not truly random. 63 * <p> 64 * The specified length must be at least one. If not, the method will return null. 65 * 66 * @param length the desired length of the random String to return. 67 * @return a random String of numbers and letters of the specified length. 68 */ 69 public static String randomString(int length) { 70 if (length < 1) { 71 return null; 72 } 73 // Create a char buffer to put random letters and numbers in. 74 final char[] randBuffer = new char[length]; 75 for (int i = 0; i < randBuffer.length; i++) { 76 randBuffer[i] = sNumbersAndLetters[sRandGen.nextInt(sNumbersAndLetters.length - 1)]; 77 } 78 79 return new String(randBuffer); 80 } 81 82 /** 83 * Returns a random String of numbers of the specified length. 84 * This means that the output is only pseudo random, i.e., each number is 85 * mathematically generated so is not truly random. 86 * <p> 87 * The specified length must be at least one. If not, the method will return null. 88 * 89 * @param length the desired length of the random String to return. 90 * @return a random String of numbers of the specified length. 91 */ 92 public static String randomStringOfNumbers(int length) { 93 if (length < 1) { 94 return null; 95 } 96 // Create a char buffer to put random letters and numbers in. 97 final char[] randBuffer = new char[length]; 98 for (int i = 0; i < randBuffer.length; i++) { 99 randBuffer[i] = sNumbers[sRandGen.nextInt(sNumbers.length - 1)]; 100 } 101 return new String(randBuffer); 102 } 103 104 /** 105 * Get a readable string displaying the time 106 * 107 * @param context The context (needed only for relative time) 108 * @param time The time 109 * 110 * @return The time string 111 */ 112 public static String getTimestampAsString(Context context, long time) { 113 final long hours = time / 3600000; 114 time %= 3600000; 115 final long mins = time / 60000; 116 time %= 60000; 117 final long sec = time / 1000; 118 time %= 1000; 119 time /= 100; 120 return String.format("%02d:%02d:%02d.%01d", hours, mins, sec, time); 121 } 122 123 /** 124 * Get a readable string displaying the time 125 * 126 * @param context The context (needed only for relative time) 127 * @param time The time 128 * 129 * @return The time string 130 */ 131 public static String getSimpleTimestampAsString(Context context, long time) { 132 final long hours = time / 3600000; 133 time %= 3600000; 134 final long mins = time / 60000; 135 time %= 60000; 136 final long sec = time / 1000; 137 return String.format("%02d:%02d:%02d", hours, mins, sec); 138 } 139 140 /** 141 * Get a readable string displaying the time 142 * 143 * @param context The context (needed only for relative time) 144 * @param time The time 145 * 146 * @return The time string 147 */ 148 public static String getDurationAsString(Context context, long time) { 149 final long hours = time / 3600000; 150 time %= 3600000; 151 final long mins = time / 60000; 152 time %= 60000; 153 final long sec = time / 1000; 154 155 if (hours == 0) { 156 if (mins == 0) { 157 return String.format(context.getString(R.string.seconds), sec); 158 } else if (mins == 1) { 159 return String.format(context.getString(R.string.minute_and_seconds), sec); 160 } else { 161 return String.format(context.getString(R.string.minutes), mins); 162 } 163 } else if (hours == 1) { 164 return String.format(context.getString(R.string.hour_and_minutes), mins); 165 } else { 166 return String.format(context.getString(R.string.hours_and_minutes), hours, mins); 167 } 168 } 169 170 /** 171 * Trim text to a maximum size 172 * 173 * @param text The text 174 * @param p The paint 175 * @param maxSize The maximum size 176 * 177 * @return The text 178 */ 179 public static String trimText(String text, Paint p, int maxSize) { 180 final int textSize = (int)p.measureText(text); 181 if (textSize > maxSize) { 182 final int chars = p.breakText(text, true, maxSize - 12, null); 183 text = text.substring(0, chars); 184 text += "..."; 185 } 186 187 return text; 188 } 189 } 190