Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2012 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.sanoma.hockeynl.standenmotor.calender;
- import android.util.Log;
- import com.google.api.client.googleapis.GoogleHeaders;
- import com.google.api.client.googleapis.batch.BatchRequest;
- import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
- import com.google.api.client.googleapis.json.GoogleJsonError;
- import com.google.api.client.util.DateTime;
- import com.google.api.services.calendar.model.Event;
- import com.google.api.services.calendar.model.EventDateTime;
- import com.sanoma.hockeynl.standenmotor.ListController;
- import com.sanoma.hockeynl.standenmotor.ListController.Teams;
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- import java.util.TimeZone;
- /**
- * Asynchronously insert a new event on a given calendar.
- *
- * original author Yaniv Inbar on http://code.google.com/p/google-api-java-client/source/browse/calendar-android-sample/src/main/java/com/google/api/services/samples/calendar/android/AsyncBatchInsertCalendars.java?repo=samples
- * @author Dylan Drost
- */
- public class AsyncBatchInsertEvent extends EventAsyncTask {
- private final static String TAG = "AsyncBatchInsertEvent";
- private final ArrayList<Teams> teamList;
- private final List<Event> events;
- private final CalendarInfo calenderInfo;
- private final String id;
- AsyncBatchInsertEvent(ListController activity, CalendarInfo calendar, ArrayList<Teams> teamList, String id) {
- super(activity);
- this.teamList = teamList;
- this.calenderInfo = calendar;
- this.id = id;
- events = new ArrayList<Event>();
- }
- @Override
- protected void doInBackground() throws IOException {
- Event tmpEvent;
- Log.d(TAG, "team ID: " + id);
- for (final Teams team : teamList){
- final String[] array = team.getProperties();
- Log.d(TAG, "team 1 ID: " + array[2]);
- Log.d(TAG, "team 2 ID: " + array[3]);
- if(array[2] == id || array[3] == id) {
- tmpEvent = new Event();
- tmpEvent.setSummary(array[4] + " - " + array[5]);
- tmpEvent.setLocation(array[11] + ", " + array[12]);
- Date startDate = null;
- final String[] formats = new String[] {
- "yyyy-MM-dd HH:mm",
- "yyyy-MM-dd",
- };
- for (final String format : formats) {
- String tmp = array[0] + " " + array[1];
- tmp = tmp.trim();
- try {
- startDate = new SimpleDateFormat(format, Locale.US).parse(tmp);
- } catch (final ParseException e) {
- //throw new RuntimeException(e);
- }
- Log.d(TAG, tmp);
- }
- if (startDate == null) {
- startDate = new Date();
- }
- final Date endDate = new Date(startDate.getTime() + 3600000);
- final DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
- tmpEvent.setStart(new EventDateTime().setDateTime(start));
- final DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
- tmpEvent.setEnd(new EventDateTime().setDateTime(end));
- events.add(tmpEvent);
- }
- }
- final BatchRequest batch = client.batch();
- /*final JsonBatchCallback<Event> callback = new JsonBatchCallback<Event>() {
- @Override
- public void onSuccess(Event t, GoogleHeaders responseHeaders) throws IOException {
- // TODO Calender ADD succes message on batch event adding
- }
- @Override
- public void onFailure(GoogleJsonError err, GoogleHeaders responseHeaders)
- throws IOException {
- Utils.logAndShowError(activity, ListController.LOGTAG, err.getMessage());
- }
- };*/
- for (final Event event : events) {
- client.events().insert(calenderInfo.id, event).queue(batch, new JsonBatchCallback<Event>() {
- @Override
- public void onSuccess(Event t, GoogleHeaders responseHeaders) throws IOException {
- // TODO Calender ADD succes message on batch event adding
- }
- @Override
- public void onFailure(GoogleJsonError err, GoogleHeaders responseHeaders)
- throws IOException {
- Utils.logAndShowError(activity, ListController.LOGTAG, err.getMessage());
- }
- });
- }
- try {
- batch.execute();
- } catch (final Exception e) {
- Log.e("ERR", "", e);
- throw new RuntimeException(e);
- }
- }
- public static void batchInsertEvents(ListController activity, CalendarInfo calender, ArrayList<Teams> teamList, String id) {
- new AsyncBatchInsertEvent(activity, calender, teamList, id).execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment