use of ca.rmen.android.networkmonitor.app.prefs.FilterPreferences.Selection in project network-monitor by caarmen.
the class KMLExport method execute.
@Override
public void execute(ProgressListener listener) {
Log.v(TAG, "export");
Formatter formatter = FormatterFactory.getFormatter(FormatterStyle.XML, mContext);
List<String> selectedColumns = new ArrayList<>(NetMonPreferences.getInstance(mContext).getSelectedColumns());
if (!selectedColumns.contains(NetMonColumns.DEVICE_LATITUDE))
selectedColumns.add(NetMonColumns.DEVICE_LATITUDE);
if (!selectedColumns.contains(NetMonColumns.DEVICE_LONGITUDE))
selectedColumns.add(NetMonColumns.DEVICE_LONGITUDE);
if (!selectedColumns.contains(mPlacemarkNameColumn))
selectedColumns.add(mPlacemarkNameColumn);
final String[] columnsToExport = new String[selectedColumns.size()];
selectedColumns.toArray(columnsToExport);
Map<String, String> columnNamesMapping = new HashMap<>(columnsToExport.length);
// Filter the results based on the user's preferences.
Selection selection = FilterPreferences.getSelectionClause(mContext);
Cursor c = mContext.getContentResolver().query(NetMonColumns.CONTENT_URI, columnsToExport, selection.selectionString, selection.selectionArgs, NetMonColumns.TIMESTAMP);
if (c != null) {
try {
Log.v(TAG, "Find user-friendly labels for columns " + Arrays.toString(columnsToExport));
for (String element : columnsToExport) {
String columnLabel = NetMonColumns.getColumnLabel(mContext, element);
Log.v(TAG, element + "->" + columnLabel);
columnNamesMapping.put(element, columnLabel);
}
Log.v(TAG, "Column names: " + Arrays.toString(columnsToExport));
KMLStyle kmlStyle = KMLStyleFactory.getKMLStyle(mPlacemarkNameColumn);
int placemarkNameColumnId = c.getColumnIndex(mPlacemarkNameColumn);
String now = DATE_FORMAT.format(new Date());
String title = mContext.getString(R.string.app_name) + ": " + columnNamesMapping.get(mPlacemarkNameColumn) + " (" + now + ")";
KMLWriter kmlWriter = new KMLWriter(mFile, title, kmlStyle, mContext.getString(R.string.export_value_unknown), columnNamesMapping);
// Write the KML placemarks to the file.
int rowCount = c.getCount();
int latitudeIndex = c.getColumnIndex(NetMonColumns.DEVICE_LATITUDE);
int longitudeIndex = c.getColumnIndex(NetMonColumns.DEVICE_LONGITUDE);
int timestampIndex = c.getColumnIndex(NetMonColumns.TIMESTAMP);
// Start writing to the file.
kmlWriter.writeHeader();
// Write one KML placemark for each row in the DB.
while (c.moveToNext() && !isCanceled()) {
Map<String, String> cellValues = new LinkedHashMap<>(c.getColumnCount());
long timestamp = -1;
if (timestampIndex >= 0) {
timestamp = c.getLong(timestampIndex);
}
for (int i = 0; i < c.getColumnCount(); i++) {
String cellValue = formatter.format(c, i);
cellValues.put(c.getColumnName(i), cellValue);
}
String placemarkName = formatter.format(c, placemarkNameColumnId);
kmlWriter.writePlacemark(placemarkName, cellValues, c.getString(latitudeIndex), c.getString(longitudeIndex), timestamp);
// Notify the listener of our progress (progress is 1-based)
if (listener != null)
listener.onProgress(c.getPosition() + 1, rowCount);
}
// Write the footer and clean up the file.
kmlWriter.writeFooter();
kmlWriter.close();
if (listener != null) {
if (isCanceled()) {
listener.onComplete(mContext.getString(R.string.export_notif_canceled_content));
} else {
listener.onComplete(mContext.getString(R.string.export_save_to_external_storage_success, mFile.getAbsolutePath()));
}
}
} catch (FileNotFoundException e) {
Log.e(TAG, "Could not export to file " + mFile + ": " + e.getMessage(), e);
} finally {
c.close();
}
}
if (listener != null)
listener.onError(mContext.getString(R.string.export_notif_error_content));
}
use of ca.rmen.android.networkmonitor.app.prefs.FilterPreferences.Selection in project network-monitor by caarmen.
the class TableFileExport method export.
/**
* @param recordCount export at most this number of records. If recordCount is 0 or less, all records will be exported.
* @return the file if it was correctly exported, null otherwise.
*/
public File export(int recordCount, ProgressListener listener) {
Log.v(TAG, "export " + (recordCount <= 0 ? "all" : recordCount) + " records");
String[] usedColumnNames = (String[]) NetMonPreferences.getInstance(mContext).getSelectedColumns().toArray();
Formatter formatter = FormatterFactory.getFormatter(mFormatterStyle, mContext);
// Order and filter the results based on the user's preferences.
SortPreferences sortPreferences = NetMonPreferences.getInstance(mContext).getSortPreferences();
Selection selection = FilterPreferences.getSelectionClause(mContext);
Uri uri = NetMonColumns.CONTENT_URI;
if (recordCount > 0)
uri = uri.buildUpon().appendQueryParameter(NetMonProvider.QUERY_PARAMETER_LIMIT, String.valueOf(recordCount)).build();
Cursor c = mContext.getContentResolver().query(uri, usedColumnNames, selection.selectionString, selection.selectionArgs, sortPreferences.getOrderByClause());
if (c != null) {
try {
for (int i = 0; i < usedColumnNames.length; i++) usedColumnNames[i] = NetMonColumns.getColumnLabel(mContext, usedColumnNames[i]);
Log.v(TAG, "Column names: " + Arrays.toString(usedColumnNames));
// Write the table rows to the file.
int rowsAvailable = c.getCount();
// Start writing to the file.
writeHeader(usedColumnNames);
while (c.moveToNext() && !isCanceled()) {
String[] cellValues = new String[c.getColumnCount()];
for (int i = 0; i < c.getColumnCount(); i++) cellValues[i] = formatter.format(c, i);
writeRow(c.getPosition(), cellValues);
// Notify the listener of our progress (progress is 1-based)
if (listener != null)
listener.onProgress(c.getPosition() + 1, rowsAvailable);
// Some file exports need to create the whole file in memory
// before saving it. (This is currently the case with
// the Excel export, whether we use jexcelapi or poi).
// On some devices, with large exports, we may not
// have enough memory to export the whole file.
// Here we detect a low memory situation, and stop
// creating rows.
long maxMemory = Runtime.getRuntime().maxMemory();
long allocatedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long pctFreeMemory = ((maxMemory - allocatedMemory) * 100) / maxMemory;
if (c.getPosition() % 100 == 0) {
Log.v(TAG, "pctFreeMemory:" + pctFreeMemory);
}
if (pctFreeMemory < THRESHOLD_LOW_MEMORY_PCT) {
Log.v(TAG, "Not enough memory to export the whole file");
if (listener != null) {
listener.onWarning(mContext.getString(R.string.export_warning_file_too_big_message));
}
break;
}
}
// Write the footer and clean up the file.
writeFooter();
if (listener != null) {
if (isCanceled()) {
listener.onComplete(mContext.getString(R.string.export_notif_canceled_content));
} else {
listener.onComplete(mContext.getString(R.string.export_save_to_external_storage_success, mFile.getAbsolutePath()));
}
}
return mFile;
} catch (IOException e) {
Log.e(TAG, "export Could not export file " + mFile + ": " + e.getMessage(), e);
} finally {
c.close();
}
}
if (listener != null)
listener.onError(mContext.getString(R.string.export_notif_error_content));
return null;
}
Aggregations