blob: 652ad67de97846874c50ee7dd1d4019fd3e76a8e [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;
Erik648c59c2010-08-13 12:49:26 -070053 private int mPreviousView;
54 private int mCurrentView;
Erik954c8712010-08-06 10:12:34 -070055
56 // Create an observer so that we can update the views whenever a
57 // Calendar event changes.
58 private ContentObserver mObserver = new ContentObserver(new Handler())
59 {
60 @Override
61 public boolean deliverSelfNotifications() {
62 return true;
63 }
64
65 @Override
66 public void onChange(boolean selfChange) {
67 eventsChanged();
68 }
69 };
Michael Chan83b0fe32010-07-08 16:46:26 -070070
Michael Chan49701592010-06-30 11:04:03 -070071 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -070072 protected void onCreate(Bundle icicle) {
73 super.onCreate(icicle);
74
75 // This needs to be created before setContentView
Michael Chan0558def2010-07-22 21:30:32 -070076 mController = CalendarController.getInstance(this);
Erik954c8712010-08-06 10:12:34 -070077 // Get time from intent or icicle
78 long timeMillis;
79 if (icicle != null) {
80 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
81 } else {
82 timeMillis = Utils.timeFromIntentInMillis(getIntent());
83 }
84 int viewType = Utils.getViewTypeFromIntentAndSharedPref(this);
85 Time t = new Time();
86 t.set(timeMillis);
87
Erik9fc409f2010-07-28 11:40:47 -070088 mIsMultipane = (getResources().getConfiguration().screenLayout &
89 Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
90
Erik3f348f32010-08-10 13:17:19 -070091 // Must be the first to register because this activity can modify the
92 // list of event handlers in it's handle method. This affects who the
93 // rest of the handlers the controller dispatches to are.
94 mController.registerEventHandler(HANDLER_KEY, this);
Michael Chan83b0fe32010-07-08 16:46:26 -070095
Michael Chan49701592010-06-30 11:04:03 -070096 setContentView(R.layout.all_in_one);
Michael Chan83b0fe32010-07-08 16:46:26 -070097
Erik954c8712010-08-06 10:12:34 -070098 initFragments(timeMillis, viewType);
Erik648c59c2010-08-13 12:49:26 -070099 mPreviousView = viewType;
100 mCurrentView = viewType;
Mason Tang8e3d4302010-07-12 17:39:30 -0700101
102 // Listen for changes that would require this to be refreshed
103 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
104 prefs.registerOnSharedPreferenceChangeListener(this);
Erik954c8712010-08-06 10:12:34 -0700105 mContentResolver = getContentResolver();
106 }
107
108 @Override
109 protected void onResume() {
110 super.onResume();
111 mContentResolver.registerContentObserver(Calendar.Events.CONTENT_URI, true, mObserver);
Mason Tang8e3d4302010-07-12 17:39:30 -0700112 }
113
114 @Override
Michael Chand6734db2010-07-22 00:48:08 -0700115 protected void onPause() {
116 super.onPause();
Erik954c8712010-08-06 10:12:34 -0700117 mContentResolver.unregisterContentObserver(mObserver);
Michael Chand6734db2010-07-22 00:48:08 -0700118 //FRAG_TODO save highlighted days of the week;
119 Utils.setDefaultView(this, mController.getViewType());
120 }
121
122 @Override
123 public void onSaveInstanceState(Bundle outState) {
124 super.onSaveInstanceState(outState);
125
126 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
127 }
128
129 @Override
Mason Tang8e3d4302010-07-12 17:39:30 -0700130 protected void onDestroy() {
131 super.onDestroy();
132
133 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
134 prefs.unregisterOnSharedPreferenceChangeListener(this);
Eriked61b482010-08-13 13:59:50 -0700135 CalendarController.removeInstance(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700136 }
137
Michael Chand6734db2010-07-22 00:48:08 -0700138 private void initFragments(long timeMillis, int viewType) {
Michael Chan3458a172010-07-13 17:58:21 -0700139 FragmentTransaction ft = openFragmentTransaction();
140
Erik9fc409f2010-07-28 11:40:47 -0700141 if (mIsMultipane) {
Erikc7003b42010-07-27 13:25:20 -0700142 Fragment miniMonthFrag = new MonthFragment(false, timeMillis, true);
Michael Chanab29d9e2010-07-21 06:08:47 -0700143 ft.replace(R.id.mini_month, miniMonthFrag);
Erik3f348f32010-08-10 13:17:19 -0700144 mController.registerEventHandler(R.id.mini_month, (EventHandler) miniMonthFrag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700145
146 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
147 ft.replace(R.id.calendar_list, selectCalendarsFrag);
148 } else {
149 findViewById(R.id.mini_month).setVisibility(View.GONE);
150 findViewById(R.id.calendar_list).setVisibility(View.GONE);
151 }
Michael Chan3458a172010-07-13 17:58:21 -0700152
Michael Chand6734db2010-07-22 00:48:08 -0700153 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -0700154
155 ft.commit(); // this needs to be after setMainPane()
156
Michael Chand6734db2010-07-22 00:48:08 -0700157 Time t = new Time();
158 t.set(timeMillis);
159 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan49701592010-06-30 11:04:03 -0700160 }
Erik2051f122010-07-02 13:45:45 -0700161
162 @Override
Erik648c59c2010-08-13 12:49:26 -0700163 public void onBackPressed() {
164 if (mPreviousView == mCurrentView) {
165 super.onBackPressed();
166 } else {
167 mCurrentView = mPreviousView;
168 mController.sendEvent(this, EventType.GO_TO, null, null, -1, mCurrentView);
169 }
170 }
171
172 @Override
Erik2051f122010-07-02 13:45:45 -0700173 public boolean onCreateOptionsMenu(Menu menu) {
174 super.onCreateOptionsMenu(menu);
175
Erik2051f122010-07-02 13:45:45 -0700176 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
177 return true;
178 }
Michael Chan3458a172010-07-13 17:58:21 -0700179
180 @Override
181 public boolean onOptionsItemSelected(MenuItem item) {
182 Time t = null;
183 int viewType = ViewType.CURRENT;
184 switch (item.getItemId()) {
Erikba1b94a2010-07-20 17:50:50 -0700185 case R.id.action_refresh:
186 mController.refreshCalendars();
187 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700188 case R.id.action_day:
189 viewType = ViewType.DAY;
190 break;
191 case R.id.action_week:
192 viewType = ViewType.WEEK;
193 break;
194 case R.id.action_month:
195 viewType = ViewType.MONTH;
196 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700197 case R.id.action_agenda:
198 viewType = ViewType.AGENDA;
199 break;
Michael Chan3458a172010-07-13 17:58:21 -0700200 case R.id.action_today:
201 viewType = ViewType.CURRENT;
202 t = new Time();
203 t.setToNow();
204 break;
Mason Tang09487852010-07-26 15:55:36 -0700205 case R.id.action_search:
206 onSearchRequested();
207 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700208 case R.id.action_create_event:
209 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0);
210 return true;
Michael Chan2c477fc2010-07-15 18:26:50 -0700211 case R.id.action_manage_calendars:
212 mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0);
213 return true;
Michael Chanab29d9e2010-07-21 06:08:47 -0700214 case R.id.action_settings:
215 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
216 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700217 default:
218 return false;
219 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700220 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan3458a172010-07-13 17:58:21 -0700221 return true;
222 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700223
224 @Override
225 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
226 if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
Michael Chand6734db2010-07-22 00:48:08 -0700227 initFragments(mController.getTime(), mController.getViewType());
Mason Tang8e3d4302010-07-12 17:39:30 -0700228 }
229 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700230
231 private void setMainPane(FragmentTransaction ft, int viewId, int viewType,
232 long timeMillis, boolean force) {
233 if(!force && mController.getPreviousViewType() == viewType) {
234 return;
235 }
236
Erik648c59c2010-08-13 12:49:26 -0700237 if (viewType != mCurrentView) {
238 // The rules for this previous view are different than the
239 // controller's and are used for intercepting the back button.
240 mPreviousView = mCurrentView;
241 mCurrentView = viewType;
242 }
Erik3f348f32010-08-10 13:17:19 -0700243 // Create new fragment
244 Fragment frag;
Michael Chanab29d9e2010-07-21 06:08:47 -0700245 switch (viewType) {
246 case ViewType.AGENDA:
Mason Tang31df4262010-07-19 17:51:46 -0700247 frag = new AgendaFragment(timeMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700248 break;
249 case ViewType.DAY:
Michael Chan75d9b562010-07-26 16:54:25 -0700250 frag = new DayFragment(timeMillis, 1);
251 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700252 case ViewType.WEEK:
Michael Chan75d9b562010-07-26 16:54:25 -0700253 frag = new DayFragment(timeMillis, 7);
Michael Chanab29d9e2010-07-21 06:08:47 -0700254 break;
255 case ViewType.MONTH:
Erikc7003b42010-07-27 13:25:20 -0700256 frag = new MonthFragment(false, timeMillis, false);
Michael Chanab29d9e2010-07-21 06:08:47 -0700257 break;
258 default:
Michael Chand6734db2010-07-22 00:48:08 -0700259 throw new IllegalArgumentException(
260 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
Michael Chanab29d9e2010-07-21 06:08:47 -0700261 }
262
263 boolean doCommit = false;
264 if (ft == null) {
265 doCommit = true;
266 ft = openFragmentTransaction();
267 }
268
269 ft.replace(viewId, frag);
Erik954c8712010-08-06 10:12:34 -0700270 if (DEBUG) {
271 Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
272 }
Erik3f348f32010-08-10 13:17:19 -0700273 // If the key is already registered this will replace it
274 mController.registerEventHandler(viewId, (EventHandler) frag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700275
276 if (doCommit) {
277 ft.commit();
278 }
279 }
280
281 private void setTitleInActionBar(EventInfo event) {
282 if (event.eventType != EventType.GO_TO) {
283 return;
284 }
285
286 long start = event.startTime.toMillis(false /* use isDst */);
287 long end = start;
288
Michael Chand6734db2010-07-22 00:48:08 -0700289 if (event.endTime != null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700290 end = event.endTime.toMillis(false /* use isDst */);
291 }
292 String msg = DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_DATE);
293
294 ActionBar ab = getActionBar();
295 if (ab != null) {
296 ab.setTitle(msg);
297 }
298 }
299
Erik3f348f32010-08-10 13:17:19 -0700300 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700301 public long getSupportedEventTypes() {
302 return EventType.GO_TO;
303 }
304
Erik3f348f32010-08-10 13:17:19 -0700305 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700306 public void handleEvent(EventInfo event) {
307 if (event.eventType == EventType.GO_TO) {
308 // Set title bar
309 setTitleInActionBar(event);
310
311 setMainPane(null, R.id.main_pane, event.viewType,
312 event.startTime.toMillis(false), false);
313
Erik9fc409f2010-07-28 11:40:47 -0700314 if (!mIsMultipane) {
315 return;
316 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700317 if (event.viewType == ViewType.MONTH) {
318 // hide minimonth and calendar frag
Erik9fc409f2010-07-28 11:40:47 -0700319 findViewById(R.id.mini_month).setVisibility(View.GONE);
320 findViewById(R.id.calendar_list).setVisibility(View.GONE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700321 } else {
322 // show minimonth and calendar frag
Erik9fc409f2010-07-28 11:40:47 -0700323 findViewById(R.id.mini_month).setVisibility(View.VISIBLE);
324 findViewById(R.id.calendar_list).setVisibility(View.VISIBLE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700325 }
326 }
327 }
328
Erik3f348f32010-08-10 13:17:19 -0700329 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700330 public void eventsChanged() {
Erik954c8712010-08-06 10:12:34 -0700331 mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
Michael Chanab29d9e2010-07-21 06:08:47 -0700332 }
333
Erik3f348f32010-08-10 13:17:19 -0700334 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700335 public boolean getAllDay() {
336 return false;
337 }
338
Erik3f348f32010-08-10 13:17:19 -0700339 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700340 public long getSelectedTime() {
341 return 0;
342 }
343
Erik3f348f32010-08-10 13:17:19 -0700344 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700345 public void goTo(Time time, boolean animate) {
346 }
347
Erik3f348f32010-08-10 13:17:19 -0700348 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700349 public void goToToday() {
350 }
Michael Chan49701592010-06-30 11:04:03 -0700351}