blob: d19d37192fa8a80181f6ab3bc64d850f40c59f8c [file] [log] [blame]
Michael Chan49701592010-06-30 11:04:03 -07001/*
2 * Copyright (C) 2010 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
17package com.android.calendar;
18
Michael Chan83b0fe32010-07-08 16:46:26 -070019import com.android.calendar.CalendarController.EventHandler;
Michael Chanab29d9e2010-07-21 06:08:47 -070020import com.android.calendar.CalendarController.EventInfo;
Michael Chan83b0fe32010-07-08 16:46:26 -070021import com.android.calendar.CalendarController.EventType;
Michael Chan3458a172010-07-13 17:58:21 -070022import com.android.calendar.CalendarController.ViewType;
Mason Tang9a3cb142010-07-27 12:16:41 -070023import com.android.calendar.selectcalendars.SelectCalendarsFragment;
Michael Chan83b0fe32010-07-08 16:46:26 -070024
Michael Chanab29d9e2010-07-21 06:08:47 -070025import android.app.ActionBar;
Michael Chan49701592010-06-30 11:04:03 -070026import android.app.Activity;
Michael Chan83b0fe32010-07-08 16:46:26 -070027import android.app.Fragment;
Michael Chan3458a172010-07-13 17:58:21 -070028import android.app.FragmentTransaction;
Erik954c8712010-08-06 10:12:34 -070029import android.content.ContentResolver;
Mason Tang8e3d4302010-07-12 17:39:30 -070030import android.content.SharedPreferences;
31import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Michael Chanab29d9e2010-07-21 06:08:47 -070032import android.content.res.Configuration;
Erik954c8712010-08-06 10:12:34 -070033import android.database.ContentObserver;
Michael Chan49701592010-06-30 11:04:03 -070034import android.os.Bundle;
Erik954c8712010-08-06 10:12:34 -070035import android.os.Handler;
36import android.provider.Calendar;
Michael Chan3458a172010-07-13 17:58:21 -070037import android.text.format.DateUtils;
Michael Chan83b0fe32010-07-08 16:46:26 -070038import android.text.format.Time;
Erik954c8712010-08-06 10:12:34 -070039import android.util.Log;
Erik2051f122010-07-02 13:45:45 -070040import android.view.Menu;
Michael Chan3458a172010-07-13 17:58:21 -070041import android.view.MenuItem;
Michael Chanab29d9e2010-07-21 06:08:47 -070042import android.view.View;
Michael Chan49701592010-06-30 11:04:03 -070043
Michael Chanab29d9e2010-07-21 06:08:47 -070044public class AllInOneActivity extends Activity implements EventHandler,
45 OnSharedPreferenceChangeListener {
Michael Chand6734db2010-07-22 00:48:08 -070046 private static final String TAG = "AllInOneActivity";
Erik954c8712010-08-06 10:12:34 -070047 private static final boolean DEBUG = false;
Michael Chand6734db2010-07-22 00:48:08 -070048 private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
Erik3f348f32010-08-10 13:17:19 -070049 private static final int HANDLER_KEY = 0;
Michael Chan0558def2010-07-22 21:30:32 -070050 private static CalendarController mController;
Erik9fc409f2010-07-28 11:40:47 -070051 private static boolean mIsMultipane;
Erik954c8712010-08-06 10:12:34 -070052 private ContentResolver mContentResolver;
53
54 // Create an observer so that we can update the views whenever a
55 // Calendar event changes.
56 private ContentObserver mObserver = new ContentObserver(new Handler())
57 {
58 @Override
59 public boolean deliverSelfNotifications() {
60 return true;
61 }
62
63 @Override
64 public void onChange(boolean selfChange) {
65 eventsChanged();
66 }
67 };
Michael Chan83b0fe32010-07-08 16:46:26 -070068
Michael Chan49701592010-06-30 11:04:03 -070069 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -070070 protected void onCreate(Bundle icicle) {
71 super.onCreate(icicle);
72
73 // This needs to be created before setContentView
Michael Chan0558def2010-07-22 21:30:32 -070074 mController = CalendarController.getInstance(this);
Erik954c8712010-08-06 10:12:34 -070075 // Get time from intent or icicle
76 long timeMillis;
77 if (icicle != null) {
78 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
79 } else {
80 timeMillis = Utils.timeFromIntentInMillis(getIntent());
81 }
82 int viewType = Utils.getViewTypeFromIntentAndSharedPref(this);
83 Time t = new Time();
84 t.set(timeMillis);
85
Erik9fc409f2010-07-28 11:40:47 -070086 mIsMultipane = (getResources().getConfiguration().screenLayout &
87 Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
88
Erik3f348f32010-08-10 13:17:19 -070089 // Must be the first to register because this activity can modify the
90 // list of event handlers in it's handle method. This affects who the
91 // rest of the handlers the controller dispatches to are.
92 mController.registerEventHandler(HANDLER_KEY, this);
Michael Chan83b0fe32010-07-08 16:46:26 -070093
Michael Chan49701592010-06-30 11:04:03 -070094 setContentView(R.layout.all_in_one);
Michael Chan83b0fe32010-07-08 16:46:26 -070095
Michael Chan83b0fe32010-07-08 16:46:26 -070096
Erik954c8712010-08-06 10:12:34 -070097 initFragments(timeMillis, viewType);
Mason Tang8e3d4302010-07-12 17:39:30 -070098
99 // Listen for changes that would require this to be refreshed
100 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
101 prefs.registerOnSharedPreferenceChangeListener(this);
Erik954c8712010-08-06 10:12:34 -0700102 mContentResolver = getContentResolver();
103 }
104
105 @Override
106 protected void onResume() {
107 super.onResume();
108 mContentResolver.registerContentObserver(Calendar.Events.CONTENT_URI, true, mObserver);
Mason Tang8e3d4302010-07-12 17:39:30 -0700109 }
110
111 @Override
Michael Chand6734db2010-07-22 00:48:08 -0700112 protected void onPause() {
113 super.onPause();
Erik954c8712010-08-06 10:12:34 -0700114 mContentResolver.unregisterContentObserver(mObserver);
Michael Chand6734db2010-07-22 00:48:08 -0700115 //FRAG_TODO save highlighted days of the week;
116 Utils.setDefaultView(this, mController.getViewType());
117 }
118
119 @Override
120 public void onSaveInstanceState(Bundle outState) {
121 super.onSaveInstanceState(outState);
122
123 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
124 }
125
126 @Override
Mason Tang8e3d4302010-07-12 17:39:30 -0700127 protected void onDestroy() {
128 super.onDestroy();
129
130 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
131 prefs.unregisterOnSharedPreferenceChangeListener(this);
132 }
133
Michael Chand6734db2010-07-22 00:48:08 -0700134 private void initFragments(long timeMillis, int viewType) {
Michael Chan3458a172010-07-13 17:58:21 -0700135 FragmentTransaction ft = openFragmentTransaction();
136
Erik9fc409f2010-07-28 11:40:47 -0700137 if (mIsMultipane) {
Erikc7003b42010-07-27 13:25:20 -0700138 Fragment miniMonthFrag = new MonthFragment(false, timeMillis, true);
Michael Chanab29d9e2010-07-21 06:08:47 -0700139 ft.replace(R.id.mini_month, miniMonthFrag);
Erik3f348f32010-08-10 13:17:19 -0700140 mController.registerEventHandler(R.id.mini_month, (EventHandler) miniMonthFrag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700141
142 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
143 ft.replace(R.id.calendar_list, selectCalendarsFrag);
144 } else {
145 findViewById(R.id.mini_month).setVisibility(View.GONE);
146 findViewById(R.id.calendar_list).setVisibility(View.GONE);
147 }
Michael Chan3458a172010-07-13 17:58:21 -0700148
Michael Chand6734db2010-07-22 00:48:08 -0700149 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -0700150
151 ft.commit(); // this needs to be after setMainPane()
152
Michael Chand6734db2010-07-22 00:48:08 -0700153 Time t = new Time();
154 t.set(timeMillis);
155 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan49701592010-06-30 11:04:03 -0700156 }
Erik2051f122010-07-02 13:45:45 -0700157
158 @Override
159 public boolean onCreateOptionsMenu(Menu menu) {
160 super.onCreateOptionsMenu(menu);
161
Erik2051f122010-07-02 13:45:45 -0700162 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
163 return true;
164 }
Michael Chan3458a172010-07-13 17:58:21 -0700165
166 @Override
167 public boolean onOptionsItemSelected(MenuItem item) {
168 Time t = null;
169 int viewType = ViewType.CURRENT;
170 switch (item.getItemId()) {
Erikba1b94a2010-07-20 17:50:50 -0700171 case R.id.action_refresh:
172 mController.refreshCalendars();
173 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700174 case R.id.action_day:
175 viewType = ViewType.DAY;
176 break;
177 case R.id.action_week:
178 viewType = ViewType.WEEK;
179 break;
180 case R.id.action_month:
181 viewType = ViewType.MONTH;
182 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700183 case R.id.action_agenda:
184 viewType = ViewType.AGENDA;
185 break;
Michael Chan3458a172010-07-13 17:58:21 -0700186 case R.id.action_today:
187 viewType = ViewType.CURRENT;
188 t = new Time();
189 t.setToNow();
190 break;
Mason Tang09487852010-07-26 15:55:36 -0700191 case R.id.action_search:
192 onSearchRequested();
193 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700194 case R.id.action_create_event:
195 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0);
196 return true;
Michael Chan2c477fc2010-07-15 18:26:50 -0700197 case R.id.action_manage_calendars:
198 mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0);
199 return true;
Michael Chanab29d9e2010-07-21 06:08:47 -0700200 case R.id.action_settings:
201 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
202 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700203 default:
204 return false;
205 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700206 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan3458a172010-07-13 17:58:21 -0700207 return true;
208 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700209
210 @Override
211 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
212 if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
Michael Chand6734db2010-07-22 00:48:08 -0700213 initFragments(mController.getTime(), mController.getViewType());
Mason Tang8e3d4302010-07-12 17:39:30 -0700214 }
215 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700216
217 private void setMainPane(FragmentTransaction ft, int viewId, int viewType,
218 long timeMillis, boolean force) {
219 if(!force && mController.getPreviousViewType() == viewType) {
220 return;
221 }
222
Erik3f348f32010-08-10 13:17:19 -0700223 // Create new fragment
224 Fragment frag;
Michael Chanab29d9e2010-07-21 06:08:47 -0700225 switch (viewType) {
226 case ViewType.AGENDA:
Mason Tang31df4262010-07-19 17:51:46 -0700227 frag = new AgendaFragment(timeMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700228 break;
229 case ViewType.DAY:
Michael Chan75d9b562010-07-26 16:54:25 -0700230 frag = new DayFragment(timeMillis, 1);
231 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700232 case ViewType.WEEK:
Michael Chan75d9b562010-07-26 16:54:25 -0700233 frag = new DayFragment(timeMillis, 7);
Michael Chanab29d9e2010-07-21 06:08:47 -0700234 break;
235 case ViewType.MONTH:
Erikc7003b42010-07-27 13:25:20 -0700236 frag = new MonthFragment(false, timeMillis, false);
Michael Chanab29d9e2010-07-21 06:08:47 -0700237 break;
238 default:
Michael Chand6734db2010-07-22 00:48:08 -0700239 throw new IllegalArgumentException(
240 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
Michael Chanab29d9e2010-07-21 06:08:47 -0700241 }
242
243 boolean doCommit = false;
244 if (ft == null) {
245 doCommit = true;
246 ft = openFragmentTransaction();
247 }
248
249 ft.replace(viewId, frag);
Erik954c8712010-08-06 10:12:34 -0700250 if (DEBUG) {
251 Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
252 }
Erik3f348f32010-08-10 13:17:19 -0700253 // If the key is already registered this will replace it
254 mController.registerEventHandler(viewId, (EventHandler) frag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700255
256 if (doCommit) {
257 ft.commit();
258 }
259 }
260
261 private void setTitleInActionBar(EventInfo event) {
262 if (event.eventType != EventType.GO_TO) {
263 return;
264 }
265
266 long start = event.startTime.toMillis(false /* use isDst */);
267 long end = start;
268
Michael Chand6734db2010-07-22 00:48:08 -0700269 if (event.endTime != null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700270 end = event.endTime.toMillis(false /* use isDst */);
271 }
272 String msg = DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_DATE);
273
274 ActionBar ab = getActionBar();
275 if (ab != null) {
276 ab.setTitle(msg);
277 }
278 }
279
Erik3f348f32010-08-10 13:17:19 -0700280 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700281 public long getSupportedEventTypes() {
282 return EventType.GO_TO;
283 }
284
Erik3f348f32010-08-10 13:17:19 -0700285 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700286 public void handleEvent(EventInfo event) {
287 if (event.eventType == EventType.GO_TO) {
288 // Set title bar
289 setTitleInActionBar(event);
290
291 setMainPane(null, R.id.main_pane, event.viewType,
292 event.startTime.toMillis(false), false);
293
Erik9fc409f2010-07-28 11:40:47 -0700294 if (!mIsMultipane) {
295 return;
296 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700297 if (event.viewType == ViewType.MONTH) {
298 // hide minimonth and calendar frag
Erik9fc409f2010-07-28 11:40:47 -0700299 findViewById(R.id.mini_month).setVisibility(View.GONE);
300 findViewById(R.id.calendar_list).setVisibility(View.GONE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700301 } else {
302 // show minimonth and calendar frag
Erik9fc409f2010-07-28 11:40:47 -0700303 findViewById(R.id.mini_month).setVisibility(View.VISIBLE);
304 findViewById(R.id.calendar_list).setVisibility(View.VISIBLE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700305 }
306 }
307 }
308
Erik3f348f32010-08-10 13:17:19 -0700309 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700310 public void eventsChanged() {
Erik954c8712010-08-06 10:12:34 -0700311 mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
Michael Chanab29d9e2010-07-21 06:08:47 -0700312 }
313
Erik3f348f32010-08-10 13:17:19 -0700314 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700315 public boolean getAllDay() {
316 return false;
317 }
318
Erik3f348f32010-08-10 13:17:19 -0700319 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700320 public long getSelectedTime() {
321 return 0;
322 }
323
Erik3f348f32010-08-10 13:17:19 -0700324 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700325 public void goTo(Time time, boolean animate) {
326 }
327
Erik3f348f32010-08-10 13:17:19 -0700328 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700329 public void goToToday() {
330 }
Michael Chan49701592010-06-30 11:04:03 -0700331}