Implement connectivity listener
Этот коммит содержится в:
@@ -5,10 +5,11 @@
|
|||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
|
|
||||||
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:allowBackup="true" android:theme="@style/TraccarTheme">
|
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:allowBackup="true" android:theme="@style/TraccarTheme">
|
||||||
|
|
||||||
<activity android:name=".MainActivity">
|
<activity android:name=".MainActivity">
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
|
|
||||||
|
public class NetworkManager extends BroadcastReceiver {
|
||||||
|
|
||||||
|
private Context context;
|
||||||
|
private NetworkHandler handler;
|
||||||
|
|
||||||
|
public NetworkManager(Context context, NetworkHandler handler) {
|
||||||
|
this.context = context;
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface NetworkHandler {
|
||||||
|
void onNetworkUpdate(boolean isOnline);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOnline() {
|
||||||
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||||
|
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
IntentFilter filter = new IntentFilter();
|
||||||
|
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||||
|
context.registerReceiver(this, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
context.unregisterReceiver(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -20,10 +20,11 @@ import android.content.SharedPreferences;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
public class TrackingController implements PositionProvider.PositionListener {
|
public class TrackingController implements PositionProvider.PositionListener, NetworkManager.NetworkHandler {
|
||||||
|
|
||||||
private static final int RETRY_DELAY = 30 * 1000;
|
private static final int RETRY_DELAY = 30 * 1000;
|
||||||
|
|
||||||
|
private boolean isOnline;
|
||||||
private boolean isWaiting;
|
private boolean isWaiting;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
@@ -32,6 +33,7 @@ public class TrackingController implements PositionProvider.PositionListener {
|
|||||||
|
|
||||||
private PositionProvider positionProvider;
|
private PositionProvider positionProvider;
|
||||||
private DatabaseHelper databaseHelper;
|
private DatabaseHelper databaseHelper;
|
||||||
|
private NetworkManager networkManager;
|
||||||
|
|
||||||
public TrackingController(Context context) {
|
public TrackingController(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@@ -42,13 +44,18 @@ public class TrackingController implements PositionProvider.PositionListener {
|
|||||||
preferences.getString(MainActivity.KEY_PROVIDER, null),
|
preferences.getString(MainActivity.KEY_PROVIDER, null),
|
||||||
Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000);
|
Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000);
|
||||||
databaseHelper = new DatabaseHelper(context);
|
databaseHelper = new DatabaseHelper(context);
|
||||||
|
networkManager = new NetworkManager(context, this);
|
||||||
|
isOnline = networkManager.isOnline();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
read();
|
||||||
positionProvider.startUpdates();
|
positionProvider.startUpdates();
|
||||||
|
networkManager.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
networkManager.stop();
|
||||||
positionProvider.stopUpdates();
|
positionProvider.stopUpdates();
|
||||||
handler.removeCallbacksAndMessages(null);
|
handler.removeCallbacksAndMessages(null);
|
||||||
}
|
}
|
||||||
@@ -61,12 +68,38 @@ public class TrackingController implements PositionProvider.PositionListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNetworkUpdate(boolean isOnline) {
|
||||||
|
if (!this.isOnline && isOnline) {
|
||||||
|
read();
|
||||||
|
}
|
||||||
|
this.isOnline = isOnline;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// State transition examples:
|
||||||
//
|
//
|
||||||
// write -> read -> send -> delete -> read
|
// write -> read -> send -> delete -> read
|
||||||
//
|
//
|
||||||
// read -> send -> retry -> read -> send
|
// read -> send -> retry -> read -> send
|
||||||
//
|
//
|
||||||
|
|
||||||
|
private void write(Position position) {
|
||||||
|
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(Void result) {
|
||||||
|
if (isOnline && isWaiting) {
|
||||||
|
read();
|
||||||
|
isWaiting = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(RuntimeException error) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void read() {
|
private void read() {
|
||||||
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
||||||
@Override
|
@Override
|
||||||
@@ -85,22 +118,6 @@ public class TrackingController implements PositionProvider.PositionListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void write(Position position) {
|
|
||||||
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(Void result) {
|
|
||||||
if (isWaiting) {
|
|
||||||
read();
|
|
||||||
isWaiting = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(RuntimeException error) {
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void delete(Position position) {
|
private void delete(Position position) {
|
||||||
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user