diff options
Diffstat (limited to 'src/com/android/calendar/AgendaActivity.java')
-rw-r--r-- | src/com/android/calendar/AgendaActivity.java | 316 |
1 files changed, 60 insertions, 256 deletions
diff --git a/src/com/android/calendar/AgendaActivity.java b/src/com/android/calendar/AgendaActivity.java index 9f5a53ed..15c9387f 100644 --- a/src/com/android/calendar/AgendaActivity.java +++ b/src/com/android/calendar/AgendaActivity.java @@ -16,162 +16,43 @@ package com.android.calendar; +import static android.provider.Calendar.EVENT_BEGIN_TIME; +import dalvik.system.VMRuntime; + import android.app.Activity; -import android.content.AsyncQueryHandler; import android.content.BroadcastReceiver; import android.content.ContentResolver; -import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.ContentObserver; -import android.database.Cursor; -import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; -import android.provider.Calendar; -import android.provider.Calendar.Attendees; -import android.provider.Calendar.Calendars; import android.provider.Calendar.Events; -import android.provider.Calendar.Instances; import android.text.format.Time; +import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.Window; -import android.widget.AdapterView; -import android.widget.ListView; -import android.widget.ViewSwitcher; -import dalvik.system.VMRuntime; -public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory, Navigator { +public class AgendaActivity extends Activity implements Navigator { - protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time"; + private static final String TAG = "AgendaActivity"; - static final String[] PROJECTION = new String[] { - Instances._ID, // 0 - Instances.TITLE, // 1 - Instances.EVENT_LOCATION, // 2 - Instances.ALL_DAY, // 3 - Instances.HAS_ALARM, // 4 - Instances.COLOR, // 5 - Instances.RRULE, // 6 - Instances.BEGIN, // 7 - Instances.END, // 8 - Instances.EVENT_ID, // 9 - Instances.START_DAY, // 10 Julian start day - Instances.END_DAY, // 11 Julian end day - Instances.SELF_ATTENDEE_STATUS, // 12 - }; + private static boolean DEBUG = false; - public static final int INDEX_TITLE = 1; - public static final int INDEX_EVENT_LOCATION = 2; - public static final int INDEX_ALL_DAY = 3; - public static final int INDEX_HAS_ALARM = 4; - public static final int INDEX_COLOR = 5; - public static final int INDEX_RRULE = 6; - public static final int INDEX_BEGIN = 7; - public static final int INDEX_END = 8; - public static final int INDEX_EVENT_ID = 9; - public static final int INDEX_START_DAY = 10; - public static final int INDEX_END_DAY = 11; - public static final int INDEX_SELF_ATTENDEE_STATUS = 12; - - public static final String AGENDA_SORT_ORDER = "startDay ASC, begin ASC, title ASC"; + protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time"; private static final long INITIAL_HEAP_SIZE = 4*1024*1024; private ContentResolver mContentResolver; - private ViewSwitcher mViewSwitcher; + private AgendaListView mAgendaListView; - private QueryHandler mQueryHandler; - private DeleteEventHelper mDeleteEventHelper; private Time mTime; - /** - * This records the start time parameter for the last query sent to the - * AsyncQueryHandler so that we don't send it duplicate query requests. - */ - private Time mLastQueryTime = new Time(); - - private class QueryHandler extends AsyncQueryHandler { - public QueryHandler(ContentResolver cr) { - super(cr); - } - - @Override - protected void onQueryComplete(int token, Object cookie, Cursor cursor) { - - // Only set mCursor if the Activity is not finishing. Otherwise close the cursor. - if (!isFinishing()) { - AgendaListView next = (AgendaListView) mViewSwitcher.getNextView(); - next.setCursor(cursor); - mViewSwitcher.showNext(); - selectTime(); - } else { - cursor.close(); - } - } - } - - private class AgendaListView extends ListView { - private Cursor mCursor; - private AgendaByDayAdapter mDayAdapter; - private AgendaAdapter mAdapter; - - public AgendaListView(Context context) { - super(context, null); - setOnItemClickListener(mOnItemClickListener); - setChoiceMode(ListView.CHOICE_MODE_SINGLE); - mAdapter = new AgendaAdapter(AgendaActivity.this, R.layout.agenda_item); - mDayAdapter = new AgendaByDayAdapter(AgendaActivity.this, mAdapter); - } - - public void setCursor(Cursor cursor) { - if (mCursor != null) { - mCursor.close(); - } - mCursor = cursor; - mDayAdapter.calculateDays(cursor); - mAdapter.changeCursor(cursor); - setAdapter(mDayAdapter); - } - - public Cursor getCursor() { - return mCursor; - } - - public AgendaByDayAdapter getDayAdapter() { - return mDayAdapter; - } - - @Override protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - if (mCursor != null) { - mCursor.close(); - } - } - - private OnItemClickListener mOnItemClickListener = new OnItemClickListener() { - public void onItemClick(AdapterView a, View v, int position, long id) { - if (id != -1) { - // Switch to the EventInfo view - mCursor.moveToPosition(mDayAdapter.getCursorPosition(position)); - long eventId = mCursor.getLong(INDEX_EVENT_ID); - Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventId); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - intent.putExtra(Calendar.EVENT_BEGIN_TIME, mCursor.getLong(INDEX_BEGIN)); - intent.putExtra(Calendar.EVENT_END_TIME, mCursor.getLong(INDEX_END)); - startActivity(intent); - } - } - }; - } - private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -179,8 +60,7 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory if (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_DATE_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { - clearLastQueryTime(); - renewCursor(); + mAgendaListView.refresh(true); } } }; @@ -193,8 +73,7 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory @Override public void onChange(boolean selfChange) { - clearLastQueryTime(); - renewCursor(); + mAgendaListView.refresh(true); } }; @@ -206,26 +85,42 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory // TODO: We should restore the old heap size once the activity reaches the idle state VMRuntime.getRuntime().setMinimumHeapSize(INITIAL_HEAP_SIZE); - setContentView(R.layout.agenda_activity); + mAgendaListView = new AgendaListView(this); + setContentView(mAgendaListView); mContentResolver = getContentResolver(); - mQueryHandler = new QueryHandler(mContentResolver); - // Preserve the same month and event selection if this activity is - // being restored due to an orientation change + setTitle(R.string.agenda_view); + + long millis = 0; mTime = new Time(); if (icicle != null) { - mTime.set(icicle.getLong(BUNDLE_KEY_RESTORE_TIME)); - } else { - mTime.set(Utils.timeFromIntent(getIntent())); + // Returns 0 if key not found + millis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME); + if (DEBUG) { + Log.v(TAG, "Restore value from icicle: " + millis); + } } - setTitle(R.string.agenda_view); - mViewSwitcher = (ViewSwitcher) findViewById(R.id.switcher); - mViewSwitcher.setFactory(this); + if (millis == 0) { + // Returns 0 if key not found + millis = getIntent().getLongExtra(EVENT_BEGIN_TIME, 0); + if (DEBUG) { + Log.v(TAG, "Restore value from intent: " + millis); + } + } + + if (millis == 0) { + if (DEBUG) { + Log.v(TAG, "Restored from current time"); + } + millis = System.currentTimeMillis(); + } + mTime.set(millis); // Record Agenda View as the (new) default detailed view. - String activityString = CalendarApplication.ACTIVITY_NAMES[CalendarApplication.AGENDA_VIEW_ID]; + String activityString = + CalendarApplication.ACTIVITY_NAMES[CalendarApplication.AGENDA_VIEW_ID]; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = prefs.edit(); editor.putString(CalendarPreferenceActivity.KEY_DETAILED_VIEW, activityString); @@ -233,16 +128,22 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory // Record Agenda View as the (new) start view editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString); editor.commit(); - - mDeleteEventHelper = new DeleteEventHelper(this, false /* don't exit when done */); } @Override protected void onResume() { super.onResume(); + if (DEBUG) { + Log.v(TAG, "OnResume to " + mTime.toString()); + } + + SharedPreferences prefs = + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + boolean hideDeclined = prefs + .getBoolean(CalendarPreferenceActivity.KEY_HIDE_DECLINED, false); - clearLastQueryTime(); - renewCursor(); + mAgendaListView.setHideDeclinedEvents(hideDeclined); + mAgendaListView.goTo(mTime, true); // Register for Intent broadcasts IntentFilter filter = new IntentFilter(); @@ -258,7 +159,14 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putLong(BUNDLE_KEY_RESTORE_TIME, getSelectedTime()); + long firstVisibleTime = mAgendaListView.getFirstVisibleTime(); + if (firstVisibleTime >= 0) { + mTime.set(firstVisibleTime); + outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime); + if (DEBUG) { + Log.v(TAG, "onSaveInstanceState " + mTime.toString()); + } + } } @Override @@ -290,22 +198,9 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { - case KeyEvent.KEYCODE_DEL: { + case KeyEvent.KEYCODE_DEL: // Delete the currently selected event (if any) - AgendaListView current = (AgendaListView) mViewSwitcher.getCurrentView(); - Cursor cursor = current.getCursor(); - if (cursor != null) { - int position = current.getSelectedItemPosition(); - position = current.getDayAdapter().getCursorPosition(position); - if (position >= 0) { - cursor.moveToPosition(position); - long begin = cursor.getLong(INDEX_BEGIN); - long end = cursor.getLong(INDEX_END); - long eventId = cursor.getLong(INDEX_EVENT_ID); - mDeleteEventHelper.delete(begin, end, eventId, -1); - } - } - } + mAgendaListView.deleteSelectedEvent(); break; case KeyEvent.KEYCODE_BACK: @@ -315,81 +210,6 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory return super.onKeyDown(keyCode, event); } - /** - * Clears the cached value for the last query time so that renewCursor() - * will force a requery of the Calendar events. - */ - private void clearLastQueryTime() { - mLastQueryTime.year = 0; - mLastQueryTime.month = 0; - } - - private void renewCursor() { - // Avoid batching up repeated queries for the same month. This can - // happen if the user scrolls with the trackball too fast. - if (mLastQueryTime.month == mTime.month && mLastQueryTime.year == mTime.year) { - return; - } - - // Query all instances for the current month - Time time = new Time(); - time.year = mTime.year; - time.month = mTime.month; - long start = time.normalize(true); - - time.month++; - long end = time.normalize(true); - - StringBuilder path = new StringBuilder(); - path.append(start); - path.append('/'); - path.append(end); - - // Respect the preference to show/hide declined events - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - boolean hideDeclined = prefs.getBoolean(CalendarPreferenceActivity.KEY_HIDE_DECLINED, - false); - - Uri uri = Uri.withAppendedPath(Instances.CONTENT_URI, path.toString()); - - String selection; - if (hideDeclined) { - selection = Calendars.SELECTED + "=1 AND " + - Instances.SELF_ATTENDEE_STATUS + "!=" + Attendees.ATTENDEE_STATUS_DECLINED; - } else { - selection = Calendars.SELECTED + "=1"; - } - - // Cancel any previous queries that haven't started yet. This - // isn't likely to happen since we already avoid sending - // a duplicate query for the same month as the previous query. - // But if the user quickly wiggles the trackball back and forth, - // he could generate a stream of queries. - mQueryHandler.cancelOperation(0); - - mLastQueryTime.month = mTime.month; - mLastQueryTime.year = mTime.year; - mQueryHandler.startQuery(0, null, uri, PROJECTION, selection, null, - AGENDA_SORT_ORDER); - } - - private void selectTime() { - // Selects the first event of the day - AgendaListView current = (AgendaListView) mViewSwitcher.getCurrentView(); - if (current.getCursor() == null) { - return; - } - - int position = current.getDayAdapter().findDayPositionNearestTime(mTime); - current.setSelection(position); - } - - /* ViewSwitcher.ViewFactory interface methods */ - public View makeView() { - AgendaListView agendaListView = new AgendaListView(this); - return agendaListView; - } - /* Navigator interface methods */ public void goToToday() { Time now = new Time(); @@ -398,27 +218,11 @@ public class AgendaActivity extends Activity implements ViewSwitcher.ViewFactory } public void goTo(Time time) { - if (mTime.year == time.year && mTime.month == time.month) { - mTime = time; - selectTime(); - } else { - mTime = time; - renewCursor(); - } + mAgendaListView.goTo(time, false); } public long getSelectedTime() { - // Update the current time based on the selected event - AgendaListView current = (AgendaListView) mViewSwitcher.getCurrentView(); - int position = current.getSelectedItemPosition(); - position = current.getDayAdapter().getCursorPosition(position); - Cursor cursor = current.getCursor(); - if (position >= 0 && position < cursor.getCount()) { - cursor.moveToPosition(position); - mTime.set(cursor.getLong(INDEX_BEGIN)); - } - - return mTime.toMillis(true); + return mAgendaListView.getSelectedTime(); } public boolean getAllDay() { |