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