1 package ai.suanzi.rtmpclient;
3 import org.apache.log4j.Level;
4 import org.apache.log4j.Logger;
5 import de.mindpipe.android.logging.log4j.LogConfigurator;
6 import java.util.Properties;
7 import javax.mail.Transport;
8 import javax.mail.Session;
9 import javax.mail.internet.MimeMessage;
10 import javax.mail.internet.InternetAddress;
11 import javax.activation.DataHandler;
12 import javax.activation.DataSource;
13 import javax.activation.FileDataSource;
14 import javax.mail.BodyPart;
15 import javax.mail.Multipart;
16 import javax.mail.internet.MimeBodyPart;
17 import javax.mail.internet.MimeMultipart;
19 import java.util.Date;
21 import android.os.AsyncTask;
22 import android.os.StrictMode;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipOutputStream;
27 import java.io.FileOutputStream;
28 import java.io.FileInputStream;
29 import java.io.BufferedInputStream;
30 import java.io.BufferedOutputStream;
31 import java.io.IOException;
33 public class LogUtil {
35 private static Logger gLogger;
36 private static final String FILE_NAME = "log.txt";
37 private static final long MAX_SIZE = 1024 * 1024 * 10; // 10M
38 private static String logFile = "";
39 private static File DIR;
42 public static void config(File dir) {
44 final LogConfigurator logConfigurator = new LogConfigurator();
46 logFile = dir + File.separator + FILE_NAME;
47 logConfigurator.setFileName(logFile);
48 logConfigurator.setRootLevel(Level.DEBUG);
49 logConfigurator.setLevel("org.apache", Level.ERROR);
50 logConfigurator.setMaxFileSize(MAX_SIZE);
51 logConfigurator.setFilePattern("%d [%-6.6t] %-5p [%c{2}]-[%L] %m%n");
52 logConfigurator.configure();
53 gLogger = Logger.getLogger("LogUtil");
55 gLogger.error("##################################################");
56 gLogger.error("### RtmpClient for Android by " + BuildConfig.COMPANY);
57 gLogger.error("### Git Revision: " + BuildConfig.GIT_REVISION);
58 gLogger.error("### Version : " + BuildConfig.VERSION_NAME);
59 gLogger.debug("### Log file is located at: " + logFile);
62 } catch (Exception e){
63 gLogger.error("LogUtil.config error: " + e.getMessage());
69 public static void sendLogs (){
71 AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
73 private String zfile = DIR + File.separator + "log.zip";
78 protected Boolean doInBackground(Void... voids) {
80 String[] s = new String[1];
82 gLogger.debug("zipLog - " + zfile);
85 }catch (Exception e) {
86 gLogger.error("zipLog, error: " + e.getMessage());
95 protected void onPostExecute(Boolean result){
97 gLogger.debug("Zip file completed");
98 File zz = new File(zfile);
101 gLogger.error("zip file error");
109 private static final String SMTP_SERVER = "smtp.exmail.qq.com";
110 private static final String USER = "support@suanzi.ai";
111 private static final String SENT_MAIL = "support@suanzi.ai";
112 private static final String PASSWORD = "oqiDX8fcWa58CmNf";
113 private static final String RECV_MAIL = "support@suanzi.ai";
115 private static boolean sendMail(String attachment){
117 String account = UserInfo.user;
118 String subject = UserInfo.macAddr + " - " + BuildConfig.VERSION_NAME + " - " + BuildConfig.GIT_REVISION;
119 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
120 StrictMode.setThreadPolicy(policy);
122 Properties props = new Properties();
123 props.setProperty("mail.transport.protocol", "smtp");
124 props.setProperty("mail.smtp.host", SMTP_SERVER);
125 props.setProperty("mail.smtp.auth", "true");
126 /*final String smtpPort = "465";
127 props.setProperty("mail.smtp.port", smtpPort);
128 props.setProperty("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
129 props.setProperty("mail.smtp.socketFactory.fallback", "false");
130 props.setProperty("mail.smtp.socketFactory.port", smtpPort);*/
131 Session session = Session.getDefaultInstance(props);
132 session.setDebug(true);
134 MimeMessage message = new MimeMessage(session);
136 message.setFrom(new InternetAddress(SENT_MAIL, account, "UTF-8"));
137 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECV_MAIL, "support", "UTF-8"));
138 message.setSubject(subject, "UTF-8");
139 //message.setContent("hahaha", "text/html;charset=UTF-8");
140 message.setSentDate(new Date());
143 BodyPart messageBodyPart = new MimeBodyPart();
144 messageBodyPart.setText("This is message body");
145 Multipart multipart = new MimeMultipart();
146 multipart.addBodyPart(messageBodyPart);
147 messageBodyPart = new MimeBodyPart();
148 DataSource source = new FileDataSource(attachment);
149 messageBodyPart.setDataHandler(new DataHandler(source));
151 String fname = attachment.substring(attachment.lastIndexOf("/") + 1);
152 messageBodyPart.setFileName(fname);
153 multipart.addBodyPart(messageBodyPart);
155 // Send the complete message parts
156 message.setContent(multipart);
157 message.saveChanges();
159 Transport transport = session.getTransport();
160 transport.connect(USER, PASSWORD);
161 transport.sendMessage(message, message.getAllRecipients());
163 } catch (Exception e){
164 gLogger.error("send mail, error: " + e.getMessage());
171 private static final int BUFFER_SIZE = 8192;
172 private static void zip(String[] files, String zipFile) throws IOException {
173 BufferedInputStream origin = null;
174 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
177 byte data[] = new byte[BUFFER_SIZE];
179 for (int i = 0; i < files.length; i++) {
180 FileInputStream fi = new FileInputStream(files[i]);
181 origin = new BufferedInputStream(fi, BUFFER_SIZE);
183 ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
184 out.putNextEntry(entry);
186 while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
187 out.write(data, 0, count);
194 }catch (Exception e){