Commit da5258bb authored by Lucas Braz Cunha's avatar Lucas Braz Cunha

AGILE#316: Notification is now created at the same time that schedule is confirmed

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent 65b47b50
......@@ -58,15 +58,6 @@
</intent-filter>
</service>
<service
android:name=".services.notification.NotificationCreateService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
<service
android:name=".services.notification.NotificationsListService"
android:exported="false"
......
......@@ -217,7 +217,33 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
}
if(schedule){
scheduleConfirmationPresenter.onScheduleClicked(scheduleConfirmation, citizen, et_scheduleNote.getText().toString());
SimpleDateFormat sdfServer = new SimpleDateFormat(Utils.SERVER_FORMAT, Locale.getDefault());
SimpleDateFormat sdfDate = new SimpleDateFormat(Utils.DATE_FORMAT, Locale.getDefault());
SimpleDateFormat sdfHour = new SimpleDateFormat(Utils.HOUR_FORMAT, Locale.getDefault());
/*
* Position zero is "don't notify"
*/
Notification notification = null;
if(sp_notification.getSelectedItemPosition() != reminderTimeArray.length - 1) {
notification = new Notification();
notification.scheduleId = scheduleConfirmation.getId();
notification.emailSent = false;
notification.description = getResources().getString(R.string.notification_content, sdfDate.format(scheduleConfirmation.getStartTime()), sdfHour.format(scheduleConfirmation.getStartTime()), scheduleConfirmation.getServiceTypeName(),
scheduleConfirmation.getLocationName(), scheduleConfirmation.getAddressStreet(), scheduleConfirmation.getAddressNumber());
notification.reminderTime = sdfServer.format(scheduleConfirmation.getStartTime().getTime() - (reminderTimeArray[sp_notification.getSelectedItemPosition()]));
if (rb_yes.isChecked()) {
notification.emailAddress = et_email.getText().toString();
}
}
scheduleConfirmationPresenter.onScheduleClicked(scheduleConfirmation, citizen, et_scheduleNote.getText().toString(), notification);
bt_schedule.setEnabled(false);
bt_back.setEnabled(false);
}
......@@ -366,40 +392,8 @@ public class ScheduleConfirmationActivity extends AppCompatActivity implements S
}
}
@Override
public void onSuccess(ScheduleConfirmation schedule){
SimpleDateFormat sdfServer = new SimpleDateFormat(Utils.SERVER_FORMAT, Locale.getDefault());
SimpleDateFormat sdfDate = new SimpleDateFormat(Utils.DATE_FORMAT, Locale.getDefault());
SimpleDateFormat sdfHour = new SimpleDateFormat(Utils.HOUR_FORMAT, Locale.getDefault());
/*
* Position zero is "don't notify"
*/
if(sp_notification.getSelectedItemPosition() != reminderTimeArray.length - 1){
Notification notification = new Notification();
notification.scheduleId = schedule.getId();
notification.emailSent = false;
notification.description = getResources().getString(R.string.notification_content, sdfDate.format(schedule.getStartTime()), sdfHour.format(schedule.getStartTime()), schedule.getServiceTypeName(),
schedule.getLocationName(), schedule.getAddressStreet(), schedule.getAddressNumber());
// TODO: 15/01/18 Apagar aqui o tempo a mais que está sendo usado no calculo da notificaçõa, está em dobro.
notification.reminderTime = sdfServer.format(schedule.getStartTime().getTime() - (reminderTimeArray[sp_notification.getSelectedItemPosition()]) - (reminderTimeArray[sp_notification.getSelectedItemPosition()]));
if(rb_yes.isChecked()){
notification.emailAddress = et_email.getText().toString();
}
scheduleConfirmationPresenter.createNotification(notification);
}else{
returnHome();
}
}
private void radioButtonNotificationSelected(int id, boolean toggle){
switch (id){
......
......@@ -3,13 +3,15 @@ package br.ufpr.c3sl.agendador.agendador.models;
import com.google.gson.annotations.SerializedName;
/**
* Created by lbc16 on 19/09/17.
* Created by Lucas Braz Cunha on 19/09/17.
*/
public class ScheduleNote {
public Schedule schedule;
public Notification notification;
public ScheduleNote(String note) {
this.schedule = new Schedule(note);
}
......
......@@ -89,9 +89,12 @@ public interface ApiEndpoints {
@PUT("citizens/{id_user}/dependants/{id_dependent}?permission=citizen")
Call<FullDependent> updateDependent(@Path("id_user") long citizenId, @Path("id_dependent") long dependentId, @Body DependentCreation dependent);
@GET("schedules?permission=citizen")
@GET("schedules?permission=citizen&history=true")
Call<CheckSchedules> requestSchedulesHistory(@QueryMap Map<String, String> options);
@GET("schedules?permission=citizen&future=true")
Call<CheckSchedules> requestFutureSchedules(@QueryMap Map<String, String> options);
@GET("forms/schedule_history?permission=citizen")
Call<FormFilters> requestCitizenSectors();
......
package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.RetryStrategy;
import com.firebase.jobdispatcher.Trigger;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
......@@ -21,13 +14,10 @@ import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.models.CitizenCompact;
import br.ufpr.c3sl.agendador.agendador.models.Notification;
import br.ufpr.c3sl.agendador.agendador.models.NotificationReturn;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleConfirmation;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleNote;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
import br.ufpr.c3sl.agendador.agendador.services.notification.LocalNotificationManager;
import br.ufpr.c3sl.agendador.agendador.services.notification.NotificationCreateService;
import okhttp3.Headers;
import retrofit2.Call;
import retrofit2.Callback;
......@@ -54,7 +44,7 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
}
public void onScheduleClicked(final ScheduleConfirmation scheduleConfirmation, CitizenCompact citizen, String note){
public void onScheduleClicked(final ScheduleConfirmation scheduleConfirmation, CitizenCompact citizen, String note, Notification notification){
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
header.put(Utils.ACCESS_TOKEN, osb.getString(Utils.ACCESS_TOKEN, null));
......@@ -65,6 +55,8 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
ScheduleNote scheduleNote = new ScheduleNote(note);
scheduleNote.notification = notification;
Call<ScheduleConfirmation> listCall = service.requestPerformScheduling(scheduleConfirmation.getId(), citizen.getId(), scheduleNote);
scheduleConfirmationPresenter.view().setProgressBar(true);
......@@ -80,7 +72,7 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
case 200:
Log.d("Server response", this.getClass().getName() + ": 200 - Sucesso!");
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().onSuccess(scheduleConfirmation);
scheduleConfirmationPresenter.view().returnHome();
break;
default:
......@@ -102,79 +94,6 @@ public class ScheduleConfirmationPresenter extends BasePresenter<ScheduleConfirm
}
public void createNotification(final Notification notification){
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
header.put(Utils.ACCESS_TOKEN, osb.getString(Utils.ACCESS_TOKEN, null));
header.put(Utils.CLIENT, osb.getString(Utils.CLIENT, null));
header.put(Utils.UID, osb.getString(Utils.UID, null));
final ApiEndpoints service = ApiUtils.request(header);
final Call<NotificationReturn> listCall = service.requestCreateNotification(notification);
scheduleConfirmationPresenter.view().setProgressBar(true);
listCall.enqueue(new Callback<NotificationReturn>() {
@Override
public void onResponse(Call<NotificationReturn> call, Response<NotificationReturn> response) {
Headers headers = response.headers();
int status = response.code();
updateHeaders(headers);
switch (status) {
case 201:
Log.d("Server response", this.getClass().getName() + ": 200 - Sucess!");
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().returnHome();
LocalNotificationManager.createLocalNotification(context, response.body(), osb, new Gson());
break;
default:
Log.e("Server response", this.getClass().getName() + ": ERRO:" + status + " na criação de uma notificação.");
scheduleNotificationJob(notification);
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().returnHome();
break;
}
}
@Override
public void onFailure(Call<NotificationReturn> call, Throwable t) {
Log.e("Server response", this.getClass().getName() + ": Request Failed, Scheduling job.");
t.printStackTrace();
scheduleNotificationJob(notification);
scheduleConfirmationPresenter.view().setProgressBar(false);
scheduleConfirmationPresenter.view().returnHome();
}
});
}
private void scheduleNotificationJob(Notification notification){
Bundle extras = new Bundle();
Gson g = new Gson();
String json = g.toJson(notification);
extras.putString(Utils.NOTIFICATION, json);
Job job = dispatcher.newJobBuilder()
.setService(NotificationCreateService.class)
.setTag(Utils.NOTIFICATION_SERVICE + notification.scheduleId)
.setTrigger(Trigger.NOW)
//will be retried if it fails.
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setConstraints(
// only run on network
Constraint.ON_ANY_NETWORK
)
.setExtras(extras)
//if device is rebooted the job will be restarted
.setLifetime(Lifetime.FOREVER)
.build();
dispatcher.mustSchedule(job);
}
@Override
protected void updateView() {}
}
......@@ -51,7 +51,7 @@ public class SchedulesPresenter extends BasePresenter<SchedulesActivity> {
// TODO: 06/10/17 Change to request "active" schedules1
Call<CheckSchedules> listCall = service.requestSchedulesHistory(new HashMap<String, String>());
Call<CheckSchedules> listCall = service.requestFutureSchedules(new HashMap<String, String>());
listCall.enqueue(new Callback<CheckSchedules>() {
......
package br.ufpr.c3sl.agendador.agendador.services.notification;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.models.Notification;
import br.ufpr.c3sl.agendador.agendador.models.NotificationReturn;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
import okhttp3.Headers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by Lucas Braz Cunha on 24/11/17.
*/
public class NotificationCreateService extends JobService {
@Override
public boolean onStartJob(final JobParameters job) {
Log.v("Notificacao", "Rodando " + NotificationCreateService.class.getName());
new Thread(new Runnable() {
public void run() {
final Bundle extras = job.getExtras();
Map<String, String> header = new HashMap<>();
ObscuredSharedPreferences osb = ObscuredSharedPreferences.getPrefs(getBaseContext(), "Agendador", Context.MODE_PRIVATE);
header.put("Content-Type", "application/json");
header.put(Utils.ACCESS_TOKEN, osb.getString(Utils.ACCESS_TOKEN, null));
header.put(Utils.CLIENT, osb.getString(Utils.CLIENT, null));
header.put(Utils.UID, osb.getString(Utils.UID, null));
Notification notification = null;
if (extras != null) {
notification = new Gson().fromJson(extras.getString(Utils.NOTIFICATION), Notification.class);
} else {
Log.e("Notificacao", "Job não recebeu os extras.");
jobFinished(job, true);
}
requestCreateNotification(header, job, osb, notification);
}}).start();
return true;
}
private void requestCreateNotification(final Map<String, String> header, final JobParameters job, final ObscuredSharedPreferences osb, final Notification notification){
final ApiEndpoints service = ApiUtils.request(header);
Call<NotificationReturn> listCall = service.requestCreateNotification(notification);
listCall.enqueue(new Callback<NotificationReturn>() {
@Override
public void onResponse(Call<NotificationReturn> call, Response<NotificationReturn> response) {
Headers headers = response.headers();
int status = response.code();
updateHeaders(headers, osb);
switch (status) {
case 200:
case 201:
Log.d("Server response", getClass().getName() + ": 200 - Sucesso!");
LocalNotificationManager.createLocalNotification(getApplicationContext(), response.body(), osb, new Gson());
jobFinished(job, false);
break;
default:
Log.e("Server response", getClass().getName() + ": ERRO:" + status);
jobFinished(job, true);
break;
}
}
@Override
public void onFailure(Call<NotificationReturn> call, Throwable t) {
Log.e("Server response", getClass().getName() + ": Requisição falhou!!");
t.printStackTrace();
jobFinished(job, true);
}
});
}
private void updateHeaders(Headers headers, ObscuredSharedPreferences osb){
if (headers.get(Utils.ACCESS_TOKEN) != null)
osb.edit().putString(Utils.ACCESS_TOKEN, headers.get(Utils.ACCESS_TOKEN)).apply();
if (headers.get(Utils.CLIENT) != null)
osb.edit().putString(Utils.CLIENT, headers.get(Utils.CLIENT)).apply();
if (headers.get(Utils.UID) != null)
osb.edit().putString(Utils.UID, headers.get(Utils.UID)).apply();
if (headers.get(Utils.EXPIRY) != null)
osb.edit().putString(Utils.EXPIRY, headers.get(Utils.EXPIRY)).apply();
}
@Override
public boolean onStopJob(JobParameters job) {
return true;
}
}
package br.ufpr.c3sl.agendador.agendador.views;
import br.ufpr.c3sl.agendador.agendador.models.ScheduleConfirmation;
/**
* Created by Lucas B. Cunha on 25/07/17.
*/
......@@ -12,8 +10,6 @@ public interface ScheduleConfirmationView {
void setProgressBar(boolean enabled);
void onSuccess(ScheduleConfirmation confirmationResponse);
void setConnectionError(boolean enabled);
void returnHome();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment