CalendarEventModel.java revision 4b441bd6544fe6d11be75f974a41afd8fa040a4f
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.calendar;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.SharedPreferences;
22import android.net.Uri;
23import android.provider.Calendar.Attendees;
24import android.provider.Calendar.Events;
25import android.text.TextUtils;
26
27import java.util.ArrayList;
28import java.util.LinkedHashMap;
29import java.util.TimeZone;
30
31/**
32 * Stores all the information needed to fill out an entry in the events table.
33 * This is a convenient way for storing information needed by the UI to write to
34 * the events table. Only fields that are important to the UI are included.
35 */
36public class CalendarEventModel {
37    public static class Attendee {
38        @Override
39        public int hashCode() {
40            return (mEmail == null) ? 0 : mEmail.hashCode();
41        }
42
43        @Override
44        public boolean equals(Object obj) {
45            if (this == obj) {
46                return true;
47            }
48            if (obj == null) {
49                return false;
50            }
51            if (!(obj instanceof Attendee)) {
52                return false;
53            }
54            Attendee other = (Attendee) obj;
55            if (!TextUtils.equals(mEmail, other.mEmail)) {
56                return false;
57            }
58            return true;
59        }
60
61        public String mName;
62        public String mEmail;
63        public int mStatus;
64
65        public Attendee(String name, String email) {
66            mName = name;
67            mEmail = email;
68            mStatus = Attendees.ATTENDEE_STATUS_NONE;
69        }
70    }
71
72    // TODO strip out fields that don't ever get used
73    /**
74     * The uri of the event in the db. This should only be null for new events.
75     */
76    public Uri mUri = null;
77    public long mId = -1;
78    public long mCalendarId = -1;
79    public String mSyncId = null;
80    public String mSyncAccount = null;
81    public String mSyncAccountType = null;
82
83    // PROVIDER_NOTES owner account comes from the calendars table
84    public String mOwnerAccount = null;
85    public String mTitle = null;
86    public String mLocation = null;
87    public String mDescription = null;
88    public String mRrule = null;
89    public String mOrganizer = null;
90    public boolean mIsOrganizer = true;
91    public boolean mIsFirstEventInSeries = true;
92
93    // This should be set the same as mStart when created and is used for making changes to
94    // recurring events. It should not be updated after it is initially set.
95    public long mOriginalStart = -1;
96    public long mStart = -1;
97
98    // This should be set the same as mEnd when created and is used for making changes to
99    // recurring events. It should not be updated after it is initially set.
100    public long mOriginalEnd = -1;
101    public long mEnd = -1;
102    public String mDuration = null;
103    public String mTimezone = null;
104    public String mTimezone2 = null;
105    public boolean mAllDay = false;
106    public boolean mHasAlarm = false;
107    public boolean mTransparency = false;
108
109    // PROVIDER_NOTES How does an event not have attendee data? The owner is added
110    // as an attendee by default.
111    public boolean mHasAttendeeData = true;
112    public int mSelfAttendeeStatus = -1;
113    public int mOwnerAttendeeId = -1;
114    public String mOriginalEvent = null;
115    public Long mOriginalTime = null;
116    public Boolean mOriginalAllDay = null;
117    public boolean mGuestsCanModify = false;
118    public boolean mGuestsCanInviteOthers = false;
119    public boolean mGuestsCanSeeGuests = false;
120
121    public int mVisibility = 0;
122    public ArrayList<Integer> mReminderMinutes;
123
124    // PROVIDER_NOTES Using EditEventHelper the owner should not be included in this
125    // list and will instead be added by saveEvent. Is this what we want?
126    public LinkedHashMap<String, Attendee> mAttendeesList;
127
128    public CalendarEventModel() {
129        mReminderMinutes = new ArrayList<Integer>();
130        mAttendeesList = new LinkedHashMap<String, Attendee>();
131        mTimezone = TimeZone.getDefault().getID();
132    }
133
134    public CalendarEventModel(Context context) {
135        this();
136
137        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
138        String defaultReminder = prefs.getString(GeneralPreferences.KEY_DEFAULT_REMINDER,
139                "0");
140        int defaultReminderMins = Integer.parseInt(defaultReminder);
141        if (defaultReminderMins != 0) {
142            mHasAlarm = true;
143            mReminderMinutes.add(defaultReminderMins);
144        }
145    }
146
147    public CalendarEventModel(Context context, Intent intent) {
148        this(context);
149
150        String title = intent.getStringExtra(Events.TITLE);
151        if (title != null) {
152            mTitle = title;
153        }
154
155        String location = intent.getStringExtra(Events.EVENT_LOCATION);
156        if (location != null) {
157            mLocation = location;
158        }
159
160        String description = intent.getStringExtra(Events.DESCRIPTION);
161        if (description != null) {
162            mDescription = description;
163        }
164
165        int transparency = intent.getIntExtra(Events.TRANSPARENCY, -1);
166        if (transparency != -1) {
167            mTransparency = transparency != 0;
168        }
169
170        int visibility = intent.getIntExtra(Events.VISIBILITY, -1);
171        if (visibility != -1) {
172            mVisibility = visibility;
173        }
174
175        String rrule = intent.getStringExtra(Events.RRULE);
176        if (rrule != null) {
177            mRrule = rrule;
178        }
179    }
180
181    public boolean isValid() {
182        if (mCalendarId == -1) {
183            return false;
184        }
185        if (TextUtils.isEmpty(mOwnerAccount)) {
186            return false;
187        }
188        return true;
189    }
190
191    private boolean isEmpty() {
192        if (mTitle.length() > 0) {
193            return false;
194        }
195
196        if (mLocation.length() > 0) {
197            return false;
198        }
199
200        if (mDescription.length() > 0) {
201            return false;
202        }
203
204        return true;
205    }
206
207    public void clear() {
208        mUri = null;
209        mId = -1;
210        mCalendarId = -1;
211
212        mSyncId = null;
213        mSyncAccount = null;
214        mSyncAccountType = null;
215        mOwnerAccount = null;
216
217        mTitle = null;
218        mLocation = null;
219        mDescription = null;
220        mRrule = null;
221        mOrganizer = null;
222        mIsOrganizer = true;
223        mIsFirstEventInSeries = true;
224
225        mOriginalStart = -1;
226        mStart = -1;
227        mOriginalEnd = -1;
228        mEnd = -1;
229        mDuration = null;
230        mTimezone = null;
231        mTimezone2 = null;
232        mAllDay = false;
233        mHasAlarm = false;
234
235        mHasAttendeeData = true;
236        mSelfAttendeeStatus = -1;
237        mOwnerAttendeeId = -1;
238        mOriginalEvent = null;
239        mOriginalTime = null;
240        mOriginalAllDay = null;
241
242        mGuestsCanModify = false;
243        mGuestsCanInviteOthers = false;
244        mGuestsCanSeeGuests = false;
245        mVisibility = 0;
246
247        mReminderMinutes = new ArrayList<Integer>();
248        mAttendeesList.clear();
249    }
250
251    public void addAttendee(Attendee attendee) {
252        mAttendeesList.put(attendee.mEmail, attendee);
253    }
254
255    public void removeAttendee(Attendee attendee) {
256        mAttendeesList.remove(attendee.mEmail);
257    }
258
259    public String getAttendeesString() {
260        StringBuilder b = new StringBuilder();
261        for (Attendee attendee : mAttendeesList.values()) {
262            String name = attendee.mName;
263            String email = attendee.mEmail;
264            String status = Integer.toString(attendee.mStatus);
265            b.append("name:").append(name);
266            b.append(" email:").append(email);
267            b.append(" status:").append(status);
268        }
269        return b.toString();
270    }
271
272    @Override
273    public int hashCode() {
274        final int prime = 31;
275        int result = 1;
276        result = prime * result + (mAllDay ? 1231 : 1237);
277        result = prime * result + ((mAttendeesList == null) ? 0 : getAttendeesString().hashCode());
278        result = prime * result + (int) (mCalendarId ^ (mCalendarId >>> 32));
279        result = prime * result + ((mDescription == null) ? 0 : mDescription.hashCode());
280        result = prime * result + ((mDuration == null) ? 0 : mDuration.hashCode());
281        result = prime * result + (int) (mEnd ^ (mEnd >>> 32));
282        result = prime * result + (mGuestsCanInviteOthers ? 1231 : 1237);
283        result = prime * result + (mGuestsCanModify ? 1231 : 1237);
284        result = prime * result + (mGuestsCanSeeGuests ? 1231 : 1237);
285        result = prime * result + (mHasAlarm ? 1231 : 1237);
286        result = prime * result + (mHasAttendeeData ? 1231 : 1237);
287        result = prime * result + (int) (mId ^ (mId >>> 32));
288        result = prime * result + (mIsFirstEventInSeries ? 1231 : 1237);
289        result = prime * result + (mIsOrganizer ? 1231 : 1237);
290        result = prime * result + ((mLocation == null) ? 0 : mLocation.hashCode());
291        result = prime * result + ((mOrganizer == null) ? 0 : mOrganizer.hashCode());
292        result = prime * result + ((mOriginalAllDay == null) ? 0 : mOriginalAllDay.hashCode());
293        result = prime * result + (int) (mOriginalEnd ^ (mOriginalEnd >>> 32));
294        result = prime * result + ((mOriginalEvent == null) ? 0 : mOriginalEvent.hashCode());
295        result = prime * result + (int) (mOriginalStart ^ (mOriginalStart >>> 32));
296        result = prime * result + ((mOriginalTime == null) ? 0 : mOriginalTime.hashCode());
297        result = prime * result + ((mOwnerAccount == null) ? 0 : mOwnerAccount.hashCode());
298        result = prime * result + ((mReminderMinutes == null) ? 0 : mReminderMinutes.hashCode());
299        result = prime * result + ((mRrule == null) ? 0 : mRrule.hashCode());
300        result = prime * result + mSelfAttendeeStatus;
301        result = prime * result + mOwnerAttendeeId;
302        result = prime * result + (int) (mStart ^ (mStart >>> 32));
303        result = prime * result + ((mSyncAccount == null) ? 0 : mSyncAccount.hashCode());
304        result = prime * result + ((mSyncAccountType == null) ? 0 : mSyncAccountType.hashCode());
305        result = prime * result + ((mSyncId == null) ? 0 : mSyncId.hashCode());
306        result = prime * result + ((mTimezone == null) ? 0 : mTimezone.hashCode());
307        result = prime * result + ((mTimezone2 == null) ? 0 : mTimezone2.hashCode());
308        result = prime * result + ((mTitle == null) ? 0 : mTitle.hashCode());
309        result = prime * result + (mTransparency ? 1231 : 1237);
310        result = prime * result + ((mUri == null) ? 0 : mUri.hashCode());
311        result = prime * result + mVisibility;
312        return result;
313    }
314
315    // Autogenerated equals method
316    @Override
317    public boolean equals(Object obj) {
318        if (this == obj) {
319            return true;
320        }
321        if (obj == null) {
322            return false;
323        }
324        if (!(obj instanceof CalendarEventModel)) {
325            return false;
326        }
327
328        CalendarEventModel other = (CalendarEventModel) obj;
329        if (mAllDay != other.mAllDay) {
330            return false;
331        }
332        if (mAttendeesList == null) {
333            if (other.mAttendeesList != null) {
334                return false;
335            }
336        } else if (!TextUtils.equals(getAttendeesString(), other.getAttendeesString())) {
337            return false;
338        }
339
340        if (mCalendarId != other.mCalendarId) {
341            return false;
342        }
343
344        if (mDescription == null) {
345            if (other.mDescription != null) {
346                return false;
347            }
348        } else if (!mDescription.equals(other.mDescription)) {
349            return false;
350        }
351
352        if (mDuration == null) {
353            if (other.mDuration != null) {
354                return false;
355            }
356        } else if (!mDuration.equals(other.mDuration)) {
357            return false;
358        }
359
360        if (mEnd != other.mEnd) {
361            return false;
362        }
363        if (mGuestsCanInviteOthers != other.mGuestsCanInviteOthers) {
364            return false;
365        }
366        if (mGuestsCanModify != other.mGuestsCanModify) {
367            return false;
368        }
369        if (mGuestsCanSeeGuests != other.mGuestsCanSeeGuests) {
370            return false;
371        }
372        if (mHasAlarm != other.mHasAlarm) {
373            return false;
374        }
375        if (mHasAttendeeData != other.mHasAttendeeData) {
376            return false;
377        }
378        if (mId != other.mId) {
379            return false;
380        }
381        if (mIsFirstEventInSeries != other.mIsFirstEventInSeries) {
382            return false;
383        }
384        if (mIsOrganizer != other.mIsOrganizer) {
385            return false;
386        }
387
388        if (mLocation == null) {
389            if (other.mLocation != null) {
390                return false;
391            }
392        } else if (!mLocation.equals(other.mLocation)) {
393            return false;
394        }
395
396        if (mOrganizer == null) {
397            if (other.mOrganizer != null) {
398                return false;
399            }
400        } else if (!mOrganizer.equals(other.mOrganizer)) {
401            return false;
402        }
403
404        if (mOriginalAllDay == null) {
405            if (other.mOriginalAllDay != null) {
406                return false;
407            }
408        } else if (!mOriginalAllDay.equals(other.mOriginalAllDay)) {
409            return false;
410        }
411
412        if (mOriginalEnd != other.mOriginalEnd) {
413            return false;
414        }
415
416        if (mOriginalEvent == null) {
417            if (other.mOriginalEvent != null) {
418                return false;
419            }
420        } else if (!mOriginalEvent.equals(other.mOriginalEvent)) {
421            return false;
422        }
423
424        if (mOriginalStart != other.mOriginalStart) {
425            return false;
426        }
427
428        if (mOriginalTime == null) {
429            if (other.mOriginalTime != null) {
430                return false;
431            }
432        } else if (!mOriginalTime.equals(other.mOriginalTime)) {
433            return false;
434        }
435
436        if (mOwnerAccount == null) {
437            if (other.mOwnerAccount != null) {
438                return false;
439            }
440        } else if (!mOwnerAccount.equals(other.mOwnerAccount)) {
441            return false;
442        }
443
444        if (mReminderMinutes == null) {
445            if (other.mReminderMinutes != null) {
446                return false;
447            }
448        } else if (!mReminderMinutes.equals(other.mReminderMinutes)) {
449            return false;
450        }
451
452        if (mRrule == null) {
453            if (other.mRrule != null) {
454                return false;
455            }
456        } else if (!mRrule.equals(other.mRrule)) {
457            return false;
458        }
459
460        if (mSelfAttendeeStatus != other.mSelfAttendeeStatus) {
461            return false;
462        }
463        if (mOwnerAttendeeId != other.mOwnerAttendeeId) {
464            return false;
465        }
466        if (mStart != other.mStart) {
467            return false;
468        }
469        if (mSyncAccount == null) {
470            if (other.mSyncAccount != null) {
471                return false;
472            }
473        } else if (!mSyncAccount.equals(other.mSyncAccount)) {
474            return false;
475        }
476
477        if (mSyncAccountType == null) {
478            if (other.mSyncAccountType != null) {
479                return false;
480            }
481        } else if (!mSyncAccountType.equals(other.mSyncAccountType)) {
482            return false;
483        }
484
485        if (mSyncId == null) {
486            if (other.mSyncId != null) {
487                return false;
488            }
489        } else if (!mSyncId.equals(other.mSyncId)) {
490            return false;
491        }
492
493        if (mTimezone == null) {
494            if (other.mTimezone != null) {
495                return false;
496            }
497        } else if (!mTimezone.equals(other.mTimezone)) {
498            return false;
499        }
500
501        if (mTimezone2 == null) {
502            if (other.mTimezone2 != null) {
503                return false;
504            }
505        } else if (!mTimezone2.equals(other.mTimezone2)) {
506            return false;
507        }
508
509        if (mTitle == null) {
510            if (other.mTitle != null) {
511                return false;
512            }
513        } else if (!mTitle.equals(other.mTitle)) {
514            return false;
515        }
516
517        if (mTransparency != other.mTransparency) {
518            return false;
519        }
520
521        if (mUri == null) {
522            if (other.mUri != null) {
523                return false;
524            }
525        } else if (!mUri.equals(other.mUri)) {
526            return false;
527        }
528
529        if (mVisibility != other.mVisibility) {
530            return false;
531        }
532
533        return true;
534    }
535}
536