source: other-projects/tipple-android/tipple-lib/src/org/greenstone/android/tipple/base/TippleLog.java@ 26899

Last change on this file since 26899 was 26899, checked in by davidb, 11 years ago

Tipple reborn after Chris's Summer of Code 2013

File size: 5.5 KB
Line 
1package org.greenstone.android.tipple.base;
2
3import java.io.File;
4import java.io.PrintStream;
5import java.text.DecimalFormat;
6import java.util.Calendar;
7import java.util.Date;
8
9import org.greenstone.android.tipple.R;
10
11import android.app.Activity;
12import android.app.AlertDialog;
13import android.content.DialogInterface;
14import android.location.Location;
15
16public class TippleLog
17{
18 protected Activity activity_;
19 protected String log_directory_;
20
21 public PrintStream TIPLOG = null;
22 protected boolean logging_enabled_ = false;
23 protected File logging_file_;
24 protected String logging_filename_final_;
25
26
27 public TippleLog(Activity activity, String logDirectory)
28 {
29 activity_ = activity;
30 log_directory_ = logDirectory;
31
32 //boolean logfile_ok = initLogging();
33 //alertLogging(logfile_ok);
34 }
35
36 public void optStartLog()
37 {
38 if (TIPLOG!=null) {
39 TIPLOG.println("<Log>");
40 }
41 }
42
43 public void optStopLog()
44 {
45 if (TIPLOG != null) {
46 TIPLOG.println("</Log>");
47
48 TIPLOG.close();
49
50
51 File logging_file_final = new File(logging_filename_final_);
52 logging_file_.renameTo(logging_file_final);
53
54
55 }
56 }
57
58 public void optMessage(String action, String message_line)
59 {
60 if (logging_enabled_) {
61
62 TIPLOG.println(" <LogEntry>");
63
64 final long timestamp = System.currentTimeMillis();
65 final Calendar cal = Calendar.getInstance();
66 cal.setTimeInMillis(timestamp);
67 Date date = cal.getTime();
68 int hour = date.getHours();
69 int minute = date.getMinutes();
70 int seconds = date.getSeconds();
71
72 TIPLOG.println(" <Timestamp>"+timestamp+"</Timestamp>");
73 TIPLOG.println(" <Time>" + hour + ":" + minute + ":" + seconds + "</Time>");
74
75 Location loc = TippleLocationListener.lastKnownLocation;
76 if (loc!=null) {
77 double longitude = loc.getLongitude();
78 double latitude = loc.getLatitude();
79 double accuracy = loc.getAccuracy();
80 TIPLOG.println(" <Longitude>"+longitude+"</Longitude>");
81 TIPLOG.println(" <Latitude>"+latitude+"</Latitude>");
82 TIPLOG.println(" <Accuracy>"+accuracy+"</Accuracy>");
83 }
84 TIPLOG.println(" <Action>"+action+"</Action>");
85 if (message_line != "") {
86 TIPLOG.println(" <Message>"+message_line+"</Message>");
87 }
88 TIPLOG.println(" </LogEntry>");
89 }
90 }
91
92 public void optMessage(String action)
93 {
94 optMessage(action,"");
95 }
96
97
98 DialogInterface.OnClickListener contEnableCallback = new DialogInterface.OnClickListener() {
99 public void onClick(DialogInterface dialog, int which) {
100 logging_enabled_ = true;
101 dialog.cancel();
102 //dialog.notifyAll();
103 }
104 };
105
106 DialogInterface.OnClickListener contDisableCallback = new DialogInterface.OnClickListener() {
107 public void onClick(DialogInterface dialog, int which) {
108
109 // Delete logging file, then allow application to proceed
110 logging_file_.delete();
111 logging_file_ = null;
112 TIPLOG = null;
113
114 logging_enabled_ = false;
115
116 //SharedPreferences.Editor editor = activity_.sharedPreferences.edit();
117 //editor.putBoolean("showUserTrail", false);
118 //editor.commit();
119
120 dialog.cancel();
121 }
122 };
123
124 DialogInterface.OnClickListener stopCallback = new DialogInterface.OnClickListener() {
125 public void onClick(DialogInterface dialog, int which) {
126
127 // Delete logging file, then finish application
128
129 logging_file_.delete();
130 logging_file_ = null;
131 TIPLOG = null;
132
133 logging_enabled_ = false;
134
135 activity_.finish();
136 }
137 };
138
139 protected boolean initLogging()
140 {
141 boolean logfile_ok = false;
142
143 try {
144
145 for (int i=1; i<1000; i++) {
146
147 DecimalFormat zeroFill = new DecimalFormat("000");
148 String formatted_i = zeroFill.format(i);
149
150 File potential_log_file = new File(log_directory_,"UserInteract-" + formatted_i + ".log");
151
152 if (potential_log_file.exists()) { continue; }
153
154 logging_file_ = new File(log_directory_,"UserInteract-" + formatted_i);
155 logging_filename_final_ = logging_file_.getAbsolutePath() + ".log";
156
157 TIPLOG = new PrintStream(logging_file_);
158 if (TIPLOG != null) {
159 logfile_ok = true;
160 break;
161 }
162 }
163 }
164 catch (Exception e) {
165 logging_file_ = null;
166 e.printStackTrace();
167 }
168
169 return logfile_ok;
170 }
171
172 protected void alertLogging(boolean logfile_ok)
173 {
174 AlertDialog.Builder confirm_log_alert = new AlertDialog.Builder(activity_);
175
176 if (logfile_ok) {
177 confirm_log_alert.setTitle("Logging Activity");
178 confirm_log_alert.setMessage("Tipple can log your user interaction. Do you want this activated?");
179 confirm_log_alert.setPositiveButton(R.string.logging_ok_enable,contEnableCallback);
180 confirm_log_alert.setNegativeButton(R.string.logging_ok_disable,contDisableCallback);
181 }
182 else {
183 confirm_log_alert.setTitle("Error");
184 confirm_log_alert.setMessage("Failed to initialize user activity log file");
185 confirm_log_alert.setPositiveButton(R.string.logging_error_continue,contDisableCallback);
186 confirm_log_alert.setNegativeButton(R.string.logging_error_stop,stopCallback);
187 }
188
189 confirm_log_alert.show();
190 }
191}
Note: See TracBrowser for help on using the repository browser.