CalendarEventModel.java revision 352e1a2f834460bd54e03ce94cadd36a46debacb
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 String mOrganizerDisplayName = null; 91 /** 92 * Read-Only - Derived from other fields 93 */ 94 public boolean mIsOrganizer = true; 95 public boolean mIsFirstEventInSeries = true; 96 97 // This should be set the same as mStart when created and is used for making changes to 98 // recurring events. It should not be updated after it is initially set. 99 public long mOriginalStart = -1; 100 public long mStart = -1; 101 102 // This should be set the same as mEnd when created and is used for making changes to 103 // recurring events. It should not be updated after it is initially set. 104 public long mOriginalEnd = -1; 105 public long mEnd = -1; 106 public String mDuration = null; 107 public String mTimezone = null; 108 public String mTimezone2 = null; 109 public boolean mAllDay = false; 110 public boolean mHasAlarm = false; 111 public boolean mTransparency = false; 112 113 // PROVIDER_NOTES How does an event not have attendee data? The owner is added 114 // as an attendee by default. 115 public boolean mHasAttendeeData = true; 116 public int mSelfAttendeeStatus = -1; 117 public int mOwnerAttendeeId = -1; 118 public String mOriginalEvent = null; 119 public Long mOriginalTime = null; 120 public Boolean mOriginalAllDay = null; 121 public boolean mGuestsCanModify = false; 122 public boolean mGuestsCanInviteOthers = false; 123 public boolean mGuestsCanSeeGuests = false; 124 125// NEW vvvvvvvvvvvvvvv 126 // FRAG_TODO update equal/hashcode/clear 127 public boolean mOrganizerCanRespond = false; 128 public int mCalendarAccessLevel = 0; 129 130 // The model can't be updated with a calendar cursor until it has been 131 // updated with an event cursor. 132 public boolean mModelUpdatedWithEventCursor; 133// NEW ^^^^^^^^^^^^^ 134 135 public int mVisibility = 0; 136 public ArrayList<Integer> mReminderMinutes; 137 138 // PROVIDER_NOTES Using EditEventHelper the owner should not be included in this 139 // list and will instead be added by saveEvent. Is this what we want? 140 public LinkedHashMap<String, Attendee> mAttendeesList; 141 142 public CalendarEventModel() { 143 mReminderMinutes = new ArrayList<Integer>(); 144 mAttendeesList = new LinkedHashMap<String, Attendee>(); 145 mTimezone = TimeZone.getDefault().getID(); 146 } 147 148 public CalendarEventModel(Context context) { 149 this(); 150 151 SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); 152 String defaultReminder = prefs.getString(GeneralPreferences.KEY_DEFAULT_REMINDER, 153 "0"); 154 int defaultReminderMins = Integer.parseInt(defaultReminder); 155 if (defaultReminderMins != 0) { 156 mHasAlarm = true; 157 mReminderMinutes.add(defaultReminderMins); 158 } 159 } 160 161 public CalendarEventModel(Context context, Intent intent) { 162 this(context); 163 164 String title = intent.getStringExtra(Events.TITLE); 165 if (title != null) { 166 mTitle = title; 167 } 168 169 String location = intent.getStringExtra(Events.EVENT_LOCATION); 170 if (location != null) { 171 mLocation = location; 172 } 173 174 String description = intent.getStringExtra(Events.DESCRIPTION); 175 if (description != null) { 176 mDescription = description; 177 } 178 179 int transparency = intent.getIntExtra(Events.TRANSPARENCY, -1); 180 if (transparency != -1) { 181 mTransparency = transparency != 0; 182 } 183 184 int visibility = intent.getIntExtra(Events.VISIBILITY, -1); 185 if (visibility != -1) { 186 mVisibility = visibility; 187 } 188 189 String rrule = intent.getStringExtra(Events.RRULE); 190 if (rrule != null) { 191 mRrule = rrule; 192 } 193 } 194 195 public boolean isValid() { 196 if (mCalendarId == -1) { 197 return false; 198 } 199 if (TextUtils.isEmpty(mOwnerAccount)) { 200 return false; 201 } 202 return true; 203 } 204 205 private boolean isEmpty() { 206 if (mTitle.length() > 0) { 207 return false; 208 } 209 210 if (mLocation.length() > 0) { 211 return false; 212 } 213 214 if (mDescription.length() > 0) { 215 return false; 216 } 217 218 return true; 219 } 220 221 public void clear() { 222 mUri = null; 223 mId = -1; 224 mCalendarId = -1; 225 226 mSyncId = null; 227 mSyncAccount = null; 228 mSyncAccountType = null; 229 mOwnerAccount = null; 230 231 mTitle = null; 232 mLocation = null; 233 mDescription = null; 234 mRrule = null; 235 mOrganizer = null; 236 mOrganizerDisplayName = null; 237 mIsOrganizer = true; 238 mIsFirstEventInSeries = true; 239 240 mOriginalStart = -1; 241 mStart = -1; 242 mOriginalEnd = -1; 243 mEnd = -1; 244 mDuration = null; 245 mTimezone = null; 246 mTimezone2 = null; 247 mAllDay = false; 248 mHasAlarm = false; 249 250 mHasAttendeeData = true; 251 mSelfAttendeeStatus = -1; 252 mOwnerAttendeeId = -1; 253 mOriginalEvent = null; 254 mOriginalTime = null; 255 mOriginalAllDay = null; 256 257 mGuestsCanModify = false; 258 mGuestsCanInviteOthers = false; 259 mGuestsCanSeeGuests = false; 260 mVisibility = 0; 261 262 mReminderMinutes = new ArrayList<Integer>(); 263 mAttendeesList.clear(); 264 } 265 266 public void addAttendee(Attendee attendee) { 267 mAttendeesList.put(attendee.mEmail, attendee); 268 } 269 270 public void removeAttendee(Attendee attendee) { 271 mAttendeesList.remove(attendee.mEmail); 272 } 273 274 public String getAttendeesString() { 275 StringBuilder b = new StringBuilder(); 276 for (Attendee attendee : mAttendeesList.values()) { 277 String name = attendee.mName; 278 String email = attendee.mEmail; 279 String status = Integer.toString(attendee.mStatus); 280 b.append("name:").append(name); 281 b.append(" email:").append(email); 282 b.append(" status:").append(status); 283 } 284 return b.toString(); 285 } 286 287 @Override 288 public int hashCode() { 289 final int prime = 31; 290 int result = 1; 291 result = prime * result + (mAllDay ? 1231 : 1237); 292 result = prime * result + ((mAttendeesList == null) ? 0 : getAttendeesString().hashCode()); 293 result = prime * result + (int) (mCalendarId ^ (mCalendarId >>> 32)); 294 result = prime * result + ((mDescription == null) ? 0 : mDescription.hashCode()); 295 result = prime * result + ((mDuration == null) ? 0 : mDuration.hashCode()); 296 result = prime * result + (int) (mEnd ^ (mEnd >>> 32)); 297 result = prime * result + (mGuestsCanInviteOthers ? 1231 : 1237); 298 result = prime * result + (mGuestsCanModify ? 1231 : 1237); 299 result = prime * result + (mGuestsCanSeeGuests ? 1231 : 1237); 300 result = prime * result + (mHasAlarm ? 1231 : 1237); 301 result = prime * result + (mHasAttendeeData ? 1231 : 1237); 302 result = prime * result + (int) (mId ^ (mId >>> 32)); 303 result = prime * result + (mIsFirstEventInSeries ? 1231 : 1237); 304 result = prime * result + (mIsOrganizer ? 1231 : 1237); 305 result = prime * result + ((mLocation == null) ? 0 : mLocation.hashCode()); 306 result = prime * result + ((mOrganizer == null) ? 0 : mOrganizer.hashCode()); 307 result = prime * result + ((mOriginalAllDay == null) ? 0 : mOriginalAllDay.hashCode()); 308 result = prime * result + (int) (mOriginalEnd ^ (mOriginalEnd >>> 32)); 309 result = prime * result + ((mOriginalEvent == null) ? 0 : mOriginalEvent.hashCode()); 310 result = prime * result + (int) (mOriginalStart ^ (mOriginalStart >>> 32)); 311 result = prime * result + ((mOriginalTime == null) ? 0 : mOriginalTime.hashCode()); 312 result = prime * result + ((mOwnerAccount == null) ? 0 : mOwnerAccount.hashCode()); 313 result = prime * result + ((mReminderMinutes == null) ? 0 : mReminderMinutes.hashCode()); 314 result = prime * result + ((mRrule == null) ? 0 : mRrule.hashCode()); 315 result = prime * result + mSelfAttendeeStatus; 316 result = prime * result + mOwnerAttendeeId; 317 result = prime * result + (int) (mStart ^ (mStart >>> 32)); 318 result = prime * result + ((mSyncAccount == null) ? 0 : mSyncAccount.hashCode()); 319 result = prime * result + ((mSyncAccountType == null) ? 0 : mSyncAccountType.hashCode()); 320 result = prime * result + ((mSyncId == null) ? 0 : mSyncId.hashCode()); 321 result = prime * result + ((mTimezone == null) ? 0 : mTimezone.hashCode()); 322 result = prime * result + ((mTimezone2 == null) ? 0 : mTimezone2.hashCode()); 323 result = prime * result + ((mTitle == null) ? 0 : mTitle.hashCode()); 324 result = prime * result + (mTransparency ? 1231 : 1237); 325 result = prime * result + ((mUri == null) ? 0 : mUri.hashCode()); 326 result = prime * result + mVisibility; 327 return result; 328 } 329 330 // Autogenerated equals method 331 @Override 332 public boolean equals(Object obj) { 333 if (this == obj) { 334 return true; 335 } 336 if (obj == null) { 337 return false; 338 } 339 if (!(obj instanceof CalendarEventModel)) { 340 return false; 341 } 342 343 CalendarEventModel other = (CalendarEventModel) obj; 344 if (mAllDay != other.mAllDay) { 345 return false; 346 } 347 if (mAttendeesList == null) { 348 if (other.mAttendeesList != null) { 349 return false; 350 } 351 } else if (!TextUtils.equals(getAttendeesString(), other.getAttendeesString())) { 352 return false; 353 } 354 355 if (mCalendarId != other.mCalendarId) { 356 return false; 357 } 358 359 if (mDescription == null) { 360 if (other.mDescription != null) { 361 return false; 362 } 363 } else if (!mDescription.equals(other.mDescription)) { 364 return false; 365 } 366 367 if (mDuration == null) { 368 if (other.mDuration != null) { 369 return false; 370 } 371 } else if (!mDuration.equals(other.mDuration)) { 372 return false; 373 } 374 375 if (mEnd != other.mEnd) { 376 return false; 377 } 378 if (mGuestsCanInviteOthers != other.mGuestsCanInviteOthers) { 379 return false; 380 } 381 if (mGuestsCanModify != other.mGuestsCanModify) { 382 return false; 383 } 384 if (mGuestsCanSeeGuests != other.mGuestsCanSeeGuests) { 385 return false; 386 } 387 if (mHasAlarm != other.mHasAlarm) { 388 return false; 389 } 390 if (mHasAttendeeData != other.mHasAttendeeData) { 391 return false; 392 } 393 if (mId != other.mId) { 394 return false; 395 } 396 if (mIsFirstEventInSeries != other.mIsFirstEventInSeries) { 397 return false; 398 } 399 if (mIsOrganizer != other.mIsOrganizer) { 400 return false; 401 } 402 403 if (mLocation == null) { 404 if (other.mLocation != null) { 405 return false; 406 } 407 } else if (!mLocation.equals(other.mLocation)) { 408 return false; 409 } 410 411 if (mOrganizer == null) { 412 if (other.mOrganizer != null) { 413 return false; 414 } 415 } else if (!mOrganizer.equals(other.mOrganizer)) { 416 return false; 417 } 418 419 if (mOriginalAllDay == null) { 420 if (other.mOriginalAllDay != null) { 421 return false; 422 } 423 } else if (!mOriginalAllDay.equals(other.mOriginalAllDay)) { 424 return false; 425 } 426 427 if (mOriginalEnd != other.mOriginalEnd) { 428 return false; 429 } 430 431 if (mOriginalEvent == null) { 432 if (other.mOriginalEvent != null) { 433 return false; 434 } 435 } else if (!mOriginalEvent.equals(other.mOriginalEvent)) { 436 return false; 437 } 438 439 if (mOriginalStart != other.mOriginalStart) { 440 return false; 441 } 442 443 if (mOriginalTime == null) { 444 if (other.mOriginalTime != null) { 445 return false; 446 } 447 } else if (!mOriginalTime.equals(other.mOriginalTime)) { 448 return false; 449 } 450 451 if (mOwnerAccount == null) { 452 if (other.mOwnerAccount != null) { 453 return false; 454 } 455 } else if (!mOwnerAccount.equals(other.mOwnerAccount)) { 456 return false; 457 } 458 459 if (mReminderMinutes == null) { 460 if (other.mReminderMinutes != null) { 461 return false; 462 } 463 } else if (!mReminderMinutes.equals(other.mReminderMinutes)) { 464 return false; 465 } 466 467 if (mRrule == null) { 468 if (other.mRrule != null) { 469 return false; 470 } 471 } else if (!mRrule.equals(other.mRrule)) { 472 return false; 473 } 474 475 if (mSelfAttendeeStatus != other.mSelfAttendeeStatus) { 476 return false; 477 } 478 if (mOwnerAttendeeId != other.mOwnerAttendeeId) { 479 return false; 480 } 481 if (mStart != other.mStart) { 482 return false; 483 } 484 if (mSyncAccount == null) { 485 if (other.mSyncAccount != null) { 486 return false; 487 } 488 } else if (!mSyncAccount.equals(other.mSyncAccount)) { 489 return false; 490 } 491 492 if (mSyncAccountType == null) { 493 if (other.mSyncAccountType != null) { 494 return false; 495 } 496 } else if (!mSyncAccountType.equals(other.mSyncAccountType)) { 497 return false; 498 } 499 500 if (mSyncId == null) { 501 if (other.mSyncId != null) { 502 return false; 503 } 504 } else if (!mSyncId.equals(other.mSyncId)) { 505 return false; 506 } 507 508 if (mTimezone == null) { 509 if (other.mTimezone != null) { 510 return false; 511 } 512 } else if (!mTimezone.equals(other.mTimezone)) { 513 return false; 514 } 515 516 if (mTimezone2 == null) { 517 if (other.mTimezone2 != null) { 518 return false; 519 } 520 } else if (!mTimezone2.equals(other.mTimezone2)) { 521 return false; 522 } 523 524 if (mTitle == null) { 525 if (other.mTitle != null) { 526 return false; 527 } 528 } else if (!mTitle.equals(other.mTitle)) { 529 return false; 530 } 531 532 if (mTransparency != other.mTransparency) { 533 return false; 534 } 535 536 if (mUri == null) { 537 if (other.mUri != null) { 538 return false; 539 } 540 } else if (!mUri.equals(other.mUri)) { 541 return false; 542 } 543 544 if (mVisibility != other.mVisibility) { 545 return false; 546 } 547 548 return true; 549 } 550} 551