blob: 3d15c4cb2d37ba2f5c04c8c79a038637c77be41d [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
RoboErika27a8862011-06-23 15:26:23 -070019import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
20import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
RoboErika7c03902011-06-14 11:06:44 -070021import static android.provider.CalendarContract.Attendees.ATTENDEE_STATUS;
Isaac Katzenelson60f01c22011-06-03 15:42:01 -070022import static com.android.calendar.CalendarController.EVENT_ATTENDEE_RESPONSE;
Erikdd95df52010-08-27 09:31:18 -070023
Michael Chan83b0fe32010-07-08 16:46:26 -070024import com.android.calendar.CalendarController.EventHandler;
Michael Chanab29d9e2010-07-21 06:08:47 -070025import com.android.calendar.CalendarController.EventInfo;
Michael Chan83b0fe32010-07-08 16:46:26 -070026import com.android.calendar.CalendarController.EventType;
Michael Chan3458a172010-07-13 17:58:21 -070027import com.android.calendar.CalendarController.ViewType;
Mason Tang00b8c1a2010-08-23 12:02:00 -070028import com.android.calendar.agenda.AgendaFragment;
Erik981874e2010-10-05 16:52:52 -070029import com.android.calendar.month.MonthByWeekFragment;
Mason Tang9a3cb142010-07-27 12:16:41 -070030import com.android.calendar.selectcalendars.SelectCalendarsFragment;
Michael Chan83b0fe32010-07-08 16:46:26 -070031
Michael Chan5756bbc2011-03-02 18:22:07 -080032import android.animation.Animator;
33import android.animation.Animator.AnimatorListener;
RoboErikd97f7982011-03-02 15:13:05 -080034import android.animation.ObjectAnimator;
Michael Chanab29d9e2010-07-21 06:08:47 -070035import android.app.ActionBar;
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -070036import android.app.ActionBar.Tab;
Michael Chan49701592010-06-30 11:04:03 -070037import android.app.Activity;
Michael Chan83b0fe32010-07-08 16:46:26 -070038import android.app.Fragment;
Erik63cd0532011-01-26 14:16:03 -080039import android.app.FragmentManager;
Michael Chan3458a172010-07-13 17:58:21 -070040import android.app.FragmentTransaction;
Erik954c8712010-08-06 10:12:34 -070041import android.content.ContentResolver;
Isaac Katzenelson60f01c22011-06-03 15:42:01 -070042import android.content.ContentUris;
Isaac Katzenelson6a182582011-04-21 15:46:33 -070043import android.content.Context;
Erikdd95df52010-08-27 09:31:18 -070044import android.content.Intent;
Mason Tang8e3d4302010-07-12 17:39:30 -070045import android.content.SharedPreferences;
46import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Michael Chanab29d9e2010-07-21 06:08:47 -070047import android.content.res.Configuration;
Erikeaafa2b2010-12-23 14:30:37 -080048import android.content.res.Resources;
Erik954c8712010-08-06 10:12:34 -070049import android.database.ContentObserver;
Erikdd95df52010-08-27 09:31:18 -070050import android.net.Uri;
Michael Chan49701592010-06-30 11:04:03 -070051import android.os.Bundle;
Erik954c8712010-08-06 10:12:34 -070052import android.os.Handler;
RoboErika7c03902011-06-14 11:06:44 -070053import android.provider.CalendarContract;
54import android.provider.CalendarContract.Events;
Erik6c255102010-09-29 14:43:00 -070055import android.text.TextUtils;
56import android.text.format.DateFormat;
Michael Chan3458a172010-07-13 17:58:21 -070057import android.text.format.DateUtils;
Michael Chan83b0fe32010-07-08 16:46:26 -070058import android.text.format.Time;
Erik954c8712010-08-06 10:12:34 -070059import android.util.Log;
Erik2051f122010-07-02 13:45:45 -070060import android.view.Menu;
Michael Chan3458a172010-07-13 17:58:21 -070061import android.view.MenuItem;
Michael Chanab29d9e2010-07-21 06:08:47 -070062import android.view.View;
Erik259b8f92011-01-12 14:01:12 -080063import android.view.accessibility.AccessibilityEvent;
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -070064import android.widget.ArrayAdapter;
RoboErikde20d762011-02-08 17:52:25 -080065import android.widget.RelativeLayout;
66import android.widget.RelativeLayout.LayoutParams;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -070067import android.widget.SearchView;
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -070068import android.widget.SpinnerAdapter;
Erik6c255102010-09-29 14:43:00 -070069import android.widget.TextView;
70
Michael Chan2c7c8512010-12-10 14:12:57 -080071import java.util.List;
Erik6c255102010-09-29 14:43:00 -070072import java.util.Locale;
73import java.util.TimeZone;
Michael Chan49701592010-06-30 11:04:03 -070074
Michael Chanab29d9e2010-07-21 06:08:47 -070075public class AllInOneActivity extends Activity implements EventHandler,
Adam Powell28f9b352011-01-20 18:36:10 -080076 OnSharedPreferenceChangeListener, SearchView.OnQueryTextListener,
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -070077 ActionBar.TabListener, ActionBar.OnNavigationListener {
Michael Chand6734db2010-07-22 00:48:08 -070078 private static final String TAG = "AllInOneActivity";
Erik954c8712010-08-06 10:12:34 -070079 private static final boolean DEBUG = false;
Erik63cd0532011-01-26 14:16:03 -080080 private static final String EVENT_INFO_FRAGMENT_TAG = "EventInfoFragment";
Michael Chand6734db2010-07-22 00:48:08 -070081 private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
Erik7b92da22010-09-23 14:55:22 -070082 private static final String BUNDLE_KEY_EVENT_ID = "key_event_id";
RoboErik277eb972011-02-24 16:05:04 -080083 private static final String BUNDLE_KEY_RESTORE_VIEW = "key_restore_view";
Erik3f348f32010-08-10 13:17:19 -070084 private static final int HANDLER_KEY = 0;
RoboErikde20d762011-02-08 17:52:25 -080085 private static final long CONTROLS_ANIMATE_DURATION = 400;
Michael Chan5756bbc2011-03-02 18:22:07 -080086 private static int CONTROLS_ANIMATE_WIDTH = 267;
RoboErikde20d762011-02-08 17:52:25 -080087 private static float mScale = 0;
Erikeaafa2b2010-12-23 14:30:37 -080088
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -070089 // Indices of buttons for the drop down menu (tabs replacement)
90 // Must match the strings in the array buttons_list in arrays.xml and the
91 // OnNavigationListener
92 private static final int BUTTON_DAY_INDEX = 0;
93 private static final int BUTTON_WEEK_INDEX = 1;
94 private static final int BUTTON_MONTH_INDEX = 2;
95 private static final int BUTTON_AGENDA_INDEX = 3;
96
Michael Chan0558def2010-07-22 21:30:32 -070097 private static CalendarController mController;
Erik9fc409f2010-07-28 11:40:47 -070098 private static boolean mIsMultipane;
Isaac Katzenelson62967cb2011-06-14 13:27:20 -070099 private static boolean mIsTabletConfig;
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700100 private static boolean mShowAgendaWithMonth;
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700101 private static boolean mShowEventDetailsWithAgenda;
Michael Chan6191c122010-11-02 16:23:47 -0700102 private boolean mOnSaveInstanceStateCalled = false;
Erik954c8712010-08-06 10:12:34 -0700103 private ContentResolver mContentResolver;
Erik648c59c2010-08-13 12:49:26 -0700104 private int mPreviousView;
105 private int mCurrentView;
Erikcb811892010-09-28 13:44:19 -0700106 private boolean mPaused = true;
107 private boolean mUpdateOnResume = false;
RoboErikde20d762011-02-08 17:52:25 -0800108 private boolean mHideControls = false;
Michael Chan5756bbc2011-03-02 18:22:07 -0800109 private boolean mShowSideViews = true;
Erik6c255102010-09-29 14:43:00 -0700110 private TextView mHomeTime;
Erikeaafa2b2010-12-23 14:30:37 -0800111 private TextView mDateRange;
RoboErikde20d762011-02-08 17:52:25 -0800112 private View mMiniMonth;
113 private View mCalendarsList;
114 private View mMiniMonthContainer;
Isaac Katzenelson96a0b542011-04-13 13:43:20 -0700115 private View mSecondaryPane;
Erikc5f92d72010-11-11 16:18:34 -0800116 private String mTimeZone;
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700117 private boolean mShowCalendarControls;
Isaac Katzenelson60f01c22011-06-03 15:42:01 -0700118 private boolean mShowEventInfoFullScreen;
Erik6c255102010-09-29 14:43:00 -0700119
Michael Chan2c7c8512010-12-10 14:12:57 -0800120 private long mViewEventId = -1;
121 private long mIntentEventStartMillis = -1;
122 private long mIntentEventEndMillis = -1;
Michael Chandeced892010-12-10 15:59:35 -0800123 private int mIntentAttendeeResponse = CalendarController.ATTENDEE_NO_RESPONSE;
Michael Chan2c7c8512010-12-10 14:12:57 -0800124
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700125 // Action bar and Navigation bar (left side of Action bar)
126 private ActionBar mActionBar;
127 private ActionBar.Tab mDayTab;
128 private ActionBar.Tab mWeekTab;
129 private ActionBar.Tab mMonthTab;
Isaac Katzenelsonae7c1c62011-04-11 18:30:26 -0700130 private ActionBar.Tab mAgendaTab;
Erikcb830312011-01-19 14:41:41 -0800131 private SearchView mSearchView;
RoboErikde20d762011-02-08 17:52:25 -0800132 private MenuItem mControlsMenu;
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700133 private Menu mOptionsMenu;
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700134 private CalendarViewAdapter mActionBarMenuSpinnerAdapter;
RoboErikde20d762011-02-08 17:52:25 -0800135
Isaac Katzenelson5b2a9072011-04-08 15:23:57 -0700136 private String mHideString;
137 private String mShowString;
RoboErikde20d762011-02-08 17:52:25 -0800138
RoboErikb8549562011-02-15 14:25:23 -0800139 // Params for animating the controls on the right
Michael Chan5756bbc2011-03-02 18:22:07 -0800140 private LayoutParams mControlsParams = new LayoutParams(CONTROLS_ANIMATE_WIDTH, 0);
141
142 private AnimatorListener mSlideAnimationDoneListener = new AnimatorListener() {
143
144 @Override
145 public void onAnimationCancel(Animator animation) {
146 }
147
148 @Override
149 public void onAnimationEnd(android.animation.Animator animation) {
150 int visibility = mShowSideViews ? View.VISIBLE : View.GONE;
151 mMiniMonth.setVisibility(visibility);
152 mCalendarsList.setVisibility(visibility);
153 mMiniMonthContainer.setVisibility(visibility);
154 }
155
156 @Override
157 public void onAnimationRepeat(android.animation.Animator animation) {
158 }
159
160 @Override
161 public void onAnimationStart(android.animation.Animator animation) {
162 }
163 };
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700164
Erik6c255102010-09-29 14:43:00 -0700165 private Runnable mHomeTimeUpdater = new Runnable() {
166 @Override
167 public void run() {
168 updateHomeClock();
169 }
170 };
Erik954c8712010-08-06 10:12:34 -0700171
172 // Create an observer so that we can update the views whenever a
173 // Calendar event changes.
Erik7b92da22010-09-23 14:55:22 -0700174 private ContentObserver mObserver = new ContentObserver(new Handler()) {
Erik954c8712010-08-06 10:12:34 -0700175 @Override
176 public boolean deliverSelfNotifications() {
177 return true;
178 }
179
180 @Override
181 public void onChange(boolean selfChange) {
182 eventsChanged();
183 }
184 };
Michael Chan83b0fe32010-07-08 16:46:26 -0700185
Michael Chan49701592010-06-30 11:04:03 -0700186 @Override
Erik63cd0532011-01-26 14:16:03 -0800187 protected void onNewIntent(Intent intent) {
188 String action = intent.getAction();
189 if (Intent.ACTION_VIEW.equals(action)) {
190 parseViewAction(intent);
191 }
192 }
193
194 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -0700195 protected void onCreate(Bundle icicle) {
Erikca478672011-01-19 20:02:47 -0800196 if (Utils.getSharedPreference(this, OtherPreferences.KEY_OTHER_1, false)) {
197 setTheme(R.style.CalendarTheme_WithActionBarWallpaper);
198 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700199 super.onCreate(icicle);
200
201 // This needs to be created before setContentView
Michael Chan0558def2010-07-22 21:30:32 -0700202 mController = CalendarController.getInstance(this);
Erik954c8712010-08-06 10:12:34 -0700203 // Get time from intent or icicle
Michael Chan2c7c8512010-12-10 14:12:57 -0800204 long timeMillis = -1;
205 int viewType = -1;
Michael Chan2c7c8512010-12-10 14:12:57 -0800206 final Intent intent = getIntent();
Erik954c8712010-08-06 10:12:34 -0700207 if (icicle != null) {
208 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
RoboErik277eb972011-02-24 16:05:04 -0800209 viewType = icicle.getInt(BUNDLE_KEY_RESTORE_VIEW, -1);
Erik7b92da22010-09-23 14:55:22 -0700210 } else {
Michael Chan2c7c8512010-12-10 14:12:57 -0800211 String action = intent.getAction();
212 if (Intent.ACTION_VIEW.equals(action)) {
213 // Open EventInfo later
214 timeMillis = parseViewAction(intent);
215 }
216
217 if (timeMillis == -1) {
218 timeMillis = Utils.timeFromIntentInMillis(intent);
219 }
220 }
221
RoboErik277eb972011-02-24 16:05:04 -0800222 if (viewType == -1) {
Erik7b92da22010-09-23 14:55:22 -0700223 viewType = Utils.getViewTypeFromIntentAndSharedPref(this);
224 }
Erikc5f92d72010-11-11 16:18:34 -0800225 mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
226 Time t = new Time(mTimeZone);
Erik954c8712010-08-06 10:12:34 -0700227 t.set(timeMillis);
228
Michael Chan2c7c8512010-12-10 14:12:57 -0800229 if (icicle != null && intent != null) {
230 Log.d(TAG, "both, icicle:" + icicle.toString() + " intent:" + intent.toString());
Erik7b92da22010-09-23 14:55:22 -0700231 } else {
Michael Chan2c7c8512010-12-10 14:12:57 -0800232 Log.d(TAG, "not both, icicle:" + icicle + " intent:" + intent);
Erik7b92da22010-09-23 14:55:22 -0700233 }
234
Erikeaafa2b2010-12-23 14:30:37 -0800235 Resources res = getResources();
RoboErikde20d762011-02-08 17:52:25 -0800236 if (mScale == 0) {
237 mScale = res.getDisplayMetrics().density;
238 CONTROLS_ANIMATE_WIDTH *= mScale;
RoboErikde20d762011-02-08 17:52:25 -0800239 }
240 mHideString = res.getString(R.string.hide_controls);
241 mShowString = res.getString(R.string.show_controls);
RoboErikb8549562011-02-15 14:25:23 -0800242 mControlsParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
RoboErikb8549562011-02-15 14:25:23 -0800243
Isaac Katzenelson96a0b542011-04-13 13:43:20 -0700244 mIsMultipane = Utils.isMultiPaneConfiguration(this);
Isaac Katzenelson62967cb2011-06-14 13:27:20 -0700245 mIsTabletConfig = Utils.getConfigBool(this, R.bool.tablet_config);
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700246 mShowAgendaWithMonth = Utils.getConfigBool(this, R.bool.show_agenda_with_month);
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700247 mShowCalendarControls = Utils.getConfigBool(this, R.bool.show_calendar_controls);
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700248 mShowEventDetailsWithAgenda =
249 Utils.getConfigBool(this, R.bool.show_event_details_with_agenda);
Isaac Katzenelson60f01c22011-06-03 15:42:01 -0700250 mShowEventInfoFullScreen =
251 Utils.getConfigBool(this, R.bool.show_event_info_full_screen);
Erik9fc409f2010-07-28 11:40:47 -0700252
Erik63cd0532011-01-26 14:16:03 -0800253 Utils.setAllowWeekForDetailView(mIsMultipane);
Michael Chanb60218a2010-12-14 16:34:39 -0800254
Erikbe8191c2010-10-26 16:50:08 -0700255 // setContentView must be called before configureActionBar
256 setContentView(R.layout.all_in_one);
Isaac Katzenelson62967cb2011-06-14 13:27:20 -0700257
258 if (mIsTabletConfig) {
259 mDateRange = (TextView) findViewById(R.id.date_bar);
260 } else {
261 mDateRange = (TextView) getLayoutInflater().inflate(R.layout.date_range_title, null);
262 }
263
Erikbe8191c2010-10-26 16:50:08 -0700264 // configureActionBar auto-selects the first tab you add, so we need to
265 // call it before we set up our own fragments to make sure it doesn't
266 // overwrite us
Isaac Katzenelsona457a1b2011-06-21 15:13:57 -0700267 configureActionBar(viewType);
Erikbe8191c2010-10-26 16:50:08 -0700268
Erik3f348f32010-08-10 13:17:19 -0700269 // Must be the first to register because this activity can modify the
270 // list of event handlers in it's handle method. This affects who the
271 // rest of the handlers the controller dispatches to are.
272 mController.registerEventHandler(HANDLER_KEY, this);
Michael Chan83b0fe32010-07-08 16:46:26 -0700273
Erik6c255102010-09-29 14:43:00 -0700274 mHomeTime = (TextView) findViewById(R.id.home_time);
RoboErikde20d762011-02-08 17:52:25 -0800275 mMiniMonth = findViewById(R.id.mini_month);
276 mCalendarsList = findViewById(R.id.calendar_list);
277 mMiniMonthContainer = findViewById(R.id.mini_month_container);
Isaac Katzenelson96a0b542011-04-13 13:43:20 -0700278 mSecondaryPane = findViewById(R.id.secondary_pane);
Michael Chan83b0fe32010-07-08 16:46:26 -0700279
Erik7b92da22010-09-23 14:55:22 -0700280 initFragments(timeMillis, viewType, icicle);
Mason Tang8e3d4302010-07-12 17:39:30 -0700281
282 // Listen for changes that would require this to be refreshed
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700283 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700284 prefs.registerOnSharedPreferenceChangeListener(this);
Erikcb811892010-09-28 13:44:19 -0700285
Erik954c8712010-08-06 10:12:34 -0700286 mContentResolver = getContentResolver();
Erikbe8191c2010-10-26 16:50:08 -0700287 }
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700288
Michael Chan2c7c8512010-12-10 14:12:57 -0800289 private long parseViewAction(final Intent intent) {
290 long timeMillis = -1;
291 Uri data = intent.getData();
292 if (data != null && data.isHierarchical()) {
293 List<String> path = data.getPathSegments();
294 if (path.size() == 2 && path.get(0).equals("events")) {
295 try {
296 mViewEventId = Long.valueOf(data.getLastPathSegment());
Isaac Katzenelson96a0b542011-04-13 13:43:20 -0700297 if (mViewEventId != -1) {
RoboErika27a8862011-06-23 15:26:23 -0700298 mIntentEventStartMillis = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, 0);
299 mIntentEventEndMillis = intent.getLongExtra(EXTRA_EVENT_END_TIME, 0);
Michael Chan2c7c8512010-12-10 14:12:57 -0800300 mIntentAttendeeResponse = intent.getIntExtra(
Michael Chandeced892010-12-10 15:59:35 -0800301 ATTENDEE_STATUS, CalendarController.ATTENDEE_NO_RESPONSE);
Michael Chan2c7c8512010-12-10 14:12:57 -0800302 timeMillis = mIntentEventStartMillis;
303 }
304 } catch (NumberFormatException e) {
305 // Ignore if mViewEventId can't be parsed
306 }
307 }
308 }
309 return timeMillis;
310 }
311
Isaac Katzenelsona457a1b2011-06-21 15:13:57 -0700312 private void configureActionBar(int viewType) {
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -0700313 if (mIsTabletConfig) {
314 createTabs();
315 } else {
Isaac Katzenelsona457a1b2011-06-21 15:13:57 -0700316 createButtonsSpinner(viewType);
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -0700317 mActionBar.setCustomView(mDateRange);
318 }
Isaac Katzenelson53aad912011-07-01 12:15:09 -0700319 if (mIsMultipane) {
320 mActionBar.setDisplayOptions(
321 ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
322 } else {
323 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
324 }
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -0700325 }
326
327 private void createTabs() {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700328 mActionBar = getActionBar();
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700329 if (mActionBar == null) {
330 Log.w(TAG, "ActionBar is null.");
331 } else {
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -0700332 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700333 mDayTab = mActionBar.newTab();
334 mDayTab.setText(getString(R.string.day_view));
335 mDayTab.setTabListener(this);
336 mActionBar.addTab(mDayTab);
337 mWeekTab = mActionBar.newTab();
338 mWeekTab.setText(getString(R.string.week_view));
339 mWeekTab.setTabListener(this);
340 mActionBar.addTab(mWeekTab);
341 mMonthTab = mActionBar.newTab();
342 mMonthTab.setText(getString(R.string.month_view));
343 mMonthTab.setTabListener(this);
344 mActionBar.addTab(mMonthTab);
Isaac Katzenelsonae7c1c62011-04-11 18:30:26 -0700345 mAgendaTab = mActionBar.newTab();
346 mAgendaTab.setText(getString(R.string.agenda_view));
347 mAgendaTab.setTabListener(this);
348 mActionBar.addTab(mAgendaTab);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700349 }
Erik954c8712010-08-06 10:12:34 -0700350 }
351
Isaac Katzenelsona457a1b2011-06-21 15:13:57 -0700352 private void createButtonsSpinner(int viewType) {
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700353 mActionBarMenuSpinnerAdapter = new CalendarViewAdapter (this, viewType);
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -0700354 mActionBar = getActionBar();
355 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
356 mActionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_USE_LOGO|
357 ActionBar.DISPLAY_SHOW_TITLE);
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700358 mActionBar.setListNavigationCallbacks(mActionBarMenuSpinnerAdapter, this);
Isaac Katzenelsona457a1b2011-06-21 15:13:57 -0700359 switch (viewType) {
360 case ViewType.AGENDA:
361 mActionBar.setSelectedNavigationItem(BUTTON_AGENDA_INDEX);
362 break;
363 case ViewType.DAY:
364 mActionBar.setSelectedNavigationItem(BUTTON_DAY_INDEX);
365 break;
366 case ViewType.WEEK:
367 mActionBar.setSelectedNavigationItem(BUTTON_WEEK_INDEX);
368 break;
369 case ViewType.MONTH:
370 mActionBar.setSelectedNavigationItem(BUTTON_MONTH_INDEX);
371 break;
372 default:
373 mActionBar.setSelectedNavigationItem(BUTTON_DAY_INDEX);
374 break;
375 }
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -0700376 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700377 // Clear buttons used in the agenda view
378 private void clearOptionsMenu() {
379 if (mOptionsMenu == null) {
380 return;
381 }
382 MenuItem cancelItem = mOptionsMenu.findItem(R.id.action_cancel);
383 MenuItem deleteItem = mOptionsMenu.findItem(R.id.action_delete);
384 MenuItem editItem = mOptionsMenu.findItem(R.id.action_edit);
385 if (cancelItem != null) {
386 cancelItem.setVisible(false);
387 }
388 if (deleteItem != null) {
389 deleteItem.setVisible(false);
390 }
391 if (editItem != null) {
392 editItem.setVisible(false);
393 }
394 }
395
Erik954c8712010-08-06 10:12:34 -0700396 @Override
397 protected void onResume() {
398 super.onResume();
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700399 mContentResolver.registerContentObserver(CalendarContract.Events.CONTENT_URI,
400 true, mObserver);
Erikcb811892010-09-28 13:44:19 -0700401 if (mUpdateOnResume) {
402 initFragments(mController.getTime(), mController.getViewType(), null);
403 mUpdateOnResume = false;
404 }
Erik6c255102010-09-29 14:43:00 -0700405 updateHomeClock();
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700406 // Make sure the drop-down menu will get its date updated at midnight
407 if (mActionBarMenuSpinnerAdapter != null) {
408 mActionBarMenuSpinnerAdapter.setMidnightHandler();
409 }
410
RoboErik3b3af612011-02-24 13:42:57 -0800411 if (mControlsMenu != null) {
412 mControlsMenu.setTitle(mHideControls ? mShowString : mHideString);
413 }
Erikcb811892010-09-28 13:44:19 -0700414 mPaused = false;
Michael Chan6191c122010-11-02 16:23:47 -0700415 mOnSaveInstanceStateCalled = false;
Michael Chan2c7c8512010-12-10 14:12:57 -0800416
417 if (mViewEventId != -1 && mIntentEventStartMillis != -1 && mIntentEventEndMillis != -1) {
RoboErikf6b58a92011-02-24 14:22:11 -0800418 long currentMillis = System.currentTimeMillis();
419 long selectedTime = -1;
420 if (currentMillis > mIntentEventStartMillis && currentMillis < mIntentEventEndMillis) {
421 selectedTime = currentMillis;
422 }
Michael Chanf0868f62011-01-11 19:37:35 -0800423 mController.sendEventRelatedEventWithResponse(this, EventType.VIEW_EVENT, mViewEventId,
Isaac Katzenelson96a0b542011-04-13 13:43:20 -0700424 mIntentEventStartMillis, mIntentEventEndMillis, -1, -1,
425 mIntentAttendeeResponse, selectedTime);
Michael Chan2c7c8512010-12-10 14:12:57 -0800426 mViewEventId = -1;
427 mIntentEventStartMillis = -1;
428 mIntentEventEndMillis = -1;
429 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700430 }
431
432 @Override
Michael Chand6734db2010-07-22 00:48:08 -0700433 protected void onPause() {
434 super.onPause();
Erikcb811892010-09-28 13:44:19 -0700435 mPaused = true;
Daisuke Miyakawac9e53072010-10-14 12:18:49 -0700436 mHomeTime.removeCallbacks(mHomeTimeUpdater);
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700437 if (mActionBarMenuSpinnerAdapter != null) {
438 mActionBarMenuSpinnerAdapter.resetMidnightHandler();
439 }
Erik954c8712010-08-06 10:12:34 -0700440 mContentResolver.unregisterContentObserver(mObserver);
Erikcb811892010-09-28 13:44:19 -0700441 if (isFinishing()) {
442 // Stop listening for changes that would require this to be refreshed
443 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
444 prefs.unregisterOnSharedPreferenceChangeListener(this);
445 }
Erik7b92da22010-09-23 14:55:22 -0700446 // FRAG_TODO save highlighted days of the week;
Erikdd95df52010-08-27 09:31:18 -0700447 if (mController.getViewType() != ViewType.EDIT) {
448 Utils.setDefaultView(this, mController.getViewType());
449 }
Michael Chand6734db2010-07-22 00:48:08 -0700450 }
451
452 @Override
Erik7b92da22010-09-23 14:55:22 -0700453 protected void onUserLeaveHint() {
454 mController.sendEvent(this, EventType.USER_HOME, null, null, -1, ViewType.CURRENT);
455 super.onUserLeaveHint();
456 }
457
458 @Override
Michael Chand6734db2010-07-22 00:48:08 -0700459 public void onSaveInstanceState(Bundle outState) {
Michael Chan6191c122010-11-02 16:23:47 -0700460 mOnSaveInstanceStateCalled = true;
Michael Chand6734db2010-07-22 00:48:08 -0700461 super.onSaveInstanceState(outState);
462
463 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
Erik7b92da22010-09-23 14:55:22 -0700464 if (mCurrentView == ViewType.EDIT) {
RoboErik277eb972011-02-24 16:05:04 -0800465 outState.putInt(BUNDLE_KEY_RESTORE_VIEW, mCurrentView);
Erik7b92da22010-09-23 14:55:22 -0700466 outState.putLong(BUNDLE_KEY_EVENT_ID, mController.getEventId());
467 }
Michael Chand6734db2010-07-22 00:48:08 -0700468 }
469
470 @Override
Mason Tang8e3d4302010-07-12 17:39:30 -0700471 protected void onDestroy() {
472 super.onDestroy();
473
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700474 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700475 prefs.unregisterOnSharedPreferenceChangeListener(this);
Eriked61b482010-08-13 13:59:50 -0700476 CalendarController.removeInstance(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700477 }
478
Erik7b92da22010-09-23 14:55:22 -0700479 private void initFragments(long timeMillis, int viewType, Bundle icicle) {
Dianne Hackborn12b33302011-01-17 12:30:13 -0800480 FragmentTransaction ft = getFragmentManager().beginTransaction();
Michael Chan3458a172010-07-13 17:58:21 -0700481
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700482 if (mShowCalendarControls) {
Erik40bcd102010-11-16 15:46:40 -0800483 Fragment miniMonthFrag = new MonthByWeekFragment(timeMillis, true);
Michael Chanab29d9e2010-07-21 06:08:47 -0700484 ft.replace(R.id.mini_month, miniMonthFrag);
Erik3f348f32010-08-10 13:17:19 -0700485 mController.registerEventHandler(R.id.mini_month, (EventHandler) miniMonthFrag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700486
487 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
488 ft.replace(R.id.calendar_list, selectCalendarsFrag);
RoboErik8d1b2fd2011-02-25 10:24:43 -0800489 mController.registerEventHandler(
490 R.id.calendar_list, (EventHandler) selectCalendarsFrag);
Erikdd95df52010-08-27 09:31:18 -0700491 }
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700492 if (!mShowCalendarControls || viewType == ViewType.EDIT) {
RoboErikde20d762011-02-08 17:52:25 -0800493 mMiniMonth.setVisibility(View.GONE);
494 mCalendarsList.setVisibility(View.GONE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700495 }
Michael Chan3458a172010-07-13 17:58:21 -0700496
Erikdd95df52010-08-27 09:31:18 -0700497 EventInfo info = null;
498 if (viewType == ViewType.EDIT) {
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700499 mPreviousView = GeneralPreferences.getSharedPreferences(this).getInt(
Erik7b92da22010-09-23 14:55:22 -0700500 GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW);
Erikdd95df52010-08-27 09:31:18 -0700501
Daisuke Miyakawa4143cfa2010-09-17 19:55:04 -0700502 long eventId = -1;
Erikdd95df52010-08-27 09:31:18 -0700503 Intent intent = getIntent();
504 Uri data = intent.getData();
505 if (data != null) {
506 try {
Daisuke Miyakawa4143cfa2010-09-17 19:55:04 -0700507 eventId = Long.parseLong(data.getLastPathSegment());
Erikdd95df52010-08-27 09:31:18 -0700508 } catch (NumberFormatException e) {
509 if (DEBUG) {
510 Log.d(TAG, "Create new event");
511 }
512 }
Erik7b92da22010-09-23 14:55:22 -0700513 } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
514 eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
Erikdd95df52010-08-27 09:31:18 -0700515 }
Daisuke Miyakawa4143cfa2010-09-17 19:55:04 -0700516
RoboErika27a8862011-06-23 15:26:23 -0700517 long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
518 long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
Erikdd95df52010-08-27 09:31:18 -0700519 info = new EventInfo();
520 if (end != -1) {
521 info.endTime = new Time();
522 info.endTime.set(end);
523 }
524 if (begin != -1) {
525 info.startTime = new Time();
526 info.startTime.set(begin);
527 }
528 info.id = eventId;
529 // We set the viewtype so if the user presses back when they are
530 // done editing the controller knows we were in the Edit Event
Erik7b92da22010-09-23 14:55:22 -0700531 // screen. Likewise for eventId
Erikdd95df52010-08-27 09:31:18 -0700532 mController.setViewType(viewType);
Erik7b92da22010-09-23 14:55:22 -0700533 mController.setEventId(eventId);
Erikdd95df52010-08-27 09:31:18 -0700534 } else {
535 mPreviousView = viewType;
536 }
Isaac Katzenelson96a0b542011-04-13 13:43:20 -0700537
Erik63cd0532011-01-26 14:16:03 -0800538 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -0700539 ft.commit(); // this needs to be after setMainPane()
540
Erikc5f92d72010-11-11 16:18:34 -0800541 Time t = new Time(mTimeZone);
Michael Chand6734db2010-07-22 00:48:08 -0700542 t.set(timeMillis);
Erikdd95df52010-08-27 09:31:18 -0700543 if (viewType != ViewType.EDIT) {
544 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
545 }
Michael Chan49701592010-06-30 11:04:03 -0700546 }
Erik2051f122010-07-02 13:45:45 -0700547
548 @Override
Erik648c59c2010-08-13 12:49:26 -0700549 public void onBackPressed() {
Daisuke Miyakawa93e69eb2010-10-22 18:54:38 -0700550 if (mCurrentView == ViewType.EDIT || mCurrentView == ViewType.DETAIL) {
Erikdd95df52010-08-27 09:31:18 -0700551 mController.sendEvent(this, EventType.GO_TO, null, null, -1, mPreviousView);
Daisuke Miyakawa93e69eb2010-10-22 18:54:38 -0700552 } else {
553 super.onBackPressed();
Erik648c59c2010-08-13 12:49:26 -0700554 }
555 }
556
557 @Override
Erik2051f122010-07-02 13:45:45 -0700558 public boolean onCreateOptionsMenu(Menu menu) {
559 super.onCreateOptionsMenu(menu);
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700560 mOptionsMenu = menu;
Erik2051f122010-07-02 13:45:45 -0700561 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700562
Erikcb830312011-01-19 14:41:41 -0800563 mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
564 if (mSearchView != null) {
565 mSearchView.setIconifiedByDefault(true);
Adam Powell28f9b352011-01-20 18:36:10 -0800566 mSearchView.setOnQueryTextListener(this);
Erikcb830312011-01-19 14:41:41 -0800567 mSearchView.setSubmitButtonEnabled(true);
Michael Chan09b1b2e2011-04-05 11:24:32 -0700568 if (!mIsMultipane)
569 mSearchView.setVisibility(View.GONE);
Michael Chan71b221a2010-10-25 17:55:44 -0700570 }
Isaac Katzenelson5b2a9072011-04-08 15:23:57 -0700571
572 // Hide the "show/hide controls" button if this is a phone
573 // or the view type is "Month".
574
RoboErikde20d762011-02-08 17:52:25 -0800575 mControlsMenu = menu.findItem(R.id.action_hide_controls);
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700576 if (!mShowCalendarControls) {
Isaac Katzenelson5b2a9072011-04-08 15:23:57 -0700577 if (mControlsMenu != null) {
578 mControlsMenu.setVisible(false);
579 mControlsMenu.setEnabled(false);
580 }
581 } else if (mControlsMenu != null && mController != null
582 && mController.getViewType() == ViewType.MONTH) {
RoboErikde20d762011-02-08 17:52:25 -0800583 mControlsMenu.setVisible(false);
584 mControlsMenu.setEnabled(false);
585 }
Erik2051f122010-07-02 13:45:45 -0700586 return true;
587 }
Michael Chan3458a172010-07-13 17:58:21 -0700588
589 @Override
590 public boolean onOptionsItemSelected(MenuItem item) {
591 Time t = null;
592 int viewType = ViewType.CURRENT;
593 switch (item.getItemId()) {
Erikba1b94a2010-07-20 17:50:50 -0700594 case R.id.action_refresh:
595 mController.refreshCalendars();
596 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700597 case R.id.action_today:
598 viewType = ViewType.CURRENT;
Erikc5f92d72010-11-11 16:18:34 -0800599 t = new Time(mTimeZone);
Michael Chan3458a172010-07-13 17:58:21 -0700600 t.setToNow();
601 break;
602 case R.id.action_create_event:
RoboErik0271b412011-03-01 15:34:53 -0800603 t = new Time();
604 t.set(mController.getTime());
605 if (t.minute >= 30) {
RoboErikd97f7982011-03-02 15:13:05 -0800606 t.hour++;
RoboErik0271b412011-03-01 15:34:53 -0800607 t.minute = 0;
RoboErikd97f7982011-03-02 15:13:05 -0800608 } else {
609 t.minute = 30;
RoboErik0271b412011-03-01 15:34:53 -0800610 }
611 mController.sendEventRelatedEvent(
612 this, EventType.CREATE_EVENT, -1, t.toMillis(true), 0, 0, 0, -1);
Michael Chan3458a172010-07-13 17:58:21 -0700613 return true;
Michael Chanab29d9e2010-07-21 06:08:47 -0700614 case R.id.action_settings:
615 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
616 return true;
RoboErikde20d762011-02-08 17:52:25 -0800617 case R.id.action_hide_controls:
618 mHideControls = !mHideControls;
619 item.setTitle(mHideControls ? mShowString : mHideString);
RoboErikb8549562011-02-15 14:25:23 -0800620 final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this, "controlsOffset",
621 mHideControls ? 0 : CONTROLS_ANIMATE_WIDTH,
622 mHideControls ? CONTROLS_ANIMATE_WIDTH : 0);
623 slideAnimation.setDuration(CONTROLS_ANIMATE_DURATION);
624 ObjectAnimator.setFrameDelay(0);
625 slideAnimation.start();
RoboErikde20d762011-02-08 17:52:25 -0800626 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700627 default:
628 return false;
629 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700630 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan3458a172010-07-13 17:58:21 -0700631 return true;
632 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700633
RoboErikb8549562011-02-15 14:25:23 -0800634 /**
635 * Sets the offset of the controls on the right for animating them off/on
636 * screen. ProGuard strips this if it's not in proguard.flags
637 *
638 * @param controlsOffset The current offset in pixels
639 */
640 public void setControlsOffset(int controlsOffset) {
641 mMiniMonth.setTranslationX(controlsOffset);
642 mCalendarsList.setTranslationX(controlsOffset);
643 mHomeTime.setTranslationX(controlsOffset);
Michael Chan5756bbc2011-03-02 18:22:07 -0800644 mControlsParams.width = Math.max(0, CONTROLS_ANIMATE_WIDTH - controlsOffset);
RoboErikb8549562011-02-15 14:25:23 -0800645 mMiniMonthContainer.setLayoutParams(mControlsParams);
646 }
RoboErikde20d762011-02-08 17:52:25 -0800647
Mason Tang8e3d4302010-07-12 17:39:30 -0700648 @Override
649 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700650 if (key.equals(GeneralPreferences.KEY_WEEK_START_DAY)) {
Erikcb811892010-09-28 13:44:19 -0700651 if (mPaused) {
652 mUpdateOnResume = true;
653 } else {
654 initFragments(mController.getTime(), mController.getViewType(), null);
655 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700656 }
657 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700658
Erik63cd0532011-01-26 14:16:03 -0800659 private void setMainPane(
660 FragmentTransaction ft, int viewId, int viewType, long timeMillis, boolean force) {
Michael Chan6191c122010-11-02 16:23:47 -0700661 if (mOnSaveInstanceStateCalled) {
662 return;
663 }
Erik7b92da22010-09-23 14:55:22 -0700664 if (!force && mCurrentView == viewType) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700665 return;
666 }
667
Michael Chan5756bbc2011-03-02 18:22:07 -0800668 // Remove this when transition to and from month view looks fine.
669 boolean doTransition = viewType != ViewType.MONTH && mCurrentView != ViewType.MONTH;
RoboErikd725bfc2011-06-20 14:09:09 -0700670 FragmentManager fragmentManager = getFragmentManager();
671 // Check if our previous view was an Agenda view
672 // TODO remove this if framework ever supports nested fragments
673 if (mCurrentView == ViewType.AGENDA) {
674 // If it was, we need to do some cleanup on it to prevent the
675 // edit/delete buttons from coming back on a rotation.
676 Fragment oldFrag = fragmentManager.findFragmentById(viewId);
677 if (oldFrag instanceof AgendaFragment) {
678 ((AgendaFragment) oldFrag).removeFragments(fragmentManager);
679 }
680 }
Michael Chan5756bbc2011-03-02 18:22:07 -0800681
Erik648c59c2010-08-13 12:49:26 -0700682 if (viewType != mCurrentView) {
683 // The rules for this previous view are different than the
684 // controller's and are used for intercepting the back button.
Erikdd95df52010-08-27 09:31:18 -0700685 if (mCurrentView != ViewType.EDIT && mCurrentView > 0) {
686 mPreviousView = mCurrentView;
687 }
Erik648c59c2010-08-13 12:49:26 -0700688 mCurrentView = viewType;
689 }
Erik3f348f32010-08-10 13:17:19 -0700690 // Create new fragment
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700691 Fragment frag = null;
692 Fragment secFrag = null;
Michael Chanab29d9e2010-07-21 06:08:47 -0700693 switch (viewType) {
694 case ViewType.AGENDA:
Isaac Katzenelsonae7c1c62011-04-11 18:30:26 -0700695 if (mActionBar != null && (mActionBar.getSelectedTab() != mAgendaTab)) {
696 mActionBar.selectTab(mAgendaTab);
697 }
Isaac Katzenelson01d36972011-06-29 11:29:35 -0700698 if (mActionBarMenuSpinnerAdapter != null) {
699 mActionBar.setSelectedNavigationItem(CalendarViewAdapter.AGENDA_BUTTON_INDEX);
700 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700701 frag = new AgendaFragment(timeMillis, false);
Michael Chanab29d9e2010-07-21 06:08:47 -0700702 break;
703 case ViewType.DAY:
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700704 if (mActionBar != null && (mActionBar.getSelectedTab() != mDayTab)) {
705 mActionBar.selectTab(mDayTab);
706 }
Isaac Katzenelson01d36972011-06-29 11:29:35 -0700707 if (mActionBarMenuSpinnerAdapter != null) {
708 mActionBar.setSelectedNavigationItem(CalendarViewAdapter.DAY_BUTTON_INDEX);
709 }
Michael Chan75d9b562010-07-26 16:54:25 -0700710 frag = new DayFragment(timeMillis, 1);
711 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700712 case ViewType.WEEK:
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700713 if (mActionBar != null && (mActionBar.getSelectedTab() != mWeekTab)) {
714 mActionBar.selectTab(mWeekTab);
715 }
Isaac Katzenelson01d36972011-06-29 11:29:35 -0700716 if (mActionBarMenuSpinnerAdapter != null) {
717 mActionBar.setSelectedNavigationItem(CalendarViewAdapter.WEEK_BUTTON_INDEX);
718 }
Michael Chan75d9b562010-07-26 16:54:25 -0700719 frag = new DayFragment(timeMillis, 7);
Michael Chanab29d9e2010-07-21 06:08:47 -0700720 break;
721 case ViewType.MONTH:
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700722 if (mActionBar != null && (mActionBar.getSelectedTab() != mMonthTab)) {
723 mActionBar.selectTab(mMonthTab);
724 }
Isaac Katzenelson01d36972011-06-29 11:29:35 -0700725 if (mActionBarMenuSpinnerAdapter != null) {
726 mActionBar.setSelectedNavigationItem(CalendarViewAdapter.MONTH_BUTTON_INDEX);
727 }
Erik40bcd102010-11-16 15:46:40 -0800728 frag = new MonthByWeekFragment(timeMillis, false);
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700729 if (mShowAgendaWithMonth) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700730 secFrag = new AgendaFragment(timeMillis, false);
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700731 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700732 break;
733 default:
Michael Chand6734db2010-07-22 00:48:08 -0700734 throw new IllegalArgumentException(
735 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
Michael Chanab29d9e2010-07-21 06:08:47 -0700736 }
737
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700738 // Update the current view so that the menu can update its look according to the
739 // current view.
740 if (!mIsTabletConfig && mActionBarMenuSpinnerAdapter != null) {
741 mActionBarMenuSpinnerAdapter.setMainView(viewType);
742 }
743
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700744 // Clear unnecessary buttons from the option menu when switching from the agenda view
745 if (viewType != ViewType.AGENDA) {
746 clearOptionsMenu();
Isaac Katzenelson62967cb2011-06-14 13:27:20 -0700747 if (mIsTabletConfig) {
748 mDateRange.setVisibility(View.VISIBLE);
749 }
750 } else {
751 if (mIsTabletConfig) {
752 mDateRange.setVisibility(View.GONE);
753 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700754 }
755
Michael Chanab29d9e2010-07-21 06:08:47 -0700756 boolean doCommit = false;
757 if (ft == null) {
758 doCommit = true;
RoboErikd725bfc2011-06-20 14:09:09 -0700759 ft = fragmentManager.beginTransaction();
Michael Chanab29d9e2010-07-21 06:08:47 -0700760 }
761
Michael Chan5756bbc2011-03-02 18:22:07 -0800762 if (doTransition) {
763 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
764 }
765
Michael Chanab29d9e2010-07-21 06:08:47 -0700766 ft.replace(viewId, frag);
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700767 if (mShowAgendaWithMonth) {
Daisuke Miyakawa93e69eb2010-10-22 18:54:38 -0700768
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700769 // Show/hide secondary fragment
770
771 if (secFrag != null) {
772 ft.replace(R.id.secondary_pane, secFrag);
773 mSecondaryPane.setVisibility(View.VISIBLE);
774 } else {
775 mSecondaryPane.setVisibility(View.GONE);
RoboErikd725bfc2011-06-20 14:09:09 -0700776 Fragment f = fragmentManager.findFragmentById(R.id.secondary_pane);
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700777 if (f != null) {
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700778 ft.remove(f);
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700779 }
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700780 mController.deregisterEventHandler(R.id.secondary_pane);
781 }
782 }
Erik954c8712010-08-06 10:12:34 -0700783 if (DEBUG) {
784 Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
785 }
Erik3f348f32010-08-10 13:17:19 -0700786 // If the key is already registered this will replace it
787 mController.registerEventHandler(viewId, (EventHandler) frag);
Isaac Katzenelson6a182582011-04-21 15:46:33 -0700788 if (secFrag != null) {
789 mController.registerEventHandler(viewId, (EventHandler) secFrag);
790 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700791
792 if (doCommit) {
Michael Chane8f45c62011-03-01 14:41:03 -0800793 Log.d(TAG, "setMainPane AllInOne=" + this + " finishing:" + this.isFinishing());
Michael Chanab29d9e2010-07-21 06:08:47 -0700794 ft.commit();
795 }
796 }
797
798 private void setTitleInActionBar(EventInfo event) {
Erikeaafa2b2010-12-23 14:30:37 -0800799 if (event.eventType != EventType.UPDATE_TITLE || mActionBar == null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700800 return;
801 }
802
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700803 final long start = event.startTime.toMillis(false /* use isDst */);
804 final long end;
Michael Chand6734db2010-07-22 00:48:08 -0700805 if (event.endTime != null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700806 end = event.endTime.toMillis(false /* use isDst */);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700807 } else {
808 end = start;
Michael Chanab29d9e2010-07-21 06:08:47 -0700809 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700810
Erikeaafa2b2010-12-23 14:30:37 -0800811 final String msg = Utils.formatDateRange(this, start, end, (int) event.extraLong);
Erik259b8f92011-01-12 14:01:12 -0800812 CharSequence oldDate = mDateRange.getText();
Erikeaafa2b2010-12-23 14:30:37 -0800813 mDateRange.setText(msg);
Erik259b8f92011-01-12 14:01:12 -0800814 if (!TextUtils.equals(oldDate, msg)) {
815 mDateRange.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
816 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700817 }
818
Erik6c255102010-09-29 14:43:00 -0700819 private void updateHomeClock() {
Erikc5f92d72010-11-11 16:18:34 -0800820 mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
Erik6c255102010-09-29 14:43:00 -0700821 if (mIsMultipane && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK)
Erikc5f92d72010-11-11 16:18:34 -0800822 && !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())) {
823 Time time = new Time(mTimeZone);
Erik6c255102010-09-29 14:43:00 -0700824 time.setToNow();
825 long millis = time.toMillis(true);
826 boolean isDST = time.isDst != 0;
827 int flags = DateUtils.FORMAT_SHOW_TIME;
828 if (DateFormat.is24HourFormat(this)) {
829 flags |= DateUtils.FORMAT_24HOUR;
830 }
831 // Formats the time as
Erikc5f92d72010-11-11 16:18:34 -0800832 String timeString = (new StringBuilder(
833 Utils.formatDateRange(this, millis, millis, flags))).append(" ").append(
834 TimeZone.getTimeZone(mTimeZone).getDisplayName(
835 isDST, TimeZone.SHORT, Locale.getDefault())).toString();
Erik6c255102010-09-29 14:43:00 -0700836 mHomeTime.setText(timeString);
837 mHomeTime.setVisibility(View.VISIBLE);
838 // Update when the minute changes
839 mHomeTime.postDelayed(
Daisuke Miyakawac9e53072010-10-14 12:18:49 -0700840 mHomeTimeUpdater,
841 DateUtils.MINUTE_IN_MILLIS - (millis % DateUtils.MINUTE_IN_MILLIS));
Erik6c255102010-09-29 14:43:00 -0700842 } else {
843 mHomeTime.setVisibility(View.GONE);
844 }
845 }
846
Erik3f348f32010-08-10 13:17:19 -0700847 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700848 public long getSupportedEventTypes() {
Erikeaafa2b2010-12-23 14:30:37 -0800849 return EventType.GO_TO | EventType.VIEW_EVENT | EventType.UPDATE_TITLE;
Michael Chanab29d9e2010-07-21 06:08:47 -0700850 }
851
Erik3f348f32010-08-10 13:17:19 -0700852 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700853 public void handleEvent(EventInfo event) {
Michael Chane8f45c62011-03-01 14:41:03 -0800854 Log.d(TAG, "handleEvent AllInOne=" + this);
Michael Chanab29d9e2010-07-21 06:08:47 -0700855 if (event.eventType == EventType.GO_TO) {
Erik63cd0532011-01-26 14:16:03 -0800856 setMainPane(
857 null, R.id.main_pane, event.viewType, event.startTime.toMillis(false), false);
Erikaa3dde12011-02-03 11:16:23 -0800858 if (mSearchView != null) {
859 mSearchView.clearFocus();
860 }
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700861
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700862 if (mShowCalendarControls) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700863 if (event.viewType == ViewType.MONTH || event.viewType == ViewType.AGENDA) {
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700864 // hide minimonth and calendar frag
865 mShowSideViews = false;
866 if (mControlsMenu != null) {
867 mControlsMenu.setVisible(false);
868 mControlsMenu.setEnabled(false);
Michael Chan5756bbc2011-03-02 18:22:07 -0800869
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700870 if (!mHideControls) {
871 final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
872 "controlsOffset", 0, CONTROLS_ANIMATE_WIDTH);
873 slideAnimation.addListener(mSlideAnimationDoneListener);
874 slideAnimation.setDuration(220);
875 ObjectAnimator.setFrameDelay(0);
876 slideAnimation.start();
877 }
878 } else {
879 mMiniMonth.setVisibility(View.GONE);
880 mCalendarsList.setVisibility(View.GONE);
881 mMiniMonthContainer.setVisibility(View.GONE);
Michael Chan100bdbe2011-03-04 22:30:10 -0800882 }
Michael Chan5756bbc2011-03-02 18:22:07 -0800883 } else {
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700884 // show minimonth and calendar frag
885 mShowSideViews = true;
886 mMiniMonth.setVisibility(View.VISIBLE);
887 mCalendarsList.setVisibility(View.VISIBLE);
888 mMiniMonthContainer.setVisibility(View.VISIBLE);
889 if (mControlsMenu != null) {
890 mControlsMenu.setVisible(true);
891 mControlsMenu.setEnabled(true);
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700892 if (!mHideControls &&
893 (mController.getPreviousViewType() == ViewType.MONTH ||
894 mController.getPreviousViewType() == ViewType.AGENDA)) {
Isaac Katzenelson5ab87e32011-04-21 17:19:25 -0700895 final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this,
896 "controlsOffset", CONTROLS_ANIMATE_WIDTH, 0);
897 slideAnimation.setDuration(220);
898 ObjectAnimator.setFrameDelay(0);
899 slideAnimation.start();
900 }
Michael Chan5756bbc2011-03-02 18:22:07 -0800901 }
RoboErikde20d762011-02-08 17:52:25 -0800902 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700903 }
Michael Chan71d08832010-08-25 18:28:59 -0700904 } else if (event.eventType == EventType.VIEW_EVENT) {
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700905
906 // If in Agenda view and "show_event_details_with_agenda" is "true",
907 // do not create the event info fragment here, it will be created by the Agenda
908 // fragment
909
910 if (mCurrentView == ViewType.AGENDA && mShowEventDetailsWithAgenda) {
911 if (event.selectedTime != null) {
912 mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
913 event.selectedTime, event.id, ViewType.AGENDA);
914 }
915 } else {
Isaac Katzenelson60f01c22011-06-03 15:42:01 -0700916 if (mShowEventInfoFullScreen) {
917 // start event info as activity
918 Intent intent = new Intent(Intent.ACTION_VIEW);
919 Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, event.id);
920 intent.setData(eventUri);
921 intent.setClassName(this, EventInfoActivity.class.getName());
922 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
923 Intent.FLAG_ACTIVITY_SINGLE_TOP);
RoboErika27a8862011-06-23 15:26:23 -0700924 intent.putExtra(EXTRA_EVENT_BEGIN_TIME, event.startTime.toMillis(false));
925 intent.putExtra(EXTRA_EVENT_END_TIME, event.endTime.toMillis(false));
Isaac Katzenelson60f01c22011-06-03 15:42:01 -0700926 intent.putExtra(EVENT_ATTENDEE_RESPONSE, (int)event.extraLong);
927 startActivity(intent);
928 } else {
929 // start event info as a dialog
930 EventInfoFragment fragment = new EventInfoFragment(this,
931 event.id, event.startTime.toMillis(false),
932 event.endTime.toMillis(false), (int) event.extraLong, true);
933 // TODO Fix the temp hack below: && mCurrentView !=
934 // ViewType.AGENDA
935 if (event.selectedTime != null && mCurrentView != ViewType.AGENDA) {
936 mController.sendEvent(this, EventType.GO_TO, event.selectedTime,
937 event.selectedTime, -1, ViewType.DETAIL);
938 }
939 fragment.setDialogParams(event.x, event.y);
940 FragmentManager fm = getFragmentManager();
941 FragmentTransaction ft = fm.beginTransaction();
942 // if we have an old popup close it
943 Fragment fOld = fm.findFragmentByTag(EVENT_INFO_FRAGMENT_TAG);
944 if (fOld != null && fOld.isAdded()) {
945 ft.remove(fOld);
946 }
947 ft.add(fragment, EVENT_INFO_FRAGMENT_TAG);
948 ft.commit();
Isaac Katzenelsonccf565b2011-04-27 14:40:08 -0700949 }
Erik63cd0532011-01-26 14:16:03 -0800950 }
Erikeaafa2b2010-12-23 14:30:37 -0800951 } else if (event.eventType == EventType.UPDATE_TITLE) {
952 setTitleInActionBar(event);
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -0700953 if (!mIsTabletConfig) {
954 mActionBarMenuSpinnerAdapter.setTime(event.startTime.toMillis(false));
955 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700956 }
Erik6c255102010-09-29 14:43:00 -0700957 updateHomeClock();
Michael Chanab29d9e2010-07-21 06:08:47 -0700958 }
959
Erik3f348f32010-08-10 13:17:19 -0700960 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700961 public void eventsChanged() {
Erik954c8712010-08-06 10:12:34 -0700962 mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
Michael Chanab29d9e2010-07-21 06:08:47 -0700963 }
964
Erik3f348f32010-08-10 13:17:19 -0700965 @Override
Adam Powell28f9b352011-01-20 18:36:10 -0800966 public boolean onQueryTextChange(String newText) {
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700967 return false;
968 }
969
970 @Override
Adam Powell28f9b352011-01-20 18:36:10 -0800971 public boolean onQueryTextSubmit(String query) {
Michael Chanc03aab42011-04-21 15:59:29 -0700972 if ("TARDIS".equalsIgnoreCase(query)) {
Erikca478672011-01-19 20:02:47 -0800973 Utils.tardis();
974 }
Erikcb830312011-01-19 14:41:41 -0800975 mSearchView.clearFocus();
Michael Chan46a8b112010-12-14 16:36:27 -0800976 mController.sendEvent(this, EventType.SEARCH, null, null, -1, ViewType.CURRENT, -1, query,
Erik7b92da22010-09-23 14:55:22 -0700977 getComponentName());
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700978 return false;
979 }
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700980
981 @Override
982 public void onTabSelected(Tab tab, FragmentTransaction ft) {
Michael Chane8f45c62011-03-01 14:41:03 -0800983 Log.w(TAG, "TabSelected AllInOne=" + this + " finishing:" + this.isFinishing());
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700984 if (tab == mDayTab && mCurrentView != ViewType.DAY) {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700985 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700986 } else if (tab == mWeekTab && mCurrentView != ViewType.WEEK) {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700987 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700988 } else if (tab == mMonthTab && mCurrentView != ViewType.MONTH) {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700989 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
Isaac Katzenelsonae7c1c62011-04-11 18:30:26 -0700990 } else if (tab == mAgendaTab && mCurrentView != ViewType.AGENDA) {
991 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700992 } else {
Michael Chanece2fbd2011-01-19 14:49:37 -0800993 Log.w(TAG, "TabSelected event from unknown tab: "
994 + (tab == null ? "null" : tab.getText()));
995 Log.w(TAG, "CurrentView:" + mCurrentView + " Tab:" + tab.toString() + " Day:" + mDayTab
Isaac Katzenelsonae7c1c62011-04-11 18:30:26 -0700996 + " Week:" + mWeekTab + " Month:" + mMonthTab + " Agenda:" + mAgendaTab);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700997 }
998 }
999
1000 @Override
1001 public void onTabReselected(Tab tab, FragmentTransaction ft) {
1002 }
1003
1004 @Override
1005 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
1006 }
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -07001007
1008
1009 @Override
1010 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
1011 switch (itemPosition) {
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -07001012 case CalendarViewAdapter.DAY_BUTTON_INDEX:
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -07001013 if (mCurrentView != ViewType.DAY) {
1014 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
1015 }
1016 break;
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -07001017 case CalendarViewAdapter.WEEK_BUTTON_INDEX:
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -07001018 if (mCurrentView != ViewType.WEEK) {
1019 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
1020 }
1021 break;
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -07001022 case CalendarViewAdapter.MONTH_BUTTON_INDEX:
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -07001023 if (mCurrentView != ViewType.MONTH) {
1024 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
1025 }
1026 break;
Isaac Katzenelson9a39bdd2011-06-23 16:52:08 -07001027 case CalendarViewAdapter.AGENDA_BUTTON_INDEX:
Isaac Katzenelson2c5dd7c2011-06-16 17:56:06 -07001028 if (mCurrentView != ViewType.AGENDA) {
1029 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.AGENDA);
1030 }
1031 break;
1032 default:
1033 Log.w(TAG, "ItemSelected event from unknown button: " + itemPosition);
1034 Log.w(TAG, "CurrentView:" + mCurrentView + " Button:" + itemPosition +
1035 " Day:" + mDayTab + " Week:" + mWeekTab + " Month:" + mMonthTab +
1036 " Agenda:" + mAgendaTab);
1037 break;
1038 }
1039 return false;
1040 }
Michael Chan49701592010-06-30 11:04:03 -07001041}