Skip to content

Commit

Permalink
[ENH/FIX] Search clean-up (#717)
Browse files Browse the repository at this point in the history
* first stab at improving search network view

* trying to address inconsistencies in search results - by creating new inconsistencies

* updating util to add historical mode

---------

Co-authored-by: Andy Carra <rksh@wigle.net>
  • Loading branch information
rksh and Andy Carra authored Nov 12, 2024
1 parent 03f87f6 commit eede48c
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void setContext() {
@Test
public void testSetBackedList() {
Assert.assertTrue(null != context);
SetNetworkListAdapter setAdapter = new SetNetworkListAdapter( context, R.layout.row );
SetNetworkListAdapter setAdapter = new SetNetworkListAdapter( context, true, R.layout.row );
long start = System.currentTimeMillis();
for (Network net: btleLarge) {
setAdapter.addBluetoothLe(net);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.wigle.wigleandroid;

import static net.wigle.wigleandroid.db.DatabaseHelper.SEARCH_NETWORKS;
import static net.wigle.wigleandroid.model.Network.RSN_CAP;
import static net.wigle.wigleandroid.model.Network.SAE_CAP;
import static net.wigle.wigleandroid.model.Network.SUITE_B_192_CAP;
Expand Down Expand Up @@ -43,7 +44,6 @@
import com.google.android.gms.maps.model.LatLngBounds;

import net.wigle.wigleandroid.background.PooledQueryExecutor;
import net.wigle.wigleandroid.db.DatabaseHelper;
import net.wigle.wigleandroid.model.ConcurrentLinkedHashMap;
import net.wigle.wigleandroid.model.Network;
import net.wigle.wigleandroid.model.NetworkFilterType;
Expand Down Expand Up @@ -136,7 +136,7 @@ public void onCreate( final Bundle savedInstanceState) {

private void setupList() {
// not set by nonconfig retain
listAdapter = new SetNetworkListAdapter( this, R.layout.row );
listAdapter = new SetNetworkListAdapter( this, true, R.layout.row );
final ListView listView = findViewById( R.id.dblist );
ListFragment.setupListAdapter( listView, MainActivity.getMainActivity(), listAdapter, true );
}
Expand All @@ -163,7 +163,7 @@ private void setupMap(final LatLng center, final Bundle savedInstanceState, fina
private void setupQuery( final QueryArgs queryArgs ) {

final LatLngBounds bounds = queryArgs.getLocationBounds();
String sql = "SELECT bssid,lastlat,lastlon FROM " + DatabaseHelper.NETWORK_TABLE + " WHERE 1=1 ";
String sql = SEARCH_NETWORKS;
final String ssid = queryArgs.getSSID();
String bssid = queryArgs.getBSSID();
boolean limit = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public void onConfigurationChanged(@NonNull final Configuration newConfig ) {
private void setupList(final View view ) {
State state = MainActivity.getStaticState();
if (null != state && state.listAdapter == null) {
state.listAdapter = new SetNetworkListAdapter(requireActivity().getBaseContext(), R.layout.row );
state.listAdapter = new SetNetworkListAdapter(requireActivity().getBaseContext(), false, R.layout.row );
}
// always set our current list adapter
if (null != state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -131,7 +130,6 @@ public void onCreate(Bundle savedInstanceState) {
isDbResult = intent.getBooleanExtra(ListFragment.NETWORK_EXTRA_IS_DB_RESULT, false);
Logging.info( "bssid: " + bssid + " isDbResult: " + isDbResult);

final SimpleDateFormat format = NetworkListUtil.getConstructionTimeFormater(this);
if (null != MainActivity.getNetworkCache()) {
network = MainActivity.getNetworkCache().get(bssid);
}
Expand Down Expand Up @@ -181,7 +179,7 @@ public void onCreate(Bundle savedInstanceState) {
tv.setText( network.getType().name() );

tv = findViewById( R.id.na_firsttime );
tv.setText( NetworkListUtil.getConstructionTime(format, network ) );
tv.setText( NetworkListUtil.getTime(network, true, getApplicationContext()) );

tv = findViewById( R.id.na_chan );
Integer chan = network.getChannel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ public void onNothingSelected(AdapterView<?> parent) {
private void setupQueryButtons( final View view ) {
Button button = view.findViewById( R.id.perform_search_button);
button.setOnClickListener(buttonView -> {

RadioGroup rbg = view.findViewById(R.id.search_type_group);
int searchTypeId = rbg.getCheckedRadioButtonId();
final boolean local = searchTypeId != R.id.radio_search_wigle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public final class DatabaseHelper extends Thread {
private static final String LOCATED_NETS_QUERY_STEM = " FROM " + DatabaseHelper.NETWORK_TABLE
+ " WHERE bestlat != 0.0 AND bestlon != 0.0 AND instr(bssid, '_') <= 0";

public static final String SEARCH_NETWORKS = "SELECT bssid,lastlat,lastlon FROM " + NETWORK_TABLE + " WHERE 1=1 ";
//TODO: should search use best[lat|lon] instead of last?

private static final String LOCATED_WIFI_QUERY_STEM = " FROM " + DatabaseHelper.NETWORK_TABLE
+ " WHERE bestlat != 0.0 AND bestlon != 0.0 AND " + NetworkFilter.WIFI.getFilter()
Expand Down Expand Up @@ -1348,7 +1350,7 @@ public Network getNetwork( final String bssid ) {
try {
checkDB();
final String[] args = new String[]{ bssid };
cursor = db.rawQuery("select ssid,frequency,capabilities,type,lastlat,lastlon,bestlat,bestlon,rcois,mfgrid,service FROM "
cursor = db.rawQuery("select ssid,frequency,capabilities,type,lastlat,lastlon,bestlat,bestlon,rcois,mfgrid,service,bestlevel,lasttime FROM "
+ NETWORK_TABLE
+ " WHERE bssid = ?", args);
if ( cursor.getCount() > 0 ) {
Expand All @@ -1363,23 +1365,26 @@ public Network getNetwork( final String bssid ) {
final String rcois = cursor.getString(8);
final int mfgridInt = cursor.getInt(9);
final String service = cursor.getString(10);
final int level = cursor.getInt(11);
final long lastTime = cursor.getLong(12);

Integer mfgrid = null;
if (mfgridInt != 0) mfgrid = mfgridInt;
List<String> serviceUUIDs = service.isEmpty() ? null :
new ArrayList<>(Arrays.asList(service.split(" ")));

final NetworkType type = NetworkType.typeForCode( cursor.getString(3) );
retval = new Network( bssid, ssid, frequency, capabilities, 0, type, serviceUUIDs, mfgrid );
retval = new Network( bssid, ssid, frequency, capabilities, level, type, serviceUUIDs, mfgrid, lastTime );
if (bestlat != 0 && bestlon != 0) {
retval.setLatLng( new LatLng(bestlat, bestlon) );
}
else {
} else {
retval.setLatLng( new LatLng(lastlat, lastlon) );
}
if (!rcois.isEmpty()) {
retval.setRcois(rcois);
}


MainActivity.getNetworkCache().put( bssid, retval );
}
} catch (DBException ex ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ private Network addOrUpdateBt(final String bssid, final String ssid,
if (network == null) {
//DEBUG: MainActivity.info("new BT net: "+bssid + "(new: "+newForRun+")");
//ALIBI: using frequency to hold deviceType
network = new Network(bssid, ssid, deviceType, capabilities, strength, type, uuid16Services, mfgrId);
network = new Network(bssid, ssid, deviceType, capabilities, strength, type, uuid16Services, mfgrId, null);
networkCache.put(bssid, network);
} else if (NetworkType.BLE.equals(type) && NetworkType.BT.equals(network.getType())) {
//ALIBI: detected via standard bluetooth, updated as LE (LE should win)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class Network implements ClusterItem {

private int frequency;
private int level;
private Long lastTime;
private Integer channel;
private LatLng geoPoint;
private boolean isNew;
Expand Down Expand Up @@ -119,33 +120,36 @@ public enum NetworkBand {
*/
public Network( final ScanResult scanResult ) {
this( scanResult.BSSID, scanResult.SSID, scanResult.frequency, scanResult.capabilities,
scanResult.level, NetworkType.WIFI, null, null, null);
scanResult.level, NetworkType.WIFI, null, null, null, null);
}
public Network( final String bssid, final String ssid, final int frequency, final String capabilities,
final int level, final NetworkType type) {
this(bssid, ssid, frequency, capabilities, level, type, null, null, null);
this(bssid, ssid, frequency, capabilities, level, type, null, null, null, null);
}

public Network( final String bssid, final String ssid, final int frequency, final String capabilities,
final int level, final NetworkType type, final List<String> bleServiceUuid16s, Integer bleMfgrId) {
this(bssid, ssid, frequency, capabilities, level, type, bleServiceUuid16s, bleMfgrId, null);
final int level, final NetworkType type, final List<String> bleServiceUuid16s, Integer bleMfgrId, final Long lastTime) {
this(bssid, ssid, frequency, capabilities, level, type, bleServiceUuid16s, bleMfgrId, null, lastTime);
}

// for WiFiSearchResponse
public Network( final String bssid, final String ssid, final int frequency, final String capabilities,
final int level, final NetworkType type, final LatLng latLng ) {
this(bssid, ssid, frequency, capabilities, level, type, null, null, latLng);
this(bssid, ssid, frequency, capabilities, level, type, null, null, latLng, null);
}

private Network( final String bssid, final String ssid, final int frequency, final String capabilities,
private Network(final String bssid, final String ssid, final int frequency, final String capabilities,
final int level, final NetworkType type, final List<String> bleServiceUuid16s, Integer bleMfgrId,
final LatLng latLng ) {
final LatLng latLng, final Long lastTime ) {
this.bssid = ( bssid == null ) ? "" : bssid.toLowerCase(Locale.US);
this.ssid = ( ssid == null ) ? "" : ssid;
this.frequency = frequency;
this.capabilities = ( capabilities == null ) ? "" : capabilities;
this.level = level;
this.type = type;
if (null != lastTime && lastTime > 0L) {
this.lastTime = lastTime;
}
if (bleMfgrId != null) this.bleMfgrId = bleMfgrId;
if (NetworkType.WIFI.equals(this.type)) {
this.channel = channelForWiFiFrequencyMhz(frequency);
Expand Down Expand Up @@ -263,6 +267,10 @@ public String getRcoisOrBlank() {
return result == null ? "" : result;
}

public Long getLastTime() {
return lastTime;
}

public void setRcois(final String concatenatedRcois) {
this.concatenatedRcois = concatenatedRcois;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.wigle.wigleandroid.model.OUI;
import net.wigle.wigleandroid.util.Logging;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -29,9 +28,6 @@
*/
@Deprecated
public final class NetworkListAdapter extends AbstractListAdapter<Network> {

private final SimpleDateFormat format;

private final List<Network> unsafeNetworks = new ArrayList<>();
private final List<Network> networks = Collections.synchronizedList(unsafeNetworks);

Expand All @@ -43,12 +39,14 @@ public final class NetworkListAdapter extends AbstractListAdapter<Network> {
private final List<Network> cellNets = new ArrayList<>();
private final List<Network> wifiNets = new ArrayList<>();

private final Boolean historical;

public NetworkListAdapter(final Context context, final int rowLayout) {
super(context, rowLayout);
format = NetworkListUtil.getConstructionTimeFormater(context);
if (ListFragment.lameStatic.oui == null) {
ListFragment.lameStatic.oui = new OUI(context.getAssets());
}
historical = false;
}

public void clearWifiAndCell() {
Expand Down Expand Up @@ -272,7 +270,7 @@ public View getView(final int position, final View convertView, final ViewGroup
tv.setText(ouiString + sep);

tv = row.findViewById(R.id.time);
tv.setText(NetworkListUtil.getConstructionTime(format, network));
tv.setText(NetworkListUtil.getTime(network, historical, getContext()));

tv = row.findViewById(R.id.level_string);
final int level = network.getLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.provider.Settings;
import android.text.format.DateFormat;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
Expand All @@ -33,6 +33,16 @@
* Common utility methods for the network list
*/
public class NetworkListUtil {
//ALIBI: while this means you need a restart to get new date/time formats, dynamic calls for each refresh would be heavy.
private static final Locale l = Locale.getDefault();
private static final String timePattern = DateFormat.getBestDateTimePattern(l, "h:mm:ss a");
private static final String timePattern24 = DateFormat.getBestDateTimePattern(l, "H:mm:ss");
private static final String dateTimePattern = DateFormat.getBestDateTimePattern(l, "yyyy-MM-dd h:mm:ss a");
private static final String dateTimePattern24 = DateFormat.getBestDateTimePattern(l, "yyyy-MM-dd H:mm:ss");
private static final SimpleDateFormat timeFormatter = new SimpleDateFormat(timePattern, l);
private static final SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(dateTimePattern, l);
private static final SimpleDateFormat timeFormatter24 = new SimpleDateFormat(timePattern24, l);
private static final SimpleDateFormat dateTimeFormatter24 = new SimpleDateFormat(dateTimePattern24, l);

//color by signal strength
private static final int COLOR_1 = Color.rgb(0, 255, 0);
Expand All @@ -51,19 +61,26 @@ public class NetworkListUtil {
private static final int COLOR_6A = Color.argb(128, 255, 85, 0);
private static final int COLOR_7A = Color.argb(128, 255, 0, 0);

public static String getConstructionTime(final SimpleDateFormat format, final Network network) {
return format.format(new Date(network.getConstructionTime()));
}

public static SimpleDateFormat getConstructionTimeFormater(final Context context) {
final int value = Settings.System.getInt(context.getContentResolver(), Settings.System.TIME_12_24, -1);
SimpleDateFormat format;
if (value == 24) {
format = new SimpleDateFormat("H:mm:ss", Locale.getDefault());
public static String getTime(@NonNull final Network network, final boolean historical, @NonNull final Context context) {
final Long last = network.getLastTime();
if (null == last) {
if (historical) {
//ALIBI: if this is a historical/non-live view, we don't want construction times.
return "";
}
if (DateFormat.is24HourFormat(context)) {
return timeFormatter24.format(new Date(network.getConstructionTime()));
} else {
return timeFormatter.format(new Date(network.getConstructionTime()));
}
// SOMEDAY (SDK26+: return Instant.ofEpochSecond(network.getConstructionTime()).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(timePattern));
}
if (DateFormat.is24HourFormat(context)) {
return dateTimeFormatter24.format(new Date(network.getLastTime()));
} else {
format = new SimpleDateFormat("h:mm:ss a", Locale.getDefault());
return dateTimeFormatter.format(new Date(network.getLastTime()));
}
return format;
// SOMEDAY (SDK 26+): return Instant.ofEpochSecond(network.getConstructionTime()).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(timePattern));
}

public static int getSignalColor(final int level, final boolean alpha) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
package net.wigle.wigleandroid.ui;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.widget.ImageViewCompat;

import net.wigle.wigleandroid.AbstractListAdapter;
import net.wigle.wigleandroid.ListFragment;
import net.wigle.wigleandroid.MainActivity;
import net.wigle.wigleandroid.R;
import net.wigle.wigleandroid.model.Network;
import net.wigle.wigleandroid.model.NetworkType;
import net.wigle.wigleandroid.model.OUI;
import net.wigle.wigleandroid.util.Logging;

import java.text.SimpleDateFormat;
import java.util.Comparator;

/**
* the array adapter for a list of networks.
* note: separators aren't drawn if areAllItemsEnabled or isEnabled are false
*/
public final class SetNetworkListAdapter extends AbstractListAdapter<Network> {
private final SimpleDateFormat format;

private final SetBackedNetworkList networks = new SetBackedNetworkList();

public SetNetworkListAdapter(final Context context, final int rowLayout) {
private final boolean historical;

public SetNetworkListAdapter(final Context context, final boolean historical, final int rowLayout) {
super(context, rowLayout);
format = NetworkListUtil.getConstructionTimeFormater(context);
this.historical = historical;
if (ListFragment.lameStatic.oui == null) {
ListFragment.lameStatic.oui = new OUI(context.getAssets());
}
Expand Down Expand Up @@ -169,10 +168,11 @@ public boolean hasStableIds() {
}

@Override
public void sort(Comparator comparator) {
public void sort(@NonNull Comparator comparator) {
networks.sort(comparator);
}

@NonNull
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
// long start = System.currentTimeMillis();
Expand Down Expand Up @@ -230,7 +230,7 @@ public View getView(final int position, final View convertView, final ViewGroup
}

tv = row.findViewById(R.id.time);
tv.setText(NetworkListUtil.getConstructionTime(format, network));
tv.setText(NetworkListUtil.getTime(network, historical, getContext()));

tv = row.findViewById(R.id.level_string);
final int level = network.getLevel();
Expand Down

0 comments on commit eede48c

Please sign in to comment.