1 /* 2 * Copyright (C) 2007 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.app.Activity; 22 import android.content.BroadcastReceiver; 23 import android.content.ContentResolver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.content.SharedPreferences; 28 import android.database.ContentObserver; 29 import android.os.Bundle; 30 import android.os.Handler; 31 import android.provider.Calendar.Events; 32 import android.text.format.Time; 33 import android.util.Log; 34 import android.view.KeyEvent; 35 import android.view.Menu; 36 import android.view.MenuItem; 37 38 import dalvik.system.VMRuntime; 39 40 public class AgendaActivity extends Activity implements Navigator { 41 42 private static final String TAG = "AgendaActivity"; 43 44 private static boolean DEBUG = false; 45 46 protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time"; 47 48 private static final long INITIAL_HEAP_SIZE = 4*1024*1024; 49 50 private ContentResolver mContentResolver; 51 52 private AgendaListView mAgendaListView; 53 54 private Time mTime; 55 56 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 57 @Override 58 public void onReceive(Context context, Intent intent) { 59 String action = intent.getAction(); 60 if (action.equals(Intent.ACTION_TIME_CHANGED) 61 || action.equals(Intent.ACTION_DATE_CHANGED) 62 || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 63 mAgendaListView.refresh(true); 64 } 65 } 66 }; 67 68 private ContentObserver mObserver = new ContentObserver(new Handler()) { 69 @Override 70 public boolean deliverSelfNotifications() { 71 return true; 72 } 73 74 @Override 75 public void onChange(boolean selfChange) { 76 mAgendaListView.refresh(true); 77 } 78 }; 79 80 @Override 81 protected void onCreate(Bundle icicle) { 82 super.onCreate(icicle); 83 84 // Eliminate extra GCs during startup by setting the initial heap size to 4MB. 85 // TODO: We should restore the old heap size once the activity reaches the idle state 86 VMRuntime.getRuntime().setMinimumHeapSize(INITIAL_HEAP_SIZE); 87 88 mAgendaListView = new AgendaListView(this); 89 setContentView(mAgendaListView); 90 91 mContentResolver = getContentResolver(); 92 93 setTitle(R.string.agenda_view); 94 95 long millis = 0; 96 mTime = new Time(); 97 if (icicle != null) { 98 // Returns 0 if key not found 99 millis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME); 100 if (DEBUG) { 101 Log.v(TAG, "Restore value from icicle: " + millis); 102 } 103 } 104 105 if (millis == 0) { 106 // Returns 0 if key not found 107 millis = getIntent().getLongExtra(EVENT_BEGIN_TIME, 0); 108 if (DEBUG) { 109 Time time = new Time(); 110 time.set(millis); 111 Log.v(TAG, "Restore value from intent: " + time.toString()); 112 } 113 } 114 115 if (millis == 0) { 116 if (DEBUG) { 117 Log.v(TAG, "Restored from current time"); 118 } 119 millis = System.currentTimeMillis(); 120 } 121 mTime.set(millis); 122 } 123 124 @Override 125 protected void onNewIntent(Intent intent) { 126 long time = Utils.timeFromIntentInMillis(intent); 127 if (time > 0) { 128 mTime.set(time); 129 goTo(mTime, false); 130 } 131 } 132 133 @Override 134 protected void onResume() { 135 super.onResume(); 136 if (DEBUG) { 137 Log.v(TAG, "OnResume to " + mTime.toString()); 138 } 139 140 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences( 141 getApplicationContext()); 142 boolean hideDeclined = prefs.getBoolean( 143 CalendarPreferenceActivity.KEY_HIDE_DECLINED, false); 144 145 mAgendaListView.setHideDeclinedEvents(hideDeclined); 146 mAgendaListView.goTo(mTime, true); 147 mAgendaListView.onResume(); 148 149 // Register for Intent broadcasts 150 IntentFilter filter = new IntentFilter(); 151 filter.addAction(Intent.ACTION_TIME_CHANGED); 152 filter.addAction(Intent.ACTION_DATE_CHANGED); 153 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 154 registerReceiver(mIntentReceiver, filter); 155 156 mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver); 157 } 158 159 @Override 160 protected void onSaveInstanceState(Bundle outState) { 161 super.onSaveInstanceState(outState); 162 163 long firstVisibleTime = mAgendaListView.getFirstVisibleTime(); 164 if (firstVisibleTime > 0) { 165 mTime.set(firstVisibleTime); 166 outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime); 167 if (DEBUG) { 168 Log.v(TAG, "onSaveInstanceState " + mTime.toString()); 169 } 170 } 171 } 172 173 @Override 174 protected void onPause() { 175 super.onPause(); 176 177 mAgendaListView.onPause(); 178 mContentResolver.unregisterContentObserver(mObserver); 179 unregisterReceiver(mIntentReceiver); 180 181 // Record Agenda View as the (new) default detailed view. 182 Utils.setDefaultView(this, CalendarApplication.AGENDA_VIEW_ID); 183 } 184 185 @Override 186 public boolean onPrepareOptionsMenu(Menu menu) { 187 MenuHelper.onPrepareOptionsMenu(this, menu); 188 return super.onPrepareOptionsMenu(menu); 189 } 190 191 @Override 192 public boolean onCreateOptionsMenu(Menu menu) { 193 MenuHelper.onCreateOptionsMenu(menu); 194 return super.onCreateOptionsMenu(menu); 195 } 196 197 @Override 198 public boolean onOptionsItemSelected(MenuItem item) { 199 MenuHelper.onOptionsItemSelected(this, item, this); 200 return super.onOptionsItemSelected(item); 201 } 202 203 @Override 204 public boolean onKeyDown(int keyCode, KeyEvent event) { 205 switch (keyCode) { 206 case KeyEvent.KEYCODE_DEL: 207 // Delete the currently selected event (if any) 208 mAgendaListView.deleteSelectedEvent(); 209 break; 210 } 211 return super.onKeyDown(keyCode, event); 212 } 213 214 /* Navigator interface methods */ 215 public void goToToday() { 216 Time now = new Time(); 217 now.setToNow(); 218 mAgendaListView.goTo(now, true); // Force refresh 219 } 220 221 public void goTo(Time time, boolean animate) { 222 mAgendaListView.goTo(time, false); 223 } 224 225 public long getSelectedTime() { 226 return mAgendaListView.getSelectedTime(); 227 } 228 229 public boolean getAllDay() { 230 return false; 231 } 232 } 233 234