Initial release
Этот коммит содержится в:
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import android.os.Handler;
|
||||
import android.content.Intent;
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* Connection with autoreconnect
|
||||
*/
|
||||
public class Connection {
|
||||
|
||||
private String address;
|
||||
private int port;
|
||||
|
||||
private String connectMessage;
|
||||
|
||||
private Socket socket;
|
||||
private OutputStream socketStream;
|
||||
|
||||
private String status;
|
||||
|
||||
private Context context;
|
||||
|
||||
/**
|
||||
* Initialize connection parameters
|
||||
*/
|
||||
public Connection(String address, int port, String message) {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
connectMessage = message;
|
||||
setStatus(Traccar.STATUS_DISCONNECTED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get address
|
||||
*/
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get port
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set context
|
||||
*/
|
||||
public void setContext(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection status
|
||||
*/
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish connection
|
||||
*/
|
||||
public void connect() {
|
||||
try {
|
||||
socket = new Socket(address, port);
|
||||
socket.setSoTimeout(Traccar.SOCKET_TIMEOUT);
|
||||
socketStream = socket.getOutputStream();
|
||||
setStatus(Traccar.STATUS_CONNECTED);
|
||||
send(connectMessage);
|
||||
} catch (Exception e) {
|
||||
reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message
|
||||
*/
|
||||
public void send(String message) {
|
||||
try {
|
||||
if (socketStream != null) {
|
||||
socketStream.write(message.getBytes());
|
||||
socketStream.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close connection
|
||||
*/
|
||||
public void close() {
|
||||
handler.removeCallbacks(task);
|
||||
if (socket != null) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
socket = null;
|
||||
}
|
||||
socketStream = null;
|
||||
setStatus(Traccar.STATUS_DISCONNECTED);
|
||||
}
|
||||
|
||||
private void reconnect() {
|
||||
close();
|
||||
handler.postDelayed(task, Traccar.RECONNECT_DELAY);
|
||||
}
|
||||
|
||||
private Handler handler = new Handler();
|
||||
private ReconnectTask task = new ReconnectTask();
|
||||
|
||||
class ReconnectTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import java.util.Formatter;
|
||||
import android.location.Location;
|
||||
|
||||
/**
|
||||
* Protocol formatting
|
||||
*/
|
||||
public class Protocol {
|
||||
|
||||
/**
|
||||
* Format device id message
|
||||
*/
|
||||
public static String createId(String id) {
|
||||
StringBuilder s = new StringBuilder("$PGID,");
|
||||
Formatter f = new Formatter(s);
|
||||
|
||||
s.append(id);
|
||||
|
||||
byte checksum = 0;
|
||||
for (byte b : s.substring(1).getBytes()) {
|
||||
checksum ^= b;
|
||||
}
|
||||
f.format("*%02x\r\n", (int) checksum);
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format location message
|
||||
*/
|
||||
public static String createNmea(Location l) {
|
||||
StringBuilder s = new StringBuilder("$GPRMC,");
|
||||
Formatter f = new Formatter(s);
|
||||
|
||||
f.format("%1$tH%1$tM%1$tS.%1$tL,A,", l.getTime());
|
||||
|
||||
double lat = l.getLatitude();
|
||||
double lon = l.getLongitude();
|
||||
f.format("%02d%07.4f,%c,", (int) Math.abs(lat), Math.abs(lat) % 1 * 60, lat < 0 ? 'S' : 'N' );
|
||||
f.format("%03d%07.4f,%c,", (int) Math.abs(lon), Math.abs(lon) % 1 * 60, lon < 0 ? 'W' : 'E' );
|
||||
|
||||
f.format("%.2f,%.2f,", l.getSpeed(), l.getBearing());
|
||||
f.format("%1$td%1$tm%1$ty,,", l.getTime());
|
||||
|
||||
byte checksum = 0;
|
||||
for (byte b : s.substring(1).getBytes()) {
|
||||
checksum ^= b;
|
||||
}
|
||||
f.format("*%02x\r\n", (int) checksum);
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import java.util.*;
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.text.*;
|
||||
import android.text.method.*;
|
||||
import android.text.util.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
/**
|
||||
* Global parameters
|
||||
*/
|
||||
class Traccar {
|
||||
public static final String EXTRA_ADDRESS = "address";
|
||||
public static final String EXTRA_PORT = "port";
|
||||
public static final String EXTRA_PERIOD = "period";
|
||||
public static final String EXTRA_STATUS = "status";
|
||||
|
||||
public static final String DEFAULT_ADDRESS = "193.193.165.166";
|
||||
public static final String DEFAULT_PORT = "20100";
|
||||
public static final String DEFAULT_PERIOD = "60";
|
||||
|
||||
public static final String TRACCAR_SETTINGS = "traccar_settings";
|
||||
|
||||
public static final long RECONNECT_DELAY = 10 * 1000;
|
||||
public static final int SOCKET_TIMEOUT = 2000;
|
||||
|
||||
public static final String MSG_UPDATE = "org.traccar.client.MSG_UPDATE";
|
||||
public static final String MSG_SERVICE_CONFIG = "org.traccar.client.MSG_SERVICE_CONFIG";
|
||||
public static final String MSG_SERVICE_STATUS = "org.traccar.client.SERVICE_STATUS";
|
||||
public static final String MSG_CONNECTION_STATUS = "org.traccar.client.CONNECTION_STATUS";
|
||||
|
||||
public static final String STATUS_STOPPED = "Stopped";
|
||||
public static final String STATUS_RUNNING = "Running";
|
||||
|
||||
public static final String STATUS_CONNECTED = "Connected";
|
||||
public static final String STATUS_DISCONNECTED = "Disconnected";
|
||||
}
|
||||
|
||||
/**
|
||||
* Main user interface
|
||||
*/
|
||||
public class TraccarActivity extends Activity {
|
||||
|
||||
private EditText editAddress;
|
||||
private EditText editPort;
|
||||
private EditText editPeriod;
|
||||
private EditText editService;
|
||||
private EditText editConnection;
|
||||
private ToggleButton button;
|
||||
|
||||
public boolean isServiceRunning() {
|
||||
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
|
||||
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
|
||||
if (runningServiceInfo.service.getClassName().equals(TraccarService.class.getName())){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main);
|
||||
|
||||
// Bind elements
|
||||
editAddress = (EditText) findViewById(R.id.edit_address);
|
||||
editPort = (EditText) findViewById(R.id.edit_port);
|
||||
editPeriod = (EditText) findViewById(R.id.edit_period);
|
||||
editService = (EditText) findViewById(R.id.edit_service);
|
||||
editConnection = (EditText) findViewById(R.id.edit_connection);
|
||||
button = (ToggleButton) findViewById(R.id.button);
|
||||
|
||||
editService.setText(Traccar.STATUS_STOPPED);
|
||||
editConnection.setText(Traccar.STATUS_DISCONNECTED);
|
||||
button.setChecked(isServiceRunning());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
//super.onCreateOptionsMenu(menu);
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.main, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if(item.getItemId() == R.id.about){
|
||||
showAboutDialog();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showAboutDialog() {
|
||||
TextView message = new TextView(this);
|
||||
SpannableString str = new SpannableString(getString(R.string.about_text));
|
||||
Linkify.addLinks(str, Linkify.WEB_URLS);
|
||||
message.setText(str);
|
||||
message.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
int padding = (int) (10 * getResources().getDisplayMetrics().scaledDensity);
|
||||
message.setPadding(padding, padding, padding, padding);
|
||||
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
|
||||
dialog.setTitle(R.string.about_option);
|
||||
dialog.setView(message);
|
||||
dialog.setPositiveButton(R.string.about_close, null);
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private BroadcastReceiver serviceConfigReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
editAddress.setText(intent.getStringExtra(Traccar.EXTRA_ADDRESS));
|
||||
editPort.setText(String.valueOf(intent.getIntExtra(Traccar.EXTRA_PORT, 0)));
|
||||
editPeriod.setText(String.valueOf(intent.getIntExtra(Traccar.EXTRA_PERIOD, 0)));
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver serviceStatusReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
editService.setText(intent.getStringExtra(Traccar.EXTRA_STATUS));
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver connectionStatusReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
editConnection.setText(intent.getStringExtra(Traccar.EXTRA_STATUS));
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
loadSettings();
|
||||
|
||||
// Register receivers
|
||||
registerReceiver(serviceConfigReceiver, new IntentFilter(Traccar.MSG_SERVICE_CONFIG));
|
||||
registerReceiver(serviceStatusReceiver, new IntentFilter(Traccar.MSG_SERVICE_STATUS));
|
||||
registerReceiver(connectionStatusReceiver, new IntentFilter(Traccar.MSG_CONNECTION_STATUS));
|
||||
|
||||
// Request update
|
||||
sendBroadcast(new Intent(Traccar.MSG_UPDATE));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
saveSettings();
|
||||
|
||||
// Unregister receivers
|
||||
unregisterReceiver(serviceConfigReceiver);
|
||||
unregisterReceiver(serviceStatusReceiver);
|
||||
unregisterReceiver(connectionStatusReceiver);
|
||||
}
|
||||
|
||||
public void onButtonClick(View v) {
|
||||
if (button.isChecked()) {
|
||||
try {
|
||||
Intent serviceIntent = new Intent(this, TraccarService.class);
|
||||
serviceIntent.putExtra(Traccar.EXTRA_ADDRESS, editAddress.getText().toString());
|
||||
serviceIntent.putExtra(Traccar.EXTRA_PORT, Integer.parseInt(editPort.getText().toString()));
|
||||
serviceIntent.putExtra(Traccar.EXTRA_PERIOD, Integer.parseInt(editPeriod.getText().toString()));
|
||||
startService(serviceIntent);
|
||||
} catch (NumberFormatException e) {
|
||||
Toast.makeText(this, R.string.error_input, Toast.LENGTH_SHORT).show();
|
||||
button.setChecked(false);
|
||||
}
|
||||
} else {
|
||||
stopService(new Intent(this, TraccarService.class));
|
||||
}
|
||||
}
|
||||
|
||||
public void saveSettings() {
|
||||
SharedPreferences settings = getSharedPreferences(Traccar.TRACCAR_SETTINGS, 0);
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putString(Traccar.EXTRA_ADDRESS, editAddress.getText().toString());
|
||||
editor.putString(Traccar.EXTRA_PORT, editPort.getText().toString());
|
||||
editor.putString(Traccar.EXTRA_PERIOD, editPeriod.getText().toString());
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
public void loadSettings() {
|
||||
SharedPreferences settings = getSharedPreferences(Traccar.TRACCAR_SETTINGS, 0);
|
||||
editAddress.setText(settings.getString(Traccar.EXTRA_ADDRESS, Traccar.DEFAULT_ADDRESS));
|
||||
editPort.setText(settings.getString(Traccar.EXTRA_PORT, Traccar.DEFAULT_PORT));
|
||||
editPeriod.setText(settings.getString(Traccar.EXTRA_PERIOD, Traccar.DEFAULT_PERIOD));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
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.telephony.TelephonyManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* Background service
|
||||
*/
|
||||
public class TraccarService extends Service implements LocationListener {
|
||||
|
||||
private int period;
|
||||
private String deviceId;
|
||||
private Connection connection;
|
||||
private LocationManager locationManager;
|
||||
|
||||
/*
|
||||
* Location methods
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
connection.send(Protocol.createNmea(location));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
}
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Service methods
|
||||
*/
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// 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();
|
||||
|
||||
// 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);
|
||||
|
||||
// Report status
|
||||
registerReceiver(updateReceiver, new IntentFilter(Traccar.MSG_UPDATE));
|
||||
setStatus(Traccar.STATUS_RUNNING);
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
// Close connection
|
||||
connection.close();
|
||||
|
||||
// Stop location updates
|
||||
locationManager.removeUpdates(this);
|
||||
|
||||
// Report status
|
||||
unregisterReceiver(updateReceiver);
|
||||
setStatus(Traccar.STATUS_STOPPED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user