Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApiAsyncTask extends AsyncTask<Void, Void, Void> {
- private BlockOrderGCalDaily mActivity;
- /**
- * Constructor.
- * @param activity MainActivity that spawned this task.
- */
- ApiAsyncTask(BlockOrderGCalDaily activity) {
- this.mActivity = activity;
- }
- /**
- * Background task to call Google Calendar API.
- * @param params no parameters needed for this task.
- */
- @Override
- protected Void doInBackground(Void... params) {
- try {
- mActivity.clearResultsText();
- mActivity.updateResultsText(getDataFromApi());
- } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
- mActivity.showGooglePlayServicesAvailabilityErrorDialog(
- availabilityException.getConnectionStatusCode());
- } catch (UserRecoverableAuthIOException userRecoverableException) {
- mActivity.startActivityForResult(
- userRecoverableException.getIntent(),
- BlockOrderGCalDaily.REQUEST_AUTHORIZATION);
- } catch (IOException e) {
- mActivity.updateStatus("The following error occurred: " +
- e.getMessage());
- }
- return null;
- }
- /**
- * Fetch a list of the next 10 events from the primary calendar.
- * @return List of Strings describing returned events.
- * @throws IOException
- */
- private List<String> getDataFromApi() throws IOException {
- // List the next 10 events from the primary calendar.
- DateTime now = new DateTime(System.currentTimeMillis());
- List<String> eventStrings = new ArrayList<String>();
- Events events = mActivity.mService.events().list("9sfoekroead5j1sb8aduqgvog4@group.calendar.google.com")
- .setMaxResults(1)
- .setTimeMin(now)
- .setOrderBy("startTime")
- .setSingleEvents(true)
- .execute();
- List<Event> items = events.getItems();
- for (Event event : items) {
- DateTime start = event.getStart().getDateTime();
- if (start == null) {
- // All-day events don't have start times, so just use
- // the start date.
- start = event.getStart().getDate();
- }
- eventStrings.add(
- String.format("%s", event.getSummary()));
- }
- return eventStrings;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement