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