@Override
 public void onDeleted(android.content.Context context, int[] appWidgetIds) {
   Log.d(cTag, "onDeleted");
   // When the user deletes the widget, delete the preference associated with it.
   final int N = appWidgetIds.length;
   Editor editor = Preferences.getEditor(context);
   for (int i = 0; i < N; i++) {
     String prefKey = Preferences.getWidgetQueryKey(appWidgetIds[i]);
     editor.remove(prefKey);
   }
   editor.commit();
 }
 @Override
 public void onUpdate(
     android.content.Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
   Log.d(cTag, "onUpdate");
   final int N = appWidgetIds.length;
   for (int i = 0; i < N; i++) {
     int appWidgetId = appWidgetIds[i];
     String prefKey = Preferences.getWidgetQueryKey(appWidgetId);
     String queryName = Preferences.getWidgetQuery(context, prefKey);
     updateAppWidget(context, appWidgetManager, appWidgetId, queryName);
   }
 }
  private Uri addOrUpdateCalendarEvent(
      Id calEventId,
      String title,
      String description,
      Id projectId,
      Id contextId,
      String timezone,
      long start,
      long end,
      boolean allDay) {
    if (projectId.isInitialised()) {
      String projectName = getProjectName(projectId);
      title = projectName + " - " + title;
    }
    if (description == null) {
      description = "";
    }

    ContentValues values = new ContentValues();
    if (!TextUtils.isEmpty(timezone)) {
      values.put("eventTimezone", timezone);
    }
    values.put("calendar_id", Preferences.getCalendarId(this));
    values.put("title", title);
    values.put("allDay", allDay ? 1 : 0);
    if (start > 0L) {
      values.put("dtstart", start); // long (start date in ms)
    }
    if (end > 0L) {
      values.put("dtend", end); // long (end date in ms)
    }
    values.put("description", description);
    values.put("hasAlarm", 0);
    values.put("transparency", 0);
    values.put("visibility", 0);
    if (contextId.isInitialised()) {
      String contextName = getContextName(contextId);
      values.put("eventLocation", contextName);
    }

    Uri baseUri;
    if (OSUtils.osAtLeastFroyo()) {
      baseUri = Uri.parse("content://com.android.calendar/events");
    } else {
      baseUri = Uri.parse("content://calendar/events");
    }
    ;

    Uri eventUri = null;
    try {
      eventUri = addCalendarEntry(values, calEventId, baseUri);
    } catch (Exception e) {
      Log.e(cTag, "Attempt failed to create calendar entry", e);
      mAnalytics.onError(
          Constants.cFlurryCalendarUpdateError, e.getMessage(), getClass().getName());
    }

    return eventUri;
  }
  private void findViewsAndAddListeners() {
    // The text view for our task description, identified by its ID in the XML file.

    setupContextSpinner();
    ImageButton addContextButton = (ImageButton) findViewById(R.id.context_add);
    addContextButton.setOnClickListener(this);
    addContextButton.setOnFocusChangeListener(this);

    setupProjectSpinner();
    ImageButton addProjectButton = (ImageButton) findViewById(R.id.project_add);
    addProjectButton.setOnClickListener(this);
    addProjectButton.setOnFocusChangeListener(this);

    mCompleteEntry.setOnClickListener(this);
    mCompleteEntry.setOnFocusChangeListener(this);
    mCompletedCheckBox = (CheckBox) mCompleteEntry.findViewById(R.id.completed_entry_checkbox);

    mDeletedEntry.setOnClickListener(this);
    mDeletedEntry.setOnFocusChangeListener(this);

    mUpdateCalendarEntry.setOnClickListener(this);
    mUpdateCalendarEntry.setOnFocusChangeListener(this);
    mUpdateCalendarCheckBox =
        (CheckBox) mUpdateCalendarEntry.findViewById(R.id.update_calendar_checkbox);
    mCalendarLabel = (TextView) mUpdateCalendarEntry.findViewById(R.id.gcal_label);
    mCalendarDetail = (TextView) mUpdateCalendarEntry.findViewById(R.id.gcal_detail);

    mStartDateButton.setOnClickListener(new DateClickListener(mStartTime));

    mStartTimeButton.setOnClickListener(new TimeClickListener(mStartTime));

    mDueDateButton.setOnClickListener(new DateClickListener(mDueTime));

    mDueTimeButton.setOnClickListener(new TimeClickListener(mDueTime));

    mAllDayCheckBox.setOnCheckedChangeListener(this);

    mClearButton.setOnClickListener(this);

    ViewGroup schedulingSection = (ViewGroup) findViewById(R.id.scheduling_section);
    View schedulingEntry = findViewById(R.id.scheduling_entry);
    schedulingEntry.setOnClickListener(this);
    schedulingEntry.setOnFocusChangeListener(this);

    mSchedulingExtra = schedulingSection.findViewById(R.id.scheduling_extra);
    mExpandButton = schedulingEntry.findViewById(R.id.expand);
    mCollapseButton = schedulingEntry.findViewById(R.id.collapse);
    mSchedulingDetail = (TextView) schedulingEntry.findViewById(R.id.scheduling_detail);
    mSchedulingExpanded = mSchedulingExtra.getVisibility() == View.VISIBLE;

    // Initialize the reminder values array.
    Resources r = getResources();
    String[] strings = r.getStringArray(R.array.reminder_minutes_values);
    ArrayList<Integer> list = new ArrayList<Integer>(strings.length);
    for (String numberString : strings) {
      list.add(Integer.parseInt(numberString));
    }
    mReminderValues = list;
    String[] labels = r.getStringArray(R.array.reminder_minutes_labels);
    mReminderLabels = new ArrayList<String>(Arrays.asList(labels));

    mDefaultReminderMinutes = Preferences.getDefaultReminderMinutes(this);

    // Setup the + Add Reminder Button
    ImageButton reminderAddButton = (ImageButton) findViewById(R.id.reminder_add);
    reminderAddButton.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            addReminder();
          }
        });
  }