android: Use ListView for log messages
This is hopefully a bit more efficient for large log files than the previous single TextView. The ListView widget also provides an auto-scroll mechanism.
This commit is contained in:
+128
-107
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2012-2017 Tobias Brunner
|
* Copyright (C) 2012-2018 Tobias Brunner
|
||||||
* HSR Hochschule fuer Technik Rapperswil
|
* HSR Hochschule fuer Technik Rapperswil
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
@@ -15,14 +15,17 @@
|
|||||||
|
|
||||||
package org.strongswan.android.ui;
|
package org.strongswan.android.ui;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.FileObserver;
|
import android.os.FileObserver;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ListView;
|
||||||
|
|
||||||
import org.strongswan.android.R;
|
import org.strongswan.android.R;
|
||||||
import org.strongswan.android.logic.CharonVpnService;
|
import org.strongswan.android.logic.CharonVpnService;
|
||||||
@@ -34,16 +37,15 @@ import java.io.FileReader;
|
|||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class LogFragment extends Fragment implements Runnable
|
public class LogFragment extends Fragment
|
||||||
{
|
{
|
||||||
|
private static String SCROLL_POSITION = "SCROLL_POSITION";
|
||||||
private String mLogFilePath;
|
private String mLogFilePath;
|
||||||
private Handler mLogHandler;
|
private Handler mLogHandler;
|
||||||
private TextView mLogView;
|
private ListView mLog;
|
||||||
private LogScrollView mScrollView;
|
private LogAdapter mLogAdapter;
|
||||||
private BufferedReader mReader;
|
|
||||||
private Thread mThread;
|
|
||||||
private volatile boolean mRunning;
|
|
||||||
private FileObserver mDirectoryObserver;
|
private FileObserver mDirectoryObserver;
|
||||||
|
private int mScrollPosition;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
@@ -51,7 +53,7 @@ public class LogFragment extends Fragment implements Runnable
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
mLogFilePath = getActivity().getFilesDir() + File.separator + CharonVpnService.LOG_FILE;
|
mLogFilePath = getActivity().getFilesDir() + File.separator + CharonVpnService.LOG_FILE;
|
||||||
/* use a handler to update the log view */
|
|
||||||
mLogHandler = new Handler();
|
mLogHandler = new Handler();
|
||||||
|
|
||||||
mDirectoryObserver = new LogDirectoryObserver(getActivity().getFilesDir().getAbsolutePath());
|
mDirectoryObserver = new LogDirectoryObserver(getActivity().getFilesDir().getAbsolutePath());
|
||||||
@@ -61,16 +63,39 @@ public class LogFragment extends Fragment implements Runnable
|
|||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
View view = inflater.inflate(R.layout.log_fragment, null);
|
View view = inflater.inflate(R.layout.log_fragment, null);
|
||||||
mLogView = (TextView)view.findViewById(R.id.log_view);
|
|
||||||
mScrollView = (LogScrollView)view.findViewById(R.id.scroll_view);
|
mLogAdapter = new LogAdapter(getActivity());
|
||||||
|
mLog = view.findViewById(R.id.log);
|
||||||
|
mLog.setAdapter(mLogAdapter);
|
||||||
|
|
||||||
|
mScrollPosition = -1;
|
||||||
|
if (savedInstanceState != null)
|
||||||
|
{
|
||||||
|
mScrollPosition = savedInstanceState.getInt(SCROLL_POSITION, mScrollPosition);
|
||||||
|
}
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle outState)
|
||||||
|
{
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
|
||||||
|
if (mLog.getLastVisiblePosition() == (mLogAdapter.getCount() - 1))
|
||||||
|
{
|
||||||
|
outState.putInt(SCROLL_POSITION, -1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outState.putInt(SCROLL_POSITION, mLog.getFirstVisiblePosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart()
|
public void onStart()
|
||||||
{
|
{
|
||||||
super.onStart();
|
super.onStart();
|
||||||
startLogReader();
|
mLogAdapter.restart();
|
||||||
mDirectoryObserver.startWatching();
|
mDirectoryObserver.startWatching();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,117 +104,114 @@ public class LogFragment extends Fragment implements Runnable
|
|||||||
{
|
{
|
||||||
super.onStop();
|
super.onStop();
|
||||||
mDirectoryObserver.stopWatching();
|
mDirectoryObserver.stopWatching();
|
||||||
stopLogReader();
|
mLogAdapter.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private class LogAdapter extends ArrayAdapter<String> implements Runnable
|
||||||
* Start reading from the log file
|
|
||||||
*/
|
|
||||||
private void startLogReader()
|
|
||||||
{
|
{
|
||||||
try
|
private BufferedReader mReader;
|
||||||
|
private Thread mThread;
|
||||||
|
private volatile boolean mRunning;
|
||||||
|
|
||||||
|
public LogAdapter(@NonNull Context context)
|
||||||
{
|
{
|
||||||
mReader = new BufferedReader(new FileReader(mLogFilePath));
|
super(context, R.layout.log_list_item, R.id.log_line);
|
||||||
}
|
|
||||||
catch (FileNotFoundException e)
|
|
||||||
{
|
|
||||||
mReader = new BufferedReader(new StringReader(""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mLogView.setText("");
|
public void restart()
|
||||||
mRunning = true;
|
|
||||||
mThread = new Thread(this);
|
|
||||||
mThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop reading from the log file
|
|
||||||
*/
|
|
||||||
private void stopLogReader()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
mRunning = false;
|
if (mRunning)
|
||||||
mThread.interrupt();
|
|
||||||
mThread.join();
|
|
||||||
}
|
|
||||||
catch (InterruptedException e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the given log line to the TextView. We strip the prefix off to save
|
|
||||||
* some space in narrow views (it is not that helpful for regular users anyway).
|
|
||||||
*
|
|
||||||
* @param lines log lines to log
|
|
||||||
*/
|
|
||||||
public void logLines(final ArrayList<String> lines)
|
|
||||||
{
|
|
||||||
mLogHandler.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
{
|
||||||
mLogView.beginBatchEdit();
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
clear();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
mReader = new BufferedReader(new FileReader(mLogFilePath));
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException e)
|
||||||
|
{
|
||||||
|
mReader = new BufferedReader(new StringReader(""));
|
||||||
|
}
|
||||||
|
mRunning = true;
|
||||||
|
mThread = new Thread(this);
|
||||||
|
mThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
mRunning = false;
|
||||||
|
mThread.interrupt();
|
||||||
|
mThread.join();
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logLines(final ArrayList<String> lines)
|
||||||
|
{
|
||||||
|
mLogHandler.post(() -> {
|
||||||
|
boolean scroll = getCount() == 0;
|
||||||
|
setNotifyOnChange(false);
|
||||||
for (String line : lines)
|
for (String line : lines)
|
||||||
{
|
{
|
||||||
if (getResources().getConfiguration().screenWidthDp < 600)
|
if (getResources().getConfiguration().screenWidthDp < 600)
|
||||||
{ /* strip off prefix (month=3, day=2, time=8, thread=2, spaces=3) */
|
{ /* strip off prefix (month=3, day=2, time=8, thread=2, spaces=3) */
|
||||||
line = line.length() > 18 ? line.substring(18) : line;
|
line = line.length() > 18 ? line.substring(18) : line;
|
||||||
}
|
}
|
||||||
mLogView.append(line + '\n');
|
add(line);
|
||||||
}
|
}
|
||||||
mLogView.endBatchEdit();
|
notifyDataSetChanged();
|
||||||
/* calling autoScroll() directly does not work, probably because content
|
if (scroll)
|
||||||
* is not yet updated, so we post this to be done later */
|
{ /* scroll to the bottom or saved position after adding the first batch */
|
||||||
mScrollView.post(new Runnable() {
|
mLogHandler.post(() -> mLog.setSelection(mScrollPosition == -1 ? getCount() - 1 : mScrollPosition));
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
mScrollView.autoScroll();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
ArrayList<String> lines = null;
|
|
||||||
|
|
||||||
while (mRunning)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{ /* this works as long as the file is not truncated */
|
|
||||||
String line = mReader.readLine();
|
|
||||||
if (line == null)
|
|
||||||
{
|
|
||||||
if (lines != null)
|
|
||||||
{
|
|
||||||
logLines(lines);
|
|
||||||
lines = null;
|
|
||||||
}
|
|
||||||
/* wait until there is more to log */
|
|
||||||
Thread.sleep(1000);
|
|
||||||
}
|
}
|
||||||
else
|
});
|
||||||
{
|
|
||||||
if (lines == null)
|
|
||||||
{
|
|
||||||
lines = new ArrayList<>();
|
|
||||||
}
|
|
||||||
lines.add(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (lines != null)
|
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
{
|
{
|
||||||
logLines(lines);
|
ArrayList<String> lines = null;
|
||||||
|
|
||||||
|
while (mRunning)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{ /* this works as long as the file is not truncated */
|
||||||
|
String line = mReader.readLine();
|
||||||
|
if (line == null)
|
||||||
|
{
|
||||||
|
if (lines != null)
|
||||||
|
{
|
||||||
|
logLines(lines);
|
||||||
|
lines = null;
|
||||||
|
}
|
||||||
|
/* wait until there is more to log */
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (lines == null)
|
||||||
|
{
|
||||||
|
lines = new ArrayList<>();
|
||||||
|
}
|
||||||
|
lines.add(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (lines != null)
|
||||||
|
{
|
||||||
|
logLines(lines);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,8 +265,7 @@ public class LogFragment extends Fragment implements Runnable
|
|||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
stopLogReader();
|
mLogAdapter.restart();
|
||||||
startLogReader();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2012 Tobias Brunner
|
|
||||||
* Copyright (C) 2012 Giuliano Grassi
|
|
||||||
* Copyright (C) 2012 Ralf Sager
|
|
||||||
* HSR Hochschule fuer Technik Rapperswil
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation; either version 2 of the License, or (at your
|
|
||||||
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
||||||
* for more details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.strongswan.android.ui;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.AttributeSet;
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.ScrollView;
|
|
||||||
|
|
||||||
public class LogScrollView extends ScrollView
|
|
||||||
{
|
|
||||||
private boolean mAutoScroll = true;
|
|
||||||
|
|
||||||
public LogScrollView(Context context)
|
|
||||||
{
|
|
||||||
super(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public LogScrollView(Context context, AttributeSet attrs)
|
|
||||||
{
|
|
||||||
super(context, attrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
public LogScrollView(Context context, AttributeSet attrs, int defStyle)
|
|
||||||
{
|
|
||||||
super(context, attrs, defStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onTouchEvent(MotionEvent ev)
|
|
||||||
{
|
|
||||||
/* disable auto-scrolling when the user starts scrolling around */
|
|
||||||
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
|
|
||||||
{
|
|
||||||
mAutoScroll = false;
|
|
||||||
}
|
|
||||||
return super.onTouchEvent(ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call this to move newly added content into view by scrolling to the bottom.
|
|
||||||
* Nothing happens if auto-scrolling is disabled.
|
|
||||||
*/
|
|
||||||
public void autoScroll()
|
|
||||||
{
|
|
||||||
if (mAutoScroll)
|
|
||||||
{
|
|
||||||
fullScroll(View.FOCUS_DOWN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onScrollChanged(int l, int t, int oldl, int oldt)
|
|
||||||
{
|
|
||||||
super.onScrollChanged(l, t, oldl, oldt);
|
|
||||||
/* if the user scrolls to the bottom we enable auto-scrolling again */
|
|
||||||
if (t == getChildAt(getChildCount() - 1).getHeight() - getHeight())
|
|
||||||
{
|
|
||||||
mAutoScroll = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<!--
|
<!--
|
||||||
Copyright (C) 2012 Tobias Brunner
|
Copyright (C) 2012-2018 Tobias Brunner
|
||||||
Copyright (C) 2012 Giuliano Grassi
|
|
||||||
Copyright (C) 2012 Ralf Sager
|
|
||||||
HSR Hochschule fuer Technik Rapperswil
|
HSR Hochschule fuer Technik Rapperswil
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify it
|
This program is free software; you can redistribute it and/or modify it
|
||||||
@@ -20,23 +18,18 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical" >
|
android:orientation="vertical" >
|
||||||
|
|
||||||
<org.strongswan.android.ui.LogScrollView
|
<ListView
|
||||||
android:id="@+id/scroll_view"
|
android:id="@+id/log"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_margin="10dp"
|
android:layout_margin="10dp"
|
||||||
|
android:dividerHeight="0dp"
|
||||||
|
android:divider="@null"
|
||||||
|
android:fadeScrollbars="false"
|
||||||
android:scrollbarFadeDuration="0"
|
android:scrollbarFadeDuration="0"
|
||||||
android:scrollbarAlwaysDrawVerticalTrack="true" >
|
android:scrollbarAlwaysDrawVerticalTrack="true"
|
||||||
|
android:transcriptMode="normal">
|
||||||
|
|
||||||
<TextView
|
</ListView>
|
||||||
android:id="@+id/log_view"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:textSize="9sp"
|
|
||||||
android:typeface="monospace"
|
|
||||||
android:fontFamily="monospace" >
|
|
||||||
</TextView>
|
|
||||||
|
|
||||||
</org.strongswan.android.ui.LogScrollView>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright (C) 2012-2018 Tobias Brunner
|
||||||
|
HSR Hochschule fuer Technik Rapperswil
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify it
|
||||||
|
under the terms of the GNU General Public License as published by the
|
||||||
|
Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but
|
||||||
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
for more details.
|
||||||
|
-->
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/log_line"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="9sp"
|
||||||
|
android:typeface="monospace"
|
||||||
|
app:fontFamily="monospace" />
|
||||||
Reference in New Issue
Block a user