Back to project page Android-Universal-Notifier.
The source code is released under:
Apache License
If you think the Android project Android-Universal-Notifier listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.mairos.universalnotifier.model; /* w w w . j av a2 s . co m*/ import java.io.File; import java.util.ArrayList; import android.content.Context; import android.webkit.JavascriptInterface; import com.mairos.universalnotifier.network.GMailSender; import com.mairos.universalnotifier.network.NetworkOperations; import com.mairos.universalnotifier.network.SMSSender; import com.mairos.universalnotifier.network.NetworkOperations.ResponseResult; //functions to be called from JS code in WebView - so work in WebViewCoreThread public class JSInterpreter { private Context m_context = null; private NotificationTask m_nt; private String m_task_name; public JSInterpreter(Context f_context, NotificationTask f_nt, String f_task_name){ m_context = f_context; m_nt = f_nt; m_task_name = f_task_name; } @JavascriptInterface public void sentCustomMailNotification(String f_message, String f_server, String f_port, String f_login, String f_pwd, String f_from, String f_subject) { sendingNotification(f_message, new CustomMailInfo(f_server, f_port, f_login, f_pwd, f_from, f_subject)); } @JavascriptInterface public void sentNotification(String f_message) { sendingNotification(f_message, null); } public void sendingNotification(String f_message, CustomMailInfo f_mailInfo) { Logger.addToLog("Notification sending started! Message = '" + f_message + "', channel = " + m_nt.getChannel(), m_context ); // TODO - drop after debug if (f_message.length() >= 30 && m_nt.getChannel().equals(NotificationTask.SMS_CHANNEL)) { f_message = f_message.substring(0, 30); } if (m_nt.getChannel().equals(NotificationTask.EMAIL_CHANNEL)){ ArrayList<File> attachs = null; ArrayList<File> files = new ArrayList<File>(); if (m_nt.getAttachments().size() > 0) { attachs = new ArrayList<File>(); File newFile = null; AttachmentData ad = null; for (int i = 0; i < m_nt.getAttachments().size(); i++) { ad = m_nt.getAttachments().get(i); if (ad.getType() == AttachmentData.FTP_TYPE){ newFile = NetworkOperations.getFileFromFTP(ad.getPath(), ad.getLogin(), ad.getPassword(), ad.getFileName()); if (newFile != null) Logger.addToLog(ad.getFileName() + " loaded from ftp", m_context); else Logger.addToLog(ad.getFileName() + " NOT loaded!", m_context); } else if (ad.getType() == AttachmentData.WEB_TYPE){ ResponseResult newFileResult = NetworkOperations.getFileFromHTTP(ad.getPath(), ad.getLogin(), ad.getPassword(), ad.getFileName(), true); if (newFileResult.m_resultFile != null) { Logger.addToLog(ad.getPath() + " loaded from http", m_context); } else if (newFileResult.m_resultFile == null || !newFileResult.m_resultMessage.equals(NetworkOperations.SUCCESS)){ Logger.addToLog(ad.getPath() + " NOT loaded! " + newFileResult.m_resultMessage, m_context); } newFile = newFileResult.m_resultFile; } else if (ad.getType() == AttachmentData.FS_TYPE){ newFile = new File(ad.getPath()); } // if attachment was loaded if (newFile != null){ files.add(newFile); attachs.add(newFile); } } } try { GMailSender sender = null; if (f_mailInfo != null) sender = new GMailSender(f_mailInfo.m_login, f_mailInfo.m_pwd, f_mailInfo.m_server, f_mailInfo.m_port); else sender = new GMailSender(m_nt.getEmailLogin(), m_nt.getEmailPwd()); if (sender.sendMail((f_mailInfo == null) ? "Notification" : f_mailInfo.m_subject, f_message, (f_mailInfo == null) ? m_nt.getEmailLogin() : f_mailInfo.m_from, m_nt.getPersonsString(), attachs, m_context)){ Logger.addToLog("now trying to send mail to " + m_nt.getPersonsString() + ". No network mistakes", m_context); } } catch (Exception e) { Logger.addToLog("send mail error " + e.getMessage(), m_context); // Log.d(MainActivity.LOG, "e.getMessage() = " + e.getMessage()); } /*if (files.size() > 0) Logger.addToLog("Loaded files deleted", m_context); for (File tmpFile : files){ if (tmpFile != null) tmpFile.delete(); }*/ files.clear(); } else if (m_nt.getChannel().equals(NotificationTask.SMS_CHANNEL)){ ArrayList<String> prs = m_nt.getPersons(); for (int i = 0; i < prs.size(); i++) { Logger.addToLog("trying to send SMS to " + prs.get(i), m_context); SMSSender.sendSMS(prs.get(i), f_message, i, m_context); } } } @JavascriptInterface public void addWebAttachment(String f_URL) { m_nt.addAttachment(new AttachmentData(AttachmentData.WEB_TYPE, f_URL, "", "", "")); } @JavascriptInterface public String addFileSystemAttachment(String f_path) { File file = new File(f_path); if (!file.exists()){ m_nt.addAttachment(new AttachmentData(AttachmentData.FS_TYPE, f_path, "", "", "")); return NetworkOperations.preparXMLAnswerToJS(1, f_path + " not found in file system"); } else { m_nt.addAttachment(new AttachmentData(AttachmentData.FS_TYPE, f_path, "", "", "")); return NetworkOperations.preparXMLAnswerToJS(0, f_path + " successfully added to mail"); } } @JavascriptInterface public void addAuthorizedHTTPPictureAttachment(String f_URL, String f_login, String f_pwd, String f_fileName) { m_nt.addAttachment(new AttachmentData(AttachmentData.WEB_TYPE, f_URL, f_login, f_pwd, f_fileName)); } @JavascriptInterface public String loadAuthorizedHTTPPictureAndSave(String f_URL, String f_login, String f_pwd, String f_saveFileName) { return NetworkOperations.loadAuthorizedHTTPPictureAndSave(f_URL, f_login, f_pwd, f_saveFileName); } @JavascriptInterface public void addLog(String f_text) { Logger.addToLog("'" + m_task_name + "' from JS: " + f_text, m_context); } @JavascriptInterface public void addFtpAttachment(String f_URL, String f_login, String f_password, String f_fileName) { m_nt.addAttachment(new AttachmentData(AttachmentData.FTP_TYPE, f_URL, f_login, f_password, f_fileName)); } @JavascriptInterface public String getFtpFilesInfo(String f_URL, String f_login, String f_password, String f_folderName) { return NetworkOperations.getFtpFilesInfo(f_URL, f_login, f_password, f_folderName); } @JavascriptInterface public String saveStringToFile(String f_data, String f_path) { return NetworkOperations.saveStringToFile(f_data, f_path, m_context); } @JavascriptInterface public String loadStringFromFile(String f_path) { return NetworkOperations.loadStringFromFile(f_path, m_context); } @JavascriptInterface public void deleteFileFromFS(String f_path) { File tmpFile = new File(f_path); if (tmpFile.exists()){ tmpFile.delete(); Logger.addToLog(f_path + " deleted!", m_context); } } @JavascriptInterface public String getHTTPResporse(String f_URL) { //Log.d(MainActivity.LOG, "getHTTPResporse from " + f_URL); return NetworkOperations.httpRequest(f_URL); } private static class CustomMailInfo { public String m_server = ""; public String m_port = ""; public String m_login = ""; public String m_pwd = ""; public String m_from = ""; public String m_subject = ""; public CustomMailInfo (String f_server, String f_port, String f_login, String f_pwd, String f_from, String f_subject){ m_server = f_server; m_port = f_port; m_login = f_login; m_pwd = f_pwd; m_from = f_from; m_subject = f_subject; } } }