Created
December 13, 2018 11:34
-
-
Save huxaiphaer/0f99ece9a61867b85e353158508033b5 to your computer and use it in GitHub Desktop.
Shared preference Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CHECKSTYLE:OFF | |
package com.andela.mrm.room_availability; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.media.AudioManager; | |
import android.media.ToneGenerator; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.annotation.NonNull; | |
import android.support.constraint.ConstraintLayout; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.ProgressBar; | |
import com.andela.mrm.EventCancellationMutation; | |
import com.andela.mrm.Injection; | |
import com.andela.mrm.R; | |
import com.andela.mrm.data.Calendar.ICalendarDataRepository; | |
import com.andela.mrm.find_rooms.FindRoomActivity; | |
import com.andela.mrm.room_availability.room_availability_utils.AddEventInBackground; | |
import com.andela.mrm.room_availability.room_availability_utils.CurrentCalendarEventUtils; | |
import com.andela.mrm.room_availability.room_availability_utils.ExtendMeetingTime; | |
import com.andela.mrm.room_availability.room_availability_utils.HandleCalendarDataFetchError; | |
import com.andela.mrm.room_events.CalendarEvent; | |
import com.andela.mrm.room_events.EventScheduleActivity; | |
import com.andela.mrm.room_information.RoomInformationActivity; | |
import com.andela.mrm.room_setup.country.CountryActivity; | |
import com.andela.mrm.service.ApiService; | |
import com.andela.mrm.settings.SettingsActivity; | |
import com.andela.mrm.util.ConvertToISOFormat; | |
import com.andela.mrm.util.DateTimeUtils; | |
import com.andela.mrm.util.DeviceAwakeService; | |
import com.andela.mrm.widget.ExitModal; | |
import com.andela.mrm.widget.TimeLineScheduleView; | |
import com.apollographql.apollo.ApolloCall; | |
import com.apollographql.apollo.api.Response; | |
import com.apollographql.apollo.exception.ApolloException; | |
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; | |
import com.google.api.client.util.DateTime; | |
import com.google.api.services.calendar.model.Event; | |
import com.google.api.services.calendar.model.FreeBusyRequest; | |
import org.jetbrains.annotations.NotNull; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | |
import io.reactivex.android.schedulers.AndroidSchedulers; | |
import io.reactivex.disposables.CompositeDisposable; | |
import io.reactivex.disposables.Disposable; | |
import io.reactivex.schedulers.Schedulers; | |
import static android.view.View.VISIBLE; | |
import static com.andela.mrm.data.Calendar.CalendarDataUtils.buildRoomFreeBusyRequest; | |
import static com.andela.mrm.room_setup.country.CountryActivity.SHARED_PREF_FILENAME; | |
/** | |
* The type Room availability activity. | |
*/ | |
@SuppressFBWarnings("MS_PKGPROTECT") | |
public class RoomAvailabilityActivity extends AppCompatActivity implements | |
CountDownTimerFragment.IOnTextChangeListener, MeetingRoomDetailFragment.IOnStartCountDown { | |
private static final DateTime DAILY_START_TIME = DateTimeUtils.INSTANCE.getTime(8, 0, 0); | |
private static final DateTime DAILY_END_TIME = DateTimeUtils.INSTANCE.getTime(23, 0, 0); | |
public static ICalendarDataRepository mCalendarDataRepo; | |
/** | |
* EventCancellationMutation Callback. | |
*/ | |
private final ApolloCall.Callback<EventCancellationMutation.Data> eventCancellationCallback = | |
new ApolloCall.Callback<EventCancellationMutation.Data>() { | |
@Override | |
public void onResponse(@NotNull Response<EventCancellationMutation.Data> response) { | |
//Response handled here | |
} | |
@Override | |
public void onFailure(@NotNull ApolloException e) { | |
//failure handled here | |
} | |
}; | |
/** | |
* The Items. | |
*/ | |
public List<Event> items; | |
HandleCalendarDataFetchError handleCalendarDataFetchError; | |
ExtendMeetingTime extendMeetingTime; | |
AddEventInBackground addEventInBackground; | |
View snackBarView; | |
Timer roomAvailabilityTimeTextTimer; | |
MeetingRoomDetailFragment meetingRoomDetailFragment; | |
/** | |
* The M room information btn. | |
*/ | |
/** | |
* Shared Prefs. | |
*/ | |
SharedPreferences sharedPreferences; | |
/** | |
* The M find room btn. | |
*/ | |
@BindView(R.id.layout_find_room) | |
LinearLayout mFindRoomBtn; | |
/** | |
* The M room information btn. | |
*/ | |
/** | |
* The M room schedule btn. | |
*/ | |
@BindView(R.id.layout_schedule) | |
LinearLayout mRoomScheduleBtn; | |
/** | |
* The Room background Image. | |
*/ | |
@BindView(R.id.img_room_availability_background_image) | |
ImageView roomBackgoundImage; | |
@BindView(R.id.layout_room_info) | |
LinearLayout mRoomInformationBtn; | |
/** | |
* The M time line schedule view. | |
*/ | |
@BindView(R.id.view_time_line_strip) | |
TimeLineScheduleView mTimeLineScheduleView; | |
/** | |
* The M room availability root view. | |
*/ | |
@BindView(R.id.layout_room_availability_parent) | |
ConstraintLayout mRoomAvailabilityRootView; | |
/** | |
* ProgressBar view. | |
*/ | |
@BindView(R.id.progressBar) | |
ProgressBar progressBar; | |
private Integer status = 0; | |
private FragmentManager mFragmentManager; | |
private CompositeDisposable mDisposables; | |
private List<FreeBusy> currentfreeBusyList; | |
/** | |
* On click find room btn. | |
*/ | |
@OnClick(R.id.layout_find_room) | |
void onClickFindRoomBtn() { | |
startActivity(FindRoomActivity.newIntent(this)); | |
} | |
/** | |
* On click schedule btn. | |
*/ | |
@OnClick({R.id.layout_schedule, R.id.view_time_line_strip}) | |
void onClickScheduleBtn() { | |
startActivity(EventScheduleActivity.newIntent(this)); | |
} | |
/** | |
* On click room room info btn. | |
*/ | |
@OnClick(R.id.layout_room_info) | |
void onClickRoomRoomInfoBtn() { | |
Integer roomID = Integer.valueOf( | |
sharedPreferences.getString("room_id", "11")); | |
startActivity(RoomInformationActivity.newIntent(this, roomID)); | |
} | |
/** | |
* On click device logout btn. | |
*/ | |
@OnClick(R.id.layout_settings) | |
void onClickDeviceLogoutBtn() { | |
startActivity(SettingsActivity.newIntent(this)); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_room_availability); | |
ButterKnife.bind(this); | |
roomAvailabilityTimeTextTimer = new Timer(); | |
sharedPreferences = this.getSharedPreferences( | |
SHARED_PREF_FILENAME, Context.MODE_PRIVATE); | |
String roomBackgroundImageURL = sharedPreferences.getString( | |
"room_image_url", ""); | |
String defaultBackgroundImageUrl = "https://firebasestorage.googleapis.com" | |
+ "/v0/b/andelamrm.appspot.com/o/rooms%2F9th_January_2017-45.jpg?alt=media&token" | |
+ "=ceaa6a99-174a-4edc-a884-163612f3fd11"; | |
if (roomBackgroundImageURL == null) { | |
roomBackgroundImageURL = defaultBackgroundImageUrl; | |
} | |
GlideApp.with(this).load(roomBackgroundImageURL).placeholder(R.drawable.room) | |
.into(roomBackgoundImage); | |
mCalendarDataRepo = Injection.provideCalendarDataRepository(this); | |
handleCalendarDataFetchError = new HandleCalendarDataFetchError(); | |
handleCalendarDataFetchError.getErrorResources(getCalendarFreeBusySchedule(), | |
mRoomAvailabilityRootView, | |
this); | |
addEventInBackground = new AddEventInBackground(this); | |
extendMeetingTime = new ExtendMeetingTime(); | |
snackBarView = findViewById(R.id.frame_room_availability_details); | |
currentfreeBusyList = null; | |
setUpFragments(); | |
mDisposables = new CompositeDisposable(); | |
mDisposables.addAll(getCalendarFreeBusySchedule(), getRoomCalendarEvents()); | |
DeviceAwakeService.keepAwake(this); | |
} | |
/** | |
* @return room events. | |
*/ | |
public Disposable getRoomCalendarEvents() { | |
return mCalendarDataRepo.getRoomCalendarEvents( | |
sharedPreferences.getString(CountryActivity.SHARED_CALENDAR_ID, ""), | |
new DateTime(System.currentTimeMillis()), | |
DateTimeUtils.INSTANCE.getTime(23, | |
0, 0)) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe((calendarEvents) -> { | |
updateCheckedInCalendarEvent(calendarEvents); | |
for (CalendarEvent calendarEvent : calendarEvents) { | |
if (calendarEvent.getId() != null) { | |
createEventCancellationTimer(calendarEvent); | |
} | |
} | |
}, | |
handleCalendarDataFetchError::handleCalendarDataFetchError, | |
this::setFreeRoomTag); | |
} | |
/** | |
* update the state of the calendar event that was checked in. | |
* | |
* @param calendarEvents calendar events to update. | |
*/ | |
private void updateCheckedInCalendarEvent(List<CalendarEvent> calendarEvents) { | |
List<CalendarEvent> updateCalendarEvents = new ArrayList<>(); | |
for (CalendarEvent calendarEvent : calendarEvents) { | |
if (calendarEvent.getId() != null) { | |
if (calendarEvent.getId().equalsIgnoreCase(sharedPreferences.getString(CountryActivity.SHARED_ROOM_CHECKED_IN, null))) { | |
calendarEvent.setCheckedIn(true); | |
storeCheckedInStatusInSharedPreferences(calendarEvent); | |
} | |
updateCalendarEvents.add(calendarEvent); | |
} | |
} | |
CurrentCalendarEventUtils.calendarEvents = updateCalendarEvents; | |
} | |
/** | |
* cancel events when not checked in. | |
* | |
* @param calendarEvent get calendar events. | |
*/ | |
private void createEventCancellationTimer(CalendarEvent calendarEvent) { | |
// TODO: write test for this functionality to auto cancel event(s). | |
Long startTime = calendarEvent.getStartTime(); | |
final int waitingPeriod = 600000; | |
final int waitingPeriodTimer = 480000; | |
final long duration = startTime + waitingPeriod - System.currentTimeMillis(); | |
final long bookingTimer = startTime + waitingPeriodTimer - System.currentTimeMillis(); | |
if (duration < 0) { | |
return; | |
} | |
final Handler handler = new Handler(); | |
final Handler handler2 = new Handler(); | |
handler2.postDelayed(() -> { | |
if (!calendarEvent.isCheckedIn() | |
&& meetingRoomDetailFragment.isInWaiting()) { | |
meetingRoomDetailFragment.addUnBookingText(); | |
} | |
}, bookingTimer); | |
handler.postDelayed(() -> { | |
if (!calendarEvent.isCheckedIn()) { | |
cancelCalendarEvent(); | |
meetingRoomDetailFragment.endMeeting(); | |
} | |
}, duration); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (mDisposables != null) { | |
mDisposables.clear(); | |
} | |
} | |
/** | |
* Sets up inflatable fragments(countdown timer and details fragments). | |
*/ | |
private void setUpFragments() { | |
meetingRoomDetailFragment = new MeetingRoomDetailFragment(); | |
mFragmentManager = getSupportFragmentManager(); | |
mFragmentManager.beginTransaction() | |
.add(R.id.frame_room_availability_details, meetingRoomDetailFragment) | |
.add(R.id.frame_room_availability_countdown_timer, new CountDownTimerFragment()) | |
.commit(); | |
} | |
/** | |
* Retrieves the FreeBusy Schedule for the current room. | |
* | |
* @return Disposablex | |
*/ | |
@NonNull | |
private Disposable getCalendarFreeBusySchedule() { | |
// TODO: this should be passed in dynamically or retrieved from SharedPreferences | |
FreeBusyRequest request = buildRoomFreeBusyRequest( | |
sharedPreferences.getString(CountryActivity.SHARED_CALENDAR_ID, ""), | |
DAILY_START_TIME, DAILY_END_TIME); | |
return mCalendarDataRepo | |
.getRoomFreeBusySchedule(request) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(this::displayTimeLineData, | |
handleCalendarDataFetchError::handleCalendarDataFetchError, | |
this::activateCheckInButton); | |
} | |
/** | |
* Response handler for getCalendarFreeBusySchedule. | |
* | |
* @param freeBusyList List of TimePeriods. | |
*/ | |
public void displayTimeLineData(List<FreeBusy> freeBusyList) { | |
currentfreeBusyList = freeBusyList; | |
isEndButtonVisible(currentfreeBusyList); | |
updateTimelineData(freeBusyList); | |
} | |
/** | |
* Change Timeline Data. | |
*/ | |
public void changeTimeLineData() { | |
isEndButtonVisible(currentfreeBusyList); | |
updateTimelineData(currentfreeBusyList); | |
} | |
/** | |
* Update the timeline data. | |
* @param freeBusyList current fee busy list in the timeline | |
*/ | |
public void updateTimelineData(List<FreeBusy> freeBusyList) { | |
mTimeLineScheduleView.setTimeLineData(freeBusyList, | |
DateTimeUtils.INSTANCE.getHour(DAILY_START_TIME)); | |
} | |
/** | |
* Is Button visible to change event status | |
* | |
* @param freeBusyList List of TimePeriods. | |
*/ | |
public void isEndButtonVisible(List<FreeBusy> freeBusyList) { | |
if (meetingRoomDetailFragment.endMeetingButtonVisibility() == VISIBLE) { | |
for (FreeBusy busy : freeBusyList) { | |
if (busy.getStatus() == FreeBusy.Status.BUSY_CURRENT) { | |
busy.setStatus(FreeBusy.Status.BUSY_CURRENT_CHECKED_IN); | |
} | |
} | |
} | |
} | |
@Override | |
public void onTimeChange(int minutes, int second) { | |
MeetingRoomDetailFragment meetingRoomDetailFragment = (MeetingRoomDetailFragment) | |
getSupportFragmentManager().findFragmentById(R.id.frame_room_availability_details); | |
if (meetingRoomDetailFragment != null && status == 1) { | |
meetingRoomDetailFragment.updateMinute(minutes); | |
} | |
Boolean status = sharedPreferences.getBoolean(CountryActivity.SHARED_ROOM_BEEP_PREFERENCE, false); | |
Boolean timerStatusToZero = minutes == 0 && second < 6; | |
Boolean timerStatusToTen = minutes == 10 && second < 6; | |
if (status && meetingRoomDetailFragment != null && | |
meetingRoomDetailFragment.endMeetingButtonVisibility() == VISIBLE && | |
(timerStatusToZero || timerStatusToTen)) { | |
produceEffect(minutes, second); | |
} | |
} | |
private void produceEffect(int minutes, int seconds) { | |
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT || | |
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) == 0) { | |
Animation myFadeInAnimation = AnimationUtils.loadAnimation( | |
this, R.anim.tween); | |
roomBackgoundImage.startAnimation(myFadeInAnimation); | |
if (minutes == 0 && seconds == 0 || minutes == 10 && seconds == 0) { | |
roomBackgoundImage.clearAnimation(); | |
} | |
} else { | |
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100); | |
toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP2, 10000); | |
toneGen1.release(); | |
} | |
} | |
@Override | |
public void onCountDownComplete() { | |
Boolean status = sharedPreferences.getBoolean(CountryActivity.SHARED_ROOM_BEEP_PREFERENCE, false); | |
if (status && meetingRoomDetailFragment.endMeetingButtonVisibility() == VISIBLE) { | |
produceEffect(0, 0); | |
} | |
if (meetingRoomDetailFragment.endMeetingButtonVisibility() == VISIBLE) { | |
removeCheckedInStatusFromPrefs(); | |
} | |
endCurrentMeeting(); | |
} | |
/** | |
* Fetch the updated room events. | |
* | |
* @return updated room events. | |
*/ | |
public Disposable getUpdatedRoomCalendarEvents() { | |
return mCalendarDataRepo.getRoomCalendarEvents( | |
sharedPreferences.getString(CountryActivity.SHARED_CALENDAR_ID, ""), | |
new DateTime(System.currentTimeMillis()), | |
DateTimeUtils.INSTANCE.getTime(23, | |
0, 0)) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe((calendarEvents) -> { | |
CurrentCalendarEventUtils.calendarEvents = calendarEvents; | |
for (CalendarEvent calendarEvent : calendarEvents) { | |
if (calendarEvent.getId() != null) { | |
createEventCancellationTimer(calendarEvent); | |
} | |
} | |
}, | |
handleCalendarDataFetchError::handleCalendarDataFetchError); | |
} | |
@Override | |
public void extendStartTimeCheckInEarly( | |
long startTimeInMilliSeconds, GoogleAccountCredential credential) { | |
Map<String, String> map = new HashMap<String, String>(); | |
CountDownTimerFragment countDownTimerFragment = (CountDownTimerFragment) | |
getSupportFragmentManager() | |
.findFragmentById(R.id.frame_room_availability_countdown_timer); | |
extendMeetingTime.getContextAndView(this, mRoomAvailabilityRootView, credential); | |
extendMeetingTime.getTimeExtendResources(countDownTimerFragment, | |
sharedPreferences.getString(CountryActivity.SHARED_CALENDAR_ID, ""), map); | |
extendMeetingTime.checkInEarlyStartTime(startTimeInMilliSeconds); | |
updatedRoomCalendarEvents(); | |
runOnUiThread(() -> { | |
new Handler().postDelayed(() -> { | |
startMeetingTimer(CurrentCalendarEventUtils.calendarEvents.get(0)); | |
}, 1000); | |
}); | |
meetingRoomDetailFragment | |
.updateStartTimeAfterCheckInEarly(System.currentTimeMillis()); | |
} | |
/** | |
* Start the timer for a meeting. | |
* | |
* @param currentEvent The current active event. | |
*/ | |
public void startMeetingTimer(CalendarEvent currentEvent) { | |
CountDownTimerFragment countDownTimerFragment = (CountDownTimerFragment) | |
mFragmentManager | |
.findFragmentById(R.id.frame_room_availability_countdown_timer); | |
if (!currentEvent.isCheckedIn()) { | |
currentEvent.setCheckedIn(true); | |
} | |
storeCheckedInStatusInSharedPreferences(currentEvent); | |
meetingRoomDetailFragment.displayMeetingDuration(currentEvent); | |
if (countDownTimerFragment != null) { | |
countDownTimerFragment.stopTimer(); | |
long duration = currentEvent.getEndTime() - System.currentTimeMillis(); | |
countDownTimerFragment.startTimer(duration); | |
countDownTimerFragment.setTimeRemainingText("Time Remaining"); | |
} | |
} | |
/** | |
* store checked in status in the shared preference. | |
* | |
* @param calendarEvent checked in event | |
*/ | |
public void storeCheckedInStatusInSharedPreferences(CalendarEvent calendarEvent) { | |
SharedPreferences.Editor sharedPreferencesEditor = getSharedPreferences( | |
SHARED_PREF_FILENAME, Context.MODE_PRIVATE).edit(); | |
sharedPreferencesEditor.putString(CountryActivity.SHARED_ROOM_CHECKED_IN, calendarEvent.getId()); | |
sharedPreferencesEditor.apply(); | |
} | |
/** | |
* Update room events. | |
*/ | |
public void updateRoomCalendarEvents() { | |
runOnUiThread(() -> { | |
new Handler().postDelayed(() -> { | |
mDisposables.addAll(getCalendarFreeBusySchedule(), getRoomCalendarEvents()); | |
}, 2000); | |
}); | |
} | |
/** | |
* Update room events. | |
*/ | |
public void updatedRoomCalendarEvents() { | |
runOnUiThread(() -> { | |
new Handler().postDelayed(() -> { | |
mDisposables.addAll(getCalendarFreeBusySchedule(), getUpdatedRoomCalendarEvents()); | |
}, 1500); | |
}); | |
} | |
/** | |
* On meeting ended. | |
*/ | |
@Override | |
public void onMeetingEnded() { | |
removeCheckedInStatusFromPrefs(); | |
cancelCalendarEvent(); | |
endCurrentMeeting(); | |
} | |
/** | |
* Remove the Checked In Status | |
*/ | |
public void removeCheckedInStatusFromPrefs() { | |
SharedPreferences.Editor sharedPreferencesEditor = getSharedPreferences( | |
SHARED_PREF_FILENAME, Context.MODE_PRIVATE).edit(); | |
sharedPreferencesEditor.putString(CountryActivity.SHARED_ROOM_CHECKED_IN, null); | |
sharedPreferencesEditor.apply(); | |
} | |
/** | |
* End current meeting and how progress dialog. | |
*/ | |
private void endCurrentMeeting() { | |
progressBar.setVisibility(VISIBLE); | |
updateRoomCalendarEvents(); | |
runOnUiThread(() -> { | |
new Handler().postDelayed(() -> { | |
MeetingRoomDetailFragment meetingRoomDetailFragment = (MeetingRoomDetailFragment) | |
getSupportFragmentManager() | |
.findFragmentById(R.id.frame_room_availability_details); | |
if (meetingRoomDetailFragment != null && sharedPreferences.getString(CountryActivity.SHARED_ROOM_CHECKED_IN, null) == null) { | |
meetingRoomDetailFragment.displayCheckInScreen(); | |
} | |
progressBar.setVisibility(View.GONE); | |
}, 3000); | |
}); | |
} | |
/** | |
* cancel calendar event. | |
*/ | |
private void cancelCalendarEvent() { | |
CalendarEvent calendarEvent = CurrentCalendarEventUtils.getCurrentCalendarEvent(); | |
if (calendarEvent != null) { | |
ApiService.getApolloClient(getApplicationContext()).mutate(EventCancellationMutation | |
.builder().eventId(calendarEvent.getId()) | |
.eventTitle(calendarEvent.getSummary()) | |
.startTime(ConvertToISOFormat.convertLongToISO(calendarEvent.getStartTime())) | |
.endTime(ConvertToISOFormat.convertLongToISO(calendarEvent.getEndTime())) | |
.calendarId(sharedPreferences.getString("calendar_id", "")).build()) | |
.enqueue(eventCancellationCallback); | |
mDisposables.add(mCalendarDataRepo | |
.cancelEvent( | |
sharedPreferences.getString( | |
CountryActivity.SHARED_CALENDAR_ID, ""), | |
calendarEvent.getId()) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(() -> { | |
}, handleCalendarDataFetchError::handleCalendarDataFetchError)); | |
meetingRoomDetailFragment.removeUnBookingText(); | |
} | |
} | |
@Override | |
public void setQuickMeeting(long selectedDuration, ConstraintLayout freeDisplay, | |
GoogleAccountCredential credential) { | |
Map<String, String> map = new HashMap<>(); | |
map.put("eventSetupSuccess", getString(R.string.quick_meeting_setup_success)); | |
map.put("eventSetupFail", getString(R.string.quick_meeting_setup_fail)); | |
map.put("roomUnavailable", getString(R.string.quick_meeting_time_not_available)); | |
addEventInBackground.getContext(this); | |
addEventInBackground.getQuickMeetingResources(map, snackBarView, mDisposables); | |
addEventInBackground.setQuickMeeting(selectedDuration, freeDisplay, credential); | |
updateRoomCalendarEvents(); | |
} | |
/** | |
* Activate check in button. | |
*/ | |
public void activateCheckInButton() { | |
if (meetingRoomDetailFragment != null) { | |
meetingRoomDetailFragment.activateCheckInButton(); | |
} | |
} | |
@Override | |
public void onBackPressed() { | |
ExitModal.show(this); | |
} | |
@Override | |
public void onPointerCaptureChanged(boolean hasCapture) { | |
// | |
} | |
/** | |
* Set free meeting room tag. | |
*/ | |
private void setFreeRoomTag() { | |
CalendarEvent currentEvent = CurrentCalendarEventUtils.getCurrentCalendarEvent(); | |
CountDownTimerFragment countDownTimerFragment = (CountDownTimerFragment) | |
mFragmentManager.findFragmentById(R.id.frame_room_availability_countdown_timer); | |
if (currentEvent != null && countDownTimerFragment != null) { | |
stopTheAvailabilityTimeTextTimer(roomAvailabilityTimeTextTimer); | |
updateRoomTag(currentEvent, countDownTimerFragment); | |
} else { | |
if (CurrentCalendarEventUtils.calendarEvents.isEmpty()) { | |
stopTheAvailabilityTimeTextTimer(roomAvailabilityTimeTextTimer); | |
meetingRoomDetailFragment.displayQuickMeetingButton(); | |
meetingRoomDetailFragment.updateText("Free Till End of Day", null, null); | |
countDownTimerFragment.setTimeRemainingText("Free Till End of Day"); | |
countDownTimerFragment.stopTimer(); | |
countDownTimerFragment.setCountDownTime(0, 0, 0); | |
meetingRoomDetailFragment.onTimeUp(); | |
} else { | |
// Update the availability time text | |
long timeTillNextMeeting = CurrentCalendarEventUtils | |
.calendarEvents.get(0).getStartTime() - System.currentTimeMillis(); | |
long secs = timeTillNextMeeting / 1000; | |
if (secs >= 600) { | |
roomAvailabilityTimeTextTimer = new Timer(); | |
updateRoomAvailabilityTimeTextAndCountdownTimer(countDownTimerFragment, timeTillNextMeeting, roomAvailabilityTimeTextTimer); | |
} | |
//Determine The Check In Early Time | |
determineCheckInTime(countDownTimerFragment); | |
} | |
} | |
} | |
/** | |
* Update Room Availability Time Text And Countdown Timer. | |
* | |
* @param countDownTimerFragment The timer. | |
* @param timeTillNextMeeting The milliseconds until next meeting. | |
* @param timer The per minute timer to update the availability time text. | |
*/ | |
private void updateRoomAvailabilityTimeTextAndCountdownTimer(CountDownTimerFragment countDownTimerFragment, long timeTillNextMeeting, Timer timer) { | |
meetingRoomDetailFragment.updateAvailabilityTimeText(timeTillNextMeeting); | |
if (countDownTimerFragment != null) { | |
countDownTimerFragment.stopTimer(); | |
countDownTimerFragment.startTimer(timeTillNextMeeting); | |
updateAvailabilityTimeText(timer); | |
countDownTimerFragment.setTimeRemainingText( | |
getApplicationContext().getResources().getString(R.string.time_till_next_meeting)); | |
} | |
meetingRoomDetailFragment.displayQuickMeetingButton(); | |
} | |
/** | |
* Start Timer To Update the Room availability time text. | |
*/ | |
public void updateAvailabilityTimeText(Timer timer) { | |
timer.schedule(new TimerTask() { | |
public void run() { | |
if (meetingRoomDetailFragment.meetingRoomTextTitle.getVisibility() != VISIBLE) { | |
runOnUiThread(() -> meetingRoomDetailFragment.updateAvailabilityTimeText( | |
CurrentCalendarEventUtils.calendarEvents.get(0).getStartTime() - System.currentTimeMillis() | |
)); | |
} | |
} | |
}, 0, 60 * 1000); | |
} | |
/** | |
* Stop the Timer from running. | |
* | |
* @param timer The timer. | |
*/ | |
public void stopTheAvailabilityTimeTextTimer(Timer timer) { | |
timer.cancel(); | |
} | |
/** | |
* Determine Check In early situation. | |
* | |
* @param countDownTimerFragment The timer. | |
*/ | |
private void determineCheckInTime(CountDownTimerFragment countDownTimerFragment) { | |
long nextMeetingStartTime = CurrentCalendarEventUtils | |
.calendarEvents.get(0).getStartTime(); | |
long nextMeetingEndTime = CurrentCalendarEventUtils | |
.calendarEvents.get(0).getEndTime(); | |
String nextMeetingSummary = CurrentCalendarEventUtils | |
.calendarEvents.get(0).getSummary(); | |
long tenMinutesToMeetingStart = nextMeetingStartTime - (10 * 60 * 1000); | |
long timeToWaitingUntilCheckInEarly = tenMinutesToMeetingStart | |
- System.currentTimeMillis(); | |
runOnUiThread(() -> { | |
new Handler().postDelayed(() -> { | |
stopTheAvailabilityTimeTextTimer(roomAvailabilityTimeTextTimer); | |
long duration = nextMeetingStartTime - System.currentTimeMillis(); | |
meetingRoomDetailFragment.updateText( | |
"Booked", nextMeetingStartTime, nextMeetingEndTime); | |
meetingRoomDetailFragment.updateMeetingTitle(nextMeetingSummary); | |
meetingRoomDetailFragment.removeQuickMeetingButton(); | |
if (countDownTimerFragment != null) { | |
countDownTimerFragment.stopTimer(); | |
countDownTimerFragment.startTimer(duration); | |
countDownTimerFragment.setTimeRemainingText("Time Till Next Meeting"); | |
} | |
}, timeToWaitingUntilCheckInEarly); | |
}); | |
} | |
/** | |
* Update free meeting tag. | |
* | |
* @param currentEvent The current active event. | |
* @param countDownTimerFragment The timer. | |
*/ | |
private void updateRoomTag(CalendarEvent currentEvent, | |
CountDownTimerFragment countDownTimerFragment) { | |
if ("Available".equals(currentEvent.getSummary())) { | |
status = 1; | |
countDownTimerFragment.stopTimer(); | |
long duration = currentEvent.getEndTime() - System.currentTimeMillis(); | |
countDownTimerFragment.startTimer(duration); | |
countDownTimerFragment.setTimeRemainingText("Time Till Next Meeting"); | |
meetingRoomDetailFragment.updateMinute((int) duration); | |
meetingRoomDetailFragment.displayQuickMeetingButton(); | |
} else { | |
if (currentEvent.isCheckedIn()) { | |
meetingRoomDetailFragment.restoreCheckedMeeting(currentEvent); | |
countDownTimerFragment.stopTimer(); | |
long duration = currentEvent.getEndTime() - System.currentTimeMillis(); | |
countDownTimerFragment.startTimer(duration); | |
countDownTimerFragment.setTimeRemainingText("Time Remaining"); | |
} else if (System.currentTimeMillis() >= currentEvent.getStartTime()) { | |
long autoCancelTime = currentEvent.getStartTime() + 600000; | |
long duration = autoCancelTime - System.currentTimeMillis(); | |
meetingRoomDetailFragment.updateText( | |
"Started", currentEvent.getStartTime(), currentEvent.getEndTime()); | |
if (duration > 0 && meetingRoomDetailFragment.endMeetingButtonVisibility() == View.GONE) { | |
countDownTimerFragment.stopTimer(); | |
countDownTimerFragment.startTimer(duration); | |
countDownTimerFragment.setTimeRemainingText("Time Till Meeting is Unbooked"); | |
} | |
meetingRoomDetailFragment.updateMeetingTitle(currentEvent.getSummary()); | |
meetingRoomDetailFragment.removeQuickMeetingButton(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment