Advertisement
Guest User

Apisynctask.java to go with activity.java

a guest
Oct 9th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. public class ApiAsyncTask extends AsyncTask<Void, Void, Void> {
  2.     private BlockOrderGCalDaily mActivity;
  3.  
  4.     /**
  5.      * Constructor.
  6.      * @param activity MainActivity that spawned this task.
  7.      */
  8.     ApiAsyncTask(BlockOrderGCalDaily activity) {
  9.         this.mActivity = activity;
  10.     }
  11.  
  12.     /**
  13.      * Background task to call Google Calendar API.
  14.      * @param params no parameters needed for this task.
  15.      */
  16.     @Override
  17.     protected Void doInBackground(Void... params) {
  18.         try {
  19.             mActivity.clearResultsText();
  20.             mActivity.updateResultsText(getDataFromApi());
  21.  
  22.         } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
  23.             mActivity.showGooglePlayServicesAvailabilityErrorDialog(
  24.                     availabilityException.getConnectionStatusCode());
  25.  
  26.         } catch (UserRecoverableAuthIOException userRecoverableException) {
  27.             mActivity.startActivityForResult(
  28.                     userRecoverableException.getIntent(),
  29.                     BlockOrderGCalDaily.REQUEST_AUTHORIZATION);
  30.  
  31.         } catch (IOException e) {
  32.             mActivity.updateStatus("The following error occurred: " +
  33.                     e.getMessage());
  34.         }
  35.         return null;
  36.     }
  37.  
  38.     /**
  39.      * Fetch a list of the next 10 events from the primary calendar.
  40.      * @return List of Strings describing returned events.
  41.      * @throws IOException
  42.      */
  43.     private List<String> getDataFromApi() throws IOException {
  44.         // List the next 10 events from the primary calendar.
  45.         DateTime now = new DateTime(System.currentTimeMillis());
  46.         List<String> eventStrings = new ArrayList<String>();
  47.         Events events = mActivity.mService.events().list("9sfoekroead5j1sb8aduqgvog4@group.calendar.google.com")
  48.                 .setMaxResults(1)
  49.                 .setTimeMin(now)
  50.                 .setOrderBy("startTime")
  51.                 .setSingleEvents(true)
  52.                 .execute();
  53.         List<Event> items = events.getItems();
  54.  
  55.         for (Event event : items) {
  56.             DateTime start = event.getStart().getDateTime();
  57.             if (start == null) {
  58.                 // All-day events don't have start times, so just use
  59.                 // the start date.
  60.                 start = event.getStart().getDate();
  61.             }
  62.             eventStrings.add(
  63.                     String.format("%s", event.getSummary()));
  64.         }
  65.         return eventStrings;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement