2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.calendar;
19 import static android.provider.Calendar.EVENT_BEGIN_TIME;
20 import dalvik.system.VMRuntime;
22 import android.app.Activity;
23 import android.content.BroadcastReceiver;
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.SharedPreferences;
29 import android.database.ContentObserver;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.preference.PreferenceManager;
33 import android.provider.Calendar.Events;
34 import android.text.format.Time;
35 import android.util.Log;
36 import android.view.KeyEvent;
37 import android.view.Menu;
38 import android.view.MenuItem;
40 public class AgendaActivity extends Activity implements Navigator {
42 private static final String TAG = "AgendaActivity";
44 private static boolean DEBUG = false;
46 protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
48 private static final long INITIAL_HEAP_SIZE = 4*1024*1024;
50 private ContentResolver mContentResolver;
52 private AgendaListView mAgendaListView;
56 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
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);
68 private ContentObserver mObserver = new ContentObserver(new Handler()) {
70 public boolean deliverSelfNotifications() {
75 public void onChange(boolean selfChange) {
76 mAgendaListView.refresh(true);
81 protected void onCreate(Bundle icicle) {
82 super.onCreate(icicle);
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);
88 mAgendaListView = new AgendaListView(this);
89 setContentView(mAgendaListView);
91 mContentResolver = getContentResolver();
93 setTitle(R.string.agenda_view);
98 // Returns 0 if key not found
99 millis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
101 Log.v(TAG, "Restore value from icicle: " + millis);
106 // Returns 0 if key not found
107 millis = getIntent().getLongExtra(EVENT_BEGIN_TIME, 0);
109 Log.v(TAG, "Restore value from intent: " + millis);
115 Log.v(TAG, "Restored from current time");
117 millis = System.currentTimeMillis();
121 // Record Agenda View as the (new) default detailed view.
122 String activityString =
123 CalendarApplication.ACTIVITY_NAMES[CalendarApplication.AGENDA_VIEW_ID];
124 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
125 SharedPreferences.Editor editor = prefs.edit();
126 editor.putString(CalendarPreferenceActivity.KEY_DETAILED_VIEW, activityString);
128 // Record Agenda View as the (new) start view
129 editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString);
134 protected void onResume() {
137 Log.v(TAG, "OnResume to " + mTime.toString());
140 SharedPreferences prefs =
141 PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
142 boolean hideDeclined = prefs
143 .getBoolean(CalendarPreferenceActivity.KEY_HIDE_DECLINED, false);
145 mAgendaListView.setHideDeclinedEvents(hideDeclined);
146 mAgendaListView.goTo(mTime, true);
148 // Register for Intent broadcasts
149 IntentFilter filter = new IntentFilter();
150 filter.addAction(Intent.ACTION_TIME_CHANGED);
151 filter.addAction(Intent.ACTION_DATE_CHANGED);
152 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
153 registerReceiver(mIntentReceiver, filter);
155 mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver);
159 protected void onSaveInstanceState(Bundle outState) {
160 super.onSaveInstanceState(outState);
162 long firstVisibleTime = mAgendaListView.getFirstVisibleTime();
163 if (firstVisibleTime >= 0) {
164 mTime.set(firstVisibleTime);
165 outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime);
167 Log.v(TAG, "onSaveInstanceState " + mTime.toString());
173 protected void onPause() {
176 mContentResolver.unregisterContentObserver(mObserver);
177 unregisterReceiver(mIntentReceiver);
181 public boolean onPrepareOptionsMenu(Menu menu) {
182 MenuHelper.onPrepareOptionsMenu(this, menu);
183 return super.onPrepareOptionsMenu(menu);
187 public boolean onCreateOptionsMenu(Menu menu) {
188 MenuHelper.onCreateOptionsMenu(menu);
189 return super.onCreateOptionsMenu(menu);
193 public boolean onOptionsItemSelected(MenuItem item) {
194 MenuHelper.onOptionsItemSelected(this, item, this);
195 return super.onOptionsItemSelected(item);
199 public boolean onKeyDown(int keyCode, KeyEvent event) {
201 case KeyEvent.KEYCODE_DEL:
202 // Delete the currently selected event (if any)
203 mAgendaListView.deleteSelectedEvent();
206 case KeyEvent.KEYCODE_BACK:
210 return super.onKeyDown(keyCode, event);
213 /* Navigator interface methods */
214 public void goToToday() {
215 Time now = new Time();
216 now.set(System.currentTimeMillis());
220 public void goTo(Time time) {
221 mAgendaListView.goTo(time, false);
224 public long getSelectedTime() {
225 return mAgendaListView.getSelectedTime();
228 public boolean getAllDay() {