Files
OpenTracks/src/main/java/de/dennisguse/opentracks/util/CsvLayoutUtils.java
T
Román Martínez 7f9057c519 Added profiles for custom layouts.
Users can add severals custom layouts and select one of them in recording fragment. The last selected layout is the one that will be use in the next recording.
Fixes #938.
2021-12-07 18:07:25 +01:00

78 lines
2.2 KiB
Java

package de.dennisguse.opentracks.util;
import androidx.annotation.Nullable;
import com.google.android.material.navigation.NavigationBarView;
import java.util.Arrays;
import java.util.List;
public final class CsvLayoutUtils {
public static final String LINE_SEPARATOR = "\n";
public static final String ITEM_SEPARATOR = ";";
public static final String PROPERTY_SEPARATOR = ",";
private CsvLayoutUtils() {
}
/**
* @param csvLine Layout description in a CSV format.
* @return All CSV parts from the csvLine or null if it's malformed.
*/
@Nullable
public static List<String> getCsvLineParts(String csvLine) {
if (csvLine == null) {
return null;
}
// The line must have three items, at least: layout's name, number of columns and one field.
List<String> csvParts = Arrays.asList(csvLine.split(ITEM_SEPARATOR));
if (csvParts.size() < 3 || !hasValue(csvParts.get(0)) || !isInt(csvParts.get(1))) {
return null;
}
return csvParts;
}
/**
* @param csvField Layout's field in a CSV format.
* @return List of field parts or null if it's malformed.
*/
@Nullable
public static String[] getCsvFieldParts(@Nullable String csvField) {
if (csvField == null) {
return null;
}
// Field must have three items: key, is visible (0 or 1), is primary (0 or 1).
String[] fieldParts = csvField.split(CsvLayoutUtils.PROPERTY_SEPARATOR);
if (fieldParts.length < 3 || !hasValue(fieldParts[0]) || !hasZeroOneValue(fieldParts[1]) || !hasZeroOneValue(fieldParts[2])) {
return null;
}
return fieldParts;
}
private static boolean hasValue(String value) {
return value != null && !value.isEmpty();
}
private static boolean isInt(String value) {
if (value == null || value.isEmpty()) {
return false;
}
try {
Integer.parseInt(value);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private static boolean hasZeroOneValue(String value) {
return isInt(value) && (Integer.parseInt(value) == 0 || Integer.parseInt(value) == 1);
}
}