blob: 8ef8ef53f0bd3f826b07469ee0e718cec8a39621 [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;
23import 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;
Michael Chan83b0fe32010-07-08 16:46:26 -070044
Michael Chan49701592010-06-30 11:04:03 -070045 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -070046 protected void onCreate(Bundle icicle) {
47 super.onCreate(icicle);
48
49 // This needs to be created before setContentView
Michael Chan0558def2010-07-22 21:30:32 -070050 mController = CalendarController.getInstance(this);
Michael Chand6734db2010-07-22 00:48:08 -070051
52 // Must be the first to register so that this activity can modify the
53 // list the event handlers during dispatching.
Michael Chanab29d9e2010-07-21 06:08:47 -070054 mController.registerEventHandler(this);
Michael Chan83b0fe32010-07-08 16:46:26 -070055
Michael Chan49701592010-06-30 11:04:03 -070056 setContentView(R.layout.all_in_one);
Michael Chan83b0fe32010-07-08 16:46:26 -070057
Michael Chan3458a172010-07-13 17:58:21 -070058 // Get time from intent or icicle
Michael Chan83b0fe32010-07-08 16:46:26 -070059 long timeMillis;
60 if (icicle != null) {
Michael Chand6734db2010-07-22 00:48:08 -070061 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
Michael Chan83b0fe32010-07-08 16:46:26 -070062 } else {
63 timeMillis = Utils.timeFromIntentInMillis(getIntent());
64 }
65
Michael Chand6734db2010-07-22 00:48:08 -070066 initFragments(timeMillis, Utils.getViewTypeFromIntentAndSharedPref(this));
Mason Tang8e3d4302010-07-12 17:39:30 -070067
68 // Listen for changes that would require this to be refreshed
69 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
70 prefs.registerOnSharedPreferenceChangeListener(this);
71 }
72
73 @Override
Michael Chand6734db2010-07-22 00:48:08 -070074 protected void onPause() {
75 super.onPause();
76 //FRAG_TODO save highlighted days of the week;
77 Utils.setDefaultView(this, mController.getViewType());
78 }
79
80 @Override
81 public void onSaveInstanceState(Bundle outState) {
82 super.onSaveInstanceState(outState);
83
84 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
85 }
86
87 @Override
Mason Tang8e3d4302010-07-12 17:39:30 -070088 protected void onDestroy() {
89 super.onDestroy();
90
91 SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(this);
92 prefs.unregisterOnSharedPreferenceChangeListener(this);
93 }
94
Michael Chand6734db2010-07-22 00:48:08 -070095 private void initFragments(long timeMillis, int viewType) {
Michael Chan3458a172010-07-13 17:58:21 -070096 FragmentTransaction ft = openFragmentTransaction();
97
Michael Chanab29d9e2010-07-21 06:08:47 -070098 boolean multipane = (getResources().getConfiguration().screenLayout &
99 Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
Michael Chan3458a172010-07-13 17:58:21 -0700100
Michael Chanab29d9e2010-07-21 06:08:47 -0700101 if (multipane) {
102 Fragment miniMonthFrag = new MonthFragment(false, timeMillis);
103 ft.replace(R.id.mini_month, miniMonthFrag);
104 mController.registerEventHandler((EventHandler) miniMonthFrag);
105
106 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
107 ft.replace(R.id.calendar_list, selectCalendarsFrag);
108 } else {
109 findViewById(R.id.mini_month).setVisibility(View.GONE);
110 findViewById(R.id.calendar_list).setVisibility(View.GONE);
111 }
Michael Chan3458a172010-07-13 17:58:21 -0700112
Michael Chand6734db2010-07-22 00:48:08 -0700113 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -0700114
115 ft.commit(); // this needs to be after setMainPane()
116
Michael Chand6734db2010-07-22 00:48:08 -0700117 Time t = new Time();
118 t.set(timeMillis);
119 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan49701592010-06-30 11:04:03 -0700120 }
Erik2051f122010-07-02 13:45:45 -0700121
122 @Override
123 public boolean onCreateOptionsMenu(Menu menu) {
124 super.onCreateOptionsMenu(menu);
125
Erik2051f122010-07-02 13:45:45 -0700126 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
127 return true;
128 }
Michael Chan3458a172010-07-13 17:58:21 -0700129
130 @Override
131 public boolean onOptionsItemSelected(MenuItem item) {
132 Time t = null;
133 int viewType = ViewType.CURRENT;
134 switch (item.getItemId()) {
Erikba1b94a2010-07-20 17:50:50 -0700135 case R.id.action_refresh:
136 mController.refreshCalendars();
137 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700138 case R.id.action_day:
139 viewType = ViewType.DAY;
140 break;
141 case R.id.action_week:
142 viewType = ViewType.WEEK;
143 break;
144 case R.id.action_month:
145 viewType = ViewType.MONTH;
146 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700147 case R.id.action_agenda:
148 viewType = ViewType.AGENDA;
149 break;
Michael Chan3458a172010-07-13 17:58:21 -0700150 case R.id.action_today:
151 viewType = ViewType.CURRENT;
152 t = new Time();
153 t.setToNow();
154 break;
155 case R.id.action_create_event:
156 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0);
157 return true;
Michael Chan2c477fc2010-07-15 18:26:50 -0700158 case R.id.action_manage_calendars:
159 mController.sendEvent(this, EventType.LAUNCH_MANAGE_CALENDARS, null, null, 0, 0);
160 return true;
Michael Chanab29d9e2010-07-21 06:08:47 -0700161 case R.id.action_settings:
162 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
163 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700164 default:
165 return false;
166 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700167 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan3458a172010-07-13 17:58:21 -0700168 return true;
169 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700170
171 @Override
172 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
173 if (key.equals(CalendarPreferenceActivity.KEY_WEEK_START_DAY)) {
Michael Chand6734db2010-07-22 00:48:08 -0700174 initFragments(mController.getTime(), mController.getViewType());
Mason Tang8e3d4302010-07-12 17:39:30 -0700175 }
176 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700177
178 private void setMainPane(FragmentTransaction ft, int viewId, int viewType,
179 long timeMillis, boolean force) {
180 if(!force && mController.getPreviousViewType() == viewType) {
181 return;
182 }
183
184 // Deregister old view
185 Fragment frag = findFragmentById(viewId);
186 if (frag != null) {
187 mController.deregisterEventHandler((EventHandler) frag);
188 }
189
190 // Create new one
191 switch (viewType) {
192 case ViewType.AGENDA:
Mason Tang31df4262010-07-19 17:51:46 -0700193 frag = new AgendaFragment(timeMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700194 break;
195 case ViewType.DAY:
Michael Chan75d9b562010-07-26 16:54:25 -0700196 frag = new DayFragment(timeMillis, 1);
197 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700198 case ViewType.WEEK:
Michael Chan75d9b562010-07-26 16:54:25 -0700199 frag = new DayFragment(timeMillis, 7);
Michael Chanab29d9e2010-07-21 06:08:47 -0700200 break;
201 case ViewType.MONTH:
202 frag = new MonthFragment(false, timeMillis);
203 break;
204 default:
Michael Chand6734db2010-07-22 00:48:08 -0700205 throw new IllegalArgumentException(
206 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
Michael Chanab29d9e2010-07-21 06:08:47 -0700207 }
208
209 boolean doCommit = false;
210 if (ft == null) {
211 doCommit = true;
212 ft = openFragmentTransaction();
213 }
214
215 ft.replace(viewId, frag);
216 mController.registerEventHandler((EventHandler) frag);
217
218 if (doCommit) {
219 ft.commit();
220 }
221 }
222
223 private void setTitleInActionBar(EventInfo event) {
224 if (event.eventType != EventType.GO_TO) {
225 return;
226 }
227
228 long start = event.startTime.toMillis(false /* use isDst */);
229 long end = start;
230
Michael Chand6734db2010-07-22 00:48:08 -0700231 if (event.endTime != null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700232 end = event.endTime.toMillis(false /* use isDst */);
233 }
234 String msg = DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_DATE);
235
236 ActionBar ab = getActionBar();
237 if (ab != null) {
238 ab.setTitle(msg);
239 }
240 }
241
242 // EventHandler Interface
243 public long getSupportedEventTypes() {
244 return EventType.GO_TO;
245 }
246
247 // EventHandler Interface
248 public void handleEvent(EventInfo event) {
249 if (event.eventType == EventType.GO_TO) {
250 // Set title bar
251 setTitleInActionBar(event);
252
253 setMainPane(null, R.id.main_pane, event.viewType,
254 event.startTime.toMillis(false), false);
255
256 // FRAG_TODO only for XL screen
257 if (event.viewType == ViewType.MONTH) {
258 // hide minimonth and calendar frag
259 // show agenda view
260 } else {
261 // show minimonth and calendar frag
262 }
263 }
264 }
265
266 // EventHandler Interface
267 public void eventsChanged() {
268 }
269
270 // EventHandler Interface
271 public boolean getAllDay() {
272 return false;
273 }
274
275 // EventHandler Interface
276 public long getSelectedTime() {
277 return 0;
278 }
279
280 // EventHandler Interface
281 public void goTo(Time time, boolean animate) {
282 }
283
284 // EventHandler Interface
285 public void goToToday() {
286 }
Michael Chan49701592010-06-30 11:04:03 -0700287}