Re-implement mixed provider (fix #145)

Этот коммит содержится в:
Anton Tananaev
2015-08-20 12:25:26 +12:00
родитель 1861d9ca34
Коммит be0d556ed6
16 изменённых файлов: 205 добавлений и 45 удалений
-5
Просмотреть файл
@@ -51,11 +51,6 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
addPreferencesFromResource(R.xml.preferences);
initPreferences();
// DELME
if (PreferenceManager.getDefaultSharedPreferences(this).getString(KEY_PROVIDER, null).equals("mixed")) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString(KEY_PROVIDER, LocationManager.GPS_PROVIDER).commit();
}
if (getPreferenceScreen().getSharedPreferences().getBoolean(KEY_STATUS, false)) {
setPreferencesEnabled(false);
startService(new Intent(this, TrackingService.class));
+117
Просмотреть файл
@@ -0,0 +1,117 @@
/*
* 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.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class MixedPositionProvider extends PositionProvider implements LocationListener, GpsStatus.Listener {
private static int FIX_TIMEOUT = 30 * 1000;
private LocationListener backupListener;
private long lastFixTime;
public MixedPositionProvider(Context context, PositionListener listener) {
super(context, listener);
}
public void startUpdates() {
lastFixTime = System.currentTimeMillis();
locationManager.addGpsStatusListener(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, period, 0, this);
}
public void stopUpdates() {
locationManager.removeUpdates(this);
locationManager.removeGpsStatusListener(this);
stopBackupProvider();
}
private void startBackupProvider() {
Log.i(TAG, "backup provider start");
if (backupListener == null) {
backupListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "backup provider location");
updateLocation(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, period, 0, backupListener);
}
}
private void stopBackupProvider() {
Log.i(TAG, "backup provider stop");
if (backupListener != null) {
locationManager.removeUpdates(backupListener);
backupListener = null;
}
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "provider location");
stopBackupProvider();
lastFixTime = System.currentTimeMillis();
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Log.i(TAG, "provider enabled");
stopBackupProvider();
}
@Override
public void onProviderDisabled(String provider) {
Log.i(TAG, "provider disabled");
startBackupProvider();
}
@Override
public void onGpsStatusChanged(int event) {
if (backupListener == null && System.currentTimeMillis() - (lastFixTime + period) > FIX_TIMEOUT) {
startBackupProvider();
}
}
}
+13 -38
Просмотреть файл
@@ -21,17 +21,15 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
public class PositionProvider implements LocationListener {
public abstract class PositionProvider {
private static final String TAG = PositionProvider.class.getSimpleName();
protected static final String TAG = PositionProvider.class.getSimpleName();
public interface PositionListener {
void onPositionUpdate(Position position);
@@ -41,11 +39,13 @@ public class PositionProvider implements LocationListener {
private final Context context;
private final SharedPreferences preferences;
private final LocationManager locationManager;
protected final LocationManager locationManager;
private String deviceId;
private String type;
private final long period;
protected String type;
protected final long period;
private long lastUpdateTime;
public PositionProvider(Context context, PositionListener listener) {
this.context = context;
@@ -58,49 +58,24 @@ public class PositionProvider implements LocationListener {
period = Integer.parseInt(preferences.getString(MainActivity.KEY_INTERVAL, null)) * 1000;
type = preferences.getString(MainActivity.KEY_PROVIDER, null);
if (!type.equals(LocationManager.NETWORK_PROVIDER)) {
type = LocationManager.GPS_PROVIDER;
}
}
public void startUpdates() {
locationManager.requestLocationUpdates(type, period, 0, this);
}
public abstract void startUpdates();
public void stopUpdates() {
locationManager.removeUpdates(this);
}
public abstract void stopUpdates();
private long lastTime;
@Override
public void onLocationChanged(Location location) {
if (location != null && location.getTime() != lastTime) {
protected void updateLocation(Location location) {
if (location != null && location.getTime() != lastUpdateTime) {
Log.i(TAG, "location new");
lastTime = location.getTime();
lastUpdateTime = location.getTime();
listener.onPositionUpdate(new Position(deviceId, location, getBatteryLevel()));
} else {
Log.i(TAG, location != null ? "location old" : "location nil");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG, "provider status");
}
@Override
public void onProviderEnabled(String provider) {
Log.i(TAG, "provider enabled");
}
@Override
public void onProviderDisabled(String provider) {
Log.i(TAG, "provider disabled");
}
@TargetApi(Build.VERSION_CODES.ECLAIR)
public double getBatteryLevel() {
private double getBatteryLevel() {
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.ECLAIR) {
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
+58
Просмотреть файл
@@ -0,0 +1,58 @@
/*
* 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.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class SimplePositionProvider extends PositionProvider implements LocationListener {
public SimplePositionProvider(Context context, PositionListener listener) {
super(context, listener);
if (!type.equals(LocationManager.NETWORK_PROVIDER)) {
type = LocationManager.GPS_PROVIDER;
}
}
public void startUpdates() {
locationManager.requestLocationUpdates(type, period, 0, this);
}
public void stopUpdates() {
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
+5 -1
Просмотреть файл
@@ -55,7 +55,11 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
this.context = context;
handler = new Handler();
preferences = PreferenceManager.getDefaultSharedPreferences(context);
positionProvider = new PositionProvider(context, this);
if (preferences.getString(MainActivity.KEY_PROVIDER, null).equals("mixed")) {
positionProvider = new MixedPositionProvider(context, this);
} else {
positionProvider = new SimplePositionProvider(context, this);
}
databaseHelper = new DatabaseHelper(context);
networkManager = new NetworkManager(context, this);
isOnline = networkManager.isOnline();
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>GPS</item>
<item>Mobilní síť</item>
<item>Obojí</item>
</string-array>
<string name="settings_foreground_title">Popředí služba</string>
<string name="settings_foreground_summary">Zvýšit prioritu služba</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>GPS udbyder</item>
<item>Netværks udbyder</item>
<item>Mixet udbyder</item>
</string-array>
<string name="settings_foreground_title">Forgrunden tjeneste</string>
<string name="settings_foreground_summary">Øg tjeneste prioritet</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>GPS</item>
<item>Netzwerk</item>
<item>Gemischt</item>
</string-array>
<string name="settings_foreground_title">Vordergrund service</string>
<string name="settings_foreground_summary">Erhöhen service priorität</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>Proveedor GPS</item>
<item>Proveedor de red</item>
<item>Proveedor mixto</item>
</string-array>
<string name="settings_foreground_title">Servicio de primer plano</string>
<string name="settings_foreground_summary">Aumentar prioridad de servicio</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>Mode GPS uniquement</item>
<item>Mode cellulaire uniquement</item>
<item>Mode hybride</item>
</string-array>
<string name="settings_foreground_title">Un service de premier plan</string>
<string name="settings_foreground_summary">Augmenter la priorité de service</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>GPS-lokalisering</item>
<item>Nettverkslokalisering</item>
<item>Blandet lokalisering</item>
</string-array>
<string name="settings_foreground_title">Forgrunnen tjeneste</string>
<string name="settings_foreground_summary">Øk tjeneste prioritet</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>Provedor GPS</item>
<item>Provedor Rede</item>
<item>Provedor Misto</item>
</string-array>
<string name="settings_foreground_title">Serviço primeiro plano</string>
<string name="settings_foreground_summary">Aumentar a prioridade de serviço</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>GPS провайдер</item>
<item>Сетевой провайдер</item>
<item>Смешанный провайдер</item>
</string-array>
<string name="settings_foreground_title">Приоритетный сервис</string>
<string name="settings_foreground_summary">Увеличить приоритет сервиса</string>
+2 -1
Просмотреть файл
@@ -21,9 +21,10 @@
<string-array name="settings_provider_names">
<item>GPS provajder</item>
<item>Provajder Mreže</item>
<item>Oba zajedno provajdera</item>
</string-array>
<string name="settings_foreground_title">Prvi plan servis</string>
<string name="settings_foreground_summary">Povecanje usluga prioritet</string>
<string name="settings_foreground_summary">Povecanje usluga prioritet</string>
<string name="menu_status">Status</string>
<string name="menu_about">O klijentu</string>
+1
Просмотреть файл
@@ -21,6 +21,7 @@
<string-array name="settings_provider_names">
<item>GPS provider</item>
<item>Network provider</item>
<item>Mixed provider</item>
</string-array>
<string name="settings_foreground_title">Foreground service</string>
<string name="settings_foreground_summary">Increase service priority</string>
+1
Просмотреть файл
@@ -4,6 +4,7 @@
<string-array name="settings_provider_values" translatable="false">
<item>gps</item>
<item>network</item>
<item>mixed</item>
</string-array>
</resources>