Utils.java revision a144f86b41170e8ee7fe8d966cc51c5fc90cd44a
1/* 2 * Copyright (C) 2006 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.calendar; 18 19import static android.provider.Calendar.EVENT_BEGIN_TIME; 20 21import android.content.Context; 22import android.content.Intent; 23import android.content.SharedPreferences; 24import android.database.Cursor; 25import android.database.MatrixCursor; 26import android.graphics.drawable.Drawable; 27import android.graphics.drawable.GradientDrawable; 28import android.net.Uri; 29import android.text.format.Time; 30import android.util.Log; 31import android.view.animation.AlphaAnimation; 32import android.widget.ViewFlipper; 33 34import java.util.Calendar; 35import java.util.List; 36import java.util.Map; 37 38public class Utils { 39 private static final int CLEAR_ALPHA_MASK = 0x00FFFFFF; 40 private static final int HIGH_ALPHA = 255 << 24; 41 private static final int MED_ALPHA = 180 << 24; 42 private static final int LOW_ALPHA = 150 << 24; 43 44 protected static final String OPEN_EMAIL_MARKER = " <"; 45 protected static final String CLOSE_EMAIL_MARKER = ">"; 46 47 /* The corner should be rounded on the top right and bottom right */ 48 private static final float[] CORNERS = new float[] {0, 0, 5, 5, 5, 5, 0, 0}; 49 50 51 public static void startActivity(Context context, String className, long time) { 52 Intent intent = new Intent(Intent.ACTION_VIEW); 53 54 intent.setClassName(context, className); 55 intent.putExtra(EVENT_BEGIN_TIME, time); 56 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); 57 58 context.startActivity(intent); 59 } 60 61 static String getSharedPreference(Context context, String key, String defaultValue) { 62 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context); 63 return prefs.getString(key, defaultValue); 64 } 65 66 static void setSharedPreference(Context context, String key, String value) { 67 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context); 68 SharedPreferences.Editor editor = prefs.edit(); 69 editor.putString(key, value); 70 editor.commit(); 71 } 72 73 static void setDefaultView(Context context, int viewId) { 74 String activityString = CalendarApplication.ACTIVITY_NAMES[viewId]; 75 76 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context); 77 SharedPreferences.Editor editor = prefs.edit(); 78 if (viewId == CalendarApplication.AGENDA_VIEW_ID || 79 viewId == CalendarApplication.DAY_VIEW_ID) { 80 // Record the (new) detail start view only for Agenda and Day 81 editor.putString(CalendarPreferenceActivity.KEY_DETAILED_VIEW, activityString); 82 } 83 84 // Record the (new) start view 85 editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString); 86 editor.commit(); 87 } 88 89 public static final Time timeFromIntent(Intent intent) { 90 Time time = new Time(); 91 time.set(timeFromIntentInMillis(intent)); 92 return time; 93 } 94 95 public static MatrixCursor matrixCursorFromCursor(Cursor cursor) { 96 MatrixCursor newCursor = new MatrixCursor(cursor.getColumnNames()); 97 int numColumns = cursor.getColumnCount(); 98 String data[] = new String[numColumns]; 99 cursor.moveToPosition(-1); 100 while (cursor.moveToNext()) { 101 for (int i = 0; i < numColumns; i++) { 102 data[i] = cursor.getString(i); 103 } 104 newCursor.addRow(data); 105 } 106 return newCursor; 107 } 108 109 /** 110 * Compares two cursors to see if they contain the same data. 111 * 112 * @return Returns true of the cursors contain the same data and are not null, false 113 * otherwise 114 */ 115 public static boolean compareCursors(Cursor c1, Cursor c2) { 116 if(c1 == null || c2 == null) { 117 return false; 118 } 119 120 int numColumns = c1.getColumnCount(); 121 if (numColumns != c2.getColumnCount()) { 122 return false; 123 } 124 125 if (c1.getCount() != c2.getCount()) { 126 return false; 127 } 128 129 c1.moveToPosition(-1); 130 c2.moveToPosition(-1); 131 while(c1.moveToNext() && c2.moveToNext()) { 132 for(int i = 0; i < numColumns; i++) { 133 if(!c1.getString(i).equals(c2.getString(i))) { 134 return false; 135 } 136 } 137 } 138 139 return true; 140 } 141 142 /** 143 * If the given intent specifies a time (in milliseconds since the epoch), 144 * then that time is returned. Otherwise, the current time is returned. 145 */ 146 public static final long timeFromIntentInMillis(Intent intent) { 147 // If the time was specified, then use that. Otherwise, use the current time. 148 Uri data = intent.getData(); 149 long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1); 150 if (millis == -1 && data != null && data.isHierarchical()) { 151 List<String> path = data.getPathSegments(); 152 if(path.size() == 2 && path.get(0).equals("time")) { 153 try { 154 millis = Long.valueOf(data.getLastPathSegment()); 155 } catch (NumberFormatException e) { 156 Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time " + 157 "found. Using current time."); 158 } 159 } 160 } 161 if (millis <= 0) { 162 millis = System.currentTimeMillis(); 163 } 164 return millis; 165 } 166 167 public static final void applyAlphaAnimation(ViewFlipper v) { 168 AlphaAnimation in = new AlphaAnimation(0.0f, 1.0f); 169 170 in.setStartOffset(0); 171 in.setDuration(500); 172 173 AlphaAnimation out = new AlphaAnimation(1.0f, 0.0f); 174 175 out.setStartOffset(0); 176 out.setDuration(500); 177 178 v.setInAnimation(in); 179 v.setOutAnimation(out); 180 } 181 182 public static Drawable getColorChip(int color) { 183 /* 184 * We want the color chip to have a nice gradient using 185 * the color of the calendar. To do this we use a GradientDrawable. 186 * The color supplied has an alpha of FF so we first do: 187 * color & 0x00FFFFFF 188 * to clear the alpha. Then we add our alpha to it. 189 * We use 3 colors to get a step effect where it starts off very 190 * light and quickly becomes dark and then a slow transition to 191 * be even darker. 192 */ 193 color &= CLEAR_ALPHA_MASK; 194 int startColor = color | HIGH_ALPHA; 195 int middleColor = color | MED_ALPHA; 196 int endColor = color | LOW_ALPHA; 197 int[] colors = new int[] {startColor, middleColor, endColor}; 198 GradientDrawable d = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors); 199 d.setCornerRadii(CORNERS); 200 return d; 201 } 202 203 /** 204 * Formats the given Time object so that it gives the month and year 205 * (for example, "September 2007"). 206 * 207 * @param time the time to format 208 * @return the string containing the weekday and the date 209 */ 210 public static String formatMonthYear(Context context, Time time) { 211 return time.format(context.getResources().getString(R.string.month_year)); 212 } 213 214 // TODO: replace this with the correct i18n way to do this 215 public static final String englishNthDay[] = { 216 "", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", 217 "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", 218 "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", 219 "30th", "31st" 220 }; 221 222 public static String formatNth(int nth) { 223 return "the " + englishNthDay[nth]; 224 } 225 226 /** 227 * Sets the time to the beginning of the day (midnight) by clearing the 228 * hour, minute, and second fields. 229 */ 230 static void setTimeToStartOfDay(Time time) { 231 time.second = 0; 232 time.minute = 0; 233 time.hour = 0; 234 } 235 236 /** 237 * Get first day of week as android.text.format.Time constant. 238 * @return the first day of week in android.text.format.Time 239 */ 240 public static int getFirstDayOfWeek() { 241 int startDay = Calendar.getInstance().getFirstDayOfWeek(); 242 if (startDay == Calendar.SATURDAY) { 243 return Time.SATURDAY; 244 } else if (startDay == Calendar.MONDAY) { 245 return Time.MONDAY; 246 } else { 247 return Time.SUNDAY; 248 } 249 } 250 251 /** 252 * Determine whether the column position is Saturday or not. 253 * @param column the column position 254 * @param firstDayOfWeek the first day of week in android.text.format.Time 255 * @return true if the column is Saturday position 256 */ 257 public static boolean isSaturday(int column, int firstDayOfWeek) { 258 return (firstDayOfWeek == Time.SUNDAY && column == 6) 259 || (firstDayOfWeek == Time.MONDAY && column == 5) 260 || (firstDayOfWeek == Time.SATURDAY && column == 0); 261 } 262 263 /** 264 * Determine whether the column position is Sunday or not. 265 * @param column the column position 266 * @param firstDayOfWeek the first day of week in android.text.format.Time 267 * @return true if the column is Sunday position 268 */ 269 public static boolean isSunday(int column, int firstDayOfWeek) { 270 return (firstDayOfWeek == Time.SUNDAY && column == 0) 271 || (firstDayOfWeek == Time.MONDAY && column == 6) 272 || (firstDayOfWeek == Time.SATURDAY && column == 1); 273 } 274 275 /** 276 * Scan through a cursor of calendars and check if names are duplicated. 277 * 278 * This travels a cursor containing calendar display names and fills in the provided map with 279 * whether or not each name is repeated. 280 * @param isDuplicateName The map to put the duplicate check results in. 281 * @param cursor The query of calendars to check 282 * @param nameIndex The column of the query that contains the display name 283 */ 284 public static void checkForDuplicateNames(Map<String, Boolean> isDuplicateName, Cursor cursor, 285 int nameIndex) { 286 isDuplicateName.clear(); 287 cursor.moveToPosition(-1); 288 while (cursor.moveToNext()) { 289 String displayName = cursor.getString(nameIndex); 290 // Set it to true if we've seen this name before, false otherwise 291 if (displayName != null) { 292 isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName)); 293 } 294 } 295 } 296} 297