Add an Activity that shows the log fragment
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / LogFragment.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.ui;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileReader;
22 import java.io.StringReader;
23
24 import org.strongswan.android.R;
25 import org.strongswan.android.logic.CharonVpnService;
26
27 import android.app.Fragment;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.TextView;
34
35 public class LogFragment extends Fragment implements Runnable
36 {
37 private String mLogFilePath;
38 private Handler mLogHandler;
39 private TextView mLogView;
40 private LogScrollView mScrollView;
41 private BufferedReader mReader;
42 private Thread mThread;
43 private volatile boolean mRunning;
44
45 @Override
46 public void onCreate(Bundle savedInstanceState)
47 {
48 super.onCreate(savedInstanceState);
49
50 mLogFilePath = getActivity().getFilesDir() + File.separator + CharonVpnService.LOG_FILE;
51 /* use a handler to update the log view */
52 mLogHandler = new Handler();
53 }
54
55 @Override
56 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
57 {
58 View view = inflater.inflate(R.layout.log_fragment, null);
59 mLogView = (TextView)view.findViewById(R.id.log_view);
60 mScrollView = (LogScrollView)view.findViewById(R.id.scroll_view);
61 return view;
62 }
63
64 @Override
65 public void onStart()
66 {
67 super.onStart();
68 mLogView.setText("");
69 try
70 {
71 mReader = new BufferedReader(new FileReader(mLogFilePath));
72 }
73 catch (FileNotFoundException e)
74 {
75 mReader = new BufferedReader(new StringReader(""));
76 }
77 mRunning = true;
78 mThread = new Thread(this);
79 mThread.start();
80 }
81
82 @Override
83 public void onStop()
84 {
85 super.onStop();
86 try
87 {
88 mRunning = false;
89 mThread.interrupt();
90 mThread.join();
91 }
92 catch (InterruptedException e)
93 {
94 }
95 }
96
97 /**
98 * Write the given log line to the TextView. We strip the prefix off to save
99 * some space (it is not that helpful for regular users anyway).
100 * @param line log line to log
101 */
102 public void logLine(final String line)
103 {
104 mLogHandler.post(new Runnable() {
105 @Override
106 public void run()
107 {
108 /* strip off prefix (month=3, day=2, time=8, thread=2, spaces=3) */
109 mLogView.append((line.length() > 18 ? line.substring(18) : line) + '\n');
110 /* calling autoScroll() directly does not work, probably because content
111 * is not yet updated, so we post this to be done later */
112 mScrollView.post(new Runnable() {
113 @Override
114 public void run()
115 {
116 mScrollView.autoScroll();
117 }
118 });
119 }
120 });
121 }
122
123 @Override
124 public void run()
125 {
126 while (mRunning)
127 {
128 try
129 {
130 String line = mReader.readLine();
131 if (line == null)
132 { /* wait until there is more to log */
133 Thread.sleep(1000);
134 }
135 else
136 {
137 logLine(line);
138 }
139 }
140 catch (Exception e)
141 {
142 break;
143 }
144 }
145 }
146 }