blob: 2cac86250f6ac513c105c0a04f7aed9db333c891 [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;
Mason Tang8e3d4302010-07-12 17:39:30 -070029import android.content.SharedPreferences;
30import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Michael Chanab29d9e2010-07-21 06:08:47 -070031import android.content.res.Configuration;
Michael Chan49701592010-06-30 11:04:03 -070032import android.os.Bundle;
Michael Chan3458a172010-07-13 17:58:21 -070033import android.text.format.DateUtils;
Michael Chan83b0fe32010-07-08 16:46:26 -070034import android.text.format.Time;
Erik2051f122010-07-02 13:45:45 -070035import android.view.Menu;
Michael Chan3458a172010-07-13 17:58:21 -070036import android.view.MenuItem;
Michael Chanab29d9e2010-07-21 06:08:47 -070037import android.view.View;
Michael Chan49701592010-06-30 11:04:03 -070038
Michael Chanab29d9e2010-07-21 06:08:47 -070039public class AllInOneActivity extends Activity implements EventHandler,
40 OnSharedPreferenceChangeListener {
Michael Chand6734db2010-07-22 00:48:08 -070041 private static final String TAG = "AllInOneActivity";
42 private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
Michael Chan0558def2010-07-22 21:30:32 -070043 private static CalendarController mController;
Erik9fc409f2010-07-28 11:40:47 -070044 private static boolean mIsMultipane;
Michael Chan83b0fe32010-07-08 16:46:26 -070045
Michael Chan49701592010-06-30 11:04:03 -070046 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -070047 protected void onCreate(Bundle icicle) {
48 super.onCreate(icicle);
49
50 // This needs to be created before setContentView
Michael Chan0558def2010-07-22 21:30:32 -070051 mController = CalendarController.getInstance(this);
Michael Chand6734db2010-07-22 00:48:08 -070052
Erik9fc409f2010-07-28 11:40:47 -070053 mIsMultipane = (getResources().getConfiguration().screenLayout &
54 Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
55
Michael Chand6734db2010-07-22 00:48:08 -070056 // Must be the first to register so that this activity can modify the
57 // list the event handlers during dispatching.
Michael Chanab29d9e2010-07-21 06:08:47 -070058 mController.registerEventHandler(this);
Michael Chan83b0fe32010-07-08 16:46:26 -070059
Michael Chan49701592010-06-30 11:04:03 -070060 setContentView(R.layout.all_in_one);
Michael Chan83b0fe32010-07-08 16:46:26 -070061
Michael Chan3458a172010-07-13 17:58:21 -070062 // Get time from intent or icicle
Michael Chan83b0fe32010-07-08 16:46:26 -070063 long timeMillis;
64 if (icicle != null) {
Michael Chand6734db2010-07-22 00:48:08 -070065 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
Michael Chan83b0fe32010-07-08 16:46:26 -070066 } else {
67 timeMillis = Utils.timeFromIntentInMillis(getIntent());
68 }
69
Michael Chand6734db2010-07-22 00:48:08 -070070 initFragments(timeMillis, Utils.getViewTypeFromIntentAndSharedPref(this));
Mason Tang8e3d4302010-07-12 17:39:30 -070071
72 // Listen for changes that would require this to be refreshed
73 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
74 prefs.registerOnSharedPreferenceChangeListener(this);
75 }
76
77 @Override
Michael Chand6734db2010-07-22 00:48:08 -070078 protected void onPause() {
79 super.onPause();
80 //FRAG_TODO save highlighted days of the week;
81 Utils.setDefaultView(this, mController.getViewType());
82 }
83
84 @Override
85 public void onSaveInstanceState(Bundle outState) {
86 super.onSaveInstanceState(outState);
87
88 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
89 }
90
91 @Override
Mason Tang8e3d4302010-07-12 17:39:30 -070092 protected void onDestroy() {
93 super.onDestroy();
94
95 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
96 prefs.unregisterOnSharedPreferenceChangeListener(this);
97 }
98
Michael Chand6734db2010-07-22 00:48:08 -070099 private void initFragments(long timeMillis, int viewType) {
Michael Chan3458a172010-07-13 17:58:21 -0700100 FragmentTransaction ft = openFragmentTransaction();
101
Erik9fc409f2010-07-28 11:40:47 -0700102 if (mIsMultipane) {
Erikc7003b42010-07-27 13:25:20 -0700103 Fragment miniMonthFrag = new MonthFragment(false, timeMillis, true);
Michael Chanab29d9e2010-07-21 06:08:47 -0700104 ft.replace(R.id.mini_month, miniMonthFrag);
105 mController.registerEventHandler((EventHandler) miniMonthFrag);
106
107 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
108 ft.replace(R.id.calendar_list, selectCalendarsFrag);
109 } else {
110 findViewById(R.id.mini_month).setVisibility(View.GONE);
111 findViewById(R.id.calendar_list).setVisibility(View.GONE);
112 }
Michael Chan3458a172010-07-13 17:58:21 -0700113
Michael Chand6734db2010-07-22 00:48:08 -0700114 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -0700115
116 ft.commit(); // this needs to be after setMainPane()
117
Michael Chand6734db2010-07-22 00:48:08 -0700118 Time t = new Time();
119 t.set(timeMillis);
120 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan49701592010-06-30 11:04:03 -0700121 }
Erik2051f122010-07-02 13:45:45 -0700122
123 @Override
124 public boolean onCreateOptionsMenu(Menu menu) {
125 super.onCreateOptionsMenu(menu);
126
Erik2051f122010-07-02 13:45:45 -0700127 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
128 return true;
129 }
Michael Chan3458a172010-07-13 17:58:21 -0700130
131 @Override
132 public boolean onOptionsItemSelected(MenuItem item) {
133 Time t = null;
134 int viewType = ViewType.CURRENT;
135 switch (item.getItemId()) {
Erikba1b94a2010-07-20 17:50:50 -0700136 case R.id.action_refresh:
137 mController.refreshCalendars();
138 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700139 case R.id.action_day:
140 viewType = ViewType.DAY;
141 break;
142 case R.id.action_week:
143 viewType = ViewType.WEEK;
144 break;
145 case R.id.action_month:
146 viewType = ViewType.MONTH;
147 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700148 case R.id.action_agenda:
149 viewType = ViewType.AGENDA;
150 break;
Michael Chan3458a172010-07-13 17:58:21 -0700151 case R.id.action_today:
152 viewType = ViewType.CURRENT;
153 t = new Time();
154 t.setToNow();
155 break;
Mason Tang09487852010-07-26 15:55:36 -0700156 case R.id.action_search:
157 onSearchRequested();
158 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700159 case R.id.action_create_event:
160 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0);
161 return true;
Michael Chan2c477fc2010-07-15 18:26:50 -0700162 case R.id.action_manage_calendars:
163 mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0);
164 return true;
Michael Chanab29d9e2010-07-21 06:08:47 -0700165 case R.id.action_settings:
166 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
167 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700168 default:
169 return false;
170 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700171 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan3458a172010-07-13 17:58:21 -0700172 return true;
173 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700174
175 @Override
176 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
177 if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
Michael Chand6734db2010-07-22 00:48:08 -0700178 initFragments(mController.getTime(), mController.getViewType());
Mason Tang8e3d4302010-07-12 17:39:30 -0700179 }
180 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700181
182 private void setMainPane(FragmentTransaction ft, int viewId, int viewType,
183 long timeMillis, boolean force) {
184 if(!force && mController.getPreviousViewType() == viewType) {
185 return;
186 }
187
188 // Deregister old view
189 Fragment frag = findFragmentById(viewId);
190 if (frag != null) {
191 mController.deregisterEventHandler((EventHandler) frag);
192 }
193
194 // Create new one
195 switch (viewType) {
196 case ViewType.AGENDA:
Mason Tang31df4262010-07-19 17:51:46 -0700197 frag = new AgendaFragment(timeMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700198 break;
199 case ViewType.DAY:
Michael Chan75d9b562010-07-26 16:54:25 -0700200 frag = new DayFragment(timeMillis, 1);
201 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700202 case ViewType.WEEK:
Michael Chan75d9b562010-07-26 16:54:25 -0700203 frag = new DayFragment(timeMillis, 7);
Michael Chanab29d9e2010-07-21 06:08:47 -0700204 break;
205 case ViewType.MONTH:
Erikc7003b42010-07-27 13:25:20 -0700206 frag = new MonthFragment(false, timeMillis, false);
Michael Chanab29d9e2010-07-21 06:08:47 -0700207 break;
208 default:
Michael Chand6734db2010-07-22 00:48:08 -0700209 throw new IllegalArgumentException(
210 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
Michael Chanab29d9e2010-07-21 06:08:47 -0700211 }
212
213 boolean doCommit = false;
214 if (ft == null) {
215 doCommit = true;
216 ft = openFragmentTransaction();
217 }
218
219 ft.replace(viewId, frag);
220 mController.registerEventHandler((EventHandler) frag);
221
222 if (doCommit) {
223 ft.commit();
224 }
225 }
226
227 private void setTitleInActionBar(EventInfo event) {
228 if (event.eventType != EventType.GO_TO) {
229 return;
230 }
231
232 long start = event.startTime.toMillis(false /* use isDst */);
233 long end = start;
234
Michael Chand6734db2010-07-22 00:48:08 -0700235 if (event.endTime != null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700236 end = event.endTime.toMillis(false /* use isDst */);
237 }
238 String msg = DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_DATE);
239
240 ActionBar ab = getActionBar();
241 if (ab != null) {
242 ab.setTitle(msg);
243 }
244 }
245
246 // EventHandler Interface
247 public long getSupportedEventTypes() {
248 return EventType.GO_TO;
249 }
250
251 // EventHandler Interface
252 public void handleEvent(EventInfo event) {
253 if (event.eventType == EventType.GO_TO) {
254 // Set title bar
255 setTitleInActionBar(event);
256
257 setMainPane(null, R.id.main_pane, event.viewType,
258 event.startTime.toMillis(false), false);
259
Erik9fc409f2010-07-28 11:40:47 -0700260 if (!mIsMultipane) {
261 return;
262 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700263 if (event.viewType == ViewType.MONTH) {
264 // hide minimonth and calendar frag
Erik9fc409f2010-07-28 11:40:47 -0700265 findViewById(R.id.mini_month).setVisibility(View.GONE);
266 findViewById(R.id.calendar_list).setVisibility(View.GONE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700267 } else {
268 // show minimonth and calendar frag
Erik9fc409f2010-07-28 11:40:47 -0700269 findViewById(R.id.mini_month).setVisibility(View.VISIBLE);
270 findViewById(R.id.calendar_list).setVisibility(View.VISIBLE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700271 }
272 }
273 }
274
275 // EventHandler Interface
276 public void eventsChanged() {
277 }
278
279 // EventHandler Interface
280 public boolean getAllDay() {
281 return false;
282 }
283
284 // EventHandler Interface
285 public long getSelectedTime() {
286 return 0;
287 }
288
289 // EventHandler Interface
290 public void goTo(Time time, boolean animate) {
291 }
292
293 // EventHandler Interface
294 public void goToToday() {
295 }
Michael Chan49701592010-06-30 11:04:03 -0700296}