daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.andela.mrm.room_availability.room_availability_utils;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.os.Process;
  6. import android.support.constraint.ConstraintLayout;
  7. import android.view.View;
  8.  
  9. import com.andela.mrm.room_events.CalendarEvent;
  10. import com.andela.mrm.room_setup.country.CountryActivity;
  11. import com.andela.mrm.util.ConvergeUIUtils;
  12. import com.andela.mrm.util.InsertGoogleCalendar;
  13. import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
  14.  
  15. import java.util.Map;
  16.  
  17. import io.reactivex.disposables.CompositeDisposable;
  18.  
  19. import static com.andela.mrm.room_setup.country.CountryActivity.SHARED_CALENDAR_ID;
  20. import static com.andela.mrm.room_setup.country.CountryActivity.SHARED_PREF_FILENAME;
  21.  
  22. /**
  23.  * Add a quick meeting and refresh app in background thread.
  24.  */
  25. public class AddEventInBackground {
  26.     Context mContext;
  27.  
  28.     public AddEventInBackground(Context mContext)
  29.     {
  30.         this.mContext = mContext;
  31.     }
  32.  
  33.     SharedPreferences sharedPreferences = this.mContext.getSharedPreferences(SHARED_PREF_FILENAME,
  34.                                              Context.MODE_PRIVATE);
  35.     CountryActivity countryActivity = new CountryActivity();
  36.  
  37.     // TODO: sharedPreference should be used to retrieve calendarId instead of hard coding it.
  38.     String calendarId = countryActivity.getSharedCalendarId();
  39.  
  40.     View mView;
  41.     Map mSnackbarStringMap;
  42.     CompositeDisposable mDisposables;
  43.  
  44.     /**
  45.      * Getter for the RoomAvailablityActivity context.
  46.      *
  47.      * @param context the room availablity activity context.
  48.      */
  49.     public void getContext(Context context) {
  50.         this.mContext = context;
  51.     }
  52.  
  53.     /**
  54.      * Getter for resources from RoomAvailablityActivity needed to create meeting.
  55.      *
  56.      * @param map         map containing strings flashed to user for better UX.
  57.      * @param view        the main view.
  58.      * @param disposables disposables used for background reloading.
  59.      */
  60.     public void getQuickMeetingResources(Map map, View view, CompositeDisposable disposables) {
  61.         this.mView = view;
  62.         this.mSnackbarStringMap = map;
  63.         this.mDisposables = disposables;
  64.     }
  65.  
  66.     /**
  67.      * Set up quick meeting.
  68.      *  @param selectedDuration duration of quick meeting.
  69.      * @param freeDisplay      button clicked.
  70.      * @param credential       Google account credentials of logged in user.
  71.      */
  72.     public void setQuickMeeting(long selectedDuration, ConstraintLayout freeDisplay,
  73.                                 GoogleAccountCredential credential) {
  74.         //TODO:sharedPreference should be used tO retrieve admin email address
  75.         CalendarEvent calendarEvent = CurrentCalendarEventUtils.getCurrentCalendarEvent();
  76.         long currentTime = System.currentTimeMillis();
  77.         if (calendarEvent != null && "Available".equals(calendarEvent.getSummary())) {
  78.             long availableTime = calendarEvent.getEndTime() - currentTime;
  79.             if (availableTime > selectedDuration) {
  80.                 addEventInBackground(currentTime, selectedDuration, freeDisplay, credential);
  81.             } else {
  82.                 displayEventSnackbar(freeDisplay,
  83.                         mSnackbarStringMap.get("roomUnavailable").toString());
  84.             }
  85.         } else if (calendarEvent == null && CurrentCalendarEventUtils.calendarEvents.isEmpty()) {
  86.             addEventInBackground(currentTime, selectedDuration, freeDisplay, credential);
  87.         } else {
  88.             displayEventSnackbar(freeDisplay,
  89.                     mSnackbarStringMap.get("roomUnavailable").toString());
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Add the event and reload activity in the background.
  95.      *
  96.      * @param currentTime  current device time.
  97.      * @param selectedTime time selected for event creation.
  98.      * @param freeDisplay  button clicked.
  99.      * @param credential   credentials of signed in google account.
  100.      */
  101.     private void addEventInBackground(
  102.             long currentTime,
  103.             long selectedTime,
  104.             ConstraintLayout freeDisplay,
  105.             GoogleAccountCredential credential
  106.     ) {
  107.         new Thread(() -> {
  108.             Process.setThreadPriority(
  109.                     Process.THREAD_PRIORITY_BACKGROUND);
  110.             try {
  111.                 Boolean response = InsertGoogleCalendar.createCalendarEvent(
  112.                         credential, calendarId, currentTime, selectedTime);
  113.                 if (response) {
  114.                     displayEventSnackbar(freeDisplay,
  115.                             mSnackbarStringMap.get("eventSetupSuccess").toString());
  116.                 } else {
  117.                     displayEventSnackbar(freeDisplay,
  118.                             mSnackbarStringMap.get("eventSetupFail").toString());
  119.                 }
  120.             } catch (Exception ex) {
  121.                 // Logs disabled
  122.             }
  123.         }).start();
  124.     }
  125.  
  126.     /**
  127.      * Display snackbar with appropriate message. Change color of clicked button.
  128.      *
  129.      * @param freeDisplay   button that was clicked.
  130.      * @param displayString string to be displayed on snackbar.
  131.      */
  132.     private void displayEventSnackbar(ConstraintLayout freeDisplay, String displayString) {
  133.         ConvergeUIUtils.Companion.setButtonColor(mContext, freeDisplay);
  134.         ConvergeUIUtils.Companion.showSnackBar(mContext,
  135.                 mView,
  136.                 displayString);
  137.     }
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top