Home | History | Annotate | Download | only in calendar
      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 
     17 package com.android.calendar;
     18 
     19 import static android.provider.Calendar.EVENT_BEGIN_TIME;
     20 
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.database.Cursor;
     25 import android.database.MatrixCursor;
     26 import android.graphics.drawable.Drawable;
     27 import android.graphics.drawable.GradientDrawable;
     28 import android.net.Uri;
     29 import android.text.TextUtils;
     30 import android.text.format.Time;
     31 import android.util.Log;
     32 import android.view.animation.AlphaAnimation;
     33 import android.widget.ViewFlipper;
     34 
     35 import java.util.Calendar;
     36 import java.util.List;
     37 import java.util.Map;
     38 
     39 public 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      * Sets the time to the beginning of the day (midnight) by clearing the
    229      * hour, minute, and second fields.
    230      */
    231     static void setTimeToStartOfDay(Time time) {
    232         time.second = 0;
    233         time.minute = 0;
    234         time.hour = 0;
    235     }
    236 
    237     /**
    238      * Get first day of week as android.text.format.Time constant.
    239      * @return the first day of week in android.text.format.Time
    240      */
    241     public static int getFirstDayOfWeek() {
    242         int startDay = Calendar.getInstance().getFirstDayOfWeek();
    243         if (startDay == Calendar.SATURDAY) {
    244             return Time.SATURDAY;
    245         } else if (startDay == Calendar.MONDAY) {
    246             return Time.MONDAY;
    247         } else {
    248             return Time.SUNDAY;
    249         }
    250     }
    251 
    252     /**
    253      * Determine whether the column position is Saturday or not.
    254      * @param column the column position
    255      * @param firstDayOfWeek the first day of week in android.text.format.Time
    256      * @return true if the column is Saturday position
    257      */
    258     public static boolean isSaturday(int column, int firstDayOfWeek) {
    259         return (firstDayOfWeek == Time.SUNDAY && column == 6)
    260             || (firstDayOfWeek == Time.MONDAY && column == 5)
    261             || (firstDayOfWeek == Time.SATURDAY && column == 0);
    262     }
    263 
    264     /**
    265      * Determine whether the column position is Sunday or not.
    266      * @param column the column position
    267      * @param firstDayOfWeek the first day of week in android.text.format.Time
    268      * @return true if the column is Sunday position
    269      */
    270     public static boolean isSunday(int column, int firstDayOfWeek) {
    271         return (firstDayOfWeek == Time.SUNDAY && column == 0)
    272             || (firstDayOfWeek == Time.MONDAY && column == 6)
    273             || (firstDayOfWeek == Time.SATURDAY && column == 1);
    274     }
    275 
    276     /**
    277      * Scan through a cursor of calendars and check if names are duplicated.
    278      *
    279      * This travels a cursor containing calendar display names and fills in the provided map with
    280      * whether or not each name is repeated.
    281      * @param isDuplicateName The map to put the duplicate check results in.
    282      * @param cursor The query of calendars to check
    283      * @param nameIndex The column of the query that contains the display name
    284      */
    285     public static void checkForDuplicateNames(Map<String, Boolean> isDuplicateName, Cursor cursor,
    286             int nameIndex) {
    287         isDuplicateName.clear();
    288         cursor.moveToPosition(-1);
    289         while (cursor.moveToNext()) {
    290             String displayName = cursor.getString(nameIndex);
    291             // Set it to true if we've seen this name before, false otherwise
    292             if (displayName != null) {
    293                 isDuplicateName.put(displayName, isDuplicateName.containsKey(displayName));
    294             }
    295         }
    296     }
    297 }
    298