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

Issue AGILE#174: Done service fired on login to download user picture

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent bab29f2a
......@@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
......
......@@ -29,8 +29,12 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.UserPhotoHelper;
import br.ufpr.c3sl.agendador.agendador.helpers.Utils;
import br.ufpr.c3sl.agendador.agendador.presenters.HomePresenter;
import br.ufpr.c3sl.agendador.agendador.services.ImageUpdateService;
import br.ufpr.c3sl.agendador.agendador.views.HomeView;
......
......@@ -3,6 +3,8 @@ package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.util.Log;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
......@@ -11,6 +13,7 @@ import java.util.HashMap;
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.AccountOutput;
import br.ufpr.c3sl.agendador.agendador.models.AccountUpdate;
import br.ufpr.c3sl.agendador.agendador.models.Address;
......@@ -35,15 +38,24 @@ public class AccountPresenter extends BasePresenter<AccountView> {
private ObscuredSharedPreferences osb;
private Context context;
private AccountPresenter accountPresenter;
private boolean isPicUpdate;
public AccountPresenter(Context context) {
this.context = context;
accountPresenter = this;
isPicUpdate = false;
}
private void onSuccessfulUpdate(AccountOutput accountOutput, Headers headers) {
updateToken(headers);
accountOutput.getCitizen().save(context);
//cancelling job because photo has been updated from app.
if(isPicUpdate){
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
dispatcher.cancel(Utils.SERVICE_UPDATE_IMAGE);
isPicUpdate = false;
}
}
private void updateToken(Headers headers) {
......@@ -73,9 +85,10 @@ public class AccountPresenter extends BasePresenter<AccountView> {
UserUpdate user = new UserUpdate(body);
final AccountUpdate account;
if(body.get("photo_type") != null)
if(body.get("photo_type") != null) {
user.setImage(body.get("photo_type"), body.get("photo_name"), body.get("photo_content"));
isPicUpdate = true;
}
if (body.get("password") == null || body.get("password_confirmation") == null){
account = new AccountUpdate(user);
} else{
......
......@@ -4,11 +4,15 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import java.util.HashMap;
import java.util.Map;
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.SignOutOutput;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
......@@ -36,58 +40,16 @@ public class HomePresenter extends BasePresenter<HomeView> {
}
public void onSuccessfulSignOut() {
public void onSignOutClicked() {
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
dispatcher.cancel(Utils.SERVICE_UPDATE_IMAGE);
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);
}
public void onSignOutClicked() {
osb = ObscuredSharedPreferences.getPrefs(context, "Agendador", Context.MODE_PRIVATE);
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
header.put("access-token", osb.getString("access-token", null));
header.put("client", osb.getString("client", null));
header.put("uid", osb.getString("uid", null));
ApiEndpoints service = ApiUtils.request(header);
Call<SignOutOutput> listCall = service.signOut();
// loginPresenter.view().setProgressBar(true);
listCall.enqueue(new Callback<SignOutOutput>() {
@Override
public void onResponse(Call<SignOutOutput> call, Response<SignOutOutput> response) {
int status = response.code();
switch (status) {
case 401:
Log.d("AAAAAa-Logout","Voltou 401");
// homePresenter.view().setProgressBar(false);
break;
case 200:
Log.d("AAAAAa-Logout","Deslogou certim");
onSuccessfulSignOut();
// homePresenter.view().setProgressBar(false);
break;
default:
Log.d("AAAAAa-Logout","Deslogar falhou com retorno " + status);
break;
}
}
@Override
public void onFailure(Call<SignOutOutput> call, Throwable t) {
// homePresenter.view().setNoConnection(true);
// homePresenter.view().setProgressBar(false);
Log.d("AAAAAa-Logout","falha para deslogar, nao conectou");
}
});
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
......@@ -9,6 +10,7 @@ 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;
......@@ -35,6 +37,7 @@ import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
import br.ufpr.c3sl.agendador.agendador.services.ImageUpdateService;
import br.ufpr.c3sl.agendador.agendador.views.LoginView;
import okhttp3.Headers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -93,7 +96,7 @@ public class LoginPresenter extends BasePresenter<LoginView> {
Call<AccountOutput> listCall = service.signIn(account);
loginPresenter.view().setProgressBar(true);
Log.d("olá", "pedindo login!!!");
Log.d("Login Request", "pedindo login!!!");
listCall.enqueue(new Callback<AccountOutput>() {
@Override
......@@ -104,24 +107,24 @@ public class LoginPresenter extends BasePresenter<LoginView> {
switch (status) {
case 401:
Log.d("olá", "Voltou 401!!!");
Log.d("Login Request", "Voltou 401!!!");
loginPresenter.view().setWrongPassword(true);
loginPresenter.view().setProgressBar(false);
break;
case 200:
Log.d("olá", "Voltou 200!!!");
Log.d("Login Request", "Voltou 200!!!");
onSuccessfulLogin(accountOutput, headers);
checkLocalPhoto();
break;
default:
Log.d("olá", "Voltou " + status + "!!!");
Log.d("Login Request", "Voltou " + status + "!!!");
break;
}
}
@Override
public void onFailure(Call<AccountOutput> call, Throwable t) {
Log.d("olá", "Falhou!!!");
Log.d("Login Request", "Falhou!!!");
loginPresenter.view().setNoConnection(true);
loginPresenter.view().setProgressBar(false);
}
......@@ -130,7 +133,7 @@ public class LoginPresenter extends BasePresenter<LoginView> {
private void requestImage() {
/*Map<String, String> header = new HashMap<>();
Map<String, String> header = new HashMap<>();
final String uid = osb.getString("uid", null);
header.put("Content-Type", "application/json");
......@@ -150,9 +153,10 @@ public class LoginPresenter extends BasePresenter<LoginView> {
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
int status = response.code();
Log.d("Request Imagem", "Retorno da request de imagem: " + status);
switch (status) {
case 500:
//image not found
Log.d("Request Imagem", "500 - Servidor: Internal Error");
loginPresenter.view().setProgressBar(false);
loginPresenter.view().afterSuccessfulLogin();
break;
......@@ -160,6 +164,11 @@ public class LoginPresenter extends BasePresenter<LoginView> {
loginPresenter.view().afterSuccessfulPhoto(BitmapFactory.decodeStream(response.body().byteStream()), uid);
loginPresenter.view().setProgressBar(false);
break;
case 404:
//user has no picture on back-end side
loginPresenter.view().setProgressBar(false);
loginPresenter.view().afterSuccessfulLogin();
break;
default:
break;
}
......@@ -168,12 +177,10 @@ public class LoginPresenter extends BasePresenter<LoginView> {
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
// TODO: 17/04/17 what to do if request fails to get image?
Service running "always" to check if there is internet connection
and update info of the current activity
/*
Job scheduled to check if there is internet connection
and download user image
*/
Log.d("Aaaaaaaaaa","Agendando Job!!!!!!!1");
Bundle extras = new Bundle();
extras.putString("access-token",osb.getString("access-token", null));
......@@ -197,19 +204,18 @@ public class LoginPresenter extends BasePresenter<LoginView> {
Constraint.ON_ANY_NETWORK
)
.setExtras(extras)
//if device is rebooted the job will be restarted
.setLifetime(Lifetime.FOREVER)
.build();
dispatcher.mustSchedule(job);
Log.d("Aaaaaaaaaa","Job Agendado!!!!!!!1");
//loginPresenter.view().setNoConnection(true);
loginPresenter.view().setProgressBar(false);
//apagar...
loginPresenter.view().afterSuccessfulLogin();
// }
// });
}
});
}
......
......@@ -56,7 +56,6 @@ public class ImageUpdateService extends JobService {
switch (status) {
case 200:
UserPhotoHelper userPhotoHelper = new UserPhotoHelper(getBaseContext(), uid);
try {
userPhotoHelper.saveBitmap(BitmapFactory.decodeStream(response.body().byteStream()));
......@@ -67,7 +66,7 @@ public class ImageUpdateService extends JobService {
Log.d("AGNDDR-ImgService", "Job teve sucesso!!111!");
jobFinished(job, false);
break;
case 401:
case 404:
//user has no picture on back-end side
jobFinished(job, false);
default:
......
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