OSDN Git Service

Code drop from //branches/cupcake/...@124589
[android-x86/packages-apps-Calendar.git] / src / com / android / calendar / Utils.java
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
17 package com.android.calendar;
18
19 import static android.provider.Calendar.EVENT_BEGIN_TIME;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.text.format.Time;
23 import android.view.animation.AlphaAnimation;
24 import android.widget.ViewFlipper;
25
26 public class Utils {
27     public static void startActivity(Context context, String className, long time) {
28         Intent intent = new Intent(Intent.ACTION_VIEW);
29
30         intent.setClassName(context, className);
31         intent.putExtra(EVENT_BEGIN_TIME, time);
32
33         context.startActivity(intent);
34     }
35     
36     public static final Time timeFromIntent(Intent intent) {
37         Time time = new Time();
38         time.set(timeFromIntentInMillis(intent));
39         return time;
40     }
41
42     /**
43      * If the given intent specifies a time (in milliseconds since the epoch),
44      * then that time is returned. Otherwise, the current time is returned.
45      */
46     public static final long timeFromIntentInMillis(Intent intent) {
47         // If the time was specified, then use that.  Otherwise, use the current time.
48         long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
49         if (millis == -1) {
50             millis = System.currentTimeMillis();
51         }
52         return millis;
53     }
54
55     public static final void applyAlphaAnimation(ViewFlipper v) {
56         AlphaAnimation in = new AlphaAnimation(0.0f, 1.0f);
57
58         in.setStartOffset(0);
59         in.setDuration(500);
60
61         AlphaAnimation out = new AlphaAnimation(1.0f, 0.0f);
62
63         out.setStartOffset(0);
64         out.setDuration(500);
65
66         v.setInAnimation(in);
67         v.setOutAnimation(out);
68     }
69     
70     /**
71      * Formats the given Time object so that it gives the day of the week
72      * and the date (for example, "Monday, September 3, 2007").  If the
73      * abbrev argument is true, then abbreviated names will be used (for
74      * example, "Mon, Sep 3, 2007").
75      * 
76      * @param time the time to format
77      * @param abbrev if true, use abbreviations for the weekday and month
78      * @return the string containing the weekday and the date 
79      */
80     public static String formatDayDate(Time time, boolean abbrev) {
81         String date;
82         if (abbrev) {
83             date = time.format("%a, %b %-d, %Y");
84         } else {
85             date = time.format("%A, %B %-d, %Y");
86         }
87         return date;
88     }
89     
90     /**
91      * Formats the given Time object so that it gives the month and year
92      * (for example, "September 2007").
93      * 
94      * @param time the time to format
95      * @return the string containing the weekday and the date 
96      */
97     public static String formatMonthYear(Time time) {
98         return time.format("%B %Y");
99     }
100     
101     // TODO: replace this with the correct i18n way to do this
102     public static final String englishNthDay[] = {
103         "", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th",
104         "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th",
105         "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th",
106         "30th", "31st"
107     };
108     
109     public static String formatNth(int nth) {
110         return "the " + englishNthDay[nth];
111     }
112     
113     /**
114      * Sets the time to the beginning of the day (midnight) by clearing the
115      * hour, minute, and second fields.  
116      */
117     static void setTimeToStartOfDay(Time time) {
118         time.second = 0;
119         time.minute = 0;
120         time.hour = 0;
121     }
122 }