Commit 3bdea922 authored by Lucas Braz Cunha's avatar Lucas Braz Cunha

Issue AGILE#166: Added temporary warnings & date check to request image

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent 082e9391
......@@ -515,7 +515,8 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
//has to be done before load data.
userPhotoHelper.confirmUpdate(isPhotoDelete);
}catch (Exception e){
//TODO:notify user...
// TODO: 04/05/17 user messages & error warning
Toast.makeText(this, "Ocorreu um erro ao salvar a imagem, tente novamente.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
enableAllFields(false);
......@@ -914,14 +915,15 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
// path to folder.
final File root = new File(this.getExternalFilesDir(null) + File.separator + Utils.DIR_PICTRS + File.separator);
//TODO:how to alert user if mkdir fails?
// TODO: 04/05/17 user messages & error warning
if(!root.exists() && !root.mkdirs()){
Toast.makeText(this,"Ocorreu um erro ao preparar o armazenamento da imagem", Toast.LENGTH_LONG).show();
return;
}
if(!hasCameraPermission && !hasReadPermission){
//TODO: warn user
// TODO: 04/05/17 user messages & error warning
Toast.makeText(this, "O sistema precisa de permissão para selecionar a imagem.", Toast.LENGTH_SHORT).show();
return;
}
......@@ -1006,17 +1008,19 @@ public class AccountActivity extends AppCompatActivity implements AccountView {
userPhotoHelper.copyFileToPrivate(data.getData());
}
} catch (Exception e) {
//TODO:how to notify user?
// TODO: 04/05/17 user messages & error warning
Toast.makeText(this, "Ocorreu um problema ao salvar a imagem", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
isChangingPhoto = true;
isPhotoDelete = false;
//TODO:same as above.
// TODO: 04/05/17 user messages & error warning
if(userPhotoHelper.updateImgView(imgv_profile, userPhotoHelper.getTempFileUri()))
{
//show message if update fails
Toast.makeText(this, "Ocorreu um erro, tem certeza de que é uma imagem?", Toast.LENGTH_SHORT).show();
}
}
......
......@@ -16,10 +16,14 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import br.ufpr.c3sl.agendador.agendador.helpers.ConnectionChecker;
import br.ufpr.c3sl.agendador.agendador.helpers.ConnectionErrorDialog;
import br.ufpr.c3sl.agendador.agendador.helpers.Mask;
import br.ufpr.c3sl.agendador.agendador.helpers.UserPhotoHelper;
import br.ufpr.c3sl.agendador.agendador.presenters.LoginPresenter;
import br.ufpr.c3sl.agendador.agendador.views.LoginView;
......@@ -196,8 +200,15 @@ public class LoginActivity extends AppCompatActivity implements LoginView {
}
@Override
public void afterSuccessfulPhoto(Bitmap bitmap){
// TODO: 17/04/17 handle photo saving
public void afterSuccessfulPhoto(Bitmap bitmap, String uid){
// TODO: 17/04/17 user messages & error warning
UserPhotoHelper userPhotoHelper = new UserPhotoHelper(getBaseContext(), uid);
try {
userPhotoHelper.saveBitmap(bitmap);
} catch (IOException e) {
Toast.makeText(this, "Ocorreu um problema ao atualizar sua foto de perfil", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
afterSuccessfulLogin();
}
......
......@@ -100,7 +100,6 @@ public class DataValidador {
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
try {
Date birth = formatter.parse(birth_date);
Log.d("DataV:is_valid_birth", birth.toString());
if(birth.before(today)){
return true;
}
......
......@@ -40,6 +40,23 @@ public class UserPhotoHelper {
private Uri photoFileUri;
private Uri tempPhotoFileUri;
//Constructor for LoginActivity use
public UserPhotoHelper(Context context, String uid){
this.mContext = context;
this.filePath = mContext.getExternalFilesDir(null) + File.separator + Utils.DIR_PICTRS + File.separator + Utils.USR_PICT_FILE_NAME;
this.photo = new File(filePath + uid + ".bmp");
this.photoFileUri = Uri.fromFile(photo);
}
//Function to save bitmap received from server on login
public void saveBitmap(Bitmap bmp) throws IOException {
FileOutputStream out = new FileOutputStream(photoFileUri.getPath());
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); //100-best quality
out.close();
}
//Adding user id to handle multi-user experience
public UserPhotoHelper(Context context) {
this.mContext = context;
......@@ -56,7 +73,6 @@ public class UserPhotoHelper {
this.tempPhotoFileUri = Uri.fromFile(tempPhoto);
}
//TODO:Handle exceptions
//TODO: check usage of ".gif" files
public void copyFileToPrivate(Uri src) throws Exception {
String srcExtension = MimeTypeMap.getSingleton().
......
......@@ -23,7 +23,7 @@ public class UserOutput {
@SerializedName("id")
int mId;
@SerializedName("photo_update_at")
@SerializedName("avatar_updated_at")
String mPhoto_Update_At;
@SerializedName("photo_content_type")
......@@ -144,7 +144,7 @@ public class UserOutput {
osb.edit().putString("updated_at", this.mUpdated_At).apply();
osb.edit().putString("address.address", this.mAddress.getAddress()).apply();
osb.edit().putInt("id", this.mId).apply();
osb.edit().putString("photo_update_at", this.mPhoto_Update_At).apply();
osb.edit().putString("avatar_updated_at", this.mPhoto_Update_At).apply();
osb.edit().putString("photo_content_type", this.mPhoto_Content_Type).apply();
osb.edit().putString("photo_file_name", this.mPhoto_File_Name).apply();
osb.edit().putString("photo_file_size", this.mPhoto_File_Size).apply();
......
......@@ -7,8 +7,15 @@ import android.util.Log;
import java.io.File;
import java.io.FilenameFilter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import br.ufpr.c3sl.agendador.agendador.CepActivity;
import br.ufpr.c3sl.agendador.agendador.ForgotPasswordActivity;
......@@ -87,10 +94,8 @@ public class LoginPresenter extends BasePresenter<LoginView> {
loginPresenter.view().setProgressBar(false);
break;
case 200:
onSuccessfulLogin(accountOutput, headers);
searchPhoto();
//loginPresenter.requestImage();
checkLocalPhoto();
break;
default:
break;
......@@ -105,12 +110,11 @@ public class LoginPresenter extends BasePresenter<LoginView> {
});
}
// TODO: 17/04/17 Fazer amanhã: salvar data da ultima imagem salva localmente
//e só pedir denovo se a que tem no servidor for mais recente
private void requestImage() {
Map<String, String> header = new HashMap<>();
String uid = osb.getString("uid", null);
final String uid = osb.getString("uid", null);
header.put("Content-Type", "application/json");
header.put("access-token", osb.getString("access-token", null));
header.put("client", osb.getString("client", null));
......@@ -135,7 +139,7 @@ public class LoginPresenter extends BasePresenter<LoginView> {
loginPresenter.view().afterSuccessfulLogin();
break;
case 200:
loginPresenter.view().afterSuccessfulPhoto(BitmapFactory.decodeStream(response.body().byteStream()));
loginPresenter.view().afterSuccessfulPhoto(BitmapFactory.decodeStream(response.body().byteStream()), uid);
loginPresenter.view().setProgressBar(false);
break;
default:
......@@ -147,7 +151,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 to check connection?(only if image is newer
/*
Service running "always" to check if there is internet connection
and update info of the current activity
*/
loginPresenter.view().setNoConnection(true);
loginPresenter.view().setProgressBar(false);
}
......@@ -156,9 +163,25 @@ public class LoginPresenter extends BasePresenter<LoginView> {
}
// TODO: 03/05/17 Adapt function to scenario with different file extensions, if needed.
private void searchPhoto(){
private void checkLocalPhoto(){
final String uid = osb.getString("uid", null);
String compare, children[];
Locale locale = new Locale("pt", "BR");
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", locale);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
sdf.setLenient(false);
Date fileDate = null;
//in case of parsing exception, it will request image from server and app won't crash.
Date serverDate = Calendar.getInstance().getTime();
try {
serverDate = sdf.parse(osb.getString("avatar_updated_at", ""));
} catch (ParseException e) {
e.printStackTrace();
}
File dir = new File(context.getExternalFilesDir(null) + File.separator + Utils.DIR_PICTRS + File.separator);
FilenameFilter filter = new FilenameFilter() {
......@@ -167,13 +190,26 @@ public class LoginPresenter extends BasePresenter<LoginView> {
}
};
String[] children = dir.list(filter);
children = dir.list(filter);
if (children == null || children.length == 0) {
Log.d("Error", "(Either dir does not exist or is not a directory) or ( File not Found).");
requestImage();
}else{
loginPresenter.view().setProgressBar(false);
loginPresenter.view().afterSuccessfulLogin();
for (String name: children) {
compare = name.substring(0, name.indexOf("."));
if(compare.equals(Utils.USR_PICT_FILE_NAME + uid) ) {
File f = new File(dir, name);
fileDate = new Date(f.lastModified());
}
}
if(fileDate != null && serverDate.after(fileDate)){
requestImage();
}
else{
loginPresenter.view().setProgressBar(false);
loginPresenter.view().afterSuccessfulLogin();
}
}
......
......@@ -17,5 +17,5 @@ public interface LoginView {
void afterSuccessfulLogin();
void afterSuccessfulPhoto(Bitmap bitmap);
void afterSuccessfulPhoto(Bitmap bitmap, String uid);
}
\ No newline at end of file
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