Merge "Adding AppWidget xml flag to make widget resizable"
diff --git a/src/com/android/calendar/AllInOneActivity.java b/src/com/android/calendar/AllInOneActivity.java
index 3d79ba2..93fc511 100644
--- a/src/com/android/calendar/AllInOneActivity.java
+++ b/src/com/android/calendar/AllInOneActivity.java
@@ -361,6 +361,8 @@
 
             Fragment selectCalendarsFrag = new SelectCalendarsFragment();
             ft.replace(R.id.calendar_list, selectCalendarsFrag);
+            mController.registerEventHandler(
+                    R.id.calendar_list, (EventHandler) selectCalendarsFrag);
         }
         if (!mIsMultipane || viewType == ViewType.EDIT) {
             mMiniMonth.setVisibility(View.GONE);
diff --git a/src/com/android/calendar/DayView.java b/src/com/android/calendar/DayView.java
index 17fc310..a65b72b 100644
--- a/src/com/android/calendar/DayView.java
+++ b/src/com/android/calendar/DayView.java
@@ -367,6 +367,7 @@
 //    private static int mCalendarHourSelected;
     private static int mMoreAlldayEventsTextAlpha = MORE_EVENTS_MAX_ALPHA;
 
+    private float mAnimationDistance = 0;
     private int mViewStartX;
     private int mViewStartY;
     private int mMaxViewStartY;
@@ -1539,7 +1540,11 @@
     }
 
     private View switchViews(boolean forward, float xOffSet, float width, float velocity) {
-        if (DEBUG) Log.d(TAG, "switchViews(" + forward + ")...");
+        mAnimationDistance = width - xOffSet;
+        if (DEBUG) {
+            Log.d(TAG, "switchViews(" + forward + ") O:" + xOffSet + " Dist:" + mAnimationDistance);
+        }
+
         float progress = Math.abs(xOffSet) / width;
         if (progress > 1.0f) {
             progress = 1.0f;
@@ -3532,10 +3537,7 @@
     }
 
     private void doScroll(MotionEvent e1, MotionEvent e2, float deltaX, float deltaY) {
-        if (isAnimating()) {
-            cancelAnimation();
-            return;
-        }
+        cancelAnimation();
 
         // Use the distance from the current point to the initial touch instead
         // of deltaX and deltaY to avoid accumulating floating-point rounding
@@ -3592,28 +3594,16 @@
         invalidate();
     }
 
-    private boolean isAnimating() {
-        Animation in = mViewSwitcher.getInAnimation();
-        if (in != null && in.hasStarted() && !in.hasEnded()) {
-            return true;
-        }
-        Animation out = mViewSwitcher.getOutAnimation();
-        if (out != null && out.hasStarted() && !out.hasEnded()) {
-            return true;
-        }
-        return false;
-    }
-
     private void cancelAnimation() {
         Animation in = mViewSwitcher.getInAnimation();
         if (in != null) {
-            in.cancel();
-            in.reset();
+            // cancel() doesn't terminate cleanly.
+            in.scaleCurrentDuration(0);
         }
         Animation out = mViewSwitcher.getOutAnimation();
         if (out != null) {
-            out.cancel();
-            out.reset();
+            // cancel() doesn't terminate cleanly.
+            out.scaleCurrentDuration(0);
         }
     }
 
@@ -4353,13 +4343,19 @@
     // The rest of this file was borrowed from Launcher2 - PagedView.java
     private static final int MINIMUM_SNAP_VELOCITY = 2200;
 
-    private static class ScrollInterpolator implements Interpolator {
+    private class ScrollInterpolator implements Interpolator {
         public ScrollInterpolator() {
         }
 
         public float getInterpolation(float t) {
             t -= 1.0f;
-            return t * t * t * t * t + 1;
+            t = t * t * t * t * t + 1;
+
+            if ((1 - t) * mAnimationDistance < 1) {
+                cancelAnimation();
+            }
+
+            return t;
         }
     }
 
diff --git a/src/com/android/calendar/event/EditEventView.java b/src/com/android/calendar/event/EditEventView.java
index 41f7d37..80f346e 100644
--- a/src/com/android/calendar/event/EditEventView.java
+++ b/src/com/android/calendar/event/EditEventView.java
@@ -65,6 +65,7 @@
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
+import android.widget.CalendarView;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.DatePicker;
@@ -535,9 +536,21 @@
         }
 
         public void onClick(View v) {
-            Log.d(TAG, "To Picker: " + mTime.year +  " " + mTime.month +  " " + mTime.monthDay);
-            new DatePickerDialog(
-                    mActivity, new DateListener(v), mTime.year, mTime.month, mTime.monthDay).show();
+            DatePickerDialog dpd = new DatePickerDialog(
+                    mActivity, new DateListener(v), mTime.year, mTime.month, mTime.monthDay);
+            CalendarView cv = dpd.getDatePicker().getCalendarView();
+            cv.setShowWeekNumber(Utils.getShowWeekNumber(mActivity));
+            int startOfWeek = Utils.getFirstDayOfWeek(mActivity);
+            // Utils returns Time days while CalendarView wants Calendar days
+            if (startOfWeek == Time.SATURDAY) {
+                startOfWeek = Calendar.SATURDAY;
+            } else if (startOfWeek == Time.SUNDAY) {
+                startOfWeek = Calendar.SUNDAY;
+            } else {
+                startOfWeek = Calendar.MONDAY;
+            }
+            cv.setFirstDayOfWeek(startOfWeek);
+            dpd.show();
         }
     }
 
diff --git a/src/com/android/calendar/selectcalendars/SelectCalendarsFragment.java b/src/com/android/calendar/selectcalendars/SelectCalendarsFragment.java
index 41682a0..ce496eb 100644
--- a/src/com/android/calendar/selectcalendars/SelectCalendarsFragment.java
+++ b/src/com/android/calendar/selectcalendars/SelectCalendarsFragment.java
@@ -17,7 +17,10 @@
 package com.android.calendar.selectcalendars;
 
 import com.android.calendar.AsyncQueryService;
+import com.android.calendar.CalendarController.EventInfo;
+import com.android.calendar.CalendarController.EventType;
 import com.android.calendar.R;
+import com.android.calendar.CalendarController;
 
 import android.app.Activity;
 import android.app.Fragment;
@@ -36,7 +39,7 @@
 
 
 public class SelectCalendarsFragment extends Fragment
-    implements AdapterView.OnItemClickListener {
+        implements AdapterView.OnItemClickListener, CalendarController.EventHandler {
 
     private static final String TAG = "Calendar";
     private static final String EXPANDED_KEY = "is_expanded";
@@ -71,6 +74,7 @@
     private Activity mContext;
     private AsyncQueryService mService;
     private Object[] mTempRow = new Object[PROJECTION.length];
+    private boolean mIsPaused = true;
 
     @Override
     public void onAttach(Activity activity) {
@@ -80,7 +84,6 @@
             @Override
             protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
                 mCursor = cursor;
-                Log.d(TAG, "adding " + cursor.getCount() + " calendars.");
                 mAdapter.changeCursor(cursor);
             }
         };
@@ -124,6 +127,13 @@
         mQueryToken = mService.getNextToken();
         mService.startQuery(mQueryToken, null, Calendars.CONTENT_URI, PROJECTION, SELECTION,
                 SELECTION_ARGS, Calendars._SYNC_ACCOUNT);
+        mIsPaused = false;
+    }
+
+    @Override
+    public void onPause() {
+        mIsPaused = true;
+        super.onPause();
     }
 
     /*
@@ -140,4 +150,26 @@
         mService.startUpdate(mUpdateToken, null, uri, values, null, null, 0);
         mAdapter.setVisible(position, visibility);
     }
+
+    @Override
+    public void eventsChanged() {
+        if (mService != null) {
+            mService.cancelOperation(mQueryToken);
+            mQueryToken = mService.getNextToken();
+            mService.startQuery(mQueryToken, null, Calendars.CONTENT_URI, PROJECTION, SELECTION,
+                    SELECTION_ARGS, Calendars._SYNC_ACCOUNT);
+        }
+    }
+
+    @Override
+    public long getSupportedEventTypes() {
+        return EventType.EVENTS_CHANGED;
+    }
+
+    @Override
+    public void handleEvent(EventInfo event) {
+        if (event.eventType == EventType.EVENTS_CHANGED) {
+            eventsChanged();
+        }
+    }
 }
diff --git a/src/com/android/calendar/selectcalendars/SelectCalendarsSimpleAdapter.java b/src/com/android/calendar/selectcalendars/SelectCalendarsSimpleAdapter.java
index 1e25233..35452ee 100644
--- a/src/com/android/calendar/selectcalendars/SelectCalendarsSimpleAdapter.java
+++ b/src/com/android/calendar/selectcalendars/SelectCalendarsSimpleAdapter.java
@@ -262,4 +262,8 @@
         return mData[position].selected ? 1 : 0;
     }
 
+    @Override
+    public boolean hasStableIds() {
+        return true;
+    }
 }