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;
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.preference.PreferenceManager;
32 import android.provider.Calendar.Events;
33 import android.text.format.Time;
34 import android.util.Log;
35 import android.view.KeyEvent;
36 import android.view.Menu;
37 import android.view.MenuItem;
39 import dalvik.system.VMRuntime;
41 public class AgendaActivity extends Activity implements Navigator {
43 private static final String TAG = "AgendaActivity";
45 private static boolean DEBUG = false;
47 protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
49 private static final long INITIAL_HEAP_SIZE = 4*1024*1024;
51 private ContentResolver mContentResolver;
53 private AgendaListView mAgendaListView;
57 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
59 public void onReceive(Context context, Intent intent) {
60 String action = intent.getAction();
61 if (action.equals(Intent.ACTION_TIME_CHANGED)
62 || action.equals(Intent.ACTION_DATE_CHANGED)
63 || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
64 mAgendaListView.refresh(true);
69 private ContentObserver mObserver = new ContentObserver(new Handler()) {
71 public boolean deliverSelfNotifications() {
76 public void onChange(boolean selfChange) {
77 mAgendaListView.refresh(true);
82 protected void onCreate(Bundle icicle) {
83 super.onCreate(icicle);
85 // Eliminate extra GCs during startup by setting the initial heap size to 4MB.
86 // TODO: We should restore the old heap size once the activity reaches the idle state
87 VMRuntime.getRuntime().setMinimumHeapSize(INITIAL_HEAP_SIZE);
89 mAgendaListView = new AgendaListView(this);
90 setContentView(mAgendaListView);
92 mContentResolver = getContentResolver();
94 setTitle(R.string.agenda_view);
99 // Returns 0 if key not found
100 millis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
102 Log.v(TAG, "Restore value from icicle: " + millis);
107 // Returns 0 if key not found
108 millis = getIntent().getLongExtra(EVENT_BEGIN_TIME, 0);
110 Time time = new Time();
112 Log.v(TAG, "Restore value from intent: " + time.toString());
118 Log.v(TAG, "Restored from current time");
120 millis = System.currentTimeMillis();
126 protected void onNewIntent(Intent intent) {
127 long time = Utils.timeFromIntentInMillis(intent);
135 protected void onResume() {
138 Log.v(TAG, "OnResume to " + mTime.toString());
141 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(
142 getApplicationContext());
143 boolean hideDeclined = prefs.getBoolean(
144 CalendarPreferenceActivity.KEY_HIDE_DECLINED, false);
146 mAgendaListView.setHideDeclinedEvents(hideDeclined);
147 mAgendaListView.goTo(mTime, true);
148 mAgendaListView.onResume();
150 // Register for Intent broadcasts
151 IntentFilter filter = new IntentFilter();
152 filter.addAction(Intent.ACTION_TIME_CHANGED);
153 filter.addAction(Intent.ACTION_DATE_CHANGED);
154 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
155 registerReceiver(mIntentReceiver, filter);
157 mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver);
161 protected void onSaveInstanceState(Bundle outState) {
162 super.onSaveInstanceState(outState);
164 long firstVisibleTime = mAgendaListView.getFirstVisibleTime();
165 if (firstVisibleTime > 0) {
166 mTime.set(firstVisibleTime);
167 outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime);
169 Log.v(TAG, "onSaveInstanceState " + mTime.toString());
175 protected void onPause() {
178 mAgendaListView.onPause();
179 mContentResolver.unregisterContentObserver(mObserver);
180 unregisterReceiver(mIntentReceiver);
182 // Record Agenda View as the (new) default detailed view.
183 Utils.setDefaultView(this, CalendarApplication.AGENDA_VIEW_ID);
187 public boolean onPrepareOptionsMenu(Menu menu) {
188 MenuHelper.onPrepareOptionsMenu(this, menu);
189 return super.onPrepareOptionsMenu(menu);
193 public boolean onCreateOptionsMenu(Menu menu) {
194 MenuHelper.onCreateOptionsMenu(menu);
195 return super.onCreateOptionsMenu(menu);
199 public boolean onOptionsItemSelected(MenuItem item) {
200 MenuHelper.onOptionsItemSelected(this, item, this);
201 return super.onOptionsItemSelected(item);
205 public boolean onKeyDown(int keyCode, KeyEvent event) {
207 case KeyEvent.KEYCODE_DEL:
208 // Delete the currently selected event (if any)
209 mAgendaListView.deleteSelectedEvent();
212 return super.onKeyDown(keyCode, event);
215 /* Navigator interface methods */
216 public void goToToday() {
217 Time now = new Time();
218 now.set(System.currentTimeMillis());
222 public void goTo(Time time, boolean animate) {
223 mAgendaListView.goTo(time, false);
226 public long getSelectedTime() {
227 return mAgendaListView.getSelectedTime();
230 public boolean getAllDay() {