Skip to content

Commit

Permalink
Merge pull request #31 from jpage4500/feature/12-10
Browse files Browse the repository at this point in the history
- fix carrier showing just "," or carrier followed by a ","
  • Loading branch information
jpage4500 authored Jan 16, 2025
2 parents a7b9ae1 + f0b31c7 commit 4686f10
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
31 changes: 30 additions & 1 deletion src/main/java/com/jpage4500/devicemanager/data/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class Device {
public final static String PROP_SDK = "ro.build.version.sdk";
public final static String PROP_MODEL = "ro.product.model";
public final static String PROP_OS = "ro.build.version.release";
public final static String PROP_CARRIER = "gsm.sim.operator.alpha";
private final static String PROP_CARRIER = "gsm.sim.operator.alpha";
private final static String PROP_CARRIER_2 = "gsm.operator.alpha";
public final static String PROP_BRAND = "ro.product.brand";
public final static String PROP_NAME = "ro.product.name";

Expand Down Expand Up @@ -101,6 +102,34 @@ public String getProperty(String key) {
else return propMap.get(key);
}

/**
* get phone carrier (ie: T-Mobile)
*/
public String getCarrier() {
String prop = getProperty(Device.PROP_CARRIER);
// often we just get "," for the carrier
prop = cleanCarrierString(prop);
if (TextUtils.isEmpty(prop)) {
prop = getProperty(Device.PROP_CARRIER_2);
prop = cleanCarrierString(prop);
if (TextUtils.isEmpty(prop)) {
// TODO: try some other properties..
}
}
return prop;
}

/**
* often we just get "," for the carrier or "T-Mobile,"
* - remove the comma
*/
private String cleanCarrierString(String carrier) {
int pos = TextUtils.indexOf(carrier, ",");
if (pos == 0) return null;
else if (pos > 0) return carrier.substring(0, pos);
return carrier;
}

public String getCustomProperty(String key) {
if (customPropertyMap == null) return null;
else return customPropertyMap.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public String deviceValue(Device device, int column) {
case NAME -> TextUtils.firstValid(device.nickname, device.getProperty(Device.PROP_MODEL));
case MODEL -> device.getProperty(Device.PROP_MODEL);
case OS -> device.getProperty(Device.PROP_OS);
case CARRIER -> device.getProperty(Device.PROP_CARRIER);
case CARRIER -> device.getCarrier();
case PHONE -> device.phone;
case IMEI -> device.imei;
case FREE -> FileUtils.bytesToGigDisplayString(device.freeSpace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@
import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.*;

public class DeviceRowSorter extends TableRowSorter<TableModel> {
private static final Logger log = LoggerFactory.getLogger(DeviceRowSorter.class);

private final DeviceRowFilter deviceRowFilter;

private final Map<String, SortType> sortTypeMap;

enum SortType {
TYPE_ALPHA,
TYPE_NUMBER,
TYPE_VERSION,
}

public DeviceRowSorter(TableModel model) {
super(model);
deviceRowFilter = new DeviceRowFilter();
sortTypeMap = new HashMap<>();

// default sort
List<SortKey> sortKeys = new ArrayList<>();
Expand Down Expand Up @@ -51,8 +58,33 @@ public Comparator<?> getComparator(int c) {
if (columnType == DeviceTableModel.Columns.BATTERY) {
return Integer.compare(d1.batteryLevel, d2.batteryLevel);
}

String value1 = model.deviceValue(d1, c);
String value2 = model.deviceValue(d2, c);

if (columnType == null) {
// custom columns
String colName = model.getColumnName(c);
SortType sortType = sortTypeMap.get(colName);
if (sortType == null) {
boolean isNumber1 = TextUtils.isNumber(value1);
boolean isNumber2 = TextUtils.isNumber(value2);
if (isNumber1 && isNumber2) {
sortType = SortType.TYPE_NUMBER;
} else {
sortType = SortType.TYPE_ALPHA;
}
sortTypeMap.put(colName, sortType);
}

if (sortType == SortType.TYPE_NUMBER) {
long number1 = TextUtils.getNumberLong(value1, 0);
long number2 = TextUtils.getNumberLong(value2, 0);
return Long.compare(number1, number2);
}
}

// default sort by alpha (case insensitive)
int rc = TextUtils.compareToIgnoreCase(value1, value2);
return rc;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,15 @@ private void handleCopyClipboardFieldCommand() {

List<Device> selectedDeviceList = getSelectedDevices(true);
if (selectedDeviceList.isEmpty()) return;
if (table.getSelectedColumn() < 0) return;
int selectedColumn = table.getSelectedColumn();
int modelCol = table.convertColumnIndexToModel(selectedColumn);
if (modelCol < 0) return;
log.trace("handleCopyClipboardFieldCommand: col:{}, devices:{}", modelCol, selectedDeviceList.size());

StringBuilder sb = new StringBuilder();
for (Device device : selectedDeviceList) {
if (!sb.isEmpty()) sb.append("\n");
String value = model.deviceValue(device, table.getSelectedColumn());
String value = model.deviceValue(device, modelCol);
sb.append(value != null ? value : "");
}
StringSelection stringSelection = new StringSelection(sb.toString());
Expand Down Expand Up @@ -903,7 +906,7 @@ private void handleDeviceDetails(Device device) {
addDeviceDetail(panel, "Model", device.getProperty(Device.PROP_MODEL));
addDeviceDetail(panel, "Phone", device.phone);
addDeviceDetail(panel, "IMEI", device.imei);
addDeviceDetail(panel, "Carrier", device.getProperty(Device.PROP_CARRIER));
addDeviceDetail(panel, "Carrier", device.getCarrier());
addDeviceDetail(panel, "OS", device.getProperty(Device.PROP_OS));
addDeviceDetail(panel, "SDK", device.getProperty(Device.PROP_SDK));
addDeviceDetail(panel, "Free Space", FileUtils.bytesToDisplayString(device.freeSpace));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ public String getTextIfTruncated(int row, int col) {
if (row == -1) {
// header
JTableHeader header = getTableHeader();
TableColumn column = header.getColumnModel().getColumn(col);
TableColumnModel headerColumnModel = header.getColumnModel();
if (col < 0 || col >= headerColumnModel.getColumnCount()) return "";
TableColumn column = headerColumnModel.getColumn(col);
Object value = column.getHeaderValue();
int width = column.getWidth();
Component c = header.getDefaultRenderer().getTableCellRendererComponent(this, value, false, false, row, col);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ public static long getNumberLong(String text, long defValue) {
return defValue;
}

public static boolean isNumber(String text) {
try {
Integer.parseInt(text);
return true;
} catch (Exception ignored) {
}
return false;
}

public static int compareToIgnoreCase(String value1, String value2) {
if (value1 == value2) return 0;
else if (value1 == null || value2 == null) {
Expand Down

0 comments on commit 4686f10

Please sign in to comment.