1 package ai.suanzi.rtmpclient;
3 import android.content.Intent;
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;
21 import java.util.Date;
23 import android.os.AsyncTask;
24 import android.os.StrictMode;
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;
35 public class LogUtil {
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;
44 public static void config(File dir) {
46 final LogConfigurator logConfigurator = new LogConfigurator();
48 logFile = dir + File.separator + FILE_NAME;
49 logConfigurator.setFileName(logFile);
50 logConfigurator.setRootLevel(Level.DEBUG);
51 logConfigurator.setLevel("org.apache", Level.ERROR);
52 logConfigurator.setMaxFileSize(MAX_SIZE);
53 logConfigurator.configure();
54 gLogger = Logger.getLogger("LogUtil");
55 gLogger.error("#######################################");
56 gLogger.error("RtmpClient for Android by suanzi.ai ");
57 gLogger.error("Git Revision: " + BuildConfig.GIT_REVISION);
58 gLogger.error("Version : " + BuildConfig.VERSION_NAME);
59 gLogger.debug("Log file is located at: " + logFile);
61 } catch (Exception e){
62 gLogger.error("LogUtil.config error: " + e.getMessage());
68 public static void sendLogs (){
70 AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
72 private String zfile = DIR + File.separator + "log.zip";
76 protected Boolean doInBackground(Void... voids) {
78 String[] s = new String[1];
80 gLogger.debug("zipLog - " + zfile);
83 }catch (Exception e) {
84 gLogger.error("zipLog, error: " + e.getMessage());
93 protected void onPostExecute(Boolean result){
95 gLogger.debug("Zip file completed");
96 File zz = new File(zfile);
99 gLogger.error("zip file error");
107 private static final String SMTP_SERVER = "smtp.exmail.qq.com";
108 private static final String USER = "support@suanzi.ai";
109 private static final String SENT_MAIL = "support@suanzi.ai";
110 private static final String PASSWORD = "oqiDX8fcWa58CmNf";
111 private static final String RECV_MAIL = "support@suanzi.ai";
113 private static boolean sendMail(String attachment){
115 String account = UserInfo.user;
116 String subject = UserInfo.macAddr + " - " + BuildConfig.VERSION_NAME + " - " + BuildConfig.GIT_REVISION;
117 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
118 StrictMode.setThreadPolicy(policy);
120 Properties props = new Properties();
121 props.setProperty("mail.transport.protocol", "smtp");
122 props.setProperty("mail.smtp.host", SMTP_SERVER);
123 props.setProperty("mail.smtp.auth", "true");
124 /*final String smtpPort = "465";
125 props.setProperty("mail.smtp.port", smtpPort);
126 props.setProperty("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
127 props.setProperty("mail.smtp.socketFactory.fallback", "false");
128 props.setProperty("mail.smtp.socketFactory.port", smtpPort);*/
129 Session session = Session.getDefaultInstance(props);
130 session.setDebug(true);
132 MimeMessage message = new MimeMessage(session);
134 message.setFrom(new InternetAddress(SENT_MAIL, account, "UTF-8"));
135 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECV_MAIL, "support", "UTF-8"));
136 message.setSubject(subject, "UTF-8");
137 //message.setContent("hahaha", "text/html;charset=UTF-8");
138 message.setSentDate(new Date());
141 BodyPart messageBodyPart = new MimeBodyPart();
142 messageBodyPart.setText("This is message body");
143 Multipart multipart = new MimeMultipart();
144 multipart.addBodyPart(messageBodyPart);
145 messageBodyPart = new MimeBodyPart();
146 DataSource source = new FileDataSource(attachment);
147 messageBodyPart.setDataHandler(new DataHandler(source));
149 String fname = attachment.substring(attachment.lastIndexOf("/") + 1);
150 messageBodyPart.setFileName(fname);
151 multipart.addBodyPart(messageBodyPart);
153 // Send the complete message parts
154 message.setContent(multipart);
155 message.saveChanges();
157 Transport transport = session.getTransport();
158 transport.connect(USER, PASSWORD);
159 transport.sendMessage(message, message.getAllRecipients());
161 } catch (Exception e){
162 gLogger.error("send mail, error: " + e.getMessage());
169 private static final int BUFFER_SIZE = 8192;
170 private static void zip(String[] files, String zipFile) throws IOException {
171 BufferedInputStream origin = null;
172 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
175 byte data[] = new byte[BUFFER_SIZE];
177 for (int i = 0; i < files.length; i++) {
178 FileInputStream fi = new FileInputStream(files[i]);
179 origin = new BufferedInputStream(fi, BUFFER_SIZE);
181 ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
182 out.putNextEntry(entry);
184 while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
185 out.write(data, 0, count);
192 }catch (Exception e){