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