Small changes and warning cleanup
Этот коммит содержится в:
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import android.os.Handler;
|
||||
import android.content.Intent;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
|
||||
/**
|
||||
* Connection with autoreconnect
|
||||
@@ -29,9 +29,9 @@ public class Connection {
|
||||
|
||||
private String address;
|
||||
private int port;
|
||||
|
||||
|
||||
private String connectMessage;
|
||||
|
||||
|
||||
private Socket socket;
|
||||
private OutputStream socketStream;
|
||||
|
||||
@@ -43,54 +43,54 @@ public class Connection {
|
||||
* Initialize connection parameters
|
||||
*/
|
||||
public Connection(String address, int port, String message) {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
connectMessage = message;
|
||||
setStatus(Traccar.STATUS_DISCONNECTED);
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
connectMessage = message;
|
||||
setStatus(Traccar.STATUS_DISCONNECTED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get address
|
||||
*/
|
||||
public String getAddress() {
|
||||
return address;
|
||||
return address;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get port
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set context
|
||||
*/
|
||||
public void setContext(Context context) {
|
||||
this.context = context;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set connection status
|
||||
*/
|
||||
private void setStatus(String status) {
|
||||
if (!status.equals(this.status)) {
|
||||
this.status = status;
|
||||
if (context != null) {
|
||||
Intent intent = new Intent(Traccar.MSG_CONNECTION_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, status);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
if (!status.equals(this.status)) {
|
||||
this.status = status;
|
||||
if (context != null) {
|
||||
Intent intent = new Intent(Traccar.MSG_CONNECTION_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, status);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get connection status
|
||||
*/
|
||||
public String getStatus() {
|
||||
return status;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Establish connection
|
||||
*/
|
||||
@@ -105,7 +105,7 @@ public class Connection {
|
||||
reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send message
|
||||
*/
|
||||
@@ -124,7 +124,7 @@ public class Connection {
|
||||
* Close connection
|
||||
*/
|
||||
public void close() {
|
||||
handler.removeCallbacks(task);
|
||||
handler.removeCallbacks(task);
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
@@ -137,10 +137,10 @@ public class Connection {
|
||||
}
|
||||
|
||||
private void reconnect() {
|
||||
close();
|
||||
close();
|
||||
handler.postDelayed(task, Traccar.RECONNECT_DELAY);
|
||||
}
|
||||
|
||||
|
||||
private Handler handler = new Handler();
|
||||
private ReconnectTask task = new ReconnectTask();
|
||||
|
||||
|
||||
@@ -17,11 +17,12 @@ package org.traccar.client;
|
||||
|
||||
import java.util.Formatter;
|
||||
import java.util.Locale;
|
||||
|
||||
import android.location.Location;
|
||||
|
||||
/**
|
||||
* Protocol formatting
|
||||
*/
|
||||
* Protocol formatting
|
||||
*/
|
||||
public class Protocol {
|
||||
|
||||
/**
|
||||
@@ -38,6 +39,7 @@ public class Protocol {
|
||||
checksum ^= b;
|
||||
}
|
||||
f.format("*%02x\r\n", (int) checksum);
|
||||
f.close();
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
@@ -64,6 +66,7 @@ public class Protocol {
|
||||
checksum ^= b;
|
||||
}
|
||||
f.format("*%02x\r\n", (int) checksum);
|
||||
f.close();
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
@@ -15,23 +15,25 @@
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import android.app.Service;
|
||||
import android.content.*;
|
||||
import android.location.*;
|
||||
import android.os.*;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* Background service
|
||||
*/
|
||||
public class TraccarService extends Service implements LocationListener {
|
||||
|
||||
private int period;
|
||||
private String deviceId;
|
||||
private int period;
|
||||
private String deviceId;
|
||||
private Connection connection;
|
||||
private LocationManager locationManager;
|
||||
|
||||
@@ -57,61 +59,62 @@ public class TraccarService extends Service implements LocationListener {
|
||||
}
|
||||
|
||||
/*
|
||||
* Status methods
|
||||
* Status methods
|
||||
*/
|
||||
|
||||
private String status;
|
||||
|
||||
|
||||
private void setStatus(String status) {
|
||||
if (!status.equals(this.status)) {
|
||||
this.status = status;
|
||||
Intent intent = new Intent(Traccar.MSG_SERVICE_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, status);
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
if (!status.equals(this.status)) {
|
||||
this.status = status;
|
||||
Intent intent = new Intent(Traccar.MSG_SERVICE_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, status);
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
|
||||
private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// Send config
|
||||
intent = new Intent(Traccar.MSG_SERVICE_CONFIG);
|
||||
intent.putExtra(Traccar.EXTRA_ADDRESS, connection.getAddress());
|
||||
intent.putExtra(Traccar.EXTRA_PORT, connection.getPort());
|
||||
intent.putExtra(Traccar.EXTRA_PERIOD, period);
|
||||
sendBroadcast(intent);
|
||||
|
||||
// Send service status
|
||||
intent = new Intent(Traccar.MSG_SERVICE_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, status);
|
||||
sendBroadcast(intent);
|
||||
|
||||
// Send connection status
|
||||
intent = new Intent(Traccar.MSG_CONNECTION_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, connection.getStatus());
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// Send config
|
||||
intent = new Intent(Traccar.MSG_SERVICE_CONFIG);
|
||||
intent.putExtra(Traccar.EXTRA_ADDRESS, connection.getAddress());
|
||||
intent.putExtra(Traccar.EXTRA_PORT, connection.getPort());
|
||||
intent.putExtra(Traccar.EXTRA_PERIOD, period);
|
||||
sendBroadcast(intent);
|
||||
|
||||
// Send service status
|
||||
intent = new Intent(Traccar.MSG_SERVICE_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, status);
|
||||
sendBroadcast(intent);
|
||||
|
||||
// Send connection status
|
||||
intent = new Intent(Traccar.MSG_CONNECTION_STATUS);
|
||||
intent.putExtra(Traccar.EXTRA_STATUS, connection.getStatus());
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Service methods
|
||||
*/
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// Read device id
|
||||
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||
// Read device id
|
||||
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||
deviceId = telephonyManager.getDeviceId();
|
||||
|
||||
// Create connection
|
||||
connection = new Connection(
|
||||
intent.getStringExtra(Traccar.EXTRA_ADDRESS),
|
||||
intent.getIntExtra(Traccar.EXTRA_PORT, 0),
|
||||
Protocol.createId(deviceId));
|
||||
connection.setContext(this);
|
||||
connection.connect();
|
||||
// Create connection
|
||||
connection = new Connection(
|
||||
intent.getStringExtra(Traccar.EXTRA_ADDRESS),
|
||||
intent.getIntExtra(Traccar.EXTRA_PORT, 0),
|
||||
Protocol.createId(deviceId));
|
||||
connection.setContext(this);
|
||||
connection.connect();
|
||||
|
||||
// Request location updates
|
||||
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
// Request location updates
|
||||
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
period = intent.getIntExtra(Traccar.EXTRA_PERIOD, 0);
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, period * 1000, 0, this);
|
||||
|
||||
@@ -123,15 +126,15 @@ public class TraccarService extends Service implements LocationListener {
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
// Close connection
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
// Stop location updates
|
||||
if (locationManager != null) {
|
||||
locationManager.removeUpdates(this);
|
||||
}
|
||||
// Close connection
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
// Stop location updates
|
||||
if (locationManager != null) {
|
||||
locationManager.removeUpdates(this);
|
||||
}
|
||||
|
||||
// Report status
|
||||
unregisterReceiver(updateReceiver);
|
||||
|
||||
Ссылка в новой задаче
Block a user