blob: 725259593b21111a9659652cfbc7c4bfa808a40b [file] [log] [blame]
Michael Chanef1a51d2010-07-02 13:57:44 -07001/*
Erik954c8712010-08-06 10:12:34 -07002 * Copyright (C) 2010 The Android Open Source Project
Michael Chanef1a51d2010-07-02 13:57:44 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.calendar;
18
Michael Chan83b0fe32010-07-08 16:46:26 -070019import com.android.calendar.CalendarController.EventInfo;
20import com.android.calendar.CalendarController.EventType;
21
Michael Chanef1a51d2010-07-02 13:57:44 -070022import android.app.Fragment;
Michael Chanef1a51d2010-07-02 13:57:44 -070023import android.content.Context;
Michael Chanef1a51d2010-07-02 13:57:44 -070024import android.os.Bundle;
Michael Chanef1a51d2010-07-02 13:57:44 -070025import android.text.format.Time;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.ViewGroup.LayoutParams;
30import android.view.animation.Animation;
31import android.view.animation.AnimationUtils;
32import android.widget.ProgressBar;
33import android.widget.ViewSwitcher;
34import android.widget.ViewSwitcher.ViewFactory;
35
36/**
37 * This is the base class for Day and Week Activities.
38 */
Michael Chan83b0fe32010-07-08 16:46:26 -070039public class DayFragment extends Fragment implements CalendarController.EventHandler, ViewFactory {
Michael Chanef1a51d2010-07-02 13:57:44 -070040 /**
41 * The view id used for all the views we create. It's OK to have all child
42 * views have the same ID. This ID is used to pick which view receives
43 * focus when a view hierarchy is saved / restore
44 */
45 private static final int VIEW_ID = 1;
46
Michael Chanef1a51d2010-07-02 13:57:44 -070047 protected static final String BUNDLE_KEY_RESTORE_TIME = "key_restore_time";
48
49 protected ProgressBar mProgressBar;
50 protected ViewSwitcher mViewSwitcher;
51 protected Animation mInAnimationForward;
52 protected Animation mOutAnimationForward;
53 protected Animation mInAnimationBackward;
54 protected Animation mOutAnimationBackward;
55 EventLoader mEventLoader;
56
57 Time mSelectedDay = new Time();
58
Isaac Katzenelson0a30ef12012-04-12 11:33:39 -070059 private final Runnable mTZUpdater = new Runnable() {
Erik40bcd102010-11-16 15:46:40 -080060 @Override
61 public void run() {
62 if (!DayFragment.this.isAdded()) {
63 return;
64 }
65 String tz = Utils.getTimeZone(getActivity(), mTZUpdater);
Erik04b28892011-01-19 16:19:15 -080066 mSelectedDay.timezone = tz;
67 mSelectedDay.normalize(true);
Erik40bcd102010-11-16 15:46:40 -080068 }
69 };
70
Michael Chan75d9b562010-07-26 16:54:25 -070071 private int mNumDays;
72
Michael Chan3458a172010-07-13 17:58:21 -070073 public DayFragment() {
74 mSelectedDay.setToNow();
75 }
76
Michael Chan75d9b562010-07-26 16:54:25 -070077 public DayFragment(long timeMillis, int numOfDays) {
78 mNumDays = numOfDays;
Michael Chan3458a172010-07-13 17:58:21 -070079 if (timeMillis == 0) {
80 mSelectedDay.setToNow();
81 } else {
82 mSelectedDay.set(timeMillis);
83 }
84 }
85
Michael Chanef1a51d2010-07-02 13:57:44 -070086 @Override
87 public void onCreate(Bundle icicle) {
88 super.onCreate(icicle);
89
Michael Chanef1a51d2010-07-02 13:57:44 -070090 Context context = getActivity();
91
92 mInAnimationForward = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
93 mOutAnimationForward = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
94 mInAnimationBackward = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
95 mOutAnimationBackward = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
96
97 mEventLoader = new EventLoader(context);
98 }
99
Erik954c8712010-08-06 10:12:34 -0700100 @Override
Michael Chan83b0fe32010-07-08 16:46:26 -0700101 public View onCreateView(LayoutInflater inflater, ViewGroup container,
102 Bundle savedInstanceState) {
Michael Chan75d9b562010-07-26 16:54:25 -0700103 View v = inflater.inflate(R.layout.day_activity, null);
Michael Chanef1a51d2010-07-02 13:57:44 -0700104
105 mViewSwitcher = (ViewSwitcher) v.findViewById(R.id.switcher);
106 mViewSwitcher.setFactory(this);
107 mViewSwitcher.getCurrentView().requestFocus();
Erikeaafa2b2010-12-23 14:30:37 -0800108 ((DayView) mViewSwitcher.getCurrentView()).updateTitle();
Michael Chanef1a51d2010-07-02 13:57:44 -0700109
110 return v;
111 }
112
113 public View makeView() {
Erik40bcd102010-11-16 15:46:40 -0800114 mTZUpdater.run();
Michael Chand1904182010-07-27 13:23:47 -0700115 DayView view = new DayView(getActivity(), CalendarController
Michael Chan75d9b562010-07-26 16:54:25 -0700116 .getInstance(getActivity()), mViewSwitcher, mEventLoader, mNumDays);
Michael Chanef1a51d2010-07-02 13:57:44 -0700117 view.setId(VIEW_ID);
118 view.setLayoutParams(new ViewSwitcher.LayoutParams(
119 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RoboErik5b872522011-10-03 17:24:50 -0700120 view.setSelected(mSelectedDay, false, false);
Michael Chanef1a51d2010-07-02 13:57:44 -0700121 return view;
122 }
123
124 @Override
125 public void onResume() {
126 super.onResume();
127 mEventLoader.startBackgroundThread();
Erik04b28892011-01-19 16:19:15 -0800128 mTZUpdater.run();
Michael Chanef1a51d2010-07-02 13:57:44 -0700129 eventsChanged();
Michael Chand1904182010-07-27 13:23:47 -0700130 DayView view = (DayView) mViewSwitcher.getCurrentView();
Michael Chan61a2e872011-01-19 17:29:46 -0800131 view.handleOnResume();
Michael Chanef1a51d2010-07-02 13:57:44 -0700132 view.restartCurrentTimeUpdates();
133
Michael Chand1904182010-07-27 13:23:47 -0700134 view = (DayView) mViewSwitcher.getNextView();
Michael Chan61a2e872011-01-19 17:29:46 -0800135 view.handleOnResume();
Erik4d090a72011-01-20 14:50:25 -0800136 view.restartCurrentTimeUpdates();
Michael Chanef1a51d2010-07-02 13:57:44 -0700137 }
138
139 @Override
140 public void onSaveInstanceState(Bundle outState) {
141 super.onSaveInstanceState(outState);
142
RoboErika60657d2011-07-20 16:06:55 -0700143 long time = getSelectedTimeInMillis();
144 if (time != -1) {
145 outState.putLong(BUNDLE_KEY_RESTORE_TIME, time);
146 }
Michael Chanef1a51d2010-07-02 13:57:44 -0700147 }
148
149 @Override
150 public void onPause() {
151 super.onPause();
Michael Chand1904182010-07-27 13:23:47 -0700152 DayView view = (DayView) mViewSwitcher.getCurrentView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700153 view.cleanup();
Michael Chand1904182010-07-27 13:23:47 -0700154 view = (DayView) mViewSwitcher.getNextView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700155 view.cleanup();
156 mEventLoader.stopBackgroundThread();
Isaac Katzenelson0a30ef12012-04-12 11:33:39 -0700157
158 // Stop events cross-fade animation
159 view.stopEventsAnimation();
160 ((DayView) mViewSwitcher.getNextView()).stopEventsAnimation();
Michael Chanef1a51d2010-07-02 13:57:44 -0700161 }
162
163 void startProgressSpinner() {
164 // start the progress spinner
165 mProgressBar.setVisibility(View.VISIBLE);
166 }
167
168 void stopProgressSpinner() {
169 // stop the progress spinner
170 mProgressBar.setVisibility(View.GONE);
171 }
172
RoboErik5b872522011-10-03 17:24:50 -0700173 private void goTo(Time goToTime, boolean ignoreTime, boolean animateToday) {
Michael Chan3458a172010-07-13 17:58:21 -0700174 if (mViewSwitcher == null) {
175 // The view hasn't been set yet. Just save the time and use it later.
176 mSelectedDay.set(goToTime);
177 return;
178 }
179
Michael Chand1904182010-07-27 13:23:47 -0700180 DayView currentView = (DayView) mViewSwitcher.getCurrentView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700181
Michael Chan83b0fe32010-07-08 16:46:26 -0700182 // How does goTo time compared to what's already displaying?
Michael Chan487552b2010-07-21 03:56:08 -0700183 int diff = currentView.compareToVisibleTimeRange(goToTime);
Michael Chan83b0fe32010-07-08 16:46:26 -0700184
185 if (diff == 0) {
186 // In visible range. No need to switch view
RoboErik5b872522011-10-03 17:24:50 -0700187 currentView.setSelected(goToTime, ignoreTime, animateToday);
Michael Chan83b0fe32010-07-08 16:46:26 -0700188 } else {
189 // Figure out which way to animate
Michael Chan46a8b112010-12-14 16:36:27 -0800190 if (diff > 0) {
191 mViewSwitcher.setInAnimation(mInAnimationForward);
192 mViewSwitcher.setOutAnimation(mOutAnimationForward);
193 } else {
194 mViewSwitcher.setInAnimation(mInAnimationBackward);
195 mViewSwitcher.setOutAnimation(mOutAnimationBackward);
Michael Chan83b0fe32010-07-08 16:46:26 -0700196 }
197
Michael Chand1904182010-07-27 13:23:47 -0700198 DayView next = (DayView) mViewSwitcher.getNextView();
Michael Chan46a8b112010-12-14 16:36:27 -0800199 if (ignoreTime) {
200 next.setFirstVisibleHour(currentView.getFirstVisibleHour());
201 }
202
RoboErik5b872522011-10-03 17:24:50 -0700203 next.setSelected(goToTime, ignoreTime, animateToday);
Michael Chan83b0fe32010-07-08 16:46:26 -0700204 next.reloadEvents();
205 mViewSwitcher.showNext();
206 next.requestFocus();
Erikeaafa2b2010-12-23 14:30:37 -0800207 next.updateTitle();
Michael Chand9c9a542011-12-06 18:20:17 -0800208 next.restartCurrentTimeUpdates();
Michael Chan83b0fe32010-07-08 16:46:26 -0700209 }
Michael Chanef1a51d2010-07-02 13:57:44 -0700210 }
211
212 /**
213 * Returns the selected time in milliseconds. The milliseconds are measured
214 * in UTC milliseconds from the epoch and uniquely specifies any selectable
215 * time.
216 *
217 * @return the selected time in milliseconds
218 */
219 public long getSelectedTimeInMillis() {
RoboErika60657d2011-07-20 16:06:55 -0700220 if (mViewSwitcher == null) {
221 return -1;
222 }
Michael Chand1904182010-07-27 13:23:47 -0700223 DayView view = (DayView) mViewSwitcher.getCurrentView();
RoboErika60657d2011-07-20 16:06:55 -0700224 if (view == null) {
225 return -1;
226 }
Michael Chanef1a51d2010-07-02 13:57:44 -0700227 return view.getSelectedTimeInMillis();
228 }
229
Michael Chanef1a51d2010-07-02 13:57:44 -0700230 public void eventsChanged() {
Michael Chane6131a52010-10-28 19:06:14 -0700231 if (mViewSwitcher == null) {
232 return;
233 }
Michael Chand1904182010-07-27 13:23:47 -0700234 DayView view = (DayView) mViewSwitcher.getCurrentView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700235 view.clearCachedEvents();
236 view.reloadEvents();
Michael Chan03f5c962011-01-11 13:23:17 -0800237
238 view = (DayView) mViewSwitcher.getNextView();
239 view.clearCachedEvents();
Michael Chanef1a51d2010-07-02 13:57:44 -0700240 }
241
242 Event getSelectedEvent() {
Michael Chand1904182010-07-27 13:23:47 -0700243 DayView view = (DayView) mViewSwitcher.getCurrentView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700244 return view.getSelectedEvent();
245 }
246
247 boolean isEventSelected() {
Michael Chand1904182010-07-27 13:23:47 -0700248 DayView view = (DayView) mViewSwitcher.getCurrentView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700249 return view.isEventSelected();
250 }
251
252 Event getNewEvent() {
Michael Chand1904182010-07-27 13:23:47 -0700253 DayView view = (DayView) mViewSwitcher.getCurrentView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700254 return view.getNewEvent();
255 }
256
Michael Chand1904182010-07-27 13:23:47 -0700257 public DayView getNextView() {
258 return (DayView) mViewSwitcher.getNextView();
Michael Chanef1a51d2010-07-02 13:57:44 -0700259 }
260
Michael Chan83b0fe32010-07-08 16:46:26 -0700261 public long getSupportedEventTypes() {
Erik954c8712010-08-06 10:12:34 -0700262 return EventType.GO_TO | EventType.EVENTS_CHANGED;
Michael Chan83b0fe32010-07-08 16:46:26 -0700263 }
264
265 public void handleEvent(EventInfo msg) {
266 if (msg.eventType == EventType.GO_TO) {
267// TODO support a range of time
268// TODO support event_id
Michael Chan83b0fe32010-07-08 16:46:26 -0700269// TODO support select message
RoboErik5b872522011-10-03 17:24:50 -0700270 goTo(msg.selectedTime, (msg.extraLong & CalendarController.EXTRA_GOTO_DATE) != 0,
271 (msg.extraLong & CalendarController.EXTRA_GOTO_TODAY) != 0);
Erik954c8712010-08-06 10:12:34 -0700272 } else if (msg.eventType == EventType.EVENTS_CHANGED) {
273 eventsChanged();
Michael Chan83b0fe32010-07-08 16:46:26 -0700274 }
275 }
Michael Chanef1a51d2010-07-02 13:57:44 -0700276}