9018d9d0f8fad470acbffadc424826d2fac19ecd
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / LogUtil.java
1 package ai.suanzi.rtmpclient;
2
3 import android.content.Intent;
4
5 import org.apache.log4j.Level;
6 import org.apache.log4j.Logger;
7 import de.mindpipe.android.logging.log4j.LogConfigurator;
8 import java.util.Properties;
9 import javax.mail.Transport;
10 import javax.mail.Session;
11 import javax.mail.internet.MimeMessage;
12 import javax.mail.internet.InternetAddress;
13 import javax.activation.DataHandler;
14 import javax.activation.DataSource;
15 import javax.activation.FileDataSource;
16 import javax.mail.BodyPart;
17 import javax.mail.Multipart;
18 import javax.mail.internet.MimeBodyPart;
19 import javax.mail.internet.MimeMultipart;
20
21 import java.util.Date;
22
23 import android.os.AsyncTask;
24 import android.os.StrictMode;
25
26 import java.io.File;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipOutputStream;
29 import java.io.FileOutputStream;
30 import java.io.FileInputStream;
31 import java.io.BufferedInputStream;
32 import java.io.BufferedOutputStream;
33 import java.io.IOException;
34
35 public class LogUtil {
36
37     private static Logger gLogger;
38     private static final String FILE_NAME = "log.txt";
39     private static final long MAX_SIZE = 1024 * 1024 * 10; // 10M
40     private static String logFile = "";
41     private static File DIR;
42
43
44     public static void config(File dir) {
45         try {
46             final LogConfigurator logConfigurator = new LogConfigurator();
47             //String fname = getExternalFilesDir(null) + File.separator + "log.txt";
48             DIR = dir;
49             logFile = dir + File.separator + FILE_NAME;
50             logConfigurator.setFileName(logFile);
51             logConfigurator.setRootLevel(Level.DEBUG);
52             logConfigurator.setLevel("org.apache", Level.ERROR);
53             logConfigurator.setMaxFileSize(MAX_SIZE);
54             logConfigurator.configure();
55             gLogger = Logger.getLogger("LogUtil");
56             gLogger.error("#######################################");
57             gLogger.error("RtmpClient by suanzi.ai");
58             gLogger.debug("Log file is located at: " + logFile);
59
60         } catch (Exception e){
61             gLogger.error("LogUtil.config error: " + e.getMessage());
62             e.printStackTrace();
63         }
64     }
65
66     public static void sendLogs (){
67
68         AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
69
70             private String zfile = DIR + File.separator + "log.zip";
71
72
73             @Override
74             protected Boolean doInBackground(Void... voids) {
75
76                 // zip log.txt to log.zip
77                 String[] s = new String[1];
78                 s[0] = logFile;
79                 gLogger.debug("zipLog - " + zfile);
80                 try {
81                     zip(s, zfile);
82                 }catch (Exception e) {
83                     gLogger.error("zipLog, error: " + e.getMessage());
84                     e.printStackTrace();
85                     return false;
86                 }
87
88                 sendMail(zfile);
89                 return true;
90             }
91             @Override
92             protected void onPostExecute(Boolean result){
93                 if(result) {
94                     gLogger.debug("Zip file completed");
95                     File zz = new File(zfile);
96                     zz.delete();
97                 } else {
98                     gLogger.error("zip file error");
99                 }
100             }
101         };
102         mailTask.execute();
103
104     }
105
106
107
108
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";
114
115     private static boolean sendMail(String attachment){
116
117         String account = UserInfo.user;
118         String macAddr = UserInfo.macAddr;
119         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
120         StrictMode.setThreadPolicy(policy);
121
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);
133
134         MimeMessage message = new MimeMessage(session);
135         try {
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(macAddr, "UTF-8");
139             //message.setContent("hahaha", "text/html;charset=UTF-8");
140             message.setSentDate(new Date());
141
142
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));
150
151             String fname = attachment.substring(attachment.lastIndexOf("/") + 1);
152             messageBodyPart.setFileName(fname);
153             multipart.addBodyPart(messageBodyPart);
154
155             // Send the complete message parts
156             message.setContent(multipart);
157             message.saveChanges();
158
159             Transport transport = session.getTransport();
160             transport.connect(USER, PASSWORD);
161             transport.sendMessage(message, message.getAllRecipients());
162             transport.close();
163         } catch (Exception e){
164             gLogger.error("send mail, error: " + e.getMessage());
165             e.printStackTrace();
166             return false;
167         }
168         return true;
169     }
170
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)));
175
176         try {
177             byte data[] = new byte[BUFFER_SIZE];
178
179             for (int i = 0; i < files.length; i++) {
180                 FileInputStream fi = new FileInputStream(files[i]);
181                 origin = new BufferedInputStream(fi, BUFFER_SIZE);
182                 try {
183                     ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
184                     out.putNextEntry(entry);
185                     int count;
186                     while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
187                         out.write(data, 0, count);
188                     }
189                 }
190                 finally {
191                     origin.close();
192                 }
193             }
194         }catch (Exception e){
195             e.printStackTrace();
196         } finally {
197             out.close();
198         }
199     }
200 }