OSDN Git Service

Merge change 5874
[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 import dalvik.system.VMRuntime;
21
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;
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                 Log.v(TAG, "Restore value from intent: " + millis);
110             }
111         }
112
113         if (millis == 0) {
114             if (DEBUG) {
115                 Log.v(TAG, "Restored from current time");
116             }
117             millis = System.currentTimeMillis();
118         }
119         mTime.set(millis);
120
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);
127
128         // Record Agenda View as the (new) start view
129         editor.putString(CalendarPreferenceActivity.KEY_START_VIEW, activityString);
130         editor.commit();
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 =
141             PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
142         boolean hideDeclined = prefs
143                 .getBoolean(CalendarPreferenceActivity.KEY_HIDE_DECLINED, false);
144
145         mAgendaListView.setHideDeclinedEvents(hideDeclined);
146         mAgendaListView.goTo(mTime, true);
147
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);
154
155         mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver);
156     }
157
158     @Override
159     protected void onSaveInstanceState(Bundle outState) {
160         super.onSaveInstanceState(outState);
161
162         long firstVisibleTime = mAgendaListView.getFirstVisibleTime();
163         if (firstVisibleTime >= 0) {
164             mTime.set(firstVisibleTime);
165             outState.putLong(BUNDLE_KEY_RESTORE_TIME, firstVisibleTime);
166             if (DEBUG) {
167                 Log.v(TAG, "onSaveInstanceState " + mTime.toString());
168             }
169         }
170     }
171
172     @Override
173     protected void onPause() {
174         super.onPause();
175
176         mContentResolver.unregisterContentObserver(mObserver);
177         unregisterReceiver(mIntentReceiver);
178     }
179
180     @Override
181     public boolean onPrepareOptionsMenu(Menu menu) {
182         MenuHelper.onPrepareOptionsMenu(this, menu);
183         return super.onPrepareOptionsMenu(menu);
184     }
185
186     @Override
187     public boolean onCreateOptionsMenu(Menu menu) {
188         MenuHelper.onCreateOptionsMenu(menu);
189         return super.onCreateOptionsMenu(menu);
190     }
191
192     @Override
193     public boolean onOptionsItemSelected(MenuItem item) {
194         MenuHelper.onOptionsItemSelected(this, item, this);
195         return super.onOptionsItemSelected(item);
196     }
197
198     @Override
199     public boolean onKeyDown(int keyCode, KeyEvent event) {
200         switch (keyCode) {
201             case KeyEvent.KEYCODE_DEL:
202                 // Delete the currently selected event (if any)
203                 mAgendaListView.deleteSelectedEvent();
204                 break;
205
206             case KeyEvent.KEYCODE_BACK:
207                 finish();
208                 return true;
209         }
210         return super.onKeyDown(keyCode, event);
211     }
212
213     /* Navigator interface methods */
214     public void goToToday() {
215         Time now = new Time();
216         now.set(System.currentTimeMillis());
217         goTo(now);
218     }
219
220     public void goTo(Time time) {
221         mAgendaListView.goTo(time, false);
222     }
223
224     public long getSelectedTime() {
225         return mAgendaListView.getSelectedTime();
226     }
227
228     public boolean getAllDay() {
229         return false;
230     }
231 }
232