package org.greenstone.android.tipple; import java.io.File; import java.io.PrintStream; import java.text.DecimalFormat; import java.util.Calendar; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.location.Location; public class TippleLog { protected Activity activity_; protected String log_directory_; public PrintStream TIPLOG = null; protected boolean logging_enabled_ = false; protected File logging_file_; protected String logging_filename_final_; public TippleLog(Activity activity, String logDirectory) { activity_ = activity; log_directory_ = logDirectory; boolean logfile_ok = initLogging(); alertLogging(logfile_ok); } protected void optStartLog() { if (TIPLOG!=null) { TIPLOG.println(""); } } protected void optStopLog() { if (TIPLOG != null) { TIPLOG.println(""); TIPLOG.close(); File logging_file_final = new File(logging_filename_final_); logging_file_.renameTo(logging_file_final); } } public void optMessage(String action, String message_line) { if (logging_enabled_) { TIPLOG.println(" "); final long timestamp = System.currentTimeMillis(); final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(timestamp); Date date = cal.getTime(); int hour = date.getHours(); int minute = date.getMinutes(); int seconds = date.getSeconds(); TIPLOG.println(" "+timestamp+""); TIPLOG.println(" "); Location loc = TippleLocationListener.lastKnownLocation; if (loc!=null) { double longitude = loc.getLongitude(); double latitude = loc.getLatitude(); double accuracy = loc.getAccuracy(); TIPLOG.println(" "+longitude+""); TIPLOG.println(" "+latitude+""); TIPLOG.println(" "+accuracy+""); } TIPLOG.println(" "+action+""); if (message_line != "") { TIPLOG.println(" "+message_line+""); } TIPLOG.println(" "); } } public void optMessage(String action) { optMessage(action,""); } DialogInterface.OnClickListener contEnableCallback = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { logging_enabled_ = true; dialog.cancel(); //dialog.notifyAll(); } }; DialogInterface.OnClickListener contDisableCallback = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Delete logging file, then allow application to proceed logging_file_.delete(); logging_file_ = null; TIPLOG = null; logging_enabled_ = false; //SharedPreferences.Editor editor = activity_.sharedPreferences.edit(); //editor.putBoolean("showUserTrail", false); //editor.commit(); dialog.cancel(); } }; DialogInterface.OnClickListener stopCallback = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Delete logging file, then finish application logging_file_.delete(); logging_file_ = null; TIPLOG = null; logging_enabled_ = false; activity_.finish(); } }; protected boolean initLogging() { boolean logfile_ok = false; try { for (int i=1; i<1000; i++) { DecimalFormat zeroFill = new DecimalFormat("000"); String formatted_i = zeroFill.format(i); File potential_log_file = new File(log_directory_,"UserInteract-" + formatted_i + ".log"); if (potential_log_file.exists()) { continue; } logging_file_ = new File(log_directory_,"UserInteract-" + formatted_i); logging_filename_final_ = logging_file_.getAbsolutePath() + ".log"; TIPLOG = new PrintStream(logging_file_); if (TIPLOG != null) { logfile_ok = true; break; } } } catch (Exception e) { logging_file_ = null; e.printStackTrace(); } return logfile_ok; } protected void alertLogging(boolean logfile_ok) { AlertDialog.Builder confirm_log_alert = new AlertDialog.Builder(activity_); if (logfile_ok) { confirm_log_alert.setTitle("Logging Activity"); confirm_log_alert.setMessage("Tipple can log your user interaction. Do you want this activated?"); confirm_log_alert.setPositiveButton(R.string.logging_ok_enable,contEnableCallback); confirm_log_alert.setNegativeButton(R.string.logging_ok_disable,contDisableCallback); } else { confirm_log_alert.setTitle("Error"); confirm_log_alert.setMessage("Failed to initialize user activity log file"); confirm_log_alert.setPositiveButton(R.string.logging_error_continue,contDisableCallback); confirm_log_alert.setNegativeButton(R.string.logging_error_stop,stopCallback); } confirm_log_alert.show(); } }