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