OSDN Git Service

Quick fix to add safer time checking for time from intents.
[android-x86/packages-apps-Calendar.git] / src / com / android / calendar / Utils.java
index c282798..aa48fe0 100644 (file)
@@ -21,12 +21,16 @@ import static android.provider.Calendar.EVENT_BEGIN_TIME;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
-import android.content.res.Resources;
+import android.net.Uri;
 import android.preference.PreferenceManager;
 import android.text.format.Time;
+import android.util.Log;
 import android.view.animation.AlphaAnimation;
 import android.widget.ViewFlipper;
 
+import java.util.Calendar;
+import java.util.List;
+
 public class Utils {
     public static void startActivity(Context context, String className, long time) {
         Intent intent = new Intent(Intent.ACTION_VIEW);
@@ -38,10 +42,22 @@ public class Utils {
         context.startActivity(intent);
     }
 
+    static String getSharedPreference(Context context, String key, String defaultValue) {
+        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
+        return prefs.getString(key, defaultValue);
+    }
+
+    static void setSharedPreference(Context context, String key, String value) {
+        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
+        SharedPreferences.Editor editor = prefs.edit();
+        editor.putString(key, value);
+        editor.commit();
+    }
+
     static void setDefaultView(Context context, int viewId) {
         String activityString = CalendarApplication.ACTIVITY_NAMES[viewId];
 
-        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
         SharedPreferences.Editor editor = prefs.edit();
         if (viewId == CalendarApplication.AGENDA_VIEW_ID ||
                 viewId == CalendarApplication.DAY_VIEW_ID) {
@@ -66,8 +82,20 @@ public class Utils {
      */
     public static final long timeFromIntentInMillis(Intent intent) {
         // If the time was specified, then use that.  Otherwise, use the current time.
+        Uri data = intent.getData();
         long millis = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
-        if (millis == -1) {
+        if (millis == -1 && data != null && data.isHierarchical()) {
+            List<String> path = data.getPathSegments();
+            if(path.size() == 3 && path.get(1).equals("time")) {
+                try {
+                    millis = Long.valueOf(data.getLastPathSegment());
+                } catch (NumberFormatException e) {
+                    Log.i("Calendar", "timeFromIntentInMillis: Data existed but no valid time " +
+                            "found. Using current time.");
+                }
+            }
+        }
+        if (millis <= 0) {
             millis = System.currentTimeMillis();
         }
         return millis;
@@ -95,9 +123,8 @@ public class Utils {
      * @param time the time to format
      * @return the string containing the weekday and the date
      */
-    public static String formatMonthYear(Time time) {
-        Resources res = Resources.getSystem();
-        return time.format(res.getString(com.android.internal.R.string.month_year));
+    public static String formatMonthYear(Context context, Time time) {
+        return time.format(context.getResources().getString(R.string.month_year));
     }
 
     // TODO: replace this with the correct i18n way to do this
@@ -121,4 +148,43 @@ public class Utils {
         time.minute = 0;
         time.hour = 0;
     }
+
+    /**
+     * Get first day of week as android.text.format.Time constant.
+     * @return the first day of week in android.text.format.Time
+     */
+    public static int getFirstDayOfWeek() {
+        int startDay = Calendar.getInstance().getFirstDayOfWeek();
+        if (startDay == Calendar.SATURDAY) {
+            return Time.SATURDAY;
+        } else if (startDay == Calendar.MONDAY) {
+            return Time.MONDAY;
+        } else {
+            return Time.SUNDAY;
+        }
+    }
+
+    /**
+     * Determine whether the column position is Saturday or not.
+     * @param column the column position
+     * @param firstDayOfWeek the first day of week in android.text.format.Time
+     * @return true if the column is Saturday position
+     */
+    public static boolean isSaturday(int column, int firstDayOfWeek) {
+        return (firstDayOfWeek == Time.SUNDAY && column == 6)
+            || (firstDayOfWeek == Time.MONDAY && column == 5)
+            || (firstDayOfWeek == Time.SATURDAY && column == 0);
+    }
+
+    /**
+     * Determine whether the column position is Sunday or not.
+     * @param column the column position
+     * @param firstDayOfWeek the first day of week in android.text.format.Time
+     * @return true if the column is Sunday position
+     */
+    public static boolean isSunday(int column, int firstDayOfWeek) {
+        return (firstDayOfWeek == Time.SUNDAY && column == 0)
+            || (firstDayOfWeek == Time.MONDAY && column == 6)
+            || (firstDayOfWeek == Time.SATURDAY && column == 1);
+    }
 }