Utils.java revision d7c7f2ab6fdb55451ead2d54819ba8f37af2d0a7
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.database.Cursor;
25import android.database.MatrixCursor;
26import android.graphics.drawable.Drawable;
27import android.graphics.drawable.GradientDrawable;
28import android.net.Uri;
29import android.text.TextUtils;
30import android.text.format.Time;
31import android.util.Log;
32import android.view.animation.AlphaAnimation;
33import android.widget.ViewFlipper;
34
35import java.util.Calendar;
36import java.util.List;
37import java.util.Map;
38
39public class Utils {
40    private static final int CLEAR_ALPHA_MASK = 0x00FFFFFF;
41    private static final int HIGH_ALPHA = 255 << 24;
42    private static final int MED_ALPHA = 180 << 24;
43    private static final int LOW_ALPHA = 150 << 24;
44
45    protected static final String OPEN_EMAIL_MARKER = " <";
46    protected static final String CLOSE_EMAIL_MARKER = ">";
47
48    /* The corner should be rounded on the top right and bottom right */
49    private static final float[] CORNERS = new float[] {0, 0, 5, 5, 5, 5, 0, 0};
50
51
52    public static void startActivity(Context context, String className, long time) {
53        Intent intent = new Intent(Intent.ACTION_VIEW);
54
55        intent.setClassName(context, className);
56        intent.putExtra(EVENT_BEGIN_TIME, time);
57        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
58
59        context.startActivity(intent);
60    }
61
62    static String getSharedPreference(Context context, String key, String defaultValue) {
63        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
64        return prefs.getString(key, defaultValue);
65    }
66
67    static void setSharedPreference(Context context, String key, String value) {
68        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
69        SharedPreferences.Editor editor = prefs.edit();
70        editor.putString(key, value);
71        editor.commit();
72    }
73
74    static void setDefaultView(Context context, int viewId) {
75        String activityString = CalendarApplication.ACTIVITY_NAMES[viewId];
76
77        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
78        SharedPreferences.Editor editor = prefs.edit();
79        if (viewId == CalendarApplication.AGENDA_VIEW_ID ||
80                viewId == CalendarApplication.DAY_VIEW_ID) {
81            // Record the (new) detail start view only for Agenda and Day
82            editor.putString(CalendarPreferenceActivity.KEY_DETAILED_VIEW, activityString);
83        }
84
85        // Record the (new) start view
86        editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString);
87        editor.commit();
88    }
89
90    public static final Time timeFromIntent(Intent intent) {
91        Time time = new Time();
92        time.set(timeFromIntentInMillis(intent));
93        return time;
94    }
95
96    public static MatrixCursor matrixCursorFromCursor(Cursor cursor) {
97        MatrixCursor newCursor = new MatrixCursor(cursor.getColumnNames());
98        int numColumns = cursor.getColumnCount();
99        String data[] = new String[numColumns];
100        cursor.moveToPosition(-1);
101        while (cursor.moveToNext()) {
102            for (int i = 0; i < numColumns; i++) {
103                data[i] = cursor.getString(i);
104            }
105            newCursor.addRow(data);
106        }
107        return newCursor;
108    }
109
110    /**
111     * Compares two cursors to see if they contain the same data.
112     *
113     * @return Returns true of the cursors contain the same data and are not null, false
114     * otherwise
115     */
116    public static boolean compareCursors(Cursor c1, Cursor c2) {
117        if(c1 == null || c2 == null) {
118            return false;
119        }
120
121        int numColumns = c1.getColumnCount();
122        if (numColumns != c2.getColumnCount()) {
123            return false;
124        }
125
126        if (c1.getCount() != c2.getCount()) {
127            return false;
128        }
129
130        c1.moveToPosition(-1);
131        c2.moveToPosition(-1);
132        while(c1.moveToNext() && c2.moveToNext()) {
133            for(int i = 0; i < numColumns; i++) {
134                if(!TextUtils.equals(c1.getString(i), c2.getString(i))) {
135                    return false;
136                }
137            }
138        }
139
140        return true;
141    }
142
143    /**
144     * If the given intent specifies a time (in milliseconds since the epoch),
145     * then that time is returned. Otherwise, the current time is returned.
146     */
147    public static final long timeFromIntentInMillis(Intent intent) {
148        // If the time was specified, then use that.  Otherwise, use the current time.
149        Uri data = intent.getData();
150        long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
151        if (millis == -1 && data != null && data.isHierarchical()) {
152            List<String> path = data.getPathSegments();
153            if(path.size() == 2 && path.get(0).equals("time")) {
154                try {
155                    millis = Long.valueOf(data.getLastPathSegment());
156                } catch (NumberFormatException e) {
157                    Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time " +
158                            "found. Using current time.");
159                }
160            }
161        }
162        if (millis <= 0) {
163            millis = System.currentTimeMillis();
164        }
165        return millis;
166    }
167
168    public static final void applyAlphaAnimation(ViewFlipper v) {
169        AlphaAnimation in = new AlphaAnimation(0.0f, 1.0f);
170
171        in.setStartOffset(0);
172        in.setDuration(500);
173
174        AlphaAnimation out = new AlphaAnimation(1.0f, 0.0f);
175
176        out.setStartOffset(0);
177        out.setDuration(500);
178
179        v.setInAnimation(in);
180        v.setOutAnimation(out);
181    }
182
183    public static Drawable getColorChip(int color) {
184        /*
185         * We want the color chip to have a nice gradient using
186         * the color of the calendar. To do this we use a GradientDrawable.
187         * The color supplied has an alpha of FF so we first do:
188         * color & 0x00FFFFFF
189         * to clear the alpha. Then we add our alpha to it.
190         * We use 3 colors to get a step effect where it starts off very
191         * light and quickly becomes dark and then a slow transition to
192         * be even darker.
193         */
194        color &= CLEAR_ALPHA_MASK;
195        int startColor = color | HIGH_ALPHA;
196        int middleColor = color | MED_ALPHA;
197        int endColor = color | LOW_ALPHA;
198        int[] colors = new int[] {startColor, middleColor, endColor};
199        GradientDrawable d = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
200        d.setCornerRadii(CORNERS);
201        return d;
202    }
203
204    /**
205     * Formats the given Time object so that it gives the month and year
206     * (for example, "September 2007").
207     *
208     * @param time the time to format
209     * @return the string containing the weekday and the date
210     */
211    public static String formatMonthYear(Context context, Time time) {
212        return time.format(context.getResources().getString(R.string.month_year));
213    }
214
215    // TODO: replace this with the correct i18n way to do this
216    public static final String englishNthDay[] = {
217        "", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th",
218        "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th",
219        "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th",
220        "30th", "31st"
221    };
222
223    public static String formatNth(int nth) {
224        return "the " + englishNthDay[nth];
225    }
226
227    /**
228     * Returns a list joined together by the provided delimiter, for example,
229     * ["a", "b", "c"] could be joined into "a,b,c"
230     *
231     * @param things the things to join together
232     * @param delim the delimiter to use
233     * @return a string contained the things joined together
234     */
235    public static String join(List<?> things, String delim) {
236        StringBuilder builder = new StringBuilder();
237        boolean first = true;
238        for (Object thing : things) {
239            if (first) {
240                first = false;
241            } else {
242                builder.append(delim);
243            }
244            builder.append(thing.toString());
245        }
246        return builder.toString();
247    }
248
249    /**
250     * Sets the time to the beginning of the day (midnight) by clearing the
251     * hour, minute, and second fields.
252     */
253    static void setTimeToStartOfDay(Time time) {
254        time.second = 0;
255        time.minute = 0;
256        time.hour = 0;
257    }
258
259    /**
260     * Get first day of week as android.text.format.Time constant.
261     * @return the first day of week in android.text.format.Time
262     */
263    public static int getFirstDayOfWeek() {
264        int startDay = Calendar.getInstance().getFirstDayOfWeek();
265        if (startDay == Calendar.SATURDAY) {
266            return Time.SATURDAY;
267        } else if (startDay == Calendar.MONDAY) {
268            return Time.MONDAY;
269        } else {
270            return Time.SUNDAY;
271        }
272    }
273
274    /**
275     * Determine whether the column position is Saturday or not.
276     * @param column the column position
277     * @param firstDayOfWeek the first day of week in android.text.format.Time
278     * @return true if the column is Saturday position
279     */
280    public static boolean isSaturday(int column, int firstDayOfWeek) {
281        return (firstDayOfWeek == Time.SUNDAY && column == 6)
282            || (firstDayOfWeek == Time.MONDAY && column == 5)
283            || (firstDayOfWeek == Time.SATURDAY && column == 0);
284    }
285
286    /**
287     * Determine whether the column position is Sunday or not.
288     * @param column the column position
289     * @param firstDayOfWeek the first day of week in android.text.format.Time
290     * @return true if the column is Sunday position
291     */
292    public static boolean isSunday(int column, int firstDayOfWeek) {
293        return (firstDayOfWeek == Time.SUNDAY && column == 0)
294            || (firstDayOfWeek == Time.MONDAY && column == 6)
295            || (firstDayOfWeek == Time.SATURDAY && column == 1);
296    }
297
298    /**
299     * Scan through a cursor of calendars and check if names are duplicated.
300     *
301     * This travels a cursor containing calendar display names and fills in the provided map with
302     * whether or not each name is repeated.
303     * @param isDuplicateName The map to put the duplicate check results in.
304     * @param cursor The query of calendars to check
305     * @param nameIndex The column of the query that contains the display name
306     */
307    public static void checkForDuplicateNames(Map<String, Boolean> isDuplicateName, Cursor cursor,
308            int nameIndex) {
309        isDuplicateName.clear();
310        cursor.moveToPosition(-1);
311        while (cursor.moveToNext()) {
312            String displayName = cursor.getString(nameIndex);
313            // Set it to true if we've seen this name before, false otherwise
314            if (displayName != null) {
315                isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName));
316            }
317        }
318    }
319}
320