Search in sources :

Example 1 with NetMonPreferences

use of ca.rmen.android.networkmonitor.app.prefs.NetMonPreferences 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;
                    }
                }
            });
        });
    });
}
Also used : HTMLExport(ca.rmen.android.networkmonitor.app.dbops.backend.export.HTMLExport) WebResourceRequest(android.webkit.WebResourceRequest) Intent(android.content.Intent) SuppressLint(android.annotation.SuppressLint) NetMonPreferences(ca.rmen.android.networkmonitor.app.prefs.NetMonPreferences) SortPreferences(ca.rmen.android.networkmonitor.app.prefs.SortPreferences) Bitmap(android.graphics.Bitmap) WebView(android.webkit.WebView) ProgressBar(android.widget.ProgressBar) File(java.io.File) TargetApi(android.annotation.TargetApi) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 2 with NetMonPreferences

use of ca.rmen.android.networkmonitor.app.prefs.NetMonPreferences in project network-monitor by caarmen.

the class NetMonService method scheduleTests.

/**
 * Start scheduling tests, using the scheduler class chosen by the user in the advanced settings.
 */
private void scheduleTests() {
    Log.v(TAG, "scheduleTests");
    NetMonPreferences prefs = NetMonPreferences.getInstance(this);
    PreferencesMigrator prefsMigrator = new PreferencesMigrator(this);
    prefsMigrator.migratePreferences();
    Class<?> schedulerClass = prefs.getSchedulerClass();
    Log.v(TAG, "Will use scheduler " + schedulerClass);
    // noinspection TryWithIdenticalCatches
    try {
        if (mScheduler == null || !mScheduler.getClass().getName().equals(schedulerClass.getName())) {
            Log.v(TAG, "Creating new scheduler " + schedulerClass);
            if (mScheduler != null)
                mScheduler.onDestroy();
            mScheduler = (Scheduler) schedulerClass.newInstance();
            mScheduler.onCreate(this);
            mScheduler.schedule(mTask, prefs.getUpdateInterval());
        } else {
            Log.v(TAG, "Rescheduling scheduler " + mScheduler);
            int interval = prefs.getUpdateInterval();
            mScheduler.setInterval(interval);
        }
    } catch (InstantiationException e) {
        Log.e(TAG, "setScheduler Could not create scheduler " + schedulerClass + ": " + e.getMessage(), e);
    } catch (IllegalAccessException e) {
        Log.e(TAG, "setScheduler Could not create scheduler " + schedulerClass + ": " + e.getMessage(), e);
    }
}
Also used : NetMonPreferences(ca.rmen.android.networkmonitor.app.prefs.NetMonPreferences) PreferencesMigrator(ca.rmen.android.networkmonitor.app.prefs.PreferencesMigrator)

Aggregations

NetMonPreferences (ca.rmen.android.networkmonitor.app.prefs.NetMonPreferences)2 SuppressLint (android.annotation.SuppressLint)1 TargetApi (android.annotation.TargetApi)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 WebResourceRequest (android.webkit.WebResourceRequest)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 ProgressBar (android.widget.ProgressBar)1 HTMLExport (ca.rmen.android.networkmonitor.app.dbops.backend.export.HTMLExport)1 PreferencesMigrator (ca.rmen.android.networkmonitor.app.prefs.PreferencesMigrator)1 SortPreferences (ca.rmen.android.networkmonitor.app.prefs.SortPreferences)1 File (java.io.File)1