Utils.java revision eca5d33e4230cf6ac3acfe3fabf853f9275f98ca
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.net.Uri; 25import android.text.format.Time; 26import android.util.Log; 27import android.view.animation.AlphaAnimation; 28import android.widget.ViewFlipper; 29 30import java.util.Calendar; 31import java.util.List; 32 33public class Utils { 34 public static void startActivity(Context context, String className, long time) { 35 Intent intent = new Intent(Intent.ACTION_VIEW); 36 37 intent.setClassName(context, className); 38 intent.putExtra(EVENT_BEGIN_TIME, time); 39 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); 40 41 context.startActivity(intent); 42 } 43 44 static String getSharedPreference(Context context, String key, String defaultValue) { 45 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context); 46 return prefs.getString(key, defaultValue); 47 } 48 49 static void setSharedPreference(Context context, String key, String value) { 50 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context); 51 SharedPreferences.Editor editor = prefs.edit(); 52 editor.putString(key, value); 53 editor.commit(); 54 } 55 56 static void setDefaultView(Context context, int viewId) { 57 String activityString = CalendarApplication.ACTIVITY_NAMES[viewId]; 58 59 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context); 60 SharedPreferences.Editor editor = prefs.edit(); 61 if (viewId == CalendarApplication.AGENDA_VIEW_ID || 62 viewId == CalendarApplication.DAY_VIEW_ID) { 63 // Record the (new) detail start view only for Agenda and Day 64 editor.putString(CalendarPreferenceActivity.KEY_DETAILED_VIEW, activityString); 65 } 66 67 // Record the (new) start view 68 editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString); 69 editor.commit(); 70 } 71 72 public static final Time timeFromIntent(Intent intent) { 73 Time time = new Time(); 74 time.set(timeFromIntentInMillis(intent)); 75 return time; 76 } 77 78 /** 79 * If the given intent specifies a time (in milliseconds since the epoch), 80 * then that time is returned. Otherwise, the current time is returned. 81 */ 82 public static final long timeFromIntentInMillis(Intent intent) { 83 // If the time was specified, then use that. Otherwise, use the current time. 84 Uri data = intent.getData(); 85 long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1); 86 if (millis == -1 && data != null && data.isHierarchical()) { 87 List<String> path = data.getPathSegments(); 88 if(path.size() == 2 && path.get(0).equals("time")) { 89 try { 90 millis = Long.valueOf(data.getLastPathSegment()); 91 } catch (NumberFormatException e) { 92 Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time " + 93 "found. Using current time."); 94 } 95 } 96 } 97 if (millis <= 0) { 98 millis = System.currentTimeMillis(); 99 } 100 return millis; 101 } 102 103 public static final void applyAlphaAnimation(ViewFlipper v) { 104 AlphaAnimation in = new AlphaAnimation(0.0f, 1.0f); 105 106 in.setStartOffset(0); 107 in.setDuration(500); 108 109 AlphaAnimation out = new AlphaAnimation(1.0f, 0.0f); 110 111 out.setStartOffset(0); 112 out.setDuration(500); 113 114 v.setInAnimation(in); 115 v.setOutAnimation(out); 116 } 117 118 /** 119 * Formats the given Time object so that it gives the month and year 120 * (for example, "September 2007"). 121 * 122 * @param time the time to format 123 * @return the string containing the weekday and the date 124 */ 125 public static String formatMonthYear(Context context, Time time) { 126 return time.format(context.getResources().getString(R.string.month_year)); 127 } 128 129 // TODO: replace this with the correct i18n way to do this 130 public static final String englishNthDay[] = { 131 "", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", 132 "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", 133 "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", 134 "30th", "31st" 135 }; 136 137 public static String formatNth(int nth) { 138 return "the " + englishNthDay[nth]; 139 } 140 141 /** 142 * Sets the time to the beginning of the day (midnight) by clearing the 143 * hour, minute, and second fields. 144 */ 145 static void setTimeToStartOfDay(Time time) { 146 time.second = 0; 147 time.minute = 0; 148 time.hour = 0; 149 } 150 151 /** 152 * Get first day of week as android.text.format.Time constant. 153 * @return the first day of week in android.text.format.Time 154 */ 155 public static int getFirstDayOfWeek() { 156 int startDay = Calendar.getInstance().getFirstDayOfWeek(); 157 if (startDay == Calendar.SATURDAY) { 158 return Time.SATURDAY; 159 } else if (startDay == Calendar.MONDAY) { 160 return Time.MONDAY; 161 } else { 162 return Time.SUNDAY; 163 } 164 } 165 166 /** 167 * Determine whether the column position is Saturday or not. 168 * @param column the column position 169 * @param firstDayOfWeek the first day of week in android.text.format.Time 170 * @return true if the column is Saturday position 171 */ 172 public static boolean isSaturday(int column, int firstDayOfWeek) { 173 return (firstDayOfWeek == Time.SUNDAY && column == 6) 174 || (firstDayOfWeek == Time.MONDAY && column == 5) 175 || (firstDayOfWeek == Time.SATURDAY && column == 0); 176 } 177 178 /** 179 * Determine whether the column position is Sunday or not. 180 * @param column the column position 181 * @param firstDayOfWeek the first day of week in android.text.format.Time 182 * @return true if the column is Sunday position 183 */ 184 public static boolean isSunday(int column, int firstDayOfWeek) { 185 return (firstDayOfWeek == Time.SUNDAY && column == 0) 186 || (firstDayOfWeek == Time.MONDAY && column == 6) 187 || (firstDayOfWeek == Time.SATURDAY && column == 1); 188 } 189} 190