Use fused location provider
Этот коммит содержится в:
@@ -58,7 +58,7 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
|
||||
public static final String KEY_INTERVAL = "interval";
|
||||
public static final String KEY_DISTANCE = "distance";
|
||||
public static final String KEY_ANGLE = "angle";
|
||||
public static final String KEY_PROVIDER = "provider";
|
||||
public static final String KEY_ACCURACY = "accuracy";
|
||||
public static final String KEY_STATUS = "status";
|
||||
|
||||
private static final int PERMISSIONS_REQUEST_LOCATION = 2;
|
||||
@@ -175,7 +175,7 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
|
||||
findPreference(KEY_INTERVAL).setEnabled(enabled);
|
||||
findPreference(KEY_DISTANCE).setEnabled(enabled);
|
||||
findPreference(KEY_ANGLE).setEnabled(enabled);
|
||||
findPreference(KEY_PROVIDER).setEnabled(enabled);
|
||||
findPreference(KEY_ACCURACY).setEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -247,13 +247,8 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen
|
||||
ALARM_MANAGER_INTERVAL, ALARM_MANAGER_INTERVAL, alarmIntent);
|
||||
} else {
|
||||
sharedPreferences.edit().putBoolean(KEY_STATUS, false).apply();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
TwoStatePreference preference = (TwoStatePreference) findPreference(KEY_STATUS);
|
||||
preference.setChecked(false);
|
||||
} else {
|
||||
CheckBoxPreference preference = (CheckBoxPreference) findPreference(KEY_STATUS);
|
||||
preference.setChecked(false);
|
||||
}
|
||||
TwoStatePreference preference = (TwoStatePreference) findPreference(KEY_STATUS);
|
||||
preference.setChecked(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 - 2017 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;
|
||||
|
||||
@SuppressWarnings("MissingPermission")
|
||||
public class MixedPositionProvider extends PositionProvider implements LocationListener, GpsStatus.Listener {
|
||||
|
||||
private static final 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);
|
||||
try {
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, requestInterval, 0, this);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
locationManager.requestLocationUpdates(
|
||||
LocationManager.NETWORK_PROVIDER, requestInterval, 0, backupListener);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 - requestInterval > FIX_TIMEOUT) {
|
||||
startBackupProvider();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,19 +15,24 @@
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.BatteryManager;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
public abstract class PositionProvider {
|
||||
import com.mapzen.android.lost.api.LocationListener;
|
||||
import com.mapzen.android.lost.api.LocationRequest;
|
||||
import com.mapzen.android.lost.api.LocationServices;
|
||||
import com.mapzen.android.lost.api.LostApiClient;
|
||||
|
||||
protected static final String TAG = PositionProvider.class.getSimpleName();
|
||||
public class PositionProvider implements LostApiClient.ConnectionCallbacks, LocationListener {
|
||||
|
||||
private static final String TAG = PositionProvider.class.getSimpleName();
|
||||
|
||||
private static final int MINIMUM_INTERVAL = 1000;
|
||||
|
||||
@@ -38,14 +43,13 @@ public abstract class PositionProvider {
|
||||
private final PositionListener listener;
|
||||
|
||||
private final Context context;
|
||||
protected final LocationManager locationManager;
|
||||
private SharedPreferences preferences;
|
||||
private LostApiClient apiClient;
|
||||
|
||||
private String deviceId;
|
||||
protected String type;
|
||||
protected long requestInterval;
|
||||
protected long interval;
|
||||
protected double distance;
|
||||
protected double angle;
|
||||
private long interval;
|
||||
private double distance;
|
||||
private double angle;
|
||||
|
||||
private Location lastLocation;
|
||||
|
||||
@@ -53,28 +57,42 @@ public abstract class PositionProvider {
|
||||
this.context = context;
|
||||
this.listener = listener;
|
||||
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
deviceId = preferences.getString(MainFragment.KEY_DEVICE, "undefined");
|
||||
interval = Long.parseLong(preferences.getString(MainFragment.KEY_INTERVAL, "600")) * 1000;
|
||||
distance = Integer.parseInt(preferences.getString(MainFragment.KEY_DISTANCE, "0"));
|
||||
angle = Integer.parseInt(preferences.getString(MainFragment.KEY_ANGLE, "0"));
|
||||
|
||||
if (distance > 0 || angle > 0) {
|
||||
requestInterval = MINIMUM_INTERVAL;
|
||||
} else {
|
||||
requestInterval = interval;
|
||||
}
|
||||
|
||||
type = preferences.getString(MainFragment.KEY_PROVIDER, "gps");
|
||||
}
|
||||
|
||||
public abstract void startUpdates();
|
||||
public void startUpdates() {
|
||||
apiClient = new LostApiClient.Builder(context).addConnectionCallbacks(this).build();
|
||||
apiClient.connect();
|
||||
}
|
||||
|
||||
public abstract void stopUpdates();
|
||||
private int getPriority(String accuracy) {
|
||||
switch (accuracy) {
|
||||
case "high":
|
||||
return LocationRequest.PRIORITY_HIGH_ACCURACY;
|
||||
case "low":
|
||||
return LocationRequest.PRIORITY_LOW_POWER;
|
||||
default:
|
||||
return LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateLocation(Location location) {
|
||||
@SuppressLint("MissingPermission")
|
||||
@Override
|
||||
public void onConnected() {
|
||||
LocationRequest request = LocationRequest.create()
|
||||
.setPriority(getPriority(preferences.getString(MainFragment.KEY_ACCURACY, "medium")))
|
||||
.setInterval(distance > 0 || angle > 0 ? MINIMUM_INTERVAL : interval);
|
||||
|
||||
LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, request, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
if (location != null && (lastLocation == null
|
||||
|| location.getTime() - lastLocation.getTime() >= interval
|
||||
|| distance > 0 && DistanceCalculator.distance(location.getLatitude(), location.getLongitude(), lastLocation.getLatitude(), lastLocation.getLongitude()) >= distance
|
||||
@@ -87,6 +105,15 @@ public abstract class PositionProvider {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionSuspended() {
|
||||
Log.i(TAG, "lost client suspended");
|
||||
}
|
||||
|
||||
public void stopUpdates() {
|
||||
apiClient.disconnect();
|
||||
}
|
||||
|
||||
public static double getBatteryLevel(Context context) {
|
||||
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
if (batteryIntent != null) {
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
*/
|
||||
package org.traccar.client;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.DrawableRes;
|
||||
@@ -34,7 +32,10 @@ import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class ShortcutActivity extends AppCompatActivity {
|
||||
import com.mapzen.android.lost.api.LocationServices;
|
||||
import com.mapzen.android.lost.api.LostApiClient;
|
||||
|
||||
public class ShortcutActivity extends AppCompatActivity implements LostApiClient.ConnectionCallbacks {
|
||||
|
||||
public static final String EXTRA_ACTION = "action";
|
||||
public static final String ACTION_START = "start";
|
||||
@@ -43,6 +44,8 @@ public class ShortcutActivity extends AppCompatActivity {
|
||||
|
||||
private static final String ALARM_SOS = "sos";
|
||||
|
||||
private LostApiClient apiClient;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -99,16 +102,16 @@ public class ShortcutActivity extends AppCompatActivity {
|
||||
setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, shortcut));
|
||||
}
|
||||
|
||||
@SuppressWarnings("MissingPermission")
|
||||
private void sendAlarm() {
|
||||
apiClient = new LostApiClient.Builder(this).addConnectionCallbacks(this).build();
|
||||
}
|
||||
|
||||
@SuppressWarnings("MissingPermission")
|
||||
@Override
|
||||
public void onConnected() {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
if (location == null) {
|
||||
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
}
|
||||
Location location = LocationServices.FusedLocationApi.getLastLocation(apiClient);
|
||||
|
||||
if (location != null) {
|
||||
|
||||
@@ -133,6 +136,12 @@ public class ShortcutActivity extends AppCompatActivity {
|
||||
} else {
|
||||
Toast.makeText(this, R.string.status_send_fail, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
apiClient.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionSuspended() {
|
||||
}
|
||||
|
||||
private boolean executeAction(Intent intent) {
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
import android.util.Log;
|
||||
|
||||
@SuppressWarnings("MissingPermission")
|
||||
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() {
|
||||
try {
|
||||
locationManager.requestLocationUpdates(type, requestInterval, 0, this);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,11 +57,7 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
||||
this.context = context;
|
||||
handler = new Handler();
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
if (preferences.getString(MainFragment.KEY_PROVIDER, "gps").equals("mixed")) {
|
||||
positionProvider = new MixedPositionProvider(context, this);
|
||||
} else {
|
||||
positionProvider = new SimplePositionProvider(context, this);
|
||||
}
|
||||
positionProvider = new PositionProvider(context, this);
|
||||
databaseHelper = new DatabaseHelper(context);
|
||||
networkManager = new NetworkManager(context, this);
|
||||
isOnline = networkManager.isOnline();
|
||||
|
||||
@@ -20,11 +20,11 @@
|
||||
<string name="settings_status_on">Stop</string>
|
||||
<string name="settings_status_off_summary">Service stopped</string>
|
||||
<string name="settings_status_on_summary">Service running</string>
|
||||
<string name="settings_provider_title">Location provider</string>
|
||||
<string name="settings_provider_summary">Source of location data</string>
|
||||
<string name="settings_provider_gps">GPS provider</string>
|
||||
<string name="settings_provider_network">Network provider</string>
|
||||
<string name="settings_provider_mixed">Mixed provider</string>
|
||||
<string name="settings_accuracy_title">Location accuracy</string>
|
||||
<string name="settings_accuracy_summary">Desired location accuracy</string>
|
||||
<string name="settings_accuracy_high">High</string>
|
||||
<string name="settings_accuracy_medium">Medium</string>
|
||||
<string name="settings_accuracy_low">Low</string>
|
||||
<string name="settings_foreground_title">Foreground service</string>
|
||||
<string name="settings_foreground_summary">Increase service priority</string>
|
||||
<string name="menu_status">Status</string>
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
|
||||
<string name="settings_url_default_value" translatable="false">http://demo.traccar.org:5055</string>
|
||||
|
||||
<string-array name="settings_provider_values" translatable="false">
|
||||
<item>gps</item>
|
||||
<item>network</item>
|
||||
<item>mixed</item>
|
||||
<string-array name="settings_accuracy_values" translatable="false">
|
||||
<item>high</item>
|
||||
<item>medium</item>
|
||||
<item>low</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="settings_provider_names">
|
||||
<item>@string/settings_provider_gps</item>
|
||||
<item>@string/settings_provider_network</item>
|
||||
<item>@string/settings_provider_mixed</item>
|
||||
<string-array name="settings_accuracy_names">
|
||||
<item>@string/settings_accuracy_high</item>
|
||||
<item>@string/settings_accuracy_medium</item>
|
||||
<item>@string/settings_accuracy_low</item>
|
||||
</string-array>
|
||||
|
||||
</resources>
|
||||
|
||||
@@ -20,6 +20,14 @@
|
||||
android:summary="@string/settings_url_summary"
|
||||
android:title="@string/settings_url_title" />
|
||||
|
||||
<ListPreference
|
||||
android:defaultValue="medium"
|
||||
android:entries="@array/settings_accuracy_names"
|
||||
android:entryValues="@array/settings_accuracy_values"
|
||||
android:key="accuracy"
|
||||
android:summary="@string/settings_accuracy_summary"
|
||||
android:title="@string/settings_accuracy_title" />
|
||||
|
||||
<EditTextPreference
|
||||
android:defaultValue="300"
|
||||
android:key="interval"
|
||||
@@ -41,12 +49,4 @@
|
||||
android:summary="@string/settings_angle_summary"
|
||||
android:title="@string/settings_angle_title" />
|
||||
|
||||
<ListPreference
|
||||
android:defaultValue="gps"
|
||||
android:entries="@array/settings_provider_names"
|
||||
android:entryValues="@array/settings_provider_values"
|
||||
android:key="provider"
|
||||
android:summary="@string/settings_provider_summary"
|
||||
android:title="@string/settings_provider_title" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
||||
Ссылка в новой задаче
Block a user