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.content.Context;
22 import android.os.AsyncTask;
23 import android.os.StrictMode;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipOutputStream;
28 import java.io.FileOutputStream;
29 import java.io.FileInputStream;
30 import java.io.BufferedInputStream;
31 import java.io.BufferedOutputStream;
32 import java.io.IOException;
34 public class LogUtil {
36 private static Logger gLogger;
37 private static final String FILE_NAME = "log.txt";
38 private static final long MAX_SIZE = 1024 * 1024 * 10; // 10M
39 private static String logFile = "";
40 //private static File DIR;
41 private static Context mContext;
44 public static void config(Context context) {
46 final LogConfigurator logConfigurator = new LogConfigurator();
48 logFile = context.getExternalFilesDir(null) + 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.setFilePattern("%d [%-6.6t] %-5p [%c{2}]-[%L] %m%n");
54 logConfigurator.configure();
55 gLogger = Logger.getLogger("LogUtil");
57 gLogger.error("##################################################");
58 gLogger.error("### RtmpClient for Android by " + BuildConfig.COMPANY);
59 gLogger.error("### Git Revision: " + BuildConfig.GIT_REVISION);
60 gLogger.error("### Version : " + BuildConfig.VERSION_NAME);
61 gLogger.debug("### Log file is located at: " + logFile);
64 } catch (Exception e){
65 gLogger.error("LogUtil.config error: " + e.getMessage());
71 public static void sendLogs (){
73 AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
75 //private String zfile = DIR + File.separator + "log.zip";
76 private String zfile = mContext.getExternalCacheDir() + File.separator + "log.zip";
81 protected Boolean doInBackground(Void... voids) {
83 String[] s = new String[1];
85 gLogger.debug("zipLog - " + zfile);
88 }catch (Exception e) {
89 gLogger.error("zipLog, error: " + e.getMessage());
98 protected void onPostExecute(Boolean result){
100 gLogger.debug("Zip file completed");
101 File zz = new File(zfile);
104 gLogger.error("zip file error");
112 private static final String SMTP_SERVER = "smtp.exmail.qq.com";
113 private static final String USER = "support@suanzi.ai";
114 private static final String SENT_MAIL = "support@suanzi.ai";
115 private static final String PASSWORD = "oqiDX8fcWa58CmNf";
116 private static final String RECV_MAIL = "support@suanzi.ai";
118 private static boolean sendMail(String attachment){
120 String account = UserInfo.user;
121 String subject = UserInfo.macAddr + " - " + BuildConfig.VERSION_NAME + " - " + BuildConfig.GIT_REVISION;
122 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
123 StrictMode.setThreadPolicy(policy);
125 Properties props = new Properties();
126 props.setProperty("mail.transport.protocol", "smtp");
127 props.setProperty("mail.smtp.host", SMTP_SERVER);
128 props.setProperty("mail.smtp.auth", "true");
129 /*final String smtpPort = "465";
130 props.setProperty("mail.smtp.port", smtpPort);
131 props.setProperty("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
132 props.setProperty("mail.smtp.socketFactory.fallback", "false");
133 props.setProperty("mail.smtp.socketFactory.port", smtpPort);*/
134 Session session = Session.getDefaultInstance(props);
135 session.setDebug(true);
137 MimeMessage message = new MimeMessage(session);
139 message.setFrom(new InternetAddress(SENT_MAIL, account, "UTF-8"));
140 message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECV_MAIL, "support", "UTF-8"));
141 message.setSubject(subject, "UTF-8");
142 //message.setContent("hahaha", "text/html;charset=UTF-8");
143 message.setSentDate(new Date());
146 BodyPart messageBodyPart = new MimeBodyPart();
147 messageBodyPart.setText("This is message body");
148 Multipart multipart = new MimeMultipart();
149 multipart.addBodyPart(messageBodyPart);
150 messageBodyPart = new MimeBodyPart();
151 DataSource source = new FileDataSource(attachment);
152 messageBodyPart.setDataHandler(new DataHandler(source));
154 String fname = attachment.substring(attachment.lastIndexOf("/") + 1);
155 messageBodyPart.setFileName(fname);
156 multipart.addBodyPart(messageBodyPart);
158 // Send the complete message parts
159 message.setContent(multipart);
160 message.saveChanges();
162 Transport transport = session.getTransport();
163 transport.connect(USER, PASSWORD);
164 transport.sendMessage(message, message.getAllRecipients());
166 } catch (Exception e){
167 gLogger.error("send mail, error: " + e.getMessage());
174 private static final int BUFFER_SIZE = 8192;
175 private static void zip(String[] files, String zipFile) throws IOException {
176 BufferedInputStream origin = null;
177 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
180 byte data[] = new byte[BUFFER_SIZE];
182 for (int i = 0; i < files.length; i++) {
183 FileInputStream fi = new FileInputStream(files[i]);
184 origin = new BufferedInputStream(fi, BUFFER_SIZE);
186 ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
187 out.putNextEntry(entry);
189 while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
190 out.write(data, 0, count);
197 }catch (Exception e){