blob: e9c088da6230302d1d739b1bb8e7359c0bbee8a5 [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
Erikdd95df52010-08-27 09:31:18 -070019import static android.provider.Calendar.EVENT_BEGIN_TIME;
20import static android.provider.Calendar.EVENT_END_TIME;
Michael Chan2c7c8512010-12-10 14:12:57 -080021import static android.provider.Calendar.AttendeesColumns.ATTENDEE_STATUS;
Erikdd95df52010-08-27 09:31:18 -070022
Michael Chan83b0fe32010-07-08 16:46:26 -070023import com.android.calendar.CalendarController.EventHandler;
Michael Chanab29d9e2010-07-21 06:08:47 -070024import com.android.calendar.CalendarController.EventInfo;
Michael Chan83b0fe32010-07-08 16:46:26 -070025import com.android.calendar.CalendarController.EventType;
Michael Chan3458a172010-07-13 17:58:21 -070026import com.android.calendar.CalendarController.ViewType;
Mason Tang00b8c1a2010-08-23 12:02:00 -070027import com.android.calendar.agenda.AgendaFragment;
Erik981874e2010-10-05 16:52:52 -070028import com.android.calendar.month.MonthByWeekFragment;
Mason Tang9a3cb142010-07-27 12:16:41 -070029import com.android.calendar.selectcalendars.SelectCalendarsFragment;
Michael Chan83b0fe32010-07-08 16:46:26 -070030
RoboErikb8549562011-02-15 14:25:23 -080031import android.animation.ObjectAnimator;
Michael Chanab29d9e2010-07-21 06:08:47 -070032import android.app.ActionBar;
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -070033import android.app.ActionBar.Tab;
Michael Chan49701592010-06-30 11:04:03 -070034import android.app.Activity;
Michael Chan83b0fe32010-07-08 16:46:26 -070035import android.app.Fragment;
Erik63cd0532011-01-26 14:16:03 -080036import android.app.FragmentManager;
Michael Chan3458a172010-07-13 17:58:21 -070037import android.app.FragmentTransaction;
Erik954c8712010-08-06 10:12:34 -070038import android.content.ContentResolver;
Erikdd95df52010-08-27 09:31:18 -070039import android.content.Intent;
Mason Tang8e3d4302010-07-12 17:39:30 -070040import android.content.SharedPreferences;
41import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Michael Chanab29d9e2010-07-21 06:08:47 -070042import android.content.res.Configuration;
Erikeaafa2b2010-12-23 14:30:37 -080043import android.content.res.Resources;
Erik954c8712010-08-06 10:12:34 -070044import android.database.ContentObserver;
Erikdd95df52010-08-27 09:31:18 -070045import android.net.Uri;
Michael Chan49701592010-06-30 11:04:03 -070046import android.os.Bundle;
Erik954c8712010-08-06 10:12:34 -070047import android.os.Handler;
48import android.provider.Calendar;
Erik6c255102010-09-29 14:43:00 -070049import android.text.TextUtils;
50import android.text.format.DateFormat;
Michael Chan3458a172010-07-13 17:58:21 -070051import android.text.format.DateUtils;
Michael Chan83b0fe32010-07-08 16:46:26 -070052import android.text.format.Time;
Erik954c8712010-08-06 10:12:34 -070053import android.util.Log;
Erik2051f122010-07-02 13:45:45 -070054import android.view.Menu;
Michael Chan3458a172010-07-13 17:58:21 -070055import android.view.MenuItem;
Michael Chanab29d9e2010-07-21 06:08:47 -070056import android.view.View;
RoboErikde20d762011-02-08 17:52:25 -080057import android.view.View.OnClickListener;
Erik259b8f92011-01-12 14:01:12 -080058import android.view.accessibility.AccessibilityEvent;
RoboErikde20d762011-02-08 17:52:25 -080059import android.widget.RelativeLayout;
60import android.widget.RelativeLayout.LayoutParams;
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -070061import android.widget.SearchView;
Erik6c255102010-09-29 14:43:00 -070062import android.widget.TextView;
63
Michael Chan2c7c8512010-12-10 14:12:57 -080064import java.util.List;
Erik6c255102010-09-29 14:43:00 -070065import java.util.Locale;
66import java.util.TimeZone;
Michael Chan49701592010-06-30 11:04:03 -070067
Michael Chanab29d9e2010-07-21 06:08:47 -070068public class AllInOneActivity extends Activity implements EventHandler,
Adam Powell28f9b352011-01-20 18:36:10 -080069 OnSharedPreferenceChangeListener, SearchView.OnQueryTextListener,
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -070070 ActionBar.TabListener {
Michael Chand6734db2010-07-22 00:48:08 -070071 private static final String TAG = "AllInOneActivity";
Erik954c8712010-08-06 10:12:34 -070072 private static final boolean DEBUG = false;
Erik63cd0532011-01-26 14:16:03 -080073 private static final String EVENT_INFO_FRAGMENT_TAG = "EventInfoFragment";
Michael Chand6734db2010-07-22 00:48:08 -070074 private static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
Erik7b92da22010-09-23 14:55:22 -070075 private static final String BUNDLE_KEY_RESTORE_EDIT = "key_restore_edit";
76 private static final String BUNDLE_KEY_EVENT_ID = "key_event_id";
Erik3f348f32010-08-10 13:17:19 -070077 private static final int HANDLER_KEY = 0;
RoboErikde20d762011-02-08 17:52:25 -080078 private static final long CONTROLS_ANIMATE_DURATION = 400;
79 private static int CONTROLS_ANIMATE_WIDTH = 283;
80 private static int CONTROLS_MARGIN_RIGHT = 16;
81 private static float mScale = 0;
Erikeaafa2b2010-12-23 14:30:37 -080082
Michael Chan0558def2010-07-22 21:30:32 -070083 private static CalendarController mController;
Erik9fc409f2010-07-28 11:40:47 -070084 private static boolean mIsMultipane;
Michael Chan6191c122010-11-02 16:23:47 -070085 private boolean mOnSaveInstanceStateCalled = false;
Erik954c8712010-08-06 10:12:34 -070086 private ContentResolver mContentResolver;
Erik648c59c2010-08-13 12:49:26 -070087 private int mPreviousView;
88 private int mCurrentView;
Erikcb811892010-09-28 13:44:19 -070089 private boolean mPaused = true;
90 private boolean mUpdateOnResume = false;
RoboErikde20d762011-02-08 17:52:25 -080091 private boolean mHideControls = false;
Erik6c255102010-09-29 14:43:00 -070092 private TextView mHomeTime;
Erikeaafa2b2010-12-23 14:30:37 -080093 private TextView mDateRange;
RoboErikde20d762011-02-08 17:52:25 -080094 private View mMiniMonth;
95 private View mCalendarsList;
96 private View mMiniMonthContainer;
Erikc5f92d72010-11-11 16:18:34 -080097 private String mTimeZone;
Erik6c255102010-09-29 14:43:00 -070098
Michael Chan2c7c8512010-12-10 14:12:57 -080099 private long mViewEventId = -1;
100 private long mIntentEventStartMillis = -1;
101 private long mIntentEventEndMillis = -1;
Michael Chandeced892010-12-10 15:59:35 -0800102 private int mIntentAttendeeResponse = CalendarController.ATTENDEE_NO_RESPONSE;
Michael Chan2c7c8512010-12-10 14:12:57 -0800103
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700104 // Action bar and Navigation bar (left side of Action bar)
105 private ActionBar mActionBar;
106 private ActionBar.Tab mDayTab;
107 private ActionBar.Tab mWeekTab;
108 private ActionBar.Tab mMonthTab;
Erikcb830312011-01-19 14:41:41 -0800109 private SearchView mSearchView;
RoboErikde20d762011-02-08 17:52:25 -0800110 private MenuItem mControlsMenu;
111
112 private String mHideString = "Hide controls";
113 private String mShowString = "Show controls";
RoboErikde20d762011-02-08 17:52:25 -0800114
RoboErikb8549562011-02-15 14:25:23 -0800115 // Params for animating the controls on the right
116 LayoutParams mControlsParams = new LayoutParams(CONTROLS_ANIMATE_WIDTH, 0);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700117
Erik6c255102010-09-29 14:43:00 -0700118 private Runnable mHomeTimeUpdater = new Runnable() {
119 @Override
120 public void run() {
121 updateHomeClock();
122 }
123 };
Erik954c8712010-08-06 10:12:34 -0700124
125 // Create an observer so that we can update the views whenever a
126 // Calendar event changes.
Erik7b92da22010-09-23 14:55:22 -0700127 private ContentObserver mObserver = new ContentObserver(new Handler()) {
Erik954c8712010-08-06 10:12:34 -0700128 @Override
129 public boolean deliverSelfNotifications() {
130 return true;
131 }
132
133 @Override
134 public void onChange(boolean selfChange) {
135 eventsChanged();
136 }
137 };
Michael Chan83b0fe32010-07-08 16:46:26 -0700138
Michael Chan49701592010-06-30 11:04:03 -0700139 @Override
Erik63cd0532011-01-26 14:16:03 -0800140 protected void onNewIntent(Intent intent) {
141 String action = intent.getAction();
142 if (Intent.ACTION_VIEW.equals(action)) {
143 parseViewAction(intent);
144 }
145 }
146
147 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -0700148 protected void onCreate(Bundle icicle) {
Erikca478672011-01-19 20:02:47 -0800149 if (Utils.getSharedPreference(this, OtherPreferences.KEY_OTHER_1, false)) {
150 setTheme(R.style.CalendarTheme_WithActionBarWallpaper);
151 }
Michael Chan83b0fe32010-07-08 16:46:26 -0700152 super.onCreate(icicle);
153
154 // This needs to be created before setContentView
Michael Chan0558def2010-07-22 21:30:32 -0700155 mController = CalendarController.getInstance(this);
Erik954c8712010-08-06 10:12:34 -0700156 // Get time from intent or icicle
Michael Chan2c7c8512010-12-10 14:12:57 -0800157 long timeMillis = -1;
158 int viewType = -1;
159 boolean restoreEdit = false;
160 final Intent intent = getIntent();
Erik954c8712010-08-06 10:12:34 -0700161 if (icicle != null) {
162 timeMillis = icicle.getLong(BUNDLE_KEY_RESTORE_TIME);
Michael Chan2c7c8512010-12-10 14:12:57 -0800163 restoreEdit = icicle.getBoolean(BUNDLE_KEY_RESTORE_EDIT, false);
Erik7b92da22010-09-23 14:55:22 -0700164 viewType = ViewType.EDIT;
165 } else {
Michael Chan2c7c8512010-12-10 14:12:57 -0800166 String action = intent.getAction();
167 if (Intent.ACTION_VIEW.equals(action)) {
168 // Open EventInfo later
169 timeMillis = parseViewAction(intent);
170 }
171
172 if (timeMillis == -1) {
173 timeMillis = Utils.timeFromIntentInMillis(intent);
174 }
175 }
176
177 if (!restoreEdit) {
Erik7b92da22010-09-23 14:55:22 -0700178 viewType = Utils.getViewTypeFromIntentAndSharedPref(this);
179 }
Erikc5f92d72010-11-11 16:18:34 -0800180 mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
181 Time t = new Time(mTimeZone);
Erik954c8712010-08-06 10:12:34 -0700182 t.set(timeMillis);
183
Michael Chan2c7c8512010-12-10 14:12:57 -0800184 if (icicle != null && intent != null) {
185 Log.d(TAG, "both, icicle:" + icicle.toString() + " intent:" + intent.toString());
Erik7b92da22010-09-23 14:55:22 -0700186 } else {
Michael Chan2c7c8512010-12-10 14:12:57 -0800187 Log.d(TAG, "not both, icicle:" + icicle + " intent:" + intent);
Erik7b92da22010-09-23 14:55:22 -0700188 }
189
Erikeaafa2b2010-12-23 14:30:37 -0800190 Resources res = getResources();
RoboErikde20d762011-02-08 17:52:25 -0800191 if (mScale == 0) {
192 mScale = res.getDisplayMetrics().density;
193 CONTROLS_ANIMATE_WIDTH *= mScale;
194 CONTROLS_MARGIN_RIGHT *= mScale;
195 }
196 mHideString = res.getString(R.string.hide_controls);
197 mShowString = res.getString(R.string.show_controls);
RoboErikb8549562011-02-15 14:25:23 -0800198 mControlsParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
199 mControlsParams.rightMargin = CONTROLS_MARGIN_RIGHT;
200
Erikeaafa2b2010-12-23 14:30:37 -0800201 mIsMultipane =
202 (res.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_XLARGE) != 0;
Erik9fc409f2010-07-28 11:40:47 -0700203
Erik63cd0532011-01-26 14:16:03 -0800204 Utils.setAllowWeekForDetailView(mIsMultipane);
Michael Chanb60218a2010-12-14 16:34:39 -0800205
Erikeaafa2b2010-12-23 14:30:37 -0800206 mDateRange = (TextView) getLayoutInflater().inflate(R.layout.date_range_title, null);
207
Erikbe8191c2010-10-26 16:50:08 -0700208 // setContentView must be called before configureActionBar
209 setContentView(R.layout.all_in_one);
210 // configureActionBar auto-selects the first tab you add, so we need to
211 // call it before we set up our own fragments to make sure it doesn't
212 // overwrite us
213 configureActionBar();
214
Erik3f348f32010-08-10 13:17:19 -0700215 // Must be the first to register because this activity can modify the
216 // list of event handlers in it's handle method. This affects who the
217 // rest of the handlers the controller dispatches to are.
218 mController.registerEventHandler(HANDLER_KEY, this);
Michael Chan83b0fe32010-07-08 16:46:26 -0700219
Erik6c255102010-09-29 14:43:00 -0700220 mHomeTime = (TextView) findViewById(R.id.home_time);
RoboErikde20d762011-02-08 17:52:25 -0800221 mMiniMonth = findViewById(R.id.mini_month);
222 mCalendarsList = findViewById(R.id.calendar_list);
223 mMiniMonthContainer = findViewById(R.id.mini_month_container);
Michael Chan83b0fe32010-07-08 16:46:26 -0700224
Erik7b92da22010-09-23 14:55:22 -0700225 initFragments(timeMillis, viewType, icicle);
Mason Tang8e3d4302010-07-12 17:39:30 -0700226
Erik6c255102010-09-29 14:43:00 -0700227
Mason Tang8e3d4302010-07-12 17:39:30 -0700228 // Listen for changes that would require this to be refreshed
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700229 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700230 prefs.registerOnSharedPreferenceChangeListener(this);
Erikcb811892010-09-28 13:44:19 -0700231
Erik954c8712010-08-06 10:12:34 -0700232 mContentResolver = getContentResolver();
Erikbe8191c2010-10-26 16:50:08 -0700233 }
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700234
Michael Chan2c7c8512010-12-10 14:12:57 -0800235 private long parseViewAction(final Intent intent) {
236 long timeMillis = -1;
237 Uri data = intent.getData();
238 if (data != null && data.isHierarchical()) {
239 List<String> path = data.getPathSegments();
240 if (path.size() == 2 && path.get(0).equals("events")) {
241 try {
242 mViewEventId = Long.valueOf(data.getLastPathSegment());
243 if(mViewEventId != -1) {
244 mIntentEventStartMillis = intent.getLongExtra(EVENT_BEGIN_TIME, 0);
245 mIntentEventEndMillis = intent.getLongExtra(EVENT_END_TIME, 0);
246 mIntentAttendeeResponse = intent.getIntExtra(
Michael Chandeced892010-12-10 15:59:35 -0800247 ATTENDEE_STATUS, CalendarController.ATTENDEE_NO_RESPONSE);
Michael Chan2c7c8512010-12-10 14:12:57 -0800248 timeMillis = mIntentEventStartMillis;
249 }
250 } catch (NumberFormatException e) {
251 // Ignore if mViewEventId can't be parsed
252 }
253 }
254 }
255 return timeMillis;
256 }
257
Erikbe8191c2010-10-26 16:50:08 -0700258 private void configureActionBar() {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700259 mActionBar = getActionBar();
Michael Chandeced892010-12-10 15:59:35 -0800260 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700261 if (mActionBar == null) {
262 Log.w(TAG, "ActionBar is null.");
263 } else {
264 mDayTab = mActionBar.newTab();
265 mDayTab.setText(getString(R.string.day_view));
266 mDayTab.setTabListener(this);
267 mActionBar.addTab(mDayTab);
268 mWeekTab = mActionBar.newTab();
269 mWeekTab.setText(getString(R.string.week_view));
270 mWeekTab.setTabListener(this);
271 mActionBar.addTab(mWeekTab);
272 mMonthTab = mActionBar.newTab();
273 mMonthTab.setText(getString(R.string.month_view));
274 mMonthTab.setTabListener(this);
275 mActionBar.addTab(mMonthTab);
Erikeaafa2b2010-12-23 14:30:37 -0800276 mActionBar.setCustomView(mDateRange);
277 mActionBar.setDisplayOptions(
278 ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700279 }
Erik954c8712010-08-06 10:12:34 -0700280 }
281
282 @Override
283 protected void onResume() {
284 super.onResume();
285 mContentResolver.registerContentObserver(Calendar.Events.CONTENT_URI, true, mObserver);
Erikcb811892010-09-28 13:44:19 -0700286 if (mUpdateOnResume) {
287 initFragments(mController.getTime(), mController.getViewType(), null);
288 mUpdateOnResume = false;
289 }
Erik6c255102010-09-29 14:43:00 -0700290 updateHomeClock();
Erikcb811892010-09-28 13:44:19 -0700291 mPaused = false;
Michael Chan6191c122010-11-02 16:23:47 -0700292 mOnSaveInstanceStateCalled = false;
Michael Chan2c7c8512010-12-10 14:12:57 -0800293
294 if (mViewEventId != -1 && mIntentEventStartMillis != -1 && mIntentEventEndMillis != -1) {
Michael Chanf0868f62011-01-11 19:37:35 -0800295 mController.sendEventRelatedEventWithResponse(this, EventType.VIEW_EVENT, mViewEventId,
Michael Chandeced892010-12-10 15:59:35 -0800296 mIntentEventStartMillis, mIntentEventEndMillis, -1, -1,
RoboErik87f993f2011-02-03 17:47:20 -0800297 mIntentAttendeeResponse, -1);
Michael Chan2c7c8512010-12-10 14:12:57 -0800298 mViewEventId = -1;
299 mIntentEventStartMillis = -1;
300 mIntentEventEndMillis = -1;
301 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700302 }
303
304 @Override
Michael Chand6734db2010-07-22 00:48:08 -0700305 protected void onPause() {
306 super.onPause();
Erikcb811892010-09-28 13:44:19 -0700307 mPaused = true;
Daisuke Miyakawac9e53072010-10-14 12:18:49 -0700308 mHomeTime.removeCallbacks(mHomeTimeUpdater);
Erik954c8712010-08-06 10:12:34 -0700309 mContentResolver.unregisterContentObserver(mObserver);
Erikcb811892010-09-28 13:44:19 -0700310 if (isFinishing()) {
311 // Stop listening for changes that would require this to be refreshed
312 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
313 prefs.unregisterOnSharedPreferenceChangeListener(this);
314 }
Erik7b92da22010-09-23 14:55:22 -0700315 // FRAG_TODO save highlighted days of the week;
Erikdd95df52010-08-27 09:31:18 -0700316 if (mController.getViewType() != ViewType.EDIT) {
317 Utils.setDefaultView(this, mController.getViewType());
318 }
Michael Chand6734db2010-07-22 00:48:08 -0700319 }
320
321 @Override
Erik7b92da22010-09-23 14:55:22 -0700322 protected void onUserLeaveHint() {
323 mController.sendEvent(this, EventType.USER_HOME, null, null, -1, ViewType.CURRENT);
324 super.onUserLeaveHint();
325 }
326
327 @Override
Michael Chand6734db2010-07-22 00:48:08 -0700328 public void onSaveInstanceState(Bundle outState) {
Michael Chan6191c122010-11-02 16:23:47 -0700329 mOnSaveInstanceStateCalled = true;
Michael Chand6734db2010-07-22 00:48:08 -0700330 super.onSaveInstanceState(outState);
331
332 outState.putLong(BUNDLE_KEY_RESTORE_TIME, mController.getTime());
Erik7b92da22010-09-23 14:55:22 -0700333 if (mCurrentView == ViewType.EDIT) {
334 outState.putBoolean(BUNDLE_KEY_RESTORE_EDIT, true);
335 outState.putLong(BUNDLE_KEY_EVENT_ID, mController.getEventId());
336 }
Michael Chand6734db2010-07-22 00:48:08 -0700337 }
338
339 @Override
Mason Tang8e3d4302010-07-12 17:39:30 -0700340 protected void onDestroy() {
341 super.onDestroy();
342
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700343 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700344 prefs.unregisterOnSharedPreferenceChangeListener(this);
Eriked61b482010-08-13 13:59:50 -0700345 CalendarController.removeInstance(this);
Mason Tang8e3d4302010-07-12 17:39:30 -0700346 }
347
Erik7b92da22010-09-23 14:55:22 -0700348 private void initFragments(long timeMillis, int viewType, Bundle icicle) {
Dianne Hackborn12b33302011-01-17 12:30:13 -0800349 FragmentTransaction ft = getFragmentManager().beginTransaction();
Michael Chan3458a172010-07-13 17:58:21 -0700350
Erik9fc409f2010-07-28 11:40:47 -0700351 if (mIsMultipane) {
Erik40bcd102010-11-16 15:46:40 -0800352 Fragment miniMonthFrag = new MonthByWeekFragment(timeMillis, true);
Michael Chanab29d9e2010-07-21 06:08:47 -0700353 ft.replace(R.id.mini_month, miniMonthFrag);
Erik3f348f32010-08-10 13:17:19 -0700354 mController.registerEventHandler(R.id.mini_month, (EventHandler) miniMonthFrag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700355
356 Fragment selectCalendarsFrag = new SelectCalendarsFragment();
357 ft.replace(R.id.calendar_list, selectCalendarsFrag);
Erikdd95df52010-08-27 09:31:18 -0700358 }
Erik7b92da22010-09-23 14:55:22 -0700359 if (!mIsMultipane || viewType == ViewType.EDIT) {
RoboErikde20d762011-02-08 17:52:25 -0800360 mMiniMonth.setVisibility(View.GONE);
361 mCalendarsList.setVisibility(View.GONE);
Michael Chanab29d9e2010-07-21 06:08:47 -0700362 }
Michael Chan3458a172010-07-13 17:58:21 -0700363
Erikdd95df52010-08-27 09:31:18 -0700364 EventInfo info = null;
365 if (viewType == ViewType.EDIT) {
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700366 mPreviousView = GeneralPreferences.getSharedPreferences(this).getInt(
Erik7b92da22010-09-23 14:55:22 -0700367 GeneralPreferences.KEY_START_VIEW, GeneralPreferences.DEFAULT_START_VIEW);
Erikdd95df52010-08-27 09:31:18 -0700368
Daisuke Miyakawa4143cfa2010-09-17 19:55:04 -0700369 long eventId = -1;
Erikdd95df52010-08-27 09:31:18 -0700370 Intent intent = getIntent();
371 Uri data = intent.getData();
372 if (data != null) {
373 try {
Daisuke Miyakawa4143cfa2010-09-17 19:55:04 -0700374 eventId = Long.parseLong(data.getLastPathSegment());
Erikdd95df52010-08-27 09:31:18 -0700375 } catch (NumberFormatException e) {
376 if (DEBUG) {
377 Log.d(TAG, "Create new event");
378 }
379 }
Erik7b92da22010-09-23 14:55:22 -0700380 } else if (icicle != null && icicle.containsKey(BUNDLE_KEY_EVENT_ID)) {
381 eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
Erikdd95df52010-08-27 09:31:18 -0700382 }
Daisuke Miyakawa4143cfa2010-09-17 19:55:04 -0700383
Erikdd95df52010-08-27 09:31:18 -0700384 long begin = intent.getLongExtra(EVENT_BEGIN_TIME, -1);
385 long end = intent.getLongExtra(EVENT_END_TIME, -1);
386 info = new EventInfo();
387 if (end != -1) {
388 info.endTime = new Time();
389 info.endTime.set(end);
390 }
391 if (begin != -1) {
392 info.startTime = new Time();
393 info.startTime.set(begin);
394 }
395 info.id = eventId;
396 // We set the viewtype so if the user presses back when they are
397 // done editing the controller knows we were in the Edit Event
Erik7b92da22010-09-23 14:55:22 -0700398 // screen. Likewise for eventId
Erikdd95df52010-08-27 09:31:18 -0700399 mController.setViewType(viewType);
Erik7b92da22010-09-23 14:55:22 -0700400 mController.setEventId(eventId);
Erikdd95df52010-08-27 09:31:18 -0700401 } else {
402 mPreviousView = viewType;
403 }
Erik63cd0532011-01-26 14:16:03 -0800404 setMainPane(ft, R.id.main_pane, viewType, timeMillis, true);
Michael Chan3458a172010-07-13 17:58:21 -0700405
406 ft.commit(); // this needs to be after setMainPane()
407
Erikc5f92d72010-11-11 16:18:34 -0800408 Time t = new Time(mTimeZone);
Michael Chand6734db2010-07-22 00:48:08 -0700409 t.set(timeMillis);
Erikdd95df52010-08-27 09:31:18 -0700410 if (viewType != ViewType.EDIT) {
411 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
412 }
Michael Chan49701592010-06-30 11:04:03 -0700413 }
Erik2051f122010-07-02 13:45:45 -0700414
415 @Override
Erik648c59c2010-08-13 12:49:26 -0700416 public void onBackPressed() {
Daisuke Miyakawa93e69eb2010-10-22 18:54:38 -0700417 if (mCurrentView == ViewType.EDIT || mCurrentView == ViewType.DETAIL) {
Erikdd95df52010-08-27 09:31:18 -0700418 mController.sendEvent(this, EventType.GO_TO, null, null, -1, mPreviousView);
Daisuke Miyakawa93e69eb2010-10-22 18:54:38 -0700419 } else {
420 super.onBackPressed();
Erik648c59c2010-08-13 12:49:26 -0700421 }
422 }
423
424 @Override
Erik2051f122010-07-02 13:45:45 -0700425 public boolean onCreateOptionsMenu(Menu menu) {
426 super.onCreateOptionsMenu(menu);
427
Erik2051f122010-07-02 13:45:45 -0700428 getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700429
Erikcb830312011-01-19 14:41:41 -0800430 mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
431 if (mSearchView != null) {
432 mSearchView.setIconifiedByDefault(true);
Adam Powell28f9b352011-01-20 18:36:10 -0800433 mSearchView.setOnQueryTextListener(this);
Erikcb830312011-01-19 14:41:41 -0800434 mSearchView.setSubmitButtonEnabled(true);
Michael Chan71b221a2010-10-25 17:55:44 -0700435 }
RoboErikde20d762011-02-08 17:52:25 -0800436 mControlsMenu = menu.findItem(R.id.action_hide_controls);
437 if (mControlsMenu != null && mController != null
438 && mController.getViewType() == ViewType.MONTH) {
439 mControlsMenu.setVisible(false);
440 mControlsMenu.setEnabled(false);
441 }
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700442
Erik2051f122010-07-02 13:45:45 -0700443 return true;
444 }
Michael Chan3458a172010-07-13 17:58:21 -0700445
446 @Override
447 public boolean onOptionsItemSelected(MenuItem item) {
448 Time t = null;
449 int viewType = ViewType.CURRENT;
450 switch (item.getItemId()) {
Erikba1b94a2010-07-20 17:50:50 -0700451 case R.id.action_refresh:
452 mController.refreshCalendars();
453 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700454 case R.id.action_today:
455 viewType = ViewType.CURRENT;
Erikc5f92d72010-11-11 16:18:34 -0800456 t = new Time(mTimeZone);
Michael Chan3458a172010-07-13 17:58:21 -0700457 t.setToNow();
458 break;
459 case R.id.action_create_event:
RoboErik87f993f2011-02-03 17:47:20 -0800460 mController.sendEventRelatedEvent(this, EventType.CREATE_EVENT, -1, 0, 0, 0, 0, -1);
Michael Chan3458a172010-07-13 17:58:21 -0700461 return true;
Michael Chanab29d9e2010-07-21 06:08:47 -0700462 case R.id.action_settings:
463 mController.sendEvent(this, EventType.LAUNCH_SETTINGS, null, null, 0, 0);
464 return true;
RoboErikde20d762011-02-08 17:52:25 -0800465 case R.id.action_hide_controls:
466 mHideControls = !mHideControls;
467 item.setTitle(mHideControls ? mShowString : mHideString);
RoboErikb8549562011-02-15 14:25:23 -0800468 final ObjectAnimator slideAnimation = ObjectAnimator.ofInt(this, "controlsOffset",
469 mHideControls ? 0 : CONTROLS_ANIMATE_WIDTH,
470 mHideControls ? CONTROLS_ANIMATE_WIDTH : 0);
471 slideAnimation.setDuration(CONTROLS_ANIMATE_DURATION);
472 ObjectAnimator.setFrameDelay(0);
473 slideAnimation.start();
RoboErikde20d762011-02-08 17:52:25 -0800474 return true;
Michael Chan3458a172010-07-13 17:58:21 -0700475 default:
476 return false;
477 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700478 mController.sendEvent(this, EventType.GO_TO, t, null, -1, viewType);
Michael Chan3458a172010-07-13 17:58:21 -0700479 return true;
480 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700481
RoboErikb8549562011-02-15 14:25:23 -0800482 /**
483 * Sets the offset of the controls on the right for animating them off/on
484 * screen. ProGuard strips this if it's not in proguard.flags
485 *
486 * @param controlsOffset The current offset in pixels
487 */
488 public void setControlsOffset(int controlsOffset) {
489 mMiniMonth.setTranslationX(controlsOffset);
490 mCalendarsList.setTranslationX(controlsOffset);
491 mHomeTime.setTranslationX(controlsOffset);
492 mControlsParams.width = Math.max(
493 0, CONTROLS_ANIMATE_WIDTH - controlsOffset - mControlsParams.rightMargin);
494 mMiniMonthContainer.setLayoutParams(mControlsParams);
495 }
RoboErikde20d762011-02-08 17:52:25 -0800496
Mason Tang8e3d4302010-07-12 17:39:30 -0700497 @Override
498 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Daisuke Miyakawa4b441bd2010-09-16 14:55:36 -0700499 if (key.equals(GeneralPreferences.KEY_WEEK_START_DAY)) {
Erikcb811892010-09-28 13:44:19 -0700500 if (mPaused) {
501 mUpdateOnResume = true;
502 } else {
503 initFragments(mController.getTime(), mController.getViewType(), null);
504 }
Mason Tang8e3d4302010-07-12 17:39:30 -0700505 }
506 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700507
Erik63cd0532011-01-26 14:16:03 -0800508 private void setMainPane(
509 FragmentTransaction ft, int viewId, int viewType, long timeMillis, boolean force) {
Michael Chan6191c122010-11-02 16:23:47 -0700510 if (mOnSaveInstanceStateCalled) {
511 return;
512 }
Erik7b92da22010-09-23 14:55:22 -0700513 if (!force && mCurrentView == viewType) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700514 return;
515 }
516
Erik648c59c2010-08-13 12:49:26 -0700517 if (viewType != mCurrentView) {
518 // The rules for this previous view are different than the
519 // controller's and are used for intercepting the back button.
Erikdd95df52010-08-27 09:31:18 -0700520 if (mCurrentView != ViewType.EDIT && mCurrentView > 0) {
521 mPreviousView = mCurrentView;
522 }
Erik648c59c2010-08-13 12:49:26 -0700523 mCurrentView = viewType;
524 }
Erik3f348f32010-08-10 13:17:19 -0700525 // Create new fragment
526 Fragment frag;
Michael Chanab29d9e2010-07-21 06:08:47 -0700527 switch (viewType) {
528 case ViewType.AGENDA:
Mason Tang31df4262010-07-19 17:51:46 -0700529 frag = new AgendaFragment(timeMillis);
Michael Chanab29d9e2010-07-21 06:08:47 -0700530 break;
531 case ViewType.DAY:
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700532 if (mActionBar != null && (mActionBar.getSelectedTab() != mDayTab)) {
533 mActionBar.selectTab(mDayTab);
534 }
Michael Chan75d9b562010-07-26 16:54:25 -0700535 frag = new DayFragment(timeMillis, 1);
536 break;
Michael Chanab29d9e2010-07-21 06:08:47 -0700537 case ViewType.WEEK:
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700538 if (mActionBar != null && (mActionBar.getSelectedTab() != mWeekTab)) {
539 mActionBar.selectTab(mWeekTab);
540 }
Michael Chan75d9b562010-07-26 16:54:25 -0700541 frag = new DayFragment(timeMillis, 7);
Michael Chanab29d9e2010-07-21 06:08:47 -0700542 break;
543 case ViewType.MONTH:
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700544 if (mActionBar != null && (mActionBar.getSelectedTab() != mMonthTab)) {
545 mActionBar.selectTab(mMonthTab);
546 }
Erik40bcd102010-11-16 15:46:40 -0800547 frag = new MonthByWeekFragment(timeMillis, false);
Michael Chanab29d9e2010-07-21 06:08:47 -0700548 break;
549 default:
Michael Chand6734db2010-07-22 00:48:08 -0700550 throw new IllegalArgumentException(
551 "Must be Agenda, Day, Week, or Month ViewType, not " + viewType);
Michael Chanab29d9e2010-07-21 06:08:47 -0700552 }
553
554 boolean doCommit = false;
555 if (ft == null) {
556 doCommit = true;
Dianne Hackborn12b33302011-01-17 12:30:13 -0800557 ft = getFragmentManager().beginTransaction();
Michael Chanab29d9e2010-07-21 06:08:47 -0700558 }
559
560 ft.replace(viewId, frag);
Daisuke Miyakawa93e69eb2010-10-22 18:54:38 -0700561
Erik954c8712010-08-06 10:12:34 -0700562 if (DEBUG) {
563 Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
564 }
Erik3f348f32010-08-10 13:17:19 -0700565 // If the key is already registered this will replace it
566 mController.registerEventHandler(viewId, (EventHandler) frag);
Michael Chanab29d9e2010-07-21 06:08:47 -0700567
568 if (doCommit) {
569 ft.commit();
570 }
571 }
572
573 private void setTitleInActionBar(EventInfo event) {
Erikeaafa2b2010-12-23 14:30:37 -0800574 if (event.eventType != EventType.UPDATE_TITLE || mActionBar == null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700575 return;
576 }
577
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700578 final long start = event.startTime.toMillis(false /* use isDst */);
579 final long end;
Michael Chand6734db2010-07-22 00:48:08 -0700580 if (event.endTime != null) {
Michael Chanab29d9e2010-07-21 06:08:47 -0700581 end = event.endTime.toMillis(false /* use isDst */);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700582 } else {
583 end = start;
Michael Chanab29d9e2010-07-21 06:08:47 -0700584 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700585
Erikeaafa2b2010-12-23 14:30:37 -0800586 final String msg = Utils.formatDateRange(this, start, end, (int) event.extraLong);
Erik259b8f92011-01-12 14:01:12 -0800587 CharSequence oldDate = mDateRange.getText();
Erikeaafa2b2010-12-23 14:30:37 -0800588 mDateRange.setText(msg);
Erik259b8f92011-01-12 14:01:12 -0800589 if (!TextUtils.equals(oldDate, msg)) {
590 mDateRange.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
591 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700592 }
593
Erik6c255102010-09-29 14:43:00 -0700594 private void updateHomeClock() {
Erikc5f92d72010-11-11 16:18:34 -0800595 mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater);
Erik6c255102010-09-29 14:43:00 -0700596 if (mIsMultipane && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK)
Erikc5f92d72010-11-11 16:18:34 -0800597 && !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())) {
598 Time time = new Time(mTimeZone);
Erik6c255102010-09-29 14:43:00 -0700599 time.setToNow();
600 long millis = time.toMillis(true);
601 boolean isDST = time.isDst != 0;
602 int flags = DateUtils.FORMAT_SHOW_TIME;
603 if (DateFormat.is24HourFormat(this)) {
604 flags |= DateUtils.FORMAT_24HOUR;
605 }
606 // Formats the time as
Erikc5f92d72010-11-11 16:18:34 -0800607 String timeString = (new StringBuilder(
608 Utils.formatDateRange(this, millis, millis, flags))).append(" ").append(
609 TimeZone.getTimeZone(mTimeZone).getDisplayName(
610 isDST, TimeZone.SHORT, Locale.getDefault())).toString();
Erik6c255102010-09-29 14:43:00 -0700611 mHomeTime.setText(timeString);
612 mHomeTime.setVisibility(View.VISIBLE);
613 // Update when the minute changes
614 mHomeTime.postDelayed(
Daisuke Miyakawac9e53072010-10-14 12:18:49 -0700615 mHomeTimeUpdater,
616 DateUtils.MINUTE_IN_MILLIS - (millis % DateUtils.MINUTE_IN_MILLIS));
Erik6c255102010-09-29 14:43:00 -0700617 } else {
618 mHomeTime.setVisibility(View.GONE);
619 }
620 }
621
Erik3f348f32010-08-10 13:17:19 -0700622 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700623 public long getSupportedEventTypes() {
Erikeaafa2b2010-12-23 14:30:37 -0800624 return EventType.GO_TO | EventType.VIEW_EVENT | EventType.UPDATE_TITLE;
Michael Chanab29d9e2010-07-21 06:08:47 -0700625 }
626
Erik3f348f32010-08-10 13:17:19 -0700627 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700628 public void handleEvent(EventInfo event) {
629 if (event.eventType == EventType.GO_TO) {
Erik63cd0532011-01-26 14:16:03 -0800630 setMainPane(
631 null, R.id.main_pane, event.viewType, event.startTime.toMillis(false), false);
Erikaa3dde12011-02-03 11:16:23 -0800632 if (mSearchView != null) {
633 mSearchView.clearFocus();
634 }
Erik9fc409f2010-07-28 11:40:47 -0700635 if (!mIsMultipane) {
636 return;
637 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700638 if (event.viewType == ViewType.MONTH) {
639 // hide minimonth and calendar frag
RoboErikde20d762011-02-08 17:52:25 -0800640 mMiniMonth.setVisibility(View.GONE);
641 mCalendarsList.setVisibility(View.GONE);
642 mMiniMonthContainer.setVisibility(View.GONE);
643 if (mControlsMenu != null) {
644 mControlsMenu.setVisible(false);
645 mControlsMenu.setEnabled(false);
646 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700647 } else {
648 // show minimonth and calendar frag
RoboErikde20d762011-02-08 17:52:25 -0800649 mMiniMonth.setVisibility(View.VISIBLE);
650 mCalendarsList.setVisibility(View.VISIBLE);
651 mMiniMonthContainer.setVisibility(View.VISIBLE);
652 if (mControlsMenu != null) {
653 mControlsMenu.setVisible(true);
654 mControlsMenu.setEnabled(true);
655 }
Michael Chanab29d9e2010-07-21 06:08:47 -0700656 }
Michael Chan71d08832010-08-25 18:28:59 -0700657 } else if (event.eventType == EventType.VIEW_EVENT) {
Michael Chan0a01fd32011-01-23 15:13:28 -0800658 EventInfoFragment fragment = new EventInfoFragment(this,
Michael Chandeced892010-12-10 15:59:35 -0800659 event.id, event.startTime.toMillis(false), event.endTime.toMillis(false),
660 (int) event.extraLong);
RoboErik87f993f2011-02-03 17:47:20 -0800661 if (event.selectedTime != null) {
662 mController.sendEvent(this, EventType.GO_TO, event.selectedTime, event.selectedTime,
663 -1, ViewType.DETAIL);
Erik63cd0532011-01-26 14:16:03 -0800664 }
Michael Chan71d08832010-08-25 18:28:59 -0700665 fragment.setDialogParams(event.x, event.y);
Erik63cd0532011-01-26 14:16:03 -0800666 FragmentManager fm = getFragmentManager();
667 FragmentTransaction ft = fm.beginTransaction();
668 // if we have an old popup close it
669 Fragment fOld = fm.findFragmentByTag(EVENT_INFO_FRAGMENT_TAG);
670 if (fOld != null) {
671 ft.remove(fOld);
672 }
673 ft.add(fragment, EVENT_INFO_FRAGMENT_TAG);
674 ft.commit();
Erikeaafa2b2010-12-23 14:30:37 -0800675 } else if (event.eventType == EventType.UPDATE_TITLE) {
676 setTitleInActionBar(event);
Michael Chanab29d9e2010-07-21 06:08:47 -0700677 }
Erik6c255102010-09-29 14:43:00 -0700678 updateHomeClock();
Michael Chanab29d9e2010-07-21 06:08:47 -0700679 }
680
Erik3f348f32010-08-10 13:17:19 -0700681 @Override
Michael Chanab29d9e2010-07-21 06:08:47 -0700682 public void eventsChanged() {
Erik954c8712010-08-06 10:12:34 -0700683 mController.sendEvent(this, EventType.EVENTS_CHANGED, null, null, -1, ViewType.CURRENT);
Michael Chanab29d9e2010-07-21 06:08:47 -0700684 }
685
Erik3f348f32010-08-10 13:17:19 -0700686 @Override
Adam Powell28f9b352011-01-20 18:36:10 -0800687 public boolean onQueryTextChange(String newText) {
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700688 return false;
689 }
690
691 @Override
Adam Powell28f9b352011-01-20 18:36:10 -0800692 public boolean onQueryTextSubmit(String query) {
Erikca478672011-01-19 20:02:47 -0800693 if (TextUtils.equals(query, "TARDIS")) {
694 Utils.tardis();
695 }
Erikcb830312011-01-19 14:41:41 -0800696 mSearchView.clearFocus();
Michael Chan46a8b112010-12-14 16:36:27 -0800697 mController.sendEvent(this, EventType.SEARCH, null, null, -1, ViewType.CURRENT, -1, query,
Erik7b92da22010-09-23 14:55:22 -0700698 getComponentName());
Daisuke Miyakawa6d2e6f72010-09-17 10:30:54 -0700699 return false;
700 }
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700701
702 @Override
703 public void onTabSelected(Tab tab, FragmentTransaction ft) {
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700704 if (tab == mDayTab && mCurrentView != ViewType.DAY) {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700705 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.DAY);
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700706 } else if (tab == mWeekTab && mCurrentView != ViewType.WEEK) {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700707 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.WEEK);
Daisuke Miyakawa40474a82010-10-25 17:07:34 -0700708 } else if (tab == mMonthTab && mCurrentView != ViewType.MONTH) {
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700709 mController.sendEvent(this, EventType.GO_TO, null, null, -1, ViewType.MONTH);
710 } else {
Michael Chanece2fbd2011-01-19 14:49:37 -0800711 Log.w(TAG, "TabSelected event from unknown tab: "
712 + (tab == null ? "null" : tab.getText()));
713 Log.w(TAG, "CurrentView:" + mCurrentView + " Tab:" + tab.toString() + " Day:" + mDayTab
714 + " Week:" + mWeekTab + " Month:" + mMonthTab);
Daisuke Miyakawa04ae7682010-10-19 14:15:39 -0700715 }
716 }
717
718 @Override
719 public void onTabReselected(Tab tab, FragmentTransaction ft) {
720 }
721
722 @Override
723 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
724 }
Michael Chan49701592010-06-30 11:04:03 -0700725}