Rename main app module
Этот коммит содержится в:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 android.app.Activity;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class AboutActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.about);
|
||||
|
||||
TextView title = (TextView) findViewById(R.id.title);
|
||||
try {
|
||||
title.setText(title.getText() + " " + getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName);
|
||||
} catch (NameNotFoundException e) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2013 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 android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
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);
|
||||
if (sharedPreferences.getBoolean(TraccarActivity.KEY_STATUS, false)) {
|
||||
context.startService(new Intent(context, TraccarService.class));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright 2013 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.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
public class ClientController implements Connection.ConnectionHandler {
|
||||
|
||||
public static final long RECONNECT_DELAY = 10 * 1000;
|
||||
|
||||
private Context context;
|
||||
|
||||
private Handler handler;
|
||||
private Queue<String> messageQueue;
|
||||
|
||||
private Connection connection;
|
||||
|
||||
private String address;
|
||||
private int port;
|
||||
private String loginMessage;
|
||||
|
||||
public ClientController(Context context, String address, int port, String loginMessage) {
|
||||
this.context = context;
|
||||
messageQueue = new LinkedList<String>();
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.loginMessage = loginMessage;
|
||||
}
|
||||
|
||||
private BroadcastReceiver connectivityListener = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_connectivity_change));
|
||||
/*if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
} else {
|
||||
reconnect();
|
||||
}*/
|
||||
}
|
||||
};
|
||||
|
||||
public void start() {
|
||||
handler = new Handler();
|
||||
connection = new Connection(this);
|
||||
connection.connect(address, port);
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
context.registerReceiver(connectivityListener, filter);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
context.unregisterReceiver(connectivityListener);
|
||||
|
||||
connection.close();
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
private void reconnect() {
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
connection.close();
|
||||
connection = new Connection(this);
|
||||
connection.connect(address, port);
|
||||
}
|
||||
|
||||
private void delayedReconnect() {
|
||||
connection.close();
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
connection = new Connection(ClientController.this);
|
||||
connection.connect(address, port);
|
||||
}
|
||||
}, RECONNECT_DELAY);
|
||||
}
|
||||
|
||||
public void setNewServer(String address, int port) {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
reconnect();
|
||||
}
|
||||
|
||||
public void setNewLogin(String loginMessage) {
|
||||
this.loginMessage = loginMessage;
|
||||
reconnect();
|
||||
}
|
||||
|
||||
public void setNewLocation(String locationMessage) {
|
||||
messageQueue.offer(locationMessage);
|
||||
if (!connection.isClosed() && !connection.isBusy()) {
|
||||
connection.send(messageQueue.poll());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(boolean result) {
|
||||
if (result) {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_connection_success));
|
||||
connection.send(loginMessage);
|
||||
} else {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_connection_fail));
|
||||
delayedReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSent(boolean result) {
|
||||
if (result) {
|
||||
if (!messageQueue.isEmpty()) {
|
||||
connection.send(messageQueue.poll());
|
||||
}
|
||||
} else {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_send_fail));
|
||||
delayedReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright 2012 - 2013 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.Closeable;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Asynchronous connection
|
||||
*
|
||||
* All methods should be called from UI thread only.
|
||||
*/
|
||||
public class Connection implements Closeable {
|
||||
|
||||
public static final String LOG_TAG = "Traccar.Connection";
|
||||
public static final int SOCKET_TIMEOUT = 10 * 1000;
|
||||
|
||||
/**
|
||||
* Callback interface
|
||||
*/
|
||||
public interface ConnectionHandler {
|
||||
void onConnected(boolean result);
|
||||
void onSent(boolean result);
|
||||
}
|
||||
|
||||
private ConnectionHandler handler;
|
||||
|
||||
private Socket socket;
|
||||
private OutputStream socketStream;
|
||||
|
||||
private boolean closed;
|
||||
private boolean busy;
|
||||
|
||||
public boolean isClosed() {
|
||||
return closed;
|
||||
}
|
||||
|
||||
public boolean isBusy() {
|
||||
return busy;
|
||||
}
|
||||
|
||||
public Connection(ConnectionHandler handler) {
|
||||
this.handler = handler;
|
||||
closed = false;
|
||||
busy = false;
|
||||
}
|
||||
|
||||
public void connect(final String address, final int port) {
|
||||
busy = true;
|
||||
|
||||
new AsyncTask<Void, Void, Boolean>() {
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params) {
|
||||
try {
|
||||
socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(address, port));
|
||||
socket.setSoTimeout(SOCKET_TIMEOUT);
|
||||
socketStream = socket.getOutputStream();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Log.w(LOG_TAG, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
if (!closed) {
|
||||
busy = false;
|
||||
handler.onConnected(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
if (!closed) {
|
||||
busy = false;
|
||||
handler.onConnected(result);
|
||||
}
|
||||
}
|
||||
|
||||
}.execute();
|
||||
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
busy = true;
|
||||
|
||||
new AsyncTask<String, Void, Boolean>() {
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... params) {
|
||||
try {
|
||||
socketStream.write(params[0].getBytes());
|
||||
socketStream.flush();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Log.w(LOG_TAG, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
if (!closed) {
|
||||
busy = false;
|
||||
handler.onSent(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
if (!closed) {
|
||||
busy = false;
|
||||
handler.onSent(result);
|
||||
}
|
||||
}
|
||||
|
||||
}.execute(message);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
try {
|
||||
if (socketStream != null) {
|
||||
socketStream.close();
|
||||
}
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(LOG_TAG, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright 2013 - 2015 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.Date;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.location.LocationProvider;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class PositionProvider {
|
||||
|
||||
public static final String PROVIDER_MIXED = "mixed";
|
||||
public static final long PERIOD_DELTA = 10 * 1000;
|
||||
public static final long RETRY_PERIOD = 60 * 1000;
|
||||
|
||||
public interface PositionListener {
|
||||
public void onPositionUpdate(Location location);
|
||||
}
|
||||
|
||||
private final Handler handler;
|
||||
private final LocationManager locationManager;
|
||||
private final long period;
|
||||
private final PositionListener listener;
|
||||
private final Context context;
|
||||
|
||||
private boolean useFine;
|
||||
private boolean useCoarse;
|
||||
|
||||
public PositionProvider(Context context, String type, long period, PositionListener listener) {
|
||||
handler = new Handler(context.getMainLooper());
|
||||
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
this.period = period;
|
||||
this.listener = listener;
|
||||
this.context = context;
|
||||
|
||||
// Determine providers
|
||||
if (type.equals(PROVIDER_MIXED)) {
|
||||
useFine = true;
|
||||
useCoarse = true;
|
||||
} else if (type.equals(LocationManager.GPS_PROVIDER)) {
|
||||
useFine = true;
|
||||
} else if (type.equals(LocationManager.NETWORK_PROVIDER)) {
|
||||
useCoarse = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void startUpdates() {
|
||||
if (useFine) {
|
||||
try {
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, period, 0, fineLocationListener);
|
||||
} catch (Exception e) {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_provider_missing));
|
||||
}
|
||||
}
|
||||
if (useCoarse) {
|
||||
try {
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, period, 0, coarseLocationListener);
|
||||
} catch (Exception e) {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_provider_missing));
|
||||
}
|
||||
}
|
||||
handler.postDelayed(updateTask, period);
|
||||
}
|
||||
|
||||
public void stopUpdates() {
|
||||
handler.removeCallbacks(updateTask);
|
||||
locationManager.removeUpdates(fineLocationListener);
|
||||
locationManager.removeUpdates(coarseLocationListener);
|
||||
}
|
||||
|
||||
private final Runnable updateTask = new Runnable() {
|
||||
|
||||
private long lastTime;
|
||||
|
||||
private boolean tryProvider(String provider) {
|
||||
Location location = locationManager.getLastKnownLocation(provider);
|
||||
|
||||
/*if (location != null) {
|
||||
Toast.makeText(context, "phone: " + new Date() + "\ngps: " + new Date(location.getTime()), Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(context, "no location", Toast.LENGTH_LONG).show();
|
||||
}*/
|
||||
|
||||
if (location != null && location.getTime() != lastTime) {
|
||||
lastTime = location.getTime();
|
||||
listener.onPositionUpdate(location);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (useFine && tryProvider(LocationManager.GPS_PROVIDER)) {
|
||||
} else if (useCoarse && tryProvider(LocationManager.NETWORK_PROVIDER)) {
|
||||
} else {
|
||||
listener.onPositionUpdate(null);
|
||||
}
|
||||
handler.postDelayed(this, period);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private final InternalLocationListener fineLocationListener = new InternalLocationListener();
|
||||
private final InternalLocationListener coarseLocationListener = new InternalLocationListener();
|
||||
|
||||
private class InternalLocationListener implements LocationListener {
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(final String provider, int status, Bundle extras) {
|
||||
if (status == LocationProvider.TEMPORARILY_UNAVAILABLE || status == LocationProvider.OUT_OF_SERVICE) {
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
locationManager.removeUpdates(InternalLocationListener.this);
|
||||
locationManager.requestLocationUpdates(provider, period, 0, InternalLocationListener.this);
|
||||
}
|
||||
}, RETRY_PERIOD);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2012 - 2014 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.Calendar;
|
||||
import java.util.Formatter;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import android.location.Location;
|
||||
|
||||
/**
|
||||
* Protocol formatting
|
||||
*/
|
||||
public class Protocol {
|
||||
|
||||
/**
|
||||
* Format device id message
|
||||
*/
|
||||
public static String createLoginMessage(String id) {
|
||||
StringBuilder s = new StringBuilder("$PGID,");
|
||||
Formatter f = new Formatter(s, Locale.ENGLISH);
|
||||
|
||||
s.append(id);
|
||||
|
||||
byte checksum = 0;
|
||||
for (byte b : s.substring(1).getBytes()) {
|
||||
checksum ^= b;
|
||||
}
|
||||
f.format("*%02x\r\n", (int) checksum);
|
||||
f.close();
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format location message
|
||||
*/
|
||||
public static String createLocationMessage(boolean extended, Location l, double battery) {
|
||||
StringBuilder s = new StringBuilder(extended ? "$TRCCR," : "$GPRMC,");
|
||||
Formatter f = new Formatter(s, Locale.ENGLISH);
|
||||
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH);
|
||||
calendar.setTimeInMillis(l.getTime());
|
||||
|
||||
if (extended) {
|
||||
|
||||
f.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS.%1$tL,A,", calendar);
|
||||
|
||||
f.format("%.6f,%.6f,", l.getLatitude(), l.getLongitude());
|
||||
f.format("%.2f,%.2f,", l.getSpeed() * 1.943844, l.getBearing());
|
||||
f.format("%.2f,", l.getAltitude());
|
||||
f.format("%.0f,", battery);
|
||||
|
||||
} else {
|
||||
|
||||
f.format("%1$tH%1$tM%1$tS.%1$tL,A,", calendar);
|
||||
|
||||
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');
|
||||
|
||||
double speed = l.getSpeed() * 1.943844; // speed in knots
|
||||
f.format("%.2f,%.2f,", speed, l.getBearing());
|
||||
f.format("%1$td%1$tm%1$ty,,", calendar);
|
||||
|
||||
}
|
||||
|
||||
byte checksum = 0;
|
||||
for (byte b : s.substring(1).getBytes()) {
|
||||
checksum ^= b;
|
||||
}
|
||||
f.format("*%02x\r\n", (int) checksum);
|
||||
f.close();
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2015 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;
|
||||
|
||||
public class RequestManager {
|
||||
|
||||
public interface RequestHandler {
|
||||
void onSuccess();
|
||||
void onFailure();
|
||||
}
|
||||
|
||||
public static void sendRequest(String request, RequestHandler handler) {
|
||||
|
||||
handler.onSuccess();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2012 - 2013 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.text.DateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
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;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
||||
public class StatusActivity extends ListActivity {
|
||||
|
||||
private static final int LIMIT = 20;
|
||||
|
||||
private static final LinkedList<String> messages = new LinkedList<String>();
|
||||
private static final Set<ArrayAdapter<String>> adapters = new HashSet<ArrayAdapter<String>>();
|
||||
|
||||
private static void notifyAdapters() {
|
||||
for (ArrayAdapter<String> adapter : adapters) {
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addMessage(String message) {
|
||||
Log.i(TraccarActivity.LOG_TAG, message);
|
||||
DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
|
||||
message = format.format(new Date()) + " - " + message;
|
||||
messages.add(message);
|
||||
while (messages.size() > LIMIT) {
|
||||
messages.removeFirst();
|
||||
}
|
||||
notifyAdapters();
|
||||
}
|
||||
|
||||
public static void clearMessages() {
|
||||
messages.clear();
|
||||
notifyAdapters();
|
||||
}
|
||||
|
||||
private ArrayAdapter<String> adapter;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.status);
|
||||
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, messages);
|
||||
setListAdapter(adapter);
|
||||
adapters.add(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
adapters.remove(adapter);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.status, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == R.id.clear) {
|
||||
clearMessages();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2012 - 2014 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 android.app.ActivityManager;
|
||||
import android.app.ActivityManager.RunningServiceInfo;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
/**
|
||||
* Main user interface
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class TraccarActivity extends PreferenceActivity {
|
||||
|
||||
public static final String LOG_TAG = "traccar";
|
||||
|
||||
public static final String KEY_ID = "id";
|
||||
public static final String KEY_ADDRESS = "address";
|
||||
public static final String KEY_PORT = "port";
|
||||
public static final String KEY_INTERVAL = "interval";
|
||||
public static final String KEY_PROVIDER = "provider";
|
||||
public static final String KEY_EXTENDED = "extended";
|
||||
public static final String KEY_STATUS = "status";
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.preferences);
|
||||
initPreferences();
|
||||
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
|
||||
if (sharedPreferences.getBoolean(KEY_STATUS, false))
|
||||
startService(new Intent(this, TraccarService.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(
|
||||
preferenceChangeListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
|
||||
preferenceChangeListener);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener() {
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if (key.equals(KEY_STATUS)) {
|
||||
if (sharedPreferences.getBoolean(KEY_STATUS, false)) {
|
||||
startService(new Intent(TraccarActivity.this, TraccarService.class));
|
||||
} else {
|
||||
stopService(new Intent(TraccarActivity.this, TraccarService.class));
|
||||
}
|
||||
} else if (key.equals(KEY_ID)) {
|
||||
findPreference(KEY_ID).setSummary(sharedPreferences.getString(KEY_ID, null));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.main, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == R.id.status) {
|
||||
startActivity(new Intent(this, StatusActivity.class));
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.about) {
|
||||
startActivity(new Intent(this, AboutActivity.class));
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private boolean isServiceRunning() {
|
||||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
if (TraccarService.class.getName().equals(service.service.getClassName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initPreferences() {
|
||||
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
||||
|
||||
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
||||
String id = telephonyManager.getDeviceId();
|
||||
|
||||
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
|
||||
|
||||
if (!sharedPreferences.contains(KEY_ID)) {
|
||||
sharedPreferences.edit().putString(KEY_ID, id).commit();
|
||||
}
|
||||
findPreference(KEY_ID).setSummary(sharedPreferences.getString(KEY_ID, id));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright 2012 - 2014 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 android.annotation.TargetApi;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.location.Location;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Background service
|
||||
*/
|
||||
public class TraccarService extends Service {
|
||||
|
||||
public static final String LOG_TAG = "Traccar.TraccarService";
|
||||
|
||||
private String id;
|
||||
private String address;
|
||||
private int port;
|
||||
private int interval;
|
||||
private String provider;
|
||||
private boolean extended;
|
||||
|
||||
private SharedPreferences sharedPreferences;
|
||||
private ClientController clientController;
|
||||
private PositionProvider positionProvider;
|
||||
|
||||
private WakeLock wakeLock;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
StatusActivity.addMessage(getString(R.string.status_service_create));
|
||||
|
||||
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
||||
wakeLock.acquire();
|
||||
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
try {
|
||||
id = sharedPreferences.getString(TraccarActivity.KEY_ID, null);
|
||||
address = sharedPreferences.getString(TraccarActivity.KEY_ADDRESS, null);
|
||||
provider = sharedPreferences.getString(TraccarActivity.KEY_PROVIDER, null);
|
||||
port = Integer.valueOf(sharedPreferences.getString(TraccarActivity.KEY_PORT, null));
|
||||
interval = Integer.valueOf(sharedPreferences.getString(TraccarActivity.KEY_INTERVAL, null));
|
||||
extended = sharedPreferences.getBoolean(TraccarActivity.KEY_EXTENDED, false);
|
||||
} catch (Exception error) {
|
||||
Log.w(LOG_TAG, error);
|
||||
}
|
||||
|
||||
clientController = new ClientController(this, address, port, Protocol.createLoginMessage(id));
|
||||
clientController.start();
|
||||
|
||||
positionProvider = new PositionProvider(this, provider, interval * 1000, positionListener);
|
||||
positionProvider.startUpdates();
|
||||
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
StatusActivity.addMessage(getString(R.string.status_service_destroy));
|
||||
|
||||
if (sharedPreferences != null) {
|
||||
sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
|
||||
}
|
||||
|
||||
if (positionProvider != null) {
|
||||
positionProvider.stopUpdates();
|
||||
}
|
||||
|
||||
if (clientController != null) {
|
||||
clientController.stop();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private PositionProvider.PositionListener positionListener = new PositionProvider.PositionListener() {
|
||||
|
||||
@Override
|
||||
public void onPositionUpdate(Location location) {
|
||||
if (location != null) {
|
||||
StatusActivity.addMessage(getString(R.string.status_location_update));
|
||||
clientController.setNewLocation(Protocol.createLocationMessage(extended, location, getBatteryLevel()));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener() {
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
StatusActivity.addMessage(getString(R.string.status_preference_update));
|
||||
try {
|
||||
if (key.equals(TraccarActivity.KEY_ADDRESS)) {
|
||||
|
||||
address = sharedPreferences.getString(TraccarActivity.KEY_ADDRESS, null);
|
||||
clientController.setNewServer(address, port);
|
||||
|
||||
} else if (key.equals(TraccarActivity.KEY_PORT)) {
|
||||
|
||||
port = Integer.valueOf(sharedPreferences.getString(TraccarActivity.KEY_PORT, null));
|
||||
clientController.setNewServer(address, port);
|
||||
|
||||
} else if (key.equals(TraccarActivity.KEY_INTERVAL)) {
|
||||
|
||||
interval = Integer.valueOf(sharedPreferences.getString(TraccarActivity.KEY_INTERVAL, null));
|
||||
positionProvider.stopUpdates();
|
||||
positionProvider = new PositionProvider(TraccarService.this, provider, interval * 1000, positionListener);
|
||||
positionProvider.startUpdates();
|
||||
|
||||
} else if (key.equals(TraccarActivity.KEY_ID)) {
|
||||
|
||||
id = sharedPreferences.getString(TraccarActivity.KEY_ID, null);
|
||||
clientController.setNewLogin(Protocol.createLoginMessage(id));
|
||||
|
||||
} else if (key.equals(TraccarActivity.KEY_PROVIDER)) {
|
||||
|
||||
provider = sharedPreferences.getString(TraccarActivity.KEY_PROVIDER, null);
|
||||
positionProvider.stopUpdates();
|
||||
positionProvider = new PositionProvider(TraccarService.this, provider, interval * 1000, positionListener);
|
||||
positionProvider.startUpdates();
|
||||
|
||||
} else if (key.equals(TraccarActivity.KEY_EXTENDED)) {
|
||||
|
||||
extended = sharedPreferences.getBoolean(TraccarActivity.KEY_EXTENDED, false);
|
||||
|
||||
}
|
||||
} catch (Exception error) {
|
||||
Log.w(LOG_TAG, error);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
Ссылка в новой задаче
Block a user