OSDN Git Service

merge from open-source master
[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
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.content.res.Resources;
25 import android.preference.PreferenceManager;
26 import android.text.format.Time;
27 import android.view.animation.AlphaAnimation;
28 import android.widget.ViewFlipper;
29
30 public class Utils {
31     public static void startActivity(Context context, String className, long time) {
32         Intent intent = new Intent(Intent.ACTION_VIEW);
33
34         intent.setClassName(context, className);
35         intent.putExtra(EVENT_BEGIN_TIME, time);
36         intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
37
38         context.startActivity(intent);
39     }
40
41     static void setDefaultView(Context context, int viewId) {
42         String activityString = CalendarApplication.ACTIVITY_NAMES[viewId];
43
44         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
45         SharedPreferences.Editor editor = prefs.edit();
46         if (viewId == CalendarApplication.AGENDA_VIEW_ID ||
47                 viewId == CalendarApplication.DAY_VIEW_ID) {
48             // Record the (new) detail start view only for Agenda and Day
49             editor.putString(CalendarPreferenceActivity.KEY_DETAILED_VIEW, activityString);
50         }
51
52         // Record the (new) start view
53         editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString);
54         editor.commit();
55     }
56
57     public static final Time timeFromIntent(Intent intent) {
58         Time time = new Time();
59         time.set(timeFromIntentInMillis(intent));
60         return time;
61     }
62
63     /**
64      * If the given intent specifies a time (in milliseconds since the epoch),
65      * then that time is returned. Otherwise, the current time is returned.
66      */
67     public static final long timeFromIntentInMillis(Intent intent) {
68         // If the time was specified, then use that.  Otherwise, use the current time.
69         long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
70         if (millis == -1) {
71             millis = System.currentTimeMillis();
72         }
73         return millis;
74     }
75
76     public static final void applyAlphaAnimation(ViewFlipper v) {
77         AlphaAnimation in = new AlphaAnimation(0.0f, 1.0f);
78
79         in.setStartOffset(0);
80         in.setDuration(500);
81
82         AlphaAnimation out = new AlphaAnimation(1.0f, 0.0f);
83
84         out.setStartOffset(0);
85         out.setDuration(500);
86
87         v.setInAnimation(in);
88         v.setOutAnimation(out);
89     }
90
91     /**
92      * Formats the given Time object so that it gives the month and year
93      * (for example, "September 2007").
94      *
95      * @param time the time to format
96      * @return the string containing the weekday and the date
97      */
98     public static String formatMonthYear(Time time) {
99         Resources res = Resources.getSystem();
100         return time.format(res.getString(com.android.internal.R.string.month_year));
101     }
102
103     // TODO: replace this with the correct i18n way to do this
104     public static final String englishNthDay[] = {
105         "", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th",
106         "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th",
107         "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th",
108         "30th", "31st"
109     };
110
111     public static String formatNth(int nth) {
112         return "the " + englishNthDay[nth];
113     }
114
115     /**
116      * Sets the time to the beginning of the day (midnight) by clearing the
117      * hour, minute, and second fields.
118      */
119     static void setTimeToStartOfDay(Time time) {
120         time.second = 0;
121         time.minute = 0;
122         time.hour = 0;
123     }
124 }