Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc63fffb67 | ||
|
|
d5150ba126 | ||
|
|
b8401b3613 |
@@ -25,10 +25,6 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
public static final String ACTION_DATA_CHANGED = "com.wytehat.btlogger.DATA_CHANGED";
|
||||
public static final String ACTION_CONFIG_CHANGED = "com.wytehat.btlogger.CONFIG_CHANGED";
|
||||
public static final String ACTION_RANGE_MODE = "com.wytehat.btlogger.RANGE_MODE";
|
||||
public static final String ACTION_ACK_LEFT_BEHIND = "com.wytehat.btlogger.ACK_LEFT_BEHIND";
|
||||
public static final String EXTRA_ADDRESS = "address";
|
||||
/** How often an unacknowledged left-behind item re-alerts. */
|
||||
private static final long ALERT_REPEAT_MS = 60000L;
|
||||
private static final int NOTIFICATION_ID = 42;
|
||||
private static final String CHANNEL_ID = "bluetooth_tracking";
|
||||
private static final String ALERT_CHANNEL_ID = "tracked_item_disconnects";
|
||||
@@ -41,9 +37,6 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
private int phoneBattery = 100;
|
||||
private final Handler handler = new Handler();
|
||||
private final HashMap<String, Long> lastPeriodicLog = new HashMap<String, Long>();
|
||||
/** Items reported left behind, address -> display name, until acknowledged. */
|
||||
private final HashMap<String, String> leftBehind = new HashMap<String, String>();
|
||||
private boolean alertLoopRunning;
|
||||
|
||||
private final BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
@@ -52,10 +45,6 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
rangeModeActive = intent.getBooleanExtra("active", false);
|
||||
return;
|
||||
}
|
||||
if (ACTION_ACK_LEFT_BEHIND.equals(action)) {
|
||||
acknowledgeLeftBehind(intent.getStringExtra(EXTRA_ADDRESS));
|
||||
return;
|
||||
}
|
||||
if (ACTION_CONFIG_CHANGED.equals(action)) {
|
||||
restartBackgroundScan();
|
||||
configureLocation();
|
||||
@@ -95,7 +84,7 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
DeviceRecord previous = db.get(address);
|
||||
db.disconnected(address, broadcastName, now, fix, infer(previous));
|
||||
if (previous != null && previous.tracked == 1 && previous.connected == 1)
|
||||
markLeftBehind(previous, infer(previous));
|
||||
showDisconnectAlert(previous, infer(previous));
|
||||
captureFresh(address, now);
|
||||
configureLocation();
|
||||
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
||||
@@ -186,7 +175,6 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
||||
filter.addAction(ACTION_CONFIG_CHANGED);
|
||||
filter.addAction(ACTION_RANGE_MODE);
|
||||
filter.addAction(ACTION_ACK_LEFT_BEHIND);
|
||||
registerReceiver(receiver, filter);
|
||||
receiverRegistered = true;
|
||||
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
|
||||
@@ -204,7 +192,6 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
|
||||
public void onDestroy() {
|
||||
handler.removeCallbacks(scanTask);
|
||||
handler.removeCallbacks(alertTask);
|
||||
try { if (bluetooth != null) bluetooth.stopLeScan(leScanCallback); }
|
||||
catch (Exception ignored) { }
|
||||
if (receiverRegistered) unregisterReceiver(receiver);
|
||||
@@ -431,99 +418,16 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
return first.getTime() >= second.getTime() ? first : second;
|
||||
}
|
||||
|
||||
/**
|
||||
* FLAG_IMMUTABLE (API 23+) is referenced by value because this module
|
||||
* still compiles against SDK 21.
|
||||
*/
|
||||
private int pendingFlags() {
|
||||
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
if (Build.VERSION.SDK_INT >= 23) flags |= 0x04000000;
|
||||
return flags;
|
||||
}
|
||||
|
||||
private int alertId(String address) {
|
||||
return 1000 + Math.abs(address.hashCode() % 100000);
|
||||
}
|
||||
|
||||
private PendingIntent acknowledgeIntent(String address) {
|
||||
Intent ack = new Intent(ACTION_ACK_LEFT_BEHIND).setPackage(getPackageName());
|
||||
if (address != null) ack.putExtra(EXTRA_ADDRESS, address);
|
||||
return PendingIntent.getBroadcast(this,
|
||||
address == null ? 0 : alertId(address), ack, pendingFlags());
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the item as left behind and keeps alerting until the user
|
||||
* taps "Acknowledge Left Behind" - a swipe alone does not clear it.
|
||||
*/
|
||||
private void markLeftBehind(DeviceRecord record, String reason) {
|
||||
if (record == null || record.address == null) return;
|
||||
leftBehind.put(record.address, record.displayName());
|
||||
showDisconnectAlert(record, reason);
|
||||
updateForegroundNotification();
|
||||
startAlertLoop();
|
||||
}
|
||||
|
||||
private void acknowledgeLeftBehind(String address) {
|
||||
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
if (address == null) {
|
||||
for (String pending : leftBehind.keySet().toArray(new String[0])) {
|
||||
try { manager.cancel(alertId(pending)); } catch (Exception ignored) { }
|
||||
}
|
||||
leftBehind.clear();
|
||||
} else {
|
||||
leftBehind.remove(address);
|
||||
try { manager.cancel(alertId(address)); } catch (Exception ignored) { }
|
||||
}
|
||||
if (leftBehind.isEmpty()) {
|
||||
handler.removeCallbacks(alertTask);
|
||||
alertLoopRunning = false;
|
||||
}
|
||||
updateForegroundNotification();
|
||||
}
|
||||
|
||||
private void startAlertLoop() {
|
||||
if (alertLoopRunning) return;
|
||||
alertLoopRunning = true;
|
||||
handler.postDelayed(alertTask, ALERT_REPEAT_MS);
|
||||
}
|
||||
|
||||
/** Re-fires every outstanding alert until each one is acknowledged. */
|
||||
private final Runnable alertTask = new Runnable() {
|
||||
public void run() {
|
||||
if (leftBehind.isEmpty()) { alertLoopRunning = false; return; }
|
||||
for (String address : leftBehind.keySet().toArray(new String[0])) {
|
||||
DeviceRecord record = db.get(address);
|
||||
if (record == null) continue;
|
||||
showDisconnectAlert(record, "Still left behind - not acknowledged");
|
||||
}
|
||||
updateForegroundNotification();
|
||||
handler.postDelayed(this, ALERT_REPEAT_MS);
|
||||
}
|
||||
};
|
||||
|
||||
private void updateForegroundNotification() {
|
||||
try {
|
||||
NotificationManager manager =
|
||||
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
manager.notify(NOTIFICATION_ID, buildNotification());
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
|
||||
private void showDisconnectAlert(DeviceRecord record, String reason) {
|
||||
Intent open = new Intent(this, MainActivity.class);
|
||||
PendingIntent pending = PendingIntent.getActivity(this, record.address.hashCode(), open,
|
||||
pendingFlags());
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
Notification.Builder builder = new Notification.Builder(this)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setContentTitle(record.displayName() + " left behind")
|
||||
.setContentTitle(record.displayName() + " disconnected")
|
||||
.setContentText(reason == null ? "Your tracked item is no longer connected" : reason)
|
||||
.setContentIntent(pending).setAutoCancel(false).setOngoing(true)
|
||||
.setContentIntent(pending).setAutoCancel(true)
|
||||
.setDefaults(Notification.DEFAULT_ALL).setPriority(Notification.PRIORITY_HIGH);
|
||||
if (Build.VERSION.SDK_INT >= 16) {
|
||||
builder.addAction(R.drawable.ic_launcher, "Acknowledge Left Behind",
|
||||
acknowledgeIntent(record.address));
|
||||
}
|
||||
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= 26) {
|
||||
try {
|
||||
@@ -541,24 +445,12 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
|
||||
private Notification buildNotification() {
|
||||
Intent open = new Intent(this, MainActivity.class);
|
||||
PendingIntent pending = PendingIntent.getActivity(this, 0, open, pendingFlags());
|
||||
int outstanding = leftBehind.size();
|
||||
String title = outstanding > 0
|
||||
? outstanding + (outstanding == 1 ? " item left behind" : " items left behind")
|
||||
: "Bluetooth Item Finder";
|
||||
String text = outstanding > 0
|
||||
? "Alerting until acknowledged: " + describeLeftBehind()
|
||||
: "Watching your tracked items";
|
||||
PendingIntent pending = PendingIntent.getActivity(this, 0, open,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
Notification.Builder builder = new Notification.Builder(this)
|
||||
.setSmallIcon(R.drawable.ic_launcher).setContentTitle(title)
|
||||
.setContentText(text).setOngoing(true)
|
||||
.setSmallIcon(R.drawable.ic_launcher).setContentTitle("Bluetooth Item Finder")
|
||||
.setContentText("Watching your tracked items").setOngoing(true)
|
||||
.setContentIntent(pending);
|
||||
if (Build.VERSION.SDK_INT >= 16 && outstanding > 0) {
|
||||
builder.addAction(R.drawable.ic_launcher,
|
||||
outstanding == 1 ? "Acknowledge Left Behind" : "Acknowledge All",
|
||||
acknowledgeIntent(outstanding == 1
|
||||
? leftBehind.keySet().iterator().next() : null));
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 26) {
|
||||
try {
|
||||
Class<?> channelClass = Class.forName("android.app.NotificationChannel");
|
||||
@@ -575,14 +467,5 @@ public class BluetoothTrackingService extends Service implements LocationListene
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private String describeLeftBehind() {
|
||||
StringBuilder names = new StringBuilder();
|
||||
for (String name : leftBehind.values()) {
|
||||
if (names.length() > 0) names.append(", ");
|
||||
names.append(name);
|
||||
}
|
||||
return names.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
package com.wytehat.btlogger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Ordering for the tracked item list.
|
||||
*
|
||||
* Default order is newest sighting first. Items that have not been seen for
|
||||
* a long time - or that carry no sighting timestamp at all - are pushed to
|
||||
* the bottom and stay there in both sort directions, so the reverse toggle
|
||||
* never buries a live item under a pile of stale ones.
|
||||
*/
|
||||
final class DeviceSort {
|
||||
|
||||
/** A device unseen for longer than this sinks to the bottom. */
|
||||
static final long STALE_AFTER_MS = 24L * 60L * 60L * 1000L;
|
||||
|
||||
private DeviceSort() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Last moment the device was actually observed. Deliberately ignores
|
||||
* updatedAt, which also moves when the user edits a name or colour.
|
||||
*/
|
||||
static long lastSeen(DeviceRecord record) {
|
||||
|
||||
if (record == null) {
|
||||
return 0L;
|
||||
}
|
||||
|
||||
long seen = record.lastRssiAt;
|
||||
|
||||
if (record.connectedAt > seen) {
|
||||
seen = record.connectedAt;
|
||||
}
|
||||
|
||||
if (record.disconnectedAt > seen) {
|
||||
seen = record.disconnectedAt;
|
||||
}
|
||||
|
||||
return seen;
|
||||
}
|
||||
|
||||
static boolean isStale(DeviceRecord record, long now) {
|
||||
|
||||
long seen = lastSeen(record);
|
||||
|
||||
return seen <= 0L ||
|
||||
now - seen > STALE_AFTER_MS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param reverse false = newest first (default), true = oldest first.
|
||||
*/
|
||||
static List<DeviceRecord> sorted(List<DeviceRecord> rows, boolean reverse) {
|
||||
|
||||
ArrayList<DeviceRecord> fresh = new ArrayList<DeviceRecord>();
|
||||
ArrayList<DeviceRecord> stale = new ArrayList<DeviceRecord>();
|
||||
|
||||
if (rows == null) {
|
||||
return fresh;
|
||||
}
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
|
||||
DeviceRecord record = rows.get(i);
|
||||
|
||||
if (record == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isStale(record, now)) {
|
||||
stale.add(record);
|
||||
} else {
|
||||
fresh.add(record);
|
||||
}
|
||||
}
|
||||
|
||||
Comparator<DeviceRecord> newestFirst =
|
||||
new Comparator<DeviceRecord>() {
|
||||
|
||||
public int compare(DeviceRecord left, DeviceRecord right) {
|
||||
|
||||
long a = lastSeen(left);
|
||||
long b = lastSeen(right);
|
||||
|
||||
if (a == b) {
|
||||
return left.displayName()
|
||||
.compareToIgnoreCase(right.displayName());
|
||||
}
|
||||
|
||||
return a > b ? -1 : 1;
|
||||
}
|
||||
};
|
||||
|
||||
Collections.sort(fresh, newestFirst);
|
||||
Collections.sort(stale, newestFirst);
|
||||
|
||||
if (reverse) {
|
||||
Collections.reverse(fresh);
|
||||
Collections.reverse(stale);
|
||||
}
|
||||
|
||||
ArrayList<DeviceRecord> result =
|
||||
new ArrayList<DeviceRecord>(fresh.size() + stale.size());
|
||||
|
||||
result.addAll(fresh);
|
||||
|
||||
/*
|
||||
* Stale entries are appended last in both directions.
|
||||
*/
|
||||
result.addAll(stale);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@ public class MainActivity extends Activity implements ItemActionListener {
|
||||
private static final int CAMERA = 200;
|
||||
private TrackerDatabase db;
|
||||
private DeviceListAdapter adapter;
|
||||
private boolean reverseSort;
|
||||
private boolean registered;
|
||||
private DeviceRecord editing;
|
||||
private final Handler refreshHandler = new Handler();
|
||||
@@ -47,18 +46,6 @@ public class MainActivity extends Activity implements ItemActionListener {
|
||||
public void onClick(View v) { startActivity(new Intent(MainActivity.this,
|
||||
MapActivity.class)); }
|
||||
});
|
||||
reverseSort = getSharedPreferences("list_settings", 0)
|
||||
.getBoolean("reverse_sort", false);
|
||||
updateSortButton();
|
||||
findViewById(R.id.sort_button).setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
reverseSort = !reverseSort;
|
||||
getSharedPreferences("list_settings", 0).edit()
|
||||
.putBoolean("reverse_sort", reverseSort).apply();
|
||||
updateSortButton();
|
||||
reload();
|
||||
}
|
||||
});
|
||||
findViewById(R.id.settings_button).setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) { showSettings(); }
|
||||
});
|
||||
@@ -360,21 +347,7 @@ public class MainActivity extends Activity implements ItemActionListener {
|
||||
PackageManager.PERMISSION_GRANTED) list.add(permission);
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
if (adapter == null) return;
|
||||
adapter.setRows(DeviceSort.sorted(db.tracked(), reverseSort));
|
||||
}
|
||||
|
||||
/*
|
||||
* Stale items stay pinned to the bottom in both directions, so the
|
||||
* label only describes how the recently seen items are ordered.
|
||||
*/
|
||||
private void updateSortButton() {
|
||||
android.widget.Button button =
|
||||
(android.widget.Button) findViewById(R.id.sort_button);
|
||||
if (button == null) return;
|
||||
button.setText(reverseSort ? "Oldest first" : "Newest first");
|
||||
}
|
||||
private void reload() { if (adapter != null) adapter.setRows(db.tracked()); }
|
||||
|
||||
private void startTracker() {
|
||||
Intent intent = new Intent(this, BluetoothTrackingService.class);
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
package com.wytehat.btlogger;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.DatePickerDialog;
|
||||
import android.app.TimePickerDialog;
|
||||
@@ -16,8 +14,6 @@ import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.View;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
@@ -37,8 +33,6 @@ import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class MapActivity extends Activity
|
||||
implements SensorEventListener {
|
||||
@@ -55,11 +49,10 @@ implements SensorEventListener {
|
||||
|
||||
private LinearLayout filterPanel;
|
||||
|
||||
|
||||
private ScrollView filterScroll;
|
||||
private Spinner categorySpinner;
|
||||
private Spinner deviceSpinner;
|
||||
private Spinner resolutionSpinner;
|
||||
|
||||
private Spinner categorySpinner;
|
||||
private Spinner deviceSpinner;
|
||||
|
||||
private CheckBox showAll;
|
||||
|
||||
@@ -78,54 +71,15 @@ implements SensorEventListener {
|
||||
private final ArrayList<String> deviceLabels =
|
||||
new ArrayList<String>();
|
||||
|
||||
private final ArrayList<String> resolutionLabels =
|
||||
new ArrayList<String>();
|
||||
private String categoryFilter = "Trackers / Tags";
|
||||
private String deviceFilter = "All";
|
||||
|
||||
/*
|
||||
* Time-bucket widths for the trail sampler.
|
||||
* 0 = every stored point (still capped by TrackPointSampler).
|
||||
*/
|
||||
private static final long[] RESOLUTION_VALUES = {
|
||||
0L,
|
||||
15L * 60L * 1000L,
|
||||
60L * 60L * 1000L,
|
||||
120L * 60L * 1000L
|
||||
};
|
||||
private boolean rebuildingCategories;
|
||||
private boolean rebuildingDevices;
|
||||
private boolean loadingMap;
|
||||
|
||||
private static final String[] RESOLUTION_LABELS = {
|
||||
"All points",
|
||||
"Every 15 min",
|
||||
"Every 1 hour",
|
||||
"Every 2 hours"
|
||||
};
|
||||
|
||||
private volatile String categoryFilter = "Trackers / Tags";
|
||||
private volatile String deviceFilter = "All";
|
||||
|
||||
private boolean rebuildingCategories;
|
||||
private boolean rebuildingDevices;
|
||||
private boolean rebuildingResolution = true;
|
||||
private volatile boolean loadingMap;
|
||||
|
||||
private volatile long historyFrom;
|
||||
private volatile long historyTo;
|
||||
|
||||
/*
|
||||
* Selected trail resolution, read by the background loader.
|
||||
*/
|
||||
private volatile long sampleInterval = RESOLUTION_VALUES[0];
|
||||
|
||||
/*
|
||||
* Map data (SQLite reads + HTML generation) is built off the main
|
||||
* thread; only the WebView load and the spinner refresh run on it.
|
||||
*/
|
||||
private final ExecutorService mapExecutor =
|
||||
Executors.newSingleThreadExecutor();
|
||||
|
||||
private final Handler ui =
|
||||
new Handler(Looper.getMainLooper());
|
||||
|
||||
private volatile boolean destroyed;
|
||||
private long historyFrom;
|
||||
private long historyTo;
|
||||
|
||||
private LocationManager locationManager;
|
||||
private SensorManager sensorManager;
|
||||
@@ -256,6 +210,7 @@ implements SensorEventListener {
|
||||
dp(4)
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* Category.
|
||||
*/
|
||||
@@ -314,74 +269,14 @@ implements SensorEventListener {
|
||||
|
||||
deviceSpinner.setAdapter(deviceAdapter);
|
||||
|
||||
filterPanel.addView(
|
||||
deviceSpinner,
|
||||
compactParams()
|
||||
);
|
||||
filterPanel.addView(
|
||||
deviceSpinner,
|
||||
compactParams()
|
||||
);
|
||||
|
||||
/*
|
||||
* Trail resolution (downsampling).
|
||||
*/
|
||||
LinearLayout resolutionRow =
|
||||
new LinearLayout(this);
|
||||
|
||||
resolutionRow.setOrientation(
|
||||
LinearLayout.HORIZONTAL
|
||||
);
|
||||
|
||||
resolutionRow.setGravity(
|
||||
android.view.Gravity.CENTER_VERTICAL
|
||||
);
|
||||
|
||||
TextView resolutionLabel =
|
||||
smallLabel("Trail detail");
|
||||
|
||||
resolutionRow.addView(
|
||||
resolutionLabel,
|
||||
new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
);
|
||||
resolutionSpinner = new Spinner(this);
|
||||
|
||||
for (int i = 0;
|
||||
i < RESOLUTION_LABELS.length;
|
||||
i++) {
|
||||
|
||||
resolutionLabels.add(
|
||||
RESOLUTION_LABELS[i]
|
||||
);
|
||||
}
|
||||
|
||||
ArrayAdapter<String> resolutionAdapter =
|
||||
new ArrayAdapter<String>(
|
||||
this,
|
||||
android.R.layout.simple_spinner_item,
|
||||
resolutionLabels
|
||||
);
|
||||
|
||||
resolutionAdapter.setDropDownViewResource(
|
||||
android.R.layout.simple_spinner_dropdown_item
|
||||
);
|
||||
|
||||
resolutionSpinner.setAdapter(
|
||||
resolutionAdapter
|
||||
);
|
||||
|
||||
resolutionRow.addView(
|
||||
resolutionSpinner,
|
||||
new LinearLayout.LayoutParams(
|
||||
0,
|
||||
dp(38),
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
filterPanel.addView(resolutionRow);
|
||||
/*
|
||||
* Time.
|
||||
*/
|
||||
/*
|
||||
* Time.
|
||||
*/
|
||||
TextView timeLabel =
|
||||
smallLabel("Location history");
|
||||
|
||||
@@ -495,8 +390,8 @@ implements SensorEventListener {
|
||||
filterPanel.addView(showAll);
|
||||
|
||||
/*
|
||||
* The panel is scrollable and bounded to a third of the
|
||||
* screen so every control stays reachable on short screens.
|
||||
* Bounded to a third of the screen and scrollable, so every
|
||||
* control stays reachable on short screens.
|
||||
*/
|
||||
filterScroll = new ScrollView(this);
|
||||
|
||||
@@ -518,6 +413,7 @@ implements SensorEventListener {
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* WebView map.
|
||||
*/
|
||||
@@ -681,51 +577,7 @@ implements SensorEventListener {
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
* Trail resolution: applied immediately so the user can compare
|
||||
* densities without reopening the panel.
|
||||
*/
|
||||
resolutionSpinner.setOnItemSelectedListener(
|
||||
new AdapterView.OnItemSelectedListener() {
|
||||
|
||||
@Override
|
||||
public void onItemSelected(
|
||||
AdapterView<?> parent,
|
||||
View view,
|
||||
int position,
|
||||
long id) {
|
||||
|
||||
if (rebuildingResolution) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (position < 0 ||
|
||||
position >=
|
||||
RESOLUTION_VALUES.length) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (sampleInterval ==
|
||||
RESOLUTION_VALUES[position]) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sampleInterval =
|
||||
RESOLUTION_VALUES[position];
|
||||
|
||||
reloadMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(
|
||||
AdapterView<?> parent) {
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
fromButton.setOnClickListener(
|
||||
fromButton.setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
@@ -837,11 +689,9 @@ implements SensorEventListener {
|
||||
}
|
||||
);
|
||||
|
||||
updateTimeButtons();
|
||||
updateTimeButtons();
|
||||
|
||||
rebuildingResolution = false;
|
||||
|
||||
rebuildDeviceFilter();
|
||||
rebuildDeviceFilter();
|
||||
|
||||
/*
|
||||
* The initial map is deliberately:
|
||||
@@ -858,10 +708,10 @@ implements SensorEventListener {
|
||||
|
||||
private LinearLayout.LayoutParams compactParams() {
|
||||
|
||||
return new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
dp(42)
|
||||
);
|
||||
return new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
dp(38)
|
||||
);
|
||||
}
|
||||
|
||||
private TextView smallLabel(String text) {
|
||||
@@ -1051,28 +901,12 @@ implements SensorEventListener {
|
||||
);
|
||||
}
|
||||
|
||||
private void rebuildDeviceFilter() {
|
||||
private void rebuildDeviceFilter() {
|
||||
|
||||
rebuildDeviceFilter(
|
||||
getMapDevices(
|
||||
showAll.isChecked()
|
||||
)
|
||||
);
|
||||
}
|
||||
List<DeviceRecord> available =
|
||||
getMapDevices();
|
||||
|
||||
/*
|
||||
* Main thread only. Accepts an already loaded device list so the
|
||||
* background loader does not repeat the query.
|
||||
*/
|
||||
private void rebuildDeviceFilter(
|
||||
List<DeviceRecord> available) {
|
||||
|
||||
if (available == null) {
|
||||
available =
|
||||
new ArrayList<DeviceRecord>();
|
||||
}
|
||||
|
||||
ArrayList<String> nextAddresses =
|
||||
ArrayList<String> nextAddresses =
|
||||
new ArrayList<String>();
|
||||
|
||||
ArrayList<String> nextLabels =
|
||||
@@ -1169,19 +1003,10 @@ implements SensorEventListener {
|
||||
rebuildingDevices = false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Safe to call from a worker thread: the caller passes the checkbox
|
||||
* state in instead of reading the view here.
|
||||
*/
|
||||
private List<DeviceRecord> getMapDevices(
|
||||
boolean includeAll) {
|
||||
private List<DeviceRecord> getMapDevices() {
|
||||
|
||||
TrackerDatabase database = db;
|
||||
|
||||
List<DeviceRecord> all =
|
||||
database == null
|
||||
? null
|
||||
: database.all();
|
||||
List<DeviceRecord> all =
|
||||
db.all();
|
||||
|
||||
ArrayList<DeviceRecord> result =
|
||||
new ArrayList<DeviceRecord>();
|
||||
@@ -1205,17 +1030,17 @@ implements SensorEventListener {
|
||||
* Normal map:
|
||||
* only explicitly map-enabled devices.
|
||||
*/
|
||||
if (!includeAll &&
|
||||
record.mapEnabled != 1) {
|
||||
if (!showAll.isChecked() &&
|
||||
record.mapEnabled != 1) {
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Normal map excludes unknown entries.
|
||||
*/
|
||||
if (!includeAll &&
|
||||
isUnknown(record)) {
|
||||
* Normal map excludes unknown entries.
|
||||
*/
|
||||
if (!showAll.isChecked() &&
|
||||
isUnknown(record)) {
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -1396,175 +1221,82 @@ implements SensorEventListener {
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void reloadMap() {
|
||||
private void reloadMap() {
|
||||
|
||||
if (loadingMap) {
|
||||
return;
|
||||
}
|
||||
if (loadingMap) {
|
||||
return;
|
||||
}
|
||||
|
||||
loadingMap = true;
|
||||
mapReady = false;
|
||||
loadingMap = true;
|
||||
mapReady = false;
|
||||
|
||||
/*
|
||||
* View state is snapshotted on the main thread; everything after
|
||||
* this point (SQLite reads, trail sampling, HTML generation) runs
|
||||
* on the worker.
|
||||
*/
|
||||
final boolean includeAll =
|
||||
showAll.isChecked();
|
||||
try {
|
||||
|
||||
mapExecutor.execute(
|
||||
new Runnable() {
|
||||
List<DeviceRecord> available =
|
||||
getMapDevices();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
refreshMapCategories(
|
||||
available
|
||||
);
|
||||
|
||||
List<DeviceRecord> available = null;
|
||||
String html = null;
|
||||
/*
|
||||
* Rebuild device list after category
|
||||
* changes.
|
||||
*/
|
||||
rebuildDeviceFilter();
|
||||
|
||||
try {
|
||||
ArrayList<DeviceRecord> rows =
|
||||
new ArrayList<DeviceRecord>();
|
||||
|
||||
available =
|
||||
getMapDevices(includeAll);
|
||||
for (int i = 0;
|
||||
i < available.size();
|
||||
i++) {
|
||||
|
||||
String device = deviceFilter;
|
||||
DeviceRecord record =
|
||||
available.get(i);
|
||||
|
||||
/*
|
||||
* The selected device may have gone away since
|
||||
* the last load; fall back to showing all.
|
||||
*/
|
||||
if (!"All".equals(device) &&
|
||||
!containsAddress(
|
||||
available,
|
||||
device
|
||||
)) {
|
||||
if (record == null ||
|
||||
!record.hasLocation) {
|
||||
continue;
|
||||
}
|
||||
|
||||
device = "All";
|
||||
}
|
||||
if (!categoryMatches(record)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ArrayList<DeviceRecord> rows =
|
||||
new ArrayList<DeviceRecord>();
|
||||
if (!"All".equals(deviceFilter) &&
|
||||
!deviceFilter.equals(
|
||||
record.address
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0;
|
||||
i < available.size();
|
||||
i++) {
|
||||
rows.add(record);
|
||||
}
|
||||
|
||||
DeviceRecord record =
|
||||
available.get(i);
|
||||
String html =
|
||||
buildHtml(
|
||||
rows,
|
||||
focus
|
||||
);
|
||||
|
||||
if (record == null ||
|
||||
!record.hasLocation) {
|
||||
continue;
|
||||
}
|
||||
web.loadDataWithBaseURL(
|
||||
"https://unpkg.com/",
|
||||
html,
|
||||
"text/html",
|
||||
"UTF-8",
|
||||
null
|
||||
);
|
||||
|
||||
if (!categoryMatches(record)) {
|
||||
continue;
|
||||
}
|
||||
} finally {
|
||||
|
||||
if (!"All".equals(device) &&
|
||||
!device.equals(
|
||||
record.address
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
loadingMap = false;
|
||||
}
|
||||
}
|
||||
|
||||
rows.add(record);
|
||||
}
|
||||
|
||||
html =
|
||||
buildHtml(
|
||||
rows,
|
||||
focus,
|
||||
device
|
||||
);
|
||||
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
final List<DeviceRecord> loaded =
|
||||
available;
|
||||
|
||||
final String content = html;
|
||||
|
||||
ui.post(
|
||||
new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
|
||||
if (destroyed ||
|
||||
web == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
refreshMapCategories(
|
||||
loaded
|
||||
);
|
||||
|
||||
/*
|
||||
* Rebuild device list after
|
||||
* category changes.
|
||||
*/
|
||||
rebuildDeviceFilter(
|
||||
loaded
|
||||
);
|
||||
|
||||
if (content != null) {
|
||||
|
||||
web.loadDataWithBaseURL(
|
||||
"https://unpkg.com/",
|
||||
content,
|
||||
"text/html",
|
||||
"UTF-8",
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
||||
loadingMap = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private boolean containsAddress(
|
||||
List<DeviceRecord> records,
|
||||
String address) {
|
||||
|
||||
if (records == null ||
|
||||
address == null) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0;
|
||||
i < records.size();
|
||||
i++) {
|
||||
|
||||
DeviceRecord record =
|
||||
records.get(i);
|
||||
|
||||
if (record != null &&
|
||||
address.equals(record.address)) {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String buildHtml(
|
||||
List<DeviceRecord> rows,
|
||||
String selectedAddress,
|
||||
String device) {
|
||||
private String buildHtml(
|
||||
List<DeviceRecord> rows,
|
||||
String selectedAddress) {
|
||||
|
||||
StringBuilder script =
|
||||
new StringBuilder();
|
||||
@@ -1573,44 +1305,29 @@ implements SensorEventListener {
|
||||
* Historical data is loaded ONLY for a
|
||||
* specifically selected device.
|
||||
*/
|
||||
if (!"All".equals(device)) {
|
||||
if (!"All".equals(deviceFilter)) {
|
||||
|
||||
for (int i = 0;
|
||||
i < rows.size();
|
||||
i++) {
|
||||
for (int i = 0;
|
||||
i < rows.size();
|
||||
i++) {
|
||||
|
||||
DeviceRecord record =
|
||||
rows.get(i);
|
||||
DeviceRecord record =
|
||||
rows.get(i);
|
||||
|
||||
if (!device.equals(
|
||||
record.address
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
if (!deviceFilter.equals(
|
||||
record.address
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
TrackerDatabase database = db;
|
||||
List<LocationPoint> history =
|
||||
db.history(
|
||||
record.address,
|
||||
historyFrom,
|
||||
historyTo
|
||||
);
|
||||
|
||||
List<LocationPoint> history =
|
||||
database == null
|
||||
? null
|
||||
: database.history(
|
||||
record.address,
|
||||
historyFrom,
|
||||
historyTo
|
||||
);
|
||||
|
||||
/*
|
||||
* Dense histories are downsampled here, on the
|
||||
* worker thread, so the WebView never receives
|
||||
* thousands of markers.
|
||||
*/
|
||||
history =
|
||||
TrackPointSampler.sample(
|
||||
history,
|
||||
sampleInterval
|
||||
);
|
||||
|
||||
appendHistory(
|
||||
appendHistory(
|
||||
script,
|
||||
record,
|
||||
history,
|
||||
@@ -2625,18 +2342,11 @@ implements SensorEventListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
protected void onDestroy() {
|
||||
|
||||
destroyed = true;
|
||||
try {
|
||||
|
||||
try {
|
||||
mapExecutor.shutdownNow();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
if (web != null) {
|
||||
if (web != null) {
|
||||
web.stopLoading();
|
||||
web.destroy();
|
||||
web = null;
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
package com.wytehat.btlogger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Downsamples dense GPS history before it is turned into map markers.
|
||||
*
|
||||
* Time-bucket sampling: the timeline is cut into fixed buckets and one
|
||||
* representative point (the most accurate fix) survives per bucket. The very
|
||||
* first and last fixes are always kept so the trail keeps its real endpoints.
|
||||
*
|
||||
* A hard ceiling is applied afterwards - even "All Points" cannot hand the
|
||||
* WebView an unbounded number of circle markers.
|
||||
*/
|
||||
final class TrackPointSampler {
|
||||
|
||||
/** Ceiling on rendered points, applied regardless of the chosen interval. */
|
||||
static final int MAX_RENDERED_POINTS = 750;
|
||||
|
||||
private TrackPointSampler() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bucketMillis bucket width, or 0 for "All Points" (cap only).
|
||||
*/
|
||||
static List<LocationPoint> sample(List<LocationPoint> points, long bucketMillis) {
|
||||
|
||||
if (points == null || points.size() < 3) {
|
||||
return points;
|
||||
}
|
||||
|
||||
List<LocationPoint> reduced =
|
||||
bucketMillis > 0
|
||||
? bucket(points, bucketMillis)
|
||||
: points;
|
||||
|
||||
return cap(reduced);
|
||||
}
|
||||
|
||||
private static List<LocationPoint> bucket(List<LocationPoint> points, long bucketMillis) {
|
||||
|
||||
ArrayList<LocationPoint> result = new ArrayList<LocationPoint>();
|
||||
|
||||
LocationPoint best = null;
|
||||
long currentBucket = 0;
|
||||
|
||||
for (int i = 0; i < points.size(); i++) {
|
||||
|
||||
LocationPoint point = points.get(i);
|
||||
|
||||
if (point == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
long bucketIndex = point.timestamp / bucketMillis;
|
||||
|
||||
if (best == null) {
|
||||
best = point;
|
||||
currentBucket = bucketIndex;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bucketIndex != currentBucket) {
|
||||
result.add(best);
|
||||
best = point;
|
||||
currentBucket = bucketIndex;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (moreAccurate(point, best)) {
|
||||
best = point;
|
||||
}
|
||||
}
|
||||
|
||||
if (best != null) {
|
||||
result.add(best);
|
||||
}
|
||||
|
||||
/*
|
||||
* Always terminate on the newest fix - that is the one the user cares
|
||||
* about when reading a trail.
|
||||
*/
|
||||
LocationPoint last = points.get(points.size() - 1);
|
||||
|
||||
if (last != null &&
|
||||
(result.isEmpty() ||
|
||||
result.get(result.size() - 1) != last)) {
|
||||
|
||||
result.add(last);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<LocationPoint> cap(List<LocationPoint> points) {
|
||||
|
||||
int size = points.size();
|
||||
|
||||
if (size <= MAX_RENDERED_POINTS) {
|
||||
return points;
|
||||
}
|
||||
|
||||
int stride = (size + MAX_RENDERED_POINTS - 1) / MAX_RENDERED_POINTS;
|
||||
|
||||
ArrayList<LocationPoint> result = new ArrayList<LocationPoint>();
|
||||
|
||||
for (int i = 0; i < size; i += stride) {
|
||||
result.add(points.get(i));
|
||||
}
|
||||
|
||||
LocationPoint last = points.get(size - 1);
|
||||
|
||||
if (result.isEmpty() ||
|
||||
result.get(result.size() - 1) != last) {
|
||||
|
||||
result.add(last);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean moreAccurate(LocationPoint candidate, LocationPoint current) {
|
||||
|
||||
if (candidate.accuracy <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return current.accuracy <= 0 ||
|
||||
candidate.accuracy < current.accuracy;
|
||||
}
|
||||
}
|
||||
@@ -17,25 +17,10 @@ public class TrackerDatabase extends SQLiteOpenHelper {
|
||||
super(context, DB_NAME, null, DB_VERSION);
|
||||
}
|
||||
|
||||
public synchronized List<LocationPoint> history(String address, long historyFrom, long historyTo)
|
||||
public List<LocationPoint> history(String address, long historyFrom, long historyTo)
|
||||
{
|
||||
ArrayList<LocationPoint> result = new ArrayList<LocationPoint>();
|
||||
if (address == null) return result;
|
||||
Cursor cursor = getReadableDatabase().query("location_history", null,
|
||||
"address=? AND logged_at>=? AND logged_at<=?",
|
||||
new String[] { address, String.valueOf(historyFrom), String.valueOf(historyTo) },
|
||||
null, null, "logged_at ASC");
|
||||
try {
|
||||
while (cursor.moveToNext()) {
|
||||
LocationPoint point = new LocationPoint();
|
||||
point.latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
|
||||
point.longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));
|
||||
point.accuracy = cursor.getFloat(cursor.getColumnIndex("accuracy"));
|
||||
point.timestamp = cursor.getLong(cursor.getColumnIndex("logged_at"));
|
||||
result.add(point);
|
||||
}
|
||||
} finally { cursor.close(); }
|
||||
return result;
|
||||
// TODO: Implement this method
|
||||
return null;
|
||||
}
|
||||
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
|
||||
@@ -6,10 +6,7 @@
|
||||
<Button android:id="@+id/map_all_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="MAP"/>
|
||||
<Button android:id="@+id/settings_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="SETTINGS"/>
|
||||
</LinearLayout>
|
||||
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical">
|
||||
<TextView android:id="@+id/tracking_status" android:text="@string/tracking_status" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:paddingLeft="12dp" android:paddingBottom="8dp" android:textColor="#2878ff"/>
|
||||
<Button android:id="@+id/sort_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Newest first" android:textSize="10sp" android:layout_marginRight="8dp"/>
|
||||
</LinearLayout>
|
||||
<TextView android:id="@+id/tracking_status" android:text="@string/tracking_status" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="12dp" android:paddingBottom="8dp" android:textColor="#2878ff"/>
|
||||
<ListView android:id="@+id/device_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="10dp" android:padding="8dp"/>
|
||||
<TextView android:id="@+id/empty_view" android:text="No tracked items yet. Use ADD / PAIR to choose an item." android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"/>
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user