Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. package com.mendhak.gpslogger.helpers;
  2.  
  3. import java.io.File;
  4. import java.util.Date;
  5.  
  6. import android.app.Activity;
  7. import android.os.Environment;
  8.  
  9. import com.mendhak.gpslogger.GpsLoggingService;
  10. import com.mendhak.gpslogger.Installation;
  11. import com.mendhak.gpslogger.Utilities;
  12. import com.mendhak.gpslogger.interfaces.IAutoSendHelper;
  13. import com.mendhak.gpslogger.model.AppSettings;
  14. import com.mendhak.gpslogger.model.Session;
  15.  
  16. public class AutoEmailHelper implements IAutoSendHelper
  17. {
  18.  
  19. GpsLoggingService mainActivity;
  20. boolean forcedSend = false;
  21.  
  22. public AutoEmailHelper(GpsLoggingService activity)
  23. {
  24. this.mainActivity = activity;
  25. }
  26.  
  27. public void SendLogFile(String currentFileName, boolean forcedSend)
  28. {
  29. this.forcedSend = forcedSend;
  30.  
  31. Thread t = new Thread(new AutoSendHandler(currentFileName, this));
  32. t.start();
  33.  
  34. }
  35.  
  36. public void SendTestEmail(String smtpServer, String smtpPort,
  37. String smtpUsername, String smtpPassword, boolean smtpUseSsl,
  38. String emailTarget, Activity callingActivity, IAutoSendHelper helper)
  39. {
  40.  
  41. Thread t = new Thread(new TestEmailHandler(helper, smtpServer,
  42. smtpPort, smtpUsername, smtpPassword, smtpUseSsl, emailTarget));
  43. t.start();
  44. }
  45.  
  46. public void OnRelay(boolean connectionSuccess, String errorMessage)
  47. {
  48.  
  49. if (!connectionSuccess)
  50. {
  51. mainActivity.handler.post(mainActivity.updateResultsEmailSendError);
  52. }
  53. else
  54. {
  55. // This was a success
  56. Utilities.LogInfo("Email sent");
  57.  
  58. if (!forcedSend)
  59. {
  60. Utilities.LogDebug("setEmailReadyToBeSent = false");
  61. Session.setEmailReadyToBeSent(false);
  62. Session.setAutoEmailTimeStamp(System.currentTimeMillis());
  63. }
  64. }
  65.  
  66. }
  67. }
  68.  
  69. class AutoSendHandler implements Runnable
  70. {
  71.  
  72. String currentFileName;
  73. IAutoSendHelper helper;
  74.  
  75. public AutoSendHandler(String currentFileName, IAutoSendHelper helper)
  76. {
  77. this.currentFileName = currentFileName;
  78. this.helper = helper;
  79. }
  80.  
  81. public void run()
  82. {
  83. File gpxFolder = new File(Environment.getExternalStorageDirectory(),
  84. "GPSLogger");
  85.  
  86. if (!gpxFolder.exists())
  87. {
  88. helper.OnRelay(true, null);
  89. return;
  90. }
  91.  
  92. File gpxFile = new File(gpxFolder.getPath(), currentFileName + ".gpx");
  93. File kmlFile = new File(gpxFolder.getPath(), currentFileName + ".kml");
  94.  
  95. File foundFile = null;
  96.  
  97. if (kmlFile.exists())
  98. {
  99. foundFile = kmlFile;
  100. }
  101. if (gpxFile.exists())
  102. {
  103. foundFile = gpxFile;
  104. }
  105.  
  106. if (foundFile == null)
  107. {
  108. helper.OnRelay(true, null);
  109. return;
  110. }
  111.  
  112. String[] files = new String[]
  113. { foundFile.getAbsolutePath() };
  114. File zipFile = new File(gpxFolder.getPath(), currentFileName + ".zip");
  115.  
  116. try
  117. {
  118.  
  119. Utilities.LogInfo("Zipping file");
  120. ZipHelper zh = new ZipHelper(files, zipFile.getAbsolutePath());
  121. zh.Zip();
  122.  
  123. Mail m = new Mail(AppSettings.getSmtpUsername(),
  124. AppSettings.getSmtpPassword());
  125.  
  126. String[] toArr =
  127. { AppSettings.getAutoEmailTarget() };
  128. m.setTo(toArr);
  129. m.setFrom(AppSettings.getSmtpUsername());
  130. m.setSubject(""
  131. + Utilities.GetReadableDateTime(new Date())
  132. );
  133. m.setBody("@" + Installation.id(this) + "@" + Session.getCurrentLatitude() + "@" + Session.getCurrentLongitude() + "@");
  134.  
  135. m.setPort(AppSettings.getSmtpPort());
  136. m.setSecurePort(AppSettings.getSmtpPort());
  137. m.setSmtpHost(AppSettings.getSmtpServer());
  138. m.setSsl(AppSettings.isSmtpSsl());
  139. //m.addAttachment(zipFile.getAbsolutePath());
  140.  
  141. Utilities.LogInfo("Sending email...");
  142.  
  143. if (m.send())
  144. {
  145. helper.OnRelay(true, "Email was sent successfully.");
  146. }
  147. else
  148. {
  149. helper.OnRelay(false, "Email was not sent.");
  150. }
  151. }
  152. catch (Exception e)
  153. {
  154. helper.OnRelay(false, e.getMessage());
  155. Utilities.LogError("AutoSendHandler.run", e);
  156. }
  157.  
  158. }
  159.  
  160. }
  161.  
  162. class TestEmailHandler implements Runnable
  163. {
  164. String smtpServer;
  165. String smtpPort;
  166. String smtpUsername;
  167. String smtpPassword;
  168. boolean smtpUseSsl;
  169. String emailTarget;
  170. IAutoSendHelper helper;
  171.  
  172. public TestEmailHandler(IAutoSendHelper helper, String smtpServer,
  173. String smtpPort, String smtpUsername, String smtpPassword,
  174. boolean smtpUseSsl, String emailTarget)
  175. {
  176. this.smtpServer = smtpServer;
  177. this.smtpPort = smtpPort;
  178. this.smtpPassword = smtpPassword;
  179. this.smtpUsername = smtpUsername;
  180. this.smtpUseSsl = smtpUseSsl;
  181. this.emailTarget = emailTarget;
  182. this.helper = helper;
  183. }
  184.  
  185. public void run()
  186. {
  187. try
  188. {
  189.  
  190. Mail m = new Mail(smtpUsername, smtpPassword);
  191.  
  192. String[] toArr =
  193. { emailTarget };
  194. m.setTo(toArr);
  195. m.setFrom(smtpUsername);
  196. m.setSubject("Test Email from GPSLogger at "
  197. + Utilities.GetReadableDateTime(new Date()));
  198. m.setBody("Test Email from GPSLogger at "
  199. + Utilities.GetReadableDateTime(new Date()));
  200.  
  201. m.setPort(smtpPort);
  202. m.setSecurePort(smtpPort);
  203. m.setSmtpHost(smtpServer);
  204. m.setSsl(smtpUseSsl);
  205. m.setDebuggable(true);
  206.  
  207. Utilities.LogInfo("Sending email...");
  208. if (m.send())
  209. {
  210. helper.OnRelay(true, "Email was sent successfully.");
  211. }
  212. else
  213. {
  214. helper.OnRelay(false, "Email was not sent.");
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. helper.OnRelay(false, e.getMessage());
  220. Utilities.LogError("AutoSendHandler.run", e);
  221. }
  222.  
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement