Create new tracking controller
Этот коммит содержится в:
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
|
public class TrackingController implements PositionProvider.PositionListener {
|
||||||
|
|
||||||
|
private Context context;
|
||||||
|
private SharedPreferences preferences;
|
||||||
|
|
||||||
|
private PositionProvider positionProvider;
|
||||||
|
|
||||||
|
public TrackingController(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
positionProvider = new PositionProvider(context, this,
|
||||||
|
preferences.getString(MainActivity.KEY_ID, null),
|
||||||
|
preferences.getString(MainActivity.KEY_PROVIDER, null),
|
||||||
|
Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
positionProvider.startUpdates();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
positionProvider.stopUpdates();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPositionUpdate(Position position) {
|
||||||
|
String request = ProtocolFormatter.formatRequest(
|
||||||
|
preferences.getString(MainActivity.KEY_ADDRESS, null),
|
||||||
|
Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)),
|
||||||
|
position.getId(), position.location, position.getBattery());
|
||||||
|
|
||||||
|
RequestManager.sendRequest(request, new RequestManager.RequestHandler() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure() {
|
||||||
|
StatusActivity.addMessage(context.getString(R.string.status_send_fail));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -18,17 +18,14 @@ package org.traccar.client;
|
|||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.os.PowerManager.WakeLock;
|
import android.os.PowerManager.WakeLock;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
|
|
||||||
public class TrackingService extends Service implements PositionProvider.PositionListener {
|
public class TrackingService extends Service {
|
||||||
|
|
||||||
|
private TrackingController trackingController;
|
||||||
|
|
||||||
private ClientController clientController;
|
|
||||||
private PositionProvider positionProvider;
|
|
||||||
|
|
||||||
private WakeLock wakeLock;
|
private WakeLock wakeLock;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -39,20 +36,8 @@ public class TrackingService extends Service implements PositionProvider.Positio
|
|||||||
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
||||||
wakeLock.acquire();
|
wakeLock.acquire();
|
||||||
|
|
||||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
trackingController = new TrackingController(this);
|
||||||
|
trackingController.start();
|
||||||
String id = sharedPreferences.getString(MainActivity.KEY_ID, null);
|
|
||||||
|
|
||||||
clientController = new ClientController(this,
|
|
||||||
sharedPreferences.getString(MainActivity.KEY_ADDRESS, null),
|
|
||||||
Integer.parseInt(sharedPreferences.getString(MainActivity.KEY_PORT, null)),
|
|
||||||
ProtocolFormatter.createLoginMessage(id));
|
|
||||||
clientController.start();
|
|
||||||
|
|
||||||
positionProvider = new PositionProvider(this, this, id,
|
|
||||||
sharedPreferences.getString(MainActivity.KEY_PROVIDER, null),
|
|
||||||
Integer.parseInt(sharedPreferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000);
|
|
||||||
positionProvider.startUpdates();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,23 +49,11 @@ public class TrackingService extends Service implements PositionProvider.Positio
|
|||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
StatusActivity.addMessage(getString(R.string.status_service_destroy));
|
StatusActivity.addMessage(getString(R.string.status_service_destroy));
|
||||||
|
|
||||||
if (positionProvider != null) {
|
if (trackingController != null) {
|
||||||
positionProvider.stopUpdates();
|
trackingController.stop();
|
||||||
}
|
|
||||||
|
|
||||||
if (clientController != null) {
|
|
||||||
clientController.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPositionUpdate(Position position) {
|
|
||||||
if (position != null) {
|
|
||||||
StatusActivity.addMessage(getString(R.string.status_location_update));
|
|
||||||
clientController.setNewLocation(ProtocolFormatter.createLocationMessage(position.location, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user