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