Re-implement status logging
Этот коммит содержится в:
@@ -23,8 +23,6 @@ import android.preference.PreferenceManager;
|
||||
|
||||
public class AutostartReceiver extends BroadcastReceiver {
|
||||
|
||||
public static final String LOG_TAG = "Traccar.AutostartReceiver";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
@@ -33,8 +33,6 @@ import android.view.MenuItem;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
|
||||
|
||||
public static final String LOG_TAG = "traccar";
|
||||
|
||||
public static final String KEY_DEVICE = "id";
|
||||
public static final String KEY_ADDRESS = "address";
|
||||
public static final String KEY_PORT = "port";
|
||||
|
||||
@@ -21,15 +21,20 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.util.Log;
|
||||
|
||||
public class NetworkManager extends BroadcastReceiver {
|
||||
|
||||
private static final String TAG = NetworkManager.class.getSimpleName();
|
||||
|
||||
private Context context;
|
||||
private NetworkHandler handler;
|
||||
private ConnectivityManager connectivityManager;
|
||||
|
||||
public NetworkManager(Context context, NetworkHandler handler) {
|
||||
this.context = context;
|
||||
this.handler = handler;
|
||||
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
}
|
||||
|
||||
public interface NetworkHandler {
|
||||
@@ -37,8 +42,7 @@ public class NetworkManager extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
public boolean isOnline() {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
|
||||
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
|
||||
}
|
||||
|
||||
@@ -55,7 +59,9 @@ public class NetworkManager extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION) && handler != null) {
|
||||
handler.onNetworkUpdate(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false));
|
||||
boolean isOnline = isOnline();
|
||||
Log.i(TAG, "network " + (isOnline ? "on" : "off"));
|
||||
handler.onNetworkUpdate(isOnline);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,12 @@ import android.os.BatteryManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
public class PositionProvider {
|
||||
|
||||
private static final String TAG = PositionProvider.class.getSimpleName();
|
||||
|
||||
public static final String PROVIDER_MIXED = "mixed";
|
||||
public static final long PROVIDER_RETRY_PERIOD = 60 * 1000;
|
||||
|
||||
@@ -104,10 +107,12 @@ public class PositionProvider {
|
||||
}*/
|
||||
|
||||
if (location != null && location.getTime() != lastTime) {
|
||||
Log.i(TAG, "location new");
|
||||
lastTime = location.getTime();
|
||||
listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel()));
|
||||
return true;
|
||||
} else {
|
||||
Log.i(TAG, location != null ? "location old" : "location nil");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.Set;
|
||||
|
||||
import android.app.ListActivity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
@@ -43,7 +42,6 @@ public class StatusActivity extends ListActivity {
|
||||
}
|
||||
|
||||
public static void addMessage(String message) {
|
||||
Log.i(MainActivity.LOG_TAG, message);
|
||||
DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
|
||||
message = format.format(new Date()) + " - " + message;
|
||||
messages.add(message);
|
||||
|
||||
@@ -19,9 +19,11 @@ import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
public class TrackingController implements PositionProvider.PositionListener, NetworkManager.NetworkHandler {
|
||||
|
||||
private static final String TAG = TrackingController.class.getSimpleName();
|
||||
private static final int RETRY_DELAY = 30 * 1000;
|
||||
|
||||
private boolean isOnline;
|
||||
@@ -49,7 +51,9 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (isOnline) {
|
||||
read();
|
||||
}
|
||||
positionProvider.startUpdates();
|
||||
networkManager.start();
|
||||
}
|
||||
@@ -84,7 +88,19 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
// read -> send -> retry -> read -> send
|
||||
//
|
||||
|
||||
private void log(String action, Position position) {
|
||||
if (position != null) {
|
||||
action += " (" +
|
||||
"id:" + position.getId() +
|
||||
" time:" + position.getTime().getTime() / 1000 +
|
||||
" lat:" + position.getLatitude() +
|
||||
" lon:" + position.getLongitude() + ")";
|
||||
}
|
||||
Log.d(TAG, action);
|
||||
}
|
||||
|
||||
private void write(Position position) {
|
||||
log("write", position);
|
||||
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
||||
@Override
|
||||
public void onSuccess(Void result) {
|
||||
@@ -101,6 +117,7 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
}
|
||||
|
||||
private void read() {
|
||||
log("read", null);
|
||||
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
||||
@Override
|
||||
public void onSuccess(Position result) {
|
||||
@@ -119,6 +136,7 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
}
|
||||
|
||||
private void delete(Position position) {
|
||||
log("delete", position);
|
||||
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
||||
@Override
|
||||
public void onSuccess(Void result) {
|
||||
@@ -133,6 +151,7 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
}
|
||||
|
||||
private void send(final Position position) {
|
||||
log("send", position);
|
||||
String request = ProtocolFormatter.formatRequest(
|
||||
preferences.getString(MainActivity.KEY_ADDRESS, null),
|
||||
Integer.parseInt(preferences.getString(MainActivity.KEY_PORT, null)),
|
||||
@@ -153,11 +172,14 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
}
|
||||
|
||||
private void retry() {
|
||||
log("retry", null);
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isOnline) {
|
||||
read();
|
||||
}
|
||||
}
|
||||
}, RETRY_DELAY);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,15 +23,19 @@ import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.util.Log;
|
||||
|
||||
public class TrackingService extends Service {
|
||||
|
||||
private static final String TAG = TrackingService.class.getSimpleName();
|
||||
|
||||
private TrackingController trackingController;
|
||||
|
||||
private WakeLock wakeLock;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.i(TAG, "service create");
|
||||
StatusActivity.addMessage(getString(R.string.status_service_create));
|
||||
|
||||
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
@@ -55,6 +59,7 @@ public class TrackingService extends Service {
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.i(TAG, "service destroy");
|
||||
StatusActivity.addMessage(getString(R.string.status_service_destroy));
|
||||
|
||||
if (trackingController != null) {
|
||||
|
||||
Ссылка в новой задаче
Block a user