Refactor position provider class

Этот коммит содержится в:
Anton Tananaev
2015-08-12 20:00:21 +12:00
родитель e4f167f17b
Коммит c008c5f65e
2 изменённых файлов: 28 добавлений и 25 удалений
+24 -9
Просмотреть файл
@@ -15,25 +15,26 @@
*/ */
package org.traccar.client; package org.traccar.client;
import java.util.Date; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.location.LocationProvider; import android.location.LocationProvider;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.widget.Toast;
public class PositionProvider { public class PositionProvider {
public static final String PROVIDER_MIXED = "mixed"; public static final String PROVIDER_MIXED = "mixed";
public static final long PERIOD_DELTA = 10 * 1000; public static final long PROVIDER_RETRY_PERIOD = 60 * 1000;
public static final long RETRY_PERIOD = 60 * 1000;
public interface PositionListener { public interface PositionListener {
public void onPositionUpdate(Location location); void onPositionUpdate(Position position);
} }
private final Handler handler; private final Handler handler;
@@ -41,16 +42,18 @@ public class PositionProvider {
private final long period; private final long period;
private final PositionListener listener; private final PositionListener listener;
private final Context context; private final Context context;
private String id;
private boolean useFine; private boolean useFine;
private boolean useCoarse; private boolean useCoarse;
public PositionProvider(Context context, String type, long period, PositionListener listener) { public PositionProvider(Context context, String type, long period, String id, PositionListener listener) {
handler = new Handler(context.getMainLooper()); handler = new Handler(context.getMainLooper());
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
this.period = period; this.period = period;
this.listener = listener; this.listener = listener;
this.context = context; this.context = context;
this.id = id;
// Determine providers // Determine providers
if (type.equals(PROVIDER_MIXED)) { if (type.equals(PROVIDER_MIXED)) {
@@ -102,7 +105,7 @@ public class PositionProvider {
if (location != null && location.getTime() != lastTime) { if (location != null && location.getTime() != lastTime) {
lastTime = location.getTime(); lastTime = location.getTime();
listener.onPositionUpdate(location); listener.onPositionUpdate(new Position(id, location, getBatteryLevel()));
return true; return true;
} else { } else {
return false; return false;
@@ -147,10 +150,22 @@ public class PositionProvider {
locationManager.removeUpdates(InternalLocationListener.this); locationManager.removeUpdates(InternalLocationListener.this);
locationManager.requestLocationUpdates(provider, period, 0, InternalLocationListener.this); locationManager.requestLocationUpdates(provider, period, 0, InternalLocationListener.this);
} }
}, RETRY_PERIOD); }, PROVIDER_RETRY_PERIOD);
} }
} }
} }
@TargetApi(Build.VERSION_CODES.ECLAIR)
public double getBatteryLevel() {
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR) {
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
return (level * 100.0) / scale;
} else {
return 0;
}
}
} }
+4 -16
Просмотреть файл
@@ -71,7 +71,7 @@ public class TrackingService extends Service implements PositionProvider.Positio
clientController = new ClientController(this, address, port, ProtocolFormatter.createLoginMessage(id)); clientController = new ClientController(this, address, port, ProtocolFormatter.createLoginMessage(id));
clientController.start(); clientController.start();
positionProvider = new PositionProvider(this, provider, interval * 1000, this); positionProvider = new PositionProvider(this, provider, interval * 1000, id, this);
positionProvider.startUpdates(); positionProvider.startUpdates();
} }
@@ -95,23 +95,11 @@ public class TrackingService extends Service implements PositionProvider.Positio
wakeLock.release(); wakeLock.release();
} }
@TargetApi(Build.VERSION_CODES.ECLAIR)
public double getBatteryLevel() {
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR) {
Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
return (level * 100.0) / scale;
} else {
return 0;
}
}
@Override @Override
public void onPositionUpdate(Location location) { public void onPositionUpdate(Position position) {
if (location != null) { if (position != null) {
StatusActivity.addMessage(getString(R.string.status_location_update)); StatusActivity.addMessage(getString(R.string.status_location_update));
clientController.setNewLocation(ProtocolFormatter.createLocationMessage(extended, location, getBatteryLevel())); clientController.setNewLocation(ProtocolFormatter.createLocationMessage(extended, position.location, 0));
} }
} }