source: other-projects/tipple-android/trunk/src/org/greenstone/android/tipple/TippleLog.java@ 24257

Last change on this file since 24257 was 24257, checked in by davidb, 13 years ago

Adaptation so code can retrieve all 'Tipple-store' files from asset area' plus have logging off from the beginning

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