Merge background task into develop #14

Merged
s416204 merged 17 commits from background-to-merge into develop 2018-10-12 00:12:34 +02:00
5 changed files with 156 additions and 25 deletions
Showing only changes of commit 5d7022a32c - Show all commits

View File

@ -355,9 +355,11 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
public void onSuccess(JwtToken jwtToken) {
String token = jwtToken.getToken();
JWT jwt = new JWT(token);
Claim role = jwt.getClaim("nameid");
Log.e("user",role.asString());
PrefUtils.storeApiKey(getApplicationContext(), token);
PrefUtils.storeUserId(getApplicationContext(), jwt);
PrefUtils.storeUserId(getApplicationContext(), role.asString());
onPostExecute(true);
}

View File

@ -145,23 +145,7 @@ public class MainActivity extends AppCompatActivity {
this.broadcastReceiver = new BroadcastLocalizationHandler();
//You should find out what causes this fancy/not reproducible error
/* Coordinate coordinate = new Coordinate(latitude);
disposable.add(coordinateService.postCoordinate(coordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() {
@Override
public void onSuccess(Coordinate coordinate) {
Log.e("Service", "onSuccess: " + coordinate);
}
@Override
public void onError(Throwable e) {
Log.e("Service", "onError: " + e.getMessage());
}
}));*/
}
private void setFragment(Fragment fragment) {

View File

@ -6,6 +6,7 @@ import java.util.List;
import io.reactivex.Observable;
import io.reactivex.Single;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
@ -37,19 +38,21 @@ public interface CoordinateService {
@GET("api/coordinates/top/online")
Call<List<Coordinate>> getOnlineCoordinates();
/*
// Create note
@FormUrlEncoded
@POST("api/coordinates")
Single <Coordinate> postCoordinate(@Body Coordinate coordinate);
/*
*/
@POST("api/coordinates")
Call<ResponseBody> postCoordinate(@Body RequestBody Coordinate);
/*
// Create note
@FormUrlEncoded
@POST("api/coordinates")
Observable <Coordinate> postCoordinate(@Field("latitude") Double latitude);
*/
@PUT("api/coordinates/{id}")
Call<Coordinate> putCoordinatesById(@Path("id") String id);

View File

@ -4,24 +4,166 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.AsyncTask;
import android.util.ArrayMap;
import android.util.Log;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.service.LdapService;
import org.json.JSONObject;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import static android.content.ContentValues.TAG;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class BroadcastLocalizationHandler extends BroadcastReceiver {
public static String str_receiver = "servicetutorial.service.receiver";
private IntentFilter filter =
new IntentFilter(str_receiver);
CompositeDisposable disposable = new CompositeDisposable();
CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class);
@Override
public void onReceive(Context arg0, Intent intent) {
Double latitude, longitude;
latitude = intent.getDoubleExtra("latitude", 0);
Coordinate coordinate = new Coordinate(latitude);
/* disposable.add(coordinateService.postCoordinate(coordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() {
@Override
public void onSuccess(Coordinate coordinate) {
Log.e("Service", "onSuccess: " + coordinate);
}
@Override
public void onError(Throwable e) {
Log.e("Service", "onError: " + e.getMessage());
}
}));*/
// longitude = intent.getDoubleExtra("longitude",0);
Log.e("Background service", String.valueOf(latitude));
final PendingResult pendingResult = goAsync();
Task asyncTask = new Task(pendingResult, intent);
asyncTask.execute();
}
private static class Task extends AsyncTask {
private final PendingResult pendingResult;
private final Intent intent;
private CompositeDisposable disposable = new CompositeDisposable();
private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService .class);
private Task(PendingResult pendingResult, Intent intent) {
this.pendingResult = pendingResult;
this.intent = intent;
}
@Override
protected Object doInBackground(Object[] objects) {
//You should find out what causes this fancy/not reproducible error
/* Coordinate coordinate = new Coordinate(52.0);
disposable.add(coordinateService.postCoordinate(52.0)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Coordinate>() {
*//* @Override
public void onSuccess(Coordinate coordinate) {
Log.e("Service", "onSuccess: " + coordinate);
onPostExecute(true);
}
*//*
@Override
public void onNext(Coordinate coordinate) {
Log.e("Service", "onSuccess: " + coordinate);
}
@Override
public void onError(Throwable e) {
Log.e("Service", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
}
}));
return null;*/
Map<String, Object> jsonParams = new ArrayMap<>();
//put something inside the map, could be null
jsonParams.put("latitude", 52.0);
jsonParams.put("longitude", 16.0);
jsonParams.put("userId", PrefUtils.getUserId(getApplicationContext()));
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),(new JSONObject(jsonParams)).toString());
//serviceCaller is the interface initialized with retrofit.create...
Call<ResponseBody> response = coordinateService.postCoordinate(body);
response.enqueue(new Callback<ResponseBody>()
{
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
{
try
{
//get your response....
Log.d(TAG, "RetroFit2.0 :RetroGetLogin: " + rawResponse.body().string());
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable)
{
// other stuff...
Log.d(TAG, "RetroFit2.0 :RetroGet error: ");
}
});
return null;
}
}
}

View File

@ -29,10 +29,10 @@ public class PrefUtils {
return getSharedPreferences(context).getString("API_KEY", null);
}
public static void storeUserId(Context applicationContext, JWT jwtToken) {
public static void storeUserId(Context applicationContext, String userId) {
SharedPreferences.Editor editor = getSharedPreferences(applicationContext).edit();
Claim role = jwtToken.getClaim("userid");
editor.putString("USER_ID", role.asString());
editor.putString("USER_ID", userId);
editor.commit();
}