Tuesday, September 6, 2016

Evnet insert to Calendar using Calendar API dosn't show on the Calendar App

Following the Google documentation I create an app that insert new events to the user's calendar.
In the following code you can see how I did it.

MakeRequastTask.class

Code:

/**
 * An asynchronous task that handles the Google Calendar API call.
 * Placing the API calls in their own task ensures the UI stays responsive.
 */
private class MakeRequestTask extends AsyncTask<Void, Void, String[][]> {
    private com.google.api.services.calendar.Calendar mServiceCalendar = null;
  private Exception mLastError = null;

    private String[][] tempArray;
    private SpreadSheet mSheet;

    MakeRequestTask(GoogleAccountCredential credential, SpreadSheet mSheet) {
        this.mSheet = mSheet;
        tempArray = new String[7][2];
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

        mServiceCalendar = new com.google.api.services.calendar.Calendar.Builder(
                transport, jsonFactory, credential)
                .setApplicationName("Bank Of Israel")
                .build();
    }

    /**
    * Background task to call Google Calendar API.
    * @param params no parameters needed for this task.
    */
    @Override
    protected String[][] doInBackground(Void... params) {
        try { 
            setDataInCalendar();

        } catch (Exception e) {
            mLastError = e;
            cancel(true);
            return null;
        }
        return null;
    }

    //--- Google Calendar ---\\

    private void setDataInCalendar() throws IOException {
        Vector<Shift> temp  = mSheet.getShifts();
        for(Shift s: temp) {
            Event e = createEvent(s);
            String calendarId = userEmailAddress;
            e = mServiceCalendar.events().insert(calendarId, e).execute();
            Log.d("MyApp", "Event created: " + e.getHtmlLink());
        }
    }

    private Event createEvent(Shift mShift) {
        final String  TimeOffSet = "-07:00";
        Event event = new Event();
        event.setSummary(mShift.getShiftName());//type
        //set Date, Start & End Times
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

        //make a String for the DateTime Object
        //format as: "yyyy-MM-ddTHH:mm:ss+UTCOffSet"
        String startTimeUTC = sd.format(mShift.getShiftDate()) +"T"+
                mShift.getStartTime().toString() + TimeOffSet ;
        DateTime startDateTime = new DateTime(startTimeUTC);
        EventDateTime start = new EventDateTime()
                .setDateTime(startDateTime);

        event.setStart(start);

        String endTimeUTC;
        //if the shift is C or C1 the end of the shift is in the next day
        if (mShift.getShiftName().equals("c1")
                ||
                mShift.getShiftName().equals("c")){
            endTimeUTC = sd.format(PatternTest.oneDayForeword(mShift.getShiftDate())) +"T"+
                    mShift.getEndTime().toString()+ TimeOffSet;
        }
        else{
            endTimeUTC = sd.format(mShift.getShiftDate()) +"T"+
                    mShift.getEndTime().toString() + IsraelTimeOffSet;
        }
        DateTime endDateTime = new DateTime(endTimeUTC);
        EventDateTime end = new EventDateTime()
                .setDateTime(endDateTime);

        event.setEnd(end);

        event.setColorId("10");
        return event;
    }
    //------------------------------------------------------------------------------------\\
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showProgressDialog();
    }

    @Override
    protected void onPostExecute(String[][] strings) {
        super.onPostExecute(strings);
            hideProgressDialog();
            Toast.makeText(SheetActivity.this, "Finish!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onCancelled() {
      hideProgressDialog();
        if (mLastError != null) {
            if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
                showGooglePlayServicesAvailabilityErrorDialog(
                        ((GooglePlayServicesAvailabilityIOException) mLastError)
                                .getConnectionStatusCode());
            } else if (mLastError instanceof UserRecoverableAuthIOException) {
                startActivityForResult(
                        ((UserRecoverableAuthIOException) mLastError).getIntent(),
                        SheetActivity.REQUEST_AUTHORIZATION);
            }
            else if (mLastError instanceof NoShiftsInFileException){
                Toast.makeText(SheetActivity.this, mLastError.getMessage() , Toast.LENGTH_SHORT).show();
            }
            else if (mLastError instanceof NoUserNameException){
                Toast.makeText(SheetActivity.this, mLastError.getMessage() , Toast.LENGTH_LONG).show();
                finish();//go back to the SignIn Activity
            }
            else {
                Toast.makeText(SheetActivity.this, "The following error occurred:\n"
                        + mLastError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(SheetActivity.this, "Request cancelled." , Toast.LENGTH_SHORT).show();
        }
    }
}

My problem is that after my app insert the new events, I can see them in the web Calendar but the Google Calendar App or any other calendar app I use on my smartphone (LG G2) doesn't show the new events (even after refreshing and resyncing), however when I'm inserting a new event using the web page the event sync perfectly with the apps on my phone.

Thank in advenc


from xda-developers http://ift.tt/2chlGWJ
via IFTTT

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home