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