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

Issue AGILE#174: Initial commit

Signed-off-by: Lucas Braz Cunha's avatarLucas B. Cunha <lbc16@inf.ufpr.br>
parent 0fe0899a
......@@ -32,4 +32,6 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.firebase:firebase-jobdispatcher:0.6.0'
}
......@@ -44,6 +44,19 @@
<activity android:name=".ForgotPasswordActivity">
</activity>
<service android:name=".services.ImageUpdateService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
</application>
</manifest>
\ No newline at end of file
package br.ufpr.c3sl.agendador.agendador;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
......@@ -20,6 +21,7 @@ import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
......@@ -30,6 +32,7 @@ import android.widget.TextView;
import br.ufpr.c3sl.agendador.agendador.helpers.ObscuredSharedPreferences;
import br.ufpr.c3sl.agendador.agendador.helpers.UserPhotoHelper;
import br.ufpr.c3sl.agendador.agendador.presenters.HomePresenter;
import br.ufpr.c3sl.agendador.agendador.services.ImageUpdateService;
import br.ufpr.c3sl.agendador.agendador.views.HomeView;
/**
......@@ -235,4 +238,5 @@ public class HomeActivity extends AppCompatActivity implements HomeView,
}
}
}
......@@ -42,6 +42,8 @@ public class UserPhotoHelper {
//Constructor for LoginActivity use
// TODO: 08/05/17 check usage of getExternalFilesDirs
//https://developer.android.com/guide/topics/data/data-storage.html
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;
......@@ -51,6 +53,10 @@ public class UserPhotoHelper {
//Function to save bitmap received from server on login
public void saveBitmap(Bitmap bmp) throws IOException {
File f2 = new File(mContext.getExternalFilesDir(null) + File.separator + Utils.DIR_PICTRS + File.separator);
f2.mkdirs();
File f = new File(photoFileUri.getPath());
f.createNewFile();
FileOutputStream out = new FileOutputStream(photoFileUri.getPath());
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); //100-best quality
out.close();
......@@ -62,10 +68,10 @@ public class UserPhotoHelper {
this.mContext = context;
osb = ObscuredSharedPreferences.getPrefs(mContext, "Agendador", Context.MODE_PRIVATE);
this.filePath = mContext.getExternalFilesDir(null) + File.separator + Utils.DIR_PICTRS + File.separator + Utils.USR_PICT_FILE_NAME;
createFile();
initVariables();
}
private void createFile() {
private void initVariables() {
this.userUid = osb.getString("uid", null);
this.photo = new File(filePath + userUid + ".bmp");
this.tempPhoto = new File(filePath + "temp_" + userUid);
......
......@@ -24,6 +24,8 @@ public class Utils {
public final static String DIR_PICTRS = "pictures";
public final static String USR_PICT_FILE_NAME = "usr_prof_pic_";
public final static String SERVICE_UPDATE_IMAGE = "br.c3sl.ufpr.image_update";
public static int getPixelValue(int dp, Context context) {
Resources resources = context.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
......
......@@ -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/devapi/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/devapi/v1/";
public static ApiEndpoints request(final Map<String, String> header) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
......
......@@ -2,6 +2,7 @@ package br.ufpr.c3sl.agendador.agendador.presenters;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
......@@ -65,13 +66,18 @@ public class HomePresenter extends BasePresenter<HomeView> {
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;
}
}
......@@ -80,6 +86,7 @@ public class HomePresenter extends BasePresenter<HomeView> {
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");
}
});
}
......
......@@ -2,9 +2,16 @@ 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;
import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.RetryStrategy;
import com.firebase.jobdispatcher.Trigger;
import java.io.File;
import java.io.FilenameFilter;
import java.text.DateFormat;
......@@ -25,9 +32,9 @@ import br.ufpr.c3sl.agendador.agendador.models.Account;
import br.ufpr.c3sl.agendador.agendador.models.AccountOutput;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
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;
......@@ -41,9 +48,15 @@ public class LoginPresenter extends BasePresenter<LoginView> {
private Context context;
private LoginPresenter loginPresenter;
private FirebaseJobDispatcher dispatcher;
public LoginPresenter(Context context) {
this.context = context;
loginPresenter = this;
dispatcher = new FirebaseJobDispatcher(
new GooglePlayDriver(context)
);
}
@Override
......@@ -80,6 +93,7 @@ public class LoginPresenter extends BasePresenter<LoginView> {
Call<AccountOutput> listCall = service.signIn(account);
loginPresenter.view().setProgressBar(true);
Log.d("olá", "pedindo login!!!");
listCall.enqueue(new Callback<AccountOutput>() {
@Override
......@@ -90,20 +104,24 @@ public class LoginPresenter extends BasePresenter<LoginView> {
switch (status) {
case 401:
Log.d("olá", "Voltou 401!!!");
loginPresenter.view().setWrongPassword(true);
loginPresenter.view().setProgressBar(false);
break;
case 200:
Log.d("olá", "Voltou 200!!!");
onSuccessfulLogin(accountOutput, headers);
checkLocalPhoto();
break;
default:
Log.d("olá", "Voltou " + status + "!!!");
break;
}
}
@Override
public void onFailure(Call<AccountOutput> call, Throwable t) {
Log.d("olá", "Falhou!!!");
loginPresenter.view().setNoConnection(true);
loginPresenter.view().setProgressBar(false);
}
......@@ -112,7 +130,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");
......@@ -147,24 +165,58 @@ 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
*/
loginPresenter.view().setNoConnection(true);
Log.d("Aaaaaaaaaa","Agendando Job!!!!!!!1");
Bundle extras = new Bundle();
extras.putString("access-token",osb.getString("access-token", null));
extras.putString("uid",osb.getString("uid", null));
extras.putString("client",osb.getString("client", null));
extras.putInt("id",osb.getInt("id", 0));
Job job = dispatcher.newJobBuilder()
.setService(ImageUpdateService.class)
.setTag(Utils.SERVICE_UPDATE_IMAGE)
//time the function gets is in seconds, 60s = 1 minutes, 1 hour = 3600
.setTrigger(Trigger.executionWindow(60, 3600))
//will be retried if it fails.
.setRecurring(true)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setConstraints(
// only run on network
Constraint.ON_ANY_NETWORK
)
.setExtras(extras)
.build();
dispatcher.mustSchedule(job);
Log.d("Aaaaaaaaaa","Job Agendado!!!!!!!1");
//loginPresenter.view().setNoConnection(true);
loginPresenter.view().setProgressBar(false);
}
});
//apagar...
loginPresenter.view().afterSuccessfulLogin();
// }
// });
}
// TODO: 03/05/17 Adapt function to scenario with different file extensions, if needed.
private void checkLocalPhoto(){
Log.d("olá", "Checando se a imagem existe!!!");
final String uid = osb.getString("uid", null);
String compare, children[];
......@@ -214,4 +266,6 @@ public class LoginPresenter extends BasePresenter<LoginView> {
}
}
}
\ No newline at end of file
package br.ufpr.c3sl.agendador.agendador.services;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import br.ufpr.c3sl.agendador.agendador.helpers.UserPhotoHelper;
import br.ufpr.c3sl.agendador.agendador.network.ApiEndpoints;
import br.ufpr.c3sl.agendador.agendador.network.ApiUtils;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by Lucas B. Cunha on 11/05/17.
*/
public class ImageUpdateService extends JobService {
/**
* The entry point to your Job. Implementations should offload work to another thread of
* execution as soon as possible.
*
* @param job
* @return whether there is more work remaining.
*/
@Override
public boolean onStartJob(final JobParameters job) {
Log.d("AGNDDR-ImgService", "Rodando " + ImageUpdateService.class.getName());
Bundle extras = job.getExtras();
Map<String, String> header = new HashMap<>();
final String uid = extras.getString("uid", null);
header.put("Content-Type", "application/json");
header.put("access-token", extras.getString("access-token", null));
header.put("client", extras.getString("client", null));
header.put("uid", uid);
ApiEndpoints service = ApiUtils.request(header);
Call<ResponseBody> listCall = service.requestPhoto(extras.getInt("id", 0), "large");
listCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
int status = response.code();
switch (status) {
case 200:
UserPhotoHelper userPhotoHelper = new UserPhotoHelper(getBaseContext(), uid);
try {
userPhotoHelper.saveBitmap(BitmapFactory.decodeStream(response.body().byteStream()));
} catch (IOException e) {
Log.d("AGNDDR-ImgService", "Ocorreu um problema ao atualizar sua foto de perfil");
e.printStackTrace();
}
Log.d("AGNDDR-ImgService", "Job teve sucesso!!111!");
jobFinished(job, false);
break;
case 401:
//user has no picture on back-end side
jobFinished(job, false);
default:
Log.d("AGNDDR-ImgService", "Job Foi executado mas recebeu retorno " + status);
jobFinished(job, true);
break;
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("AGNDDR-ImgService", "Job fahlou e será remarcado!!");
jobFinished(job, true);
}
});
return true;
}
/**
* Called when the scheduling engine has decided to interrupt the execution of a running job,
* most likely because the runtime constraints associated with the job are no longer satisfied.
*
* @param job
* @return whether the job should be retried
*/
@Override
public boolean onStopJob(JobParameters job) {
return true;
}
}
......@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......
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