use of ca.rmen.android.networkmonitor.app.dbops.backend.export.HTMLExport in project network-monitor by caarmen.
the class LogActivity method loadHTMLFile.
/**
* Read the data from the DB, export it to an HTML file, and load the HTML file in the WebView.
*/
// only loading local files, it's ok.
@SuppressLint("SetJavaScriptEnabled")
private void loadHTMLFile() {
Log.v(TAG, "loadHTMLFile");
final ProgressBar progressBar = findViewById(R.id.progress_bar);
assert progressBar != null;
progressBar.setVisibility(View.VISIBLE);
startRefreshIconAnimation();
final boolean freezeHeader = NetMonPreferences.getInstance(this).getFreezeHtmlTableHeader();
final String fixedTableHeight;
if (freezeHeader) {
// I've come to the following calculation by trial and error.
// I've noticed that when entering the web view, the density is equal to the scale/zoom, but I'm
// not sure if it's the density or zoom which really matters in this calculation.
// We subtract 100px from the scaled webview height to account for the table header.
fixedTableHeight = ((mWebView.getHeight() / getResources().getDisplayMetrics().density) - 100) + "px";
} else {
fixedTableHeight = null;
}
AsyncTask.execute(() -> {
Log.v(TAG, "loadHTMLFile:doInBackground");
// Export the DB to the HTML file.
HTMLExport htmlExport = new HTMLExport(LogActivity.this, false, fixedTableHeight);
int recordCount = NetMonPreferences.getInstance(LogActivity.this).getFilterRecordCount();
File result = htmlExport.export(recordCount, null);
runOnUiThread(() -> {
Log.v(TAG, "loadHTMLFile:onPostExecute, result=" + result);
if (isFinishing()) {
Log.v(TAG, "finishing, ignoring loadHTMLFile result");
return;
}
WebView webView = mWebView;
if (webView == null) {
Log.v(TAG, "Must be destroyed or destroying, we have no webview, ignoring loadHTMLFile result");
return;
}
if (result == null) {
Snackbar.make(webView, R.string.error_reading_log, Snackbar.LENGTH_LONG).show();
return;
}
// Load the exported HTML file into the WebView.
// Save our current horizontal scroll position so we can keep our
// horizontal position after reloading the page.
final int oldScrollX = webView.getScrollX();
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file://" + result.getAbsolutePath());
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.v(TAG, "onPageStarted");
// http://stackoverflow.com/questions/6855715/maintain-webview-content-scroll-position-on-orientation-change
if (oldScrollX > 0) {
String jsScrollX = "javascript:window:scrollTo(" + oldScrollX + " / window.devicePixelRatio,0);";
view.loadUrl(jsScrollX);
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
stopRefreshIconAnimation();
}
@Override
@TargetApi(Build.VERSION_CODES.N)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return loadUrl(request.getUrl().toString()) || super.shouldOverrideUrlLoading(view, request);
}
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return loadUrl(url) || super.shouldOverrideUrlLoading(view, url);
}
private boolean loadUrl(String url) {
Log.v(TAG, "url: " + url);
// the sorting preference (column name, ascending or descending order).
if (url.startsWith(HTMLExport.URL_SORT)) {
NetMonPreferences prefs = NetMonPreferences.getInstance(LogActivity.this);
SortPreferences oldSortPreferences = prefs.getSortPreferences();
// The new column used for sorting will be the one the user tapped on.
String newSortColumnName = url.substring(HTMLExport.URL_SORT.length());
SortPreferences.SortOrder newSortOrder = oldSortPreferences.sortOrder;
// toggle the sort order between ascending and descending.
if (newSortColumnName.equals(oldSortPreferences.sortColumnName)) {
if (oldSortPreferences.sortOrder == SortPreferences.SortOrder.DESC)
newSortOrder = SortPreferences.SortOrder.ASC;
else
newSortOrder = SortPreferences.SortOrder.DESC;
}
// Update the sorting preferences (our shared preference change listener will be notified
// and reload the page).
prefs.setSortPreferences(new SortPreferences(newSortColumnName, newSortOrder));
return true;
} else // If the user clicked on the filter icon, start the filter activity for this column.
if (url.startsWith(HTMLExport.URL_FILTER)) {
Intent intent = new Intent(LogActivity.this, FilterColumnActivity.class);
String columnName = url.substring(HTMLExport.URL_FILTER.length());
intent.putExtra(FilterColumnActivity.EXTRA_COLUMN_NAME, columnName);
startActivityForResult(intent, REQUEST_CODE_FILTER_COLUMN);
return true;
} else {
return false;
}
}
});
});
});
}
use of ca.rmen.android.networkmonitor.app.dbops.backend.export.HTMLExport in project network-monitor by caarmen.
the class DBOpIntentService method handleActionExport.
private void handleActionExport(ExportFormat exportFileFormat, Bundle extras) {
Log.d(TAG, "handleActionExport() called with exportFileFormat = [" + exportFileFormat + "]");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null)
return;
FileExport fileExport;
switch(exportFileFormat) {
case CSV:
fileExport = new CSVExport(this);
break;
case DB:
fileExport = new DBExport(this);
break;
case EXCEL:
fileExport = new ExcelExport(this);
break;
case HTML:
fileExport = new HTMLExport(this);
break;
case KML:
String placemarkColumnName = extras.getString(EXTRA_EXPORT_KML_PLACEMARK_COLUMN_NAME);
fileExport = new KMLExport(this, placemarkColumnName);
break;
case GNUPLOT:
fileExport = new GnuplotExport(this);
break;
case SUMMARY:
default:
fileExport = null;
}
mDBOperation = fileExport;
File file = null;
if (fileExport != null) {
fileExport.execute(mExportProgressListener);
file = fileExport.getFile();
}
// Start the summary report
Intent shareIntent = FileExport.getShareIntent(this, file);
// All done
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, shareIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notificationBuilder;
if (fileExport != null && fileExport.isCanceled()) {
notificationBuilder = prepareFileExportNotification(getString(R.string.export_notif_canceled_title), getString(R.string.export_notif_canceled_content), pendingIntent);
} else {
notificationBuilder = prepareFileExportNotification(getString(R.string.export_notif_complete_title), getString(R.string.export_notif_complete_content), pendingIntent);
}
notificationBuilder.addAction(R.drawable.ic_pref_share, getString(R.string.action_share), pendingIntent);
notificationManager.notify(FileExport.class.hashCode(), notificationBuilder.build());
}
use of ca.rmen.android.networkmonitor.app.dbops.backend.export.HTMLExport in project network-monitor by caarmen.
the class ReportEmailer method createAttachment.
/**
* @param fileType one of the file types the user selected in the preference
* @return the BodyPart containing the exported file of this type.
*/
private File createAttachment(String fileType) {
Log.v(TAG, "createAttachment: fileType = " + fileType);
// Get the FileExport instance which can export mContext file type.
final FileExport fileExport;
switch(fileType) {
case "csv":
fileExport = new CSVExport(mContext);
break;
case "html":
fileExport = new HTMLExport(mContext);
break;
case "excel":
fileExport = new ExcelExport(mContext);
break;
case "kml":
fileExport = new KMLExport(mContext, NetMonColumns.SOCKET_CONNECTION_TEST);
break;
case "gnuplot":
fileExport = new GnuplotExport(mContext);
break;
case "db":
default:
fileExport = new DBExport(mContext);
break;
}
fileExport.execute(null);
return fileExport.getFile();
}
Aggregations