Commit 947a48ee authored by Lucas Braz Cunha's avatar Lucas Braz Cunha

AGILE#271: [WP] Cancel notification on logout, Show android notification

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent 7ca79e11
......@@ -205,6 +205,7 @@ public class LoginActivity extends AppCompatActivity implements LoginView {
@Override
public void afterSuccessfulLogin() {
// TODO 22/12/2017: instanciar job que verifica notificações no back-end.
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
finish();
......
......@@ -43,12 +43,15 @@ public class ScheduleInfoActivity extends AppCompatActivity implements Navigatio
private long id;
private CheckSchedules.Schedule schedule;
private static final String EXIT_CONFIRMATION_DIALOG_TAG = "exit_dialog";
private int caller;
private TextView tv_name, tv_sector, tv_location, tv_type,
tv_date, tv_address, tv_situation, tv_situation_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -114,29 +117,25 @@ public class ScheduleInfoActivity extends AppCompatActivity implements Navigatio
}
});
tv_name = (TextView) findViewById(R.id.tv_schedule_citizen_content);
tv_sector = (TextView) findViewById(R.id.tv_schedule_sector_content);
tv_location = (TextView) findViewById(R.id.tv_schedule_location_content);
tv_type = (TextView) findViewById(R.id.tv_schedule_type_content);
tv_date = (TextView) findViewById(R.id.tv_schedule_date_content);
tv_address = (TextView) findViewById(R.id.tv_schedule_street_content);
tv_situation = (TextView) findViewById(R.id.tv_schedule_situation);
tv_situation_content = (TextView) findViewById(R.id.tv_schedule_situation_content);
id = getIntent().getIntExtra(Utils.ID, 0);
Bundle bundle = getIntent().getBundleExtra(Utils.SCHEDULE_BUNDLE);
CheckSchedules.Schedule schedule = null;
if(bundle != null){
DateFormat df = new SimpleDateFormat("dd/MM/yyyy - HH:mm", Locale.getDefault());
String name = bundle.getString(Utils.NAME);
id = bundle.getLong(Utils.ID);
schedule = bundle.getParcelable(Utils.SCHEDULE);
TextView tv_name = (TextView) findViewById(R.id.tv_schedule_citizen_content);
TextView tv_sector = (TextView) findViewById(R.id.tv_schedule_sector_content);
TextView tv_location = (TextView) findViewById(R.id.tv_schedule_location_content);
TextView tv_type = (TextView) findViewById(R.id.tv_schedule_type_content);
TextView tv_date = (TextView) findViewById(R.id.tv_schedule_date_content);
TextView tv_address = (TextView) findViewById(R.id.tv_schedule_street_content);
TextView tv_situation = (TextView) findViewById(R.id.tv_schedule_situation);
TextView tv_situation_content = (TextView) findViewById(R.id.tv_schedule_situation_content);
tv_name.setText(name);
tv_sector.setText(schedule.getSectorName());
tv_location.setText(schedule.getLocationName());
tv_type.setText(schedule.getServiceTypeName());
tv_date.setText(df.format(schedule.getStartTime()));
String address = schedule.getAddressStreet() + ", " + schedule.getAddressNumber();
tv_address.setText(address);
showScheduleContent(schedule);
caller = bundle.getInt(Utils.CALLER_ACTIVITY);
if(caller == Utils.SCHEDULES_ACTIVITY){
......@@ -147,6 +146,10 @@ public class ScheduleInfoActivity extends AppCompatActivity implements Navigatio
navigationView.setCheckedItem(R.id.nav_schedules_history);
tv_situation_content.setText(schedule.getSituation());
}
}else if(id != 0 ){
//pega schedule
// TODO: caso a intent veio da notificação, realizar requisição para pegar os dados.
//showScheduleContent(schedule);
}
......@@ -163,6 +166,16 @@ public class ScheduleInfoActivity extends AppCompatActivity implements Navigatio
}
private void showScheduleContent(CheckSchedules.Schedule schedule){
DateFormat df = new SimpleDateFormat("dd/MM/yyyy - HH:mm", Locale.getDefault());
tv_sector.setText(schedule.getSectorName());
tv_location.setText(schedule.getLocationName());
tv_type.setText(schedule.getServiceTypeName());
tv_date.setText(df.format(schedule.getStartTime()));
String address = schedule.getAddressStreet() + ", " + schedule.getAddressNumber();
tv_address.setText(address);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Intent intent;
......
......@@ -250,9 +250,9 @@ public class SchedulesActivity extends AppCompatActivity implements NavigationVi
maxDate = new Date();
String date;
if(schedules.getSchedules().getSchedulesList() != null) {
if(schedules.getSchedulesModel().getSchedulesList() != null) {
for (CheckSchedules.Schedule schedule : schedules.getSchedules().getSchedulesList()) {
for (CheckSchedules.Schedule schedule : schedules.getSchedulesModel().getSchedulesList()) {
date = dateFormat.format(schedule.getStartTime());
if(schedule.getStartTime().after(maxDate)){
maxDate = schedule.getStartTime();
......@@ -414,8 +414,8 @@ public class SchedulesActivity extends AppCompatActivity implements NavigationVi
long id = 0;
//searching if this schedule belongs to the citizen or the dependents.
for(int i = 0; i < schedules.getSchedules().getSchedulesList().size() && id == 0; ++i){
if(schedules.getSchedules().getSchedulesList().get(i).getId() == selectedSchedule.getId())
for(int i = 0; i < schedules.getSchedulesModel().getSchedulesList().size() && id == 0; ++i){
if(schedules.getSchedulesModel().getSchedulesList().get(i).getId() == selectedSchedule.getId())
{
id = schedules.getId();
name = schedules.getName();
......
......@@ -756,7 +756,7 @@ public class SchedulesHistoryActivity extends AppCompatActivity implements Navig
expandableListTitle = new ArrayList<>();
expandableListTitle.add(new Pair<>(schedules.getName(), schedules.getId()));
expandableListDetail.put(schedules.getName(), schedules.getSchedules().getSchedulesList());
expandableListDetail.put(schedules.getName(), schedules.getSchedulesModel().getSchedulesList());
for (CheckSchedules.DependentSchedules dependentSchedules : schedules.getDependentSchedulesList()) {
expandableListTitle.add(new Pair<>(dependentSchedules.getName(), dependentSchedules.getId()));
......
......@@ -127,6 +127,8 @@ public abstract class Utils {
public static final String ACTION_SHOW_NOTIFICATION = "br.ufpr.c3sl.agendador.SHOW_NOTIFICATION";
public static final int SHOW_SCHEDULE_INFO_ACTION = 0x11;
public static int getPixelValue(int dp, Context context) {
Resources resources = context.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
......
......@@ -41,7 +41,7 @@ public class CheckSchedules {
this.dependentSchedulesList = dependentSchedulesList;
}
public ScheduleListModel getSchedules() {
public ScheduleListModel getSchedulesModel() {
return schedules;
}
......
......@@ -14,8 +14,8 @@ import retrofit2.converter.gson.GsonConverterFactory;
*/
public abstract class ApiUtils {
// public static final String BASE_URL = "http://10.0.2.2:3000/v1/";
public static final String BASE_URL = "http://newcastle.c3sl.ufpr.br/develop/v1/";
public static final String BASE_URL = "http://10.0.2.2:3000/v1/";
// public static final String BASE_URL = "http://newcastle.c3sl.ufpr.br/develop/v1/";
public static ApiEndpoints request(final Map<String, String> header) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
......
package br.ufpr.c3sl.agendador.agendador.presenters;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.google.gson.Gson;
import java.lang.ref.WeakReference;
import java.util.List;
import br.ufpr.c3sl.agendador.agendador.LoginActivity;
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.services.notification.LocalNotificationManager;
import br.ufpr.c3sl.agendador.agendador.services.notification.NotificationReceiver;
import okhttp3.Headers;
import static android.content.Context.ALARM_SERVICE;
/**
* Created by Bruno Freitas Tissei on 2/2/17.
*/
......@@ -55,7 +67,7 @@ public abstract class BasePresenter<V> {
protected void updateHeaders(Headers headers) {
if(osb == null && context != null)
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);;
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
if(headers.get(Utils.ACCESS_TOKEN) != null)
osb.edit().putString(Utils.ACCESS_TOKEN, headers.get(Utils.ACCESS_TOKEN)).apply();
......@@ -73,11 +85,37 @@ public abstract class BasePresenter<V> {
dispatcher.cancel(Utils.SERVICE_UPDATE_IMAGE);
if(osb == null)
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
Gson gson = new Gson();
List<Notification> notificationList = LocalNotificationManager.getList(osb, gson);
Intent intent = new Intent(context, NotificationReceiver.class);
intent.setAction(Utils.ACTION_SHOW_NOTIFICATION);
PendingIntent pendingIntent;
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
for(Notification n : notificationList){
pendingIntent = PendingIntent.getBroadcast(context,
LocalNotificationManager.NOTIFICATION_REMINDER_REQUEST_CODE + (int) n.scheduleId, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
if(am != null){
am.cancel(pendingIntent);
}
}
osb.edit().clear().apply();
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Intent intent2 = new Intent(context, LoginActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
\ No newline at end of file
......@@ -55,7 +55,7 @@ public class BootReceiver extends BroadcastReceiver {
try {
date = sdfServer.parse(notification.reminderTime);
calendar.setTime(date);
LocalNotificationManager.setReminder(context, NotificationReceiver.class, calendar, (int) notification.scheduleId);
LocalNotificationManager.setReminder(context, calendar, (int) notification.scheduleId);
} catch (ParseException e) {
e.printStackTrace();
}
......
......@@ -34,7 +34,7 @@ import static android.content.Context.ALARM_SERVICE;
public abstract class LocalNotificationManager {
private static final int NOTIFICATION_REMINDER_REQUEST_CODE = 0x27;
public static final int NOTIFICATION_REMINDER_REQUEST_CODE = 0x27;
public static void createLocalNotification(Context context, Notification notification, ObscuredSharedPreferences osb, Gson gson){
......@@ -45,7 +45,7 @@ public abstract class LocalNotificationManager {
try{
calendar.setTime(sdfServer.parse(notification.reminderTime));
setReminder(context, NotificationReceiver.class, calendar, (int) notification.scheduleId);
setReminder(context, calendar, (int) notification.scheduleId);
}catch (ParseException ignored){
Log.e("Agendador", "Erro ao parsear data, para criar notificação local.");
ignored.printStackTrace();
......@@ -53,9 +53,9 @@ public abstract class LocalNotificationManager {
}
public static void setReminder(Context context, Class<?> cls, Calendar calendar, int scheduleId){
public static void setReminder(Context context, Calendar calendar, int scheduleId){
Intent intent = new Intent(context, cls);
Intent intent = new Intent(context, NotificationReceiver.class);
intent.setAction(Utils.ACTION_SHOW_NOTIFICATION);
intent.putExtra(Utils.ID, scheduleId);
......
package br.ufpr.c3sl.agendador.agendador.services.notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
......@@ -14,6 +15,7 @@ import com.google.gson.Gson;
import java.util.List;
import br.ufpr.c3sl.agendador.agendador.R;
import br.ufpr.c3sl.agendador.agendador.ScheduleInfoActivity;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.models.Notification;
......@@ -53,10 +55,15 @@ public class NotificationReceiver extends BroadcastReceiver{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
Intent showScheduleIntent = new Intent(context, ScheduleInfoActivity.class);
showScheduleIntent.putExtra(Utils.ID, id);
PendingIntent pendingIntent = PendingIntent.getActivity(context, Utils.SHOW_SCHEDULE_INFO_ACTION, showScheduleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
String alert = context.getResources().getString(R.string.notification_alert);
android.app.Notification appNotification = mBuilder.setAutoCancel(true).setContentText(content.description).setContentTitle(alert)
.setDefaults(android.app.Notification.DEFAULT_LIGHTS | android.app.Notification.DEFAULT_SOUND)
.setSmallIcon(R.drawable.img_mainact_logo).setTicker(alert)
.setSmallIcon(R.drawable.img_mainact_logo).setTicker(alert).setContentIntent(pendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.img_terms_logo)).build();
// Will display the notification in the notification bar
......
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