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