Compare commits

...

1 Commits

Author SHA1 Message Date
49b914a0b1 Add foreground execution 2018-10-09 00:40:42 +02:00
6 changed files with 238 additions and 314 deletions

View File

@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature
android:name="android.hardware.location"
@ -27,7 +29,8 @@
<receiver android:name=".utils.BroadcastLocalizationHandler"
android:enabled="true"
android:exported="false"
android:exported="true"
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="background.location.broadcast">
@ -35,8 +38,6 @@
</intent-filter>
</receiver>
<activity android:name=".activity.StartupActivity"
android:label="@string/title_activity_startup"
android:launchMode="singleInstance">
@ -61,7 +62,11 @@
</activity>
<service
android:name=".service.BackgroundLocalizationService"/>
android:name=".service.BackgroundLocalizationService"
android:launchMode="singleTop"
android:enabled="true"
android:exported="false"
/>
</application>

View File

@ -47,6 +47,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
import static android.Manifest.permission.READ_CONTACTS;
@ -367,7 +368,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
@Override
public void onError(Throwable e) {
Log.e("LoginError", "onError: " + e.getMessage());
Timber.e("onError: %s", e.getMessage());
}
}));
return true;

View File

@ -5,6 +5,7 @@ import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
@ -12,27 +13,25 @@ import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
@ -54,8 +53,8 @@ public class MainActivity extends AppCompatActivity {
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
mMainFrame = findViewById(R.id.main_frame);
mMainNav = findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
@ -95,11 +94,12 @@ public class MainActivity extends AppCompatActivity {
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext());
unRegisterLocalizationService();
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
unRegisterLocalizationService();
finish();
startActivity(i);
@ -131,10 +131,15 @@ public class MainActivity extends AppCompatActivity {
} else {
if (isTutor) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//startForegroundService(intent);
ContextCompat.startForegroundService(getApplicationContext(),intent);
} else {
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
@ -142,7 +147,7 @@ public class MainActivity extends AppCompatActivity {
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
@ -187,7 +192,7 @@ public class MainActivity extends AppCompatActivity {
Intent intent = new Intent(this, BackgroundLocalizationService.class);
stopService(intent);
} catch (IllegalArgumentException e) {
Log.d("Destroy app", "RECIEVER UNREGISTER ERROR");
Timber.d("RECIEVER UNREGISTER ERROR");
}
}
}

View File

@ -1,27 +1,48 @@
package com.uam.wmi.findmytutor.service;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.content.Context;
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.utils.PrefUtils;
import java.util.Timer;
import java.util.TimerTask;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class BackgroundLocalizationService extends Service {
public static String str_receiver = "background.location.broadcast";
private static final String TAG = "MyLocationService";
private static final String TAG = "background.location.broadcast";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 10000;
private static final float LOCATION_DISTANCE = 10f;
@ -33,6 +54,8 @@ public class BackgroundLocalizationService extends Service {
Location location;
double latitude, longitude;
// Constants
private static final int ID_SERVICE = 101;
boolean isGPSEnable = false;
@ -40,31 +63,31 @@ public class BackgroundLocalizationService extends Service {
private class LocationListener implements android.location.LocationListener {
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
LocationListener(String provider) {
Timber.e("LocationListener %s", provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
Timber.e("onLocationChanged: %s", location);
mLastLocation.set(location);
sendToBroadcast(mLastLocation);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
Timber.e("onProviderDisabled: %s", provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
Timber.e("onProviderEnabled: %s", provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
Timber.e("onStatusChanged: %s", provider);
}
}
@ -79,23 +102,95 @@ public class BackgroundLocalizationService extends Service {
return null;
}
private void startJob() throws InterruptedException {
//do job here
//job completed. Rest for 5 second before doing another one
try {
Thread.sleep(5000);
Log.e("loc","job") ;
fn_getlocation();
} catch (InterruptedException e) {
e.printStackTrace();
}
//do job again
startJob();
}
@SuppressLint("LongLogTag")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
Log.e(TAG,"onStartCommand");
super.onStartCommand(intent, flags, startId);
/*
Thread t = new Thread(() -> {
try {
startJob();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t.start();
*/
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder builder = new Notification.Builder(this, "FOREGROUND_SERVICE")
.setContentTitle(getString(R.string.app_name))
.setContentText("background")
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(101, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText("background")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(101, notification);
}
*/
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
.setContentText("Content").build();
startForeground(1001, notification);
Timber.e("onCreate");
initializeLocationManager();
@ -107,9 +202,9 @@ public class BackgroundLocalizationService extends Service {
mLocationListeners[0]
);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
Timber.i(ex, "fail to request location update, ignore");
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
Timber.d("network provider does not exist, %s", ex.getMessage());
}
try {
@ -120,29 +215,36 @@ public class BackgroundLocalizationService extends Service {
mLocationListeners[1]
);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
Timber.i(ex, "fail to request location update, ignore");
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
Timber.d("gps provider does not exist %s", ex.getMessage());
}
Timer mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
intent = new Intent(str_receiver);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
private void fn_getlocation() {
try {
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnable && !isNetworkEnable) {
} catch (Exception e) {
Timber.d("isProviderEnabled does not exist %s", e.getMessage());
}
if (isGPSEnable && isNetworkEnable) {
} else {
Log.e("loc","dzialam") ;
if (isGPSEnable) {
location = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
@ -156,13 +258,13 @@ public class BackgroundLocalizationService extends Service {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListeners[1]);
if (locationManager!=null){
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
fn_update(location);
sendToBroadcast(location);
}
}
}
@ -194,10 +296,10 @@ public class BackgroundLocalizationService extends Service {
return;
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location!=null){
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
fn_update(location);
sendToBroadcast(location);
}
}
@ -208,25 +310,28 @@ public class BackgroundLocalizationService extends Service {
}
private class TimerTaskToGetLocation extends TimerTask{
private class TimerTaskToGetLocation extends TimerTask {
@Override
public void run() {
mHandler.post(BackgroundLocalizationService.this::fn_getlocation);
}
}
private void fn_update(Location location){
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
}
/*
private void fn_update(Location location) {
intent.putExtra("latitude", location.getLatitude());
intent.putExtra("longitude", location.getLongitude());
sendToBroadcast(Location location);
}*/
private void sendToBroadcast(Location location) {
Log.e("sendToBroadcast", String.valueOf(location));
Timber.e(String.valueOf(location));
Log.e("err", String.valueOf(location));
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
intent.putExtra("latitude", location.getLatitude());
intent.putExtra("longitude", location.getLongitude());
new Task(location).execute();
// sendBroadcast(intent);
}
private void getLocation() {
@ -235,27 +340,80 @@ public class BackgroundLocalizationService extends Service {
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
Timber.e("onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
for (LocationListener mLocationListener : mLocationListeners) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
mLocationManager.removeUpdates(mLocationListener);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
Timber.i(ex, "fail to remove location listener, ignore");
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
Timber.e("initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
private class Task extends AsyncTask {
private Double latitude;
private Double longitude;
private CompositeDisposable disposable = new CompositeDisposable();
private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class);
private Task(Location location) {
this.latitude = location.getLatitude();
this.longitude = location.getLongitude();
}
@Override
protected Object doInBackground(Object[] objects) {
try {
Coordinate coordinate = new Coordinate(
this.latitude,
this.longitude,
PrefUtils.getUserStatus(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext())
);
disposable.add(
coordinateService
.postCoordinate(coordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() {
@SuppressLint("LongLogTag")
@Override
public void onSuccess(Coordinate coord) {
Log.e("CoordinateService onSuccess", String.valueOf(coord));
}
@Override
public void onError(Throwable e) {
Log.e("LoginError", "onError: " + e.getMessage());
}
}));
} catch (IllegalArgumentException e) {
Timber.e(String.valueOf(e));
}
return null;
}
}
}

View File

@ -1,229 +0,0 @@
package com.uam.wmi.findmytutor.service;
import android.Manifest;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.util.Timer;
import java.util.TimerTask;
public class GoogleService extends Service {
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
double latitude, longitude;
LocationManager locationManager;
Location location;
private Handler mHandler = new Handler();
private Timer mTimer = null;
long notify_interval = 50000;
public static String str_receiver = "background.location.broadcast";
Intent intent;
Location mLastLocation;
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
public GoogleService() {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
intent = new Intent(str_receiver);
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
.setContentText("Content").build();
startForeground(1001, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
private class LocationListener implements android.location.LocationListener {
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
sendToBroadcast(mLastLocation);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER),
new LocationListener(LocationManager.PASSIVE_PROVIDER)
};
private void fn_getlocation() {
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnable && !isNetworkEnable) {
} else {
if (isGPSEnable) {
location = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListeners[1]);
if (locationManager!=null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
fn_update(location);
}
}
}
if (isNetworkEnable) {
location = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mLocationListeners[0]);
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
fn_update(location);
}
}
}
}
}
private class TimerTaskToGetLocation extends TimerTask{
@Override
public void run() {
mHandler.post(GoogleService.this::fn_getlocation);
}
}
private void fn_update(Location location){
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
}
private void sendToBroadcast(Location location) {
Log.e("sendToBroadcast", String.valueOf(location));
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
}
private void getLocation() {
sendToBroadcast(mLastLocation);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
}
}
}
}
}

View File

@ -4,39 +4,21 @@ import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.support.v4.content.LocalBroadcastManager;
import android.util.ArrayMap;
import android.os.Build;
import android.util.Log;
import com.auth0.android.jwt.Claim;
import com.auth0.android.jwt.JWT;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.model.JwtToken;
import com.uam.wmi.findmytutor.model.LdapUser;
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 org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
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 timber.log.Timber;
import static android.content.ContentValues.TAG;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class BroadcastLocalizationHandler extends BroadcastReceiver {
@ -46,6 +28,8 @@ public class BroadcastLocalizationHandler extends BroadcastReceiver {
final PendingResult pendingResult = goAsync();
Task asyncTask = new Task(pendingResult, intent);
asyncTask.execute();
}
private static class Task extends AsyncTask {