OSDN Git Service

c6cb8db04481e601cde86dd3b07ce1326fa12994
[android-x86/packages-apps-Calendar.git] / src / com / android / calendar / AgendaActivity.java
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.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;
38
39 import dalvik.system.VMRuntime;
40
41 public class AgendaActivity extends Activity implements Navigator {
42
43     private static final String TAG = "AgendaActivity";
44
45     private static boolean DEBUG = false;
46
47     protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
48
49     private static final long INITIAL_HEAP_SIZE = 4*1024*1024;
50
51     private ContentResolver mContentResolver;
52
53     private AgendaListView mAgendaListView;
54
55     private Time mTime;
56
57     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
58         @Override
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);
65             }
66         }
67     };
68
69     private ContentObserver mObserver = new ContentObserver(new Handler()) {
70         @Override
71         public boolean deliverSelfNotifications() {
72             return true;
73         }
74
75         @Override
76         public void onChange(boolean selfChange) {
77             mAgendaListView.refresh(true);
78         }
79     };
80
81     @Override
82     protected void onCreate(Bundle icicle) {
83         super.onCreate(icicle);
84
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);
88
89         mAgendaListView = new AgendaListView(this);
90         setContentView(mAgendaListView);
91
92         mContentResolver = getContentResolver();
93
94         setTitle(R.string.agenda_view);
95
96         long millis = 0;
97         mTime = new Time();
98         if (icicle != null) {
99             // Returns 0 if key not found
100             millis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
101             if (DEBUG) {
102                 Log.v(TAG, "Restore value from icicle: " + millis);
103             }
104         }
105
106         if (millis == 0) {
107             // Returns 0 if key not found
108             millis = getIntent().getLongExtra(EVENT_BEGIN_TIME, 0);
109             if (DEBUG) {
110                 Time time = new Time();
111                 time.set(millis);
112                 Log.v(TAG, "Restore value from intent: " + time.toString());
113             }
114         }
115
116         if (millis == 0) {
117             if (DEBUG) {
118                 Log.v(TAG, "Restored from current time");
119             }
120             millis = System.currentTimeMillis();
121         }
122         mTime.set(millis);
123     }
124
125     @Override
126     protected void onNewIntent(Intent intent) {
127         long time = Utils.timeFromIntentInMillis(intent);
128         if (time > 0) {
129             mTime.set(time);
130             goTo(mTime, false);
131         }
132     }
133
134     @Override
135     protected void onResume() {
136         super.onResume();
137         if (DEBUG) {
138             Log.v(TAG, "OnResume to " + mTime.toString());
139         }
140
141         SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(
142                 getApplicationContext());
143         boolean hideDeclined = prefs.getBoolean(
144                 CalendarPreferenceActivity.KEY_HIDE_DECLINED, false);
145
146         mAgendaListView.setHideDeclinedEvents(hideDeclined);
147         mAgendaListView.goTo(mTime, true);
148         mAgendaListView.onResume();
149
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);
156
157         mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver);
158     }
159
160     @Override
161     protected void onSaveInstanceState(Bundle outState) {
162         super.onSaveInstanceState(outState);
163
164         long firstVisibleTime = mAgendaListView.getFirstVisibleTime();
165         if (firstVisibleTime > 0) {
166             mTime.set(firstVisibleTime);
167             outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime);
168             if (DEBUG) {
169                 Log.v(TAG, "onSaveInstanceState " + mTime.toString());
170             }
171         }
172     }
173
174     @Override
175     protected void onPause() {
176         super.onPause();
177
178         mAgendaListView.onPause();
179         mContentResolver.unregisterContentObserver(mObserver);
180         unregisterReceiver(mIntentReceiver);
181
182         // Record Agenda View as the (new) default detailed view.
183         Utils.setDefaultView(this, CalendarApplication.AGENDA_VIEW_ID);
184     }
185
186     @Override
187     public boolean onPrepareOptionsMenu(Menu menu) {
188         MenuHelper.onPrepareOptionsMenu(this, menu);
189         return super.onPrepareOptionsMenu(menu);
190     }
191
192     @Override
193     public boolean onCreateOptionsMenu(Menu menu) {
194         MenuHelper.onCreateOptionsMenu(menu);
195         return super.onCreateOptionsMenu(menu);
196     }
197
198     @Override
199     public boolean onOptionsItemSelected(MenuItem item) {
200         MenuHelper.onOptionsItemSelected(this, item, this);
201         return super.onOptionsItemSelected(item);
202     }
203
204     @Override
205     public boolean onKeyDown(int keyCode, KeyEvent event) {
206         switch (keyCode) {
207             case KeyEvent.KEYCODE_DEL:
208                 // Delete the currently selected event (if any)
209                 mAgendaListView.deleteSelectedEvent();
210                 break;
211         }
212         return super.onKeyDown(keyCode, event);
213     }
214
215     /* Navigator interface methods */
216     public void goToToday() {
217         Time now = new Time();
218         now.set(System.currentTimeMillis());
219         goTo(now, true);
220     }
221
222     public void goTo(Time time, boolean animate) {
223         mAgendaListView.goTo(time, false);
224     }
225
226     public long getSelectedTime() {
227         return mAgendaListView.getSelectedTime();
228     }
229
230     public boolean getAllDay() {
231         return false;
232     }
233 }
234