added workmanager background location updates, but not sending to API

This commit is contained in:
Marcin Jedynski 2018-09-21 00:09:47 +02:00
parent 2f802ad64f
commit 3fd04b220d
4 changed files with 66 additions and 2 deletions

View File

@ -45,4 +45,7 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:3.11.0"
implementation "com.squareup.okhttp3:okhttp-urlconnection:3.10.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"
implementation "android.arch.work:work-runtime:1.0.0-alpha09"
implementation "com.google.android.gms:play-services-location:15.0.1"
}

View File

@ -9,7 +9,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application

View File

@ -20,8 +20,15 @@ import com.uam.wmi.findmytutor.R;
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.utils.LocationPushWorker;
import java.util.List;
import java.util.concurrent.TimeUnit;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import static java.util.concurrent.TimeUnit.SECONDS;
public class MainActivity extends AppCompatActivity {
@ -32,7 +39,7 @@ public class MainActivity extends AppCompatActivity {
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
private PeriodicWorkRequest localizationUpdateRequest;
private MapView mapView;
@ -110,6 +117,11 @@ public class MainActivity extends AppCompatActivity {
// CoordinateService service = ApiClient.getClient(getApplicationContext())
// .create(CoordinateService.class);
if(isTutor){
localizationUpdateRequest = new PeriodicWorkRequest.Builder(LocationPushWorker.class,60, SECONDS).build();
WorkManager.getInstance().enqueue(localizationUpdateRequest);
}
}

View File

@ -0,0 +1,49 @@
package com.uam.wmi.findmytutor.utils;
import android.location.Location;
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.CoordinateService;
import java.util.concurrent.Executor;
import androidx.work.Worker;
public class LocationPushWorker extends Worker {
private CoordinateService CoordinateService;
//private FusedLocationProviderClient fusedLocationClient;
LocationPushWorker(){
this.CoordinateService = ApiClient.getClient(getApplicationContext()).create(CoordinateService.class);
}
@NonNull
@Override
public Result doWork() {
//TODO add check if User agreed to sharing location, exception handling
try{
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
fusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
Log.d("LocationUpload", "Location updated: long -> " + location.getLongitude() + "latit - >"+ location.getLatitude());
}
}
});
}
catch(SecurityException e){
Log.d("LocationException","Location not sent due to perrmision exception ");
return Result.FAILURE;
}
return Result.SUCCESS;
}
}