Search in sources :

Example 1 with Formatter

use of ca.rmen.android.networkmonitor.app.dbops.backend.export.Formatter 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));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Formatter(ca.rmen.android.networkmonitor.app.dbops.backend.export.Formatter) Selection(ca.rmen.android.networkmonitor.app.prefs.FilterPreferences.Selection) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Cursor (android.database.Cursor)1 Formatter (ca.rmen.android.networkmonitor.app.dbops.backend.export.Formatter)1 Selection (ca.rmen.android.networkmonitor.app.prefs.FilterPreferences.Selection)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1