Utils.java revision c6023058da92fc25a87499aa0e8132a1700fd0fd
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;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.Resources;
23import android.text.format.Time;
24import android.view.animation.AlphaAnimation;
25import android.widget.ViewFlipper;
26
27public class Utils {
28    public static void startActivity(Context context, String className, long time) {
29        Intent intent = new Intent(Intent.ACTION_VIEW);
30
31        intent.setClassName(context, className);
32        intent.putExtra(EVENT_BEGIN_TIME, time);
33        // TODO Setting this flag will cause the EVENT_BEGIN_TIME to be lost for existing activities
34        // Need to pass the EVENT_BEGIN_TIME via other methods or use another flag
35        // intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
36
37        context.startActivity(intent);
38    }
39
40    public static final Time timeFromIntent(Intent intent) {
41        Time time = new Time();
42        time.set(timeFromIntentInMillis(intent));
43        return time;
44    }
45
46    /**
47     * If the given intent specifies a time (in milliseconds since the epoch),
48     * then that time is returned. Otherwise, the current time is returned.
49     */
50    public static final long timeFromIntentInMillis(Intent intent) {
51        // If the time was specified, then use that.  Otherwise, use the current time.
52        long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
53        if (millis == -1) {
54            millis = System.currentTimeMillis();
55        }
56        return millis;
57    }
58
59    public static final void applyAlphaAnimation(ViewFlipper v) {
60        AlphaAnimation in = new AlphaAnimation(0.0f, 1.0f);
61
62        in.setStartOffset(0);
63        in.setDuration(500);
64
65        AlphaAnimation out = new AlphaAnimation(1.0f, 0.0f);
66
67        out.setStartOffset(0);
68        out.setDuration(500);
69
70        v.setInAnimation(in);
71        v.setOutAnimation(out);
72    }
73
74    /**
75     * Formats the given Time object so that it gives the month and year
76     * (for example, "September 2007").
77     *
78     * @param time the time to format
79     * @return the string containing the weekday and the date
80     */
81    public static String formatMonthYear(Time time) {
82        Resources res = Resources.getSystem();
83        return time.format(res.getString(com.android.internal.R.string.month_year));
84    }
85
86    // TODO: replace this with the correct i18n way to do this
87    public static final String englishNthDay[] = {
88        "", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th",
89        "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th",
90        "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th",
91        "30th", "31st"
92    };
93
94    public static String formatNth(int nth) {
95        return "the " + englishNthDay[nth];
96    }
97
98    /**
99     * Sets the time to the beginning of the day (midnight) by clearing the
100     * hour, minute, and second fields.
101     */
102    static void setTimeToStartOfDay(Time time) {
103        time.second = 0;
104        time.minute = 0;
105        time.hour = 0;
106    }
107}
108