Exemplo n.º 1
0
  // synch from google
  public void synchfromGoogle(View v) {
    showProgress(true);
    Uri.Builder eventsUriBuilder = CalendarContract.Events.CONTENT_URI.buildUpon();
    // ContentUris.appendId(eventsUriBuilder, timeNow);
    // ContentUris.appendId(eventsUriBuilder, endOfToday);
    Uri eventsUri = eventsUriBuilder.build();
    Cursor cursor = null;
    cursor =
        getContentResolver()
            .query(
                eventsUri,
                EVENT_PROJECTION,
                null,
                null,
                CalendarContract.Instances.DTSTART + " ASC");
    events = new ArrayList<>();
    while (cursor.moveToNext()) {
      if (events.size() >= MAX_TO_IMPORT) break;
      Long eventID = cursor.getLong(PROJECTION_ID_INDEX);
      String title = cursor.getString(PROJECTION_EVENT_TITLE_INDEX);
      String description = cursor.getString(PROJECTION_EVENT_DESC_INDEX);
      String location = cursor.getString(PROJECTION_EVENT_LOCATION_INDEX);
      Long startDate = cursor.getLong(PROJECTION_EVENT_DTSTART_INDEX);
      Long endDate = cursor.getLong(PROJECTION_EVENT_DTEND_INDEX);

      LoginCredentialsApplication credentialsApplication =
          (LoginCredentialsApplication) getApplicationContext();

      CreateEventObject eventObject = new CreateEventObject();
      eventObject.setEmail(credentialsApplication.getEmail());
      eventObject.setPassword(credentialsApplication.getPassword());
      eventObject.setName(title);
      eventObject.setDesc(description);
      eventObject.setLocation(location);
      eventObject.setNotify(false);
      eventObject.setnDate(0L);
      eventObject.setsDate(startDate);
      eventObject.seteDate(endDate);

      events.add(eventObject);

      // System.out.println("Event Id = " + eventID);
      // System.out.println("Event Title = " + title);
      // System.out.println("Event Desc = " + description);
      // System.out.println("Event Location = " + location);
      // System.out.println("Event StartDate = " + new Date(startDate).toString());
      // System.out.println("Event endDate = " + new Date(endDate).toString());

    }
    allEventsToSystem = events.size();
    processNextEvent();
  }
Exemplo n.º 2
0
  private void addToGoogle(EventSearchObject events) {
    Uri.Builder eventsUriBuilder = CalendarContract.Events.CONTENT_URI.buildUpon();
    // ContentUris.appendId(eventsUriBuilder, timeNow);
    // ContentUris.appendId(eventsUriBuilder, endOfToday);
    Uri eventsUri = eventsUriBuilder.build();
    ContentValues[] values;
    if (MAX_TO_EXPORT >= events.getResult().size()) {
      values = new ContentValues[events.getResult().size()];
    } else {
      values = new ContentValues[MAX_TO_EXPORT];
    }

    long calendar_id = getCalendarId();
    int i = 0;
    for (EventObject eventObject : events.getResult()) {
      if (i >= MAX_TO_EXPORT) break;
      ContentValues value = new ContentValues();
      TimeZone timeZone = TimeZone.getDefault();
      if (eventObject.getTitle() != null) {
        value.put(CalendarContract.Events.TITLE, eventObject.getTitle());
      }
      if (eventObject.getDescription() != null) {
        value.put(CalendarContract.Events.DESCRIPTION, eventObject.getDescription());
      }
      if (eventObject.getStart() != null) {
        value.put(CalendarContract.Events.DTSTART, eventObject.getStart());
      }
      if (eventObject.getEnd() != null) {
        value.put(CalendarContract.Events.DTEND, eventObject.getEnd());
      }
      if (timeZone.getID() != null) {
        value.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
      }
      if (eventObject.getLocation() != null) {
        value.put(CalendarContract.Events.EVENT_LOCATION, eventObject.getLocation());
      }
      value.put(CalendarContract.Events.CALENDAR_ID, calendar_id);

      System.out.println("Event title = " + eventObject.getTitle());
      System.out.println("Event Start Date = " + new Date(eventObject.getStart()).toString());

      values[i++] = value;
    }
    int result = getContentResolver().bulkInsert(eventsUri, values);

    System.out.println("Result is " + result);

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Status");
    alertDialog.setMessage(
        values.length + " " + getResources().getString(R.string.event_synToGoogle_success));
    alertDialog.setButton(
        AlertDialog.BUTTON_NEUTRAL,
        "OK",
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
          }
        });
    alertDialog.show();
    showProgress(false);
  }