Fix issue when fullscreen
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / LogUtil.java
1 package ai.suanzi.rtmpclient;
2
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;
18
19 import java.util.Date;
20
21 import android.content.Context;
22 import android.os.AsyncTask;
23 import android.os.StrictMode;
24
25 import java.io.File;
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;
33
34 public class LogUtil {
35
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;
42
43
44     public static void config(Context context) {
45         try {
46             final LogConfigurator logConfigurator = new LogConfigurator();
47             mContext = context;
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");
56             gLogger.error("");
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);
62             gLogger.debug("");
63
64         } catch (Exception e){
65             gLogger.error("LogUtil.config error: " + e.getMessage());
66             e.printStackTrace();
67         }
68     }
69
70
71     public static void sendLogs (){
72
73         AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
74
75             private String zfile = mContext.getExternalCacheDir() + File.separator + "log.zip";
76
77             @Override
78             protected Boolean doInBackground(Void... voids) {
79
80                 String[] s = new String[1];
81                 s[0] = logFile;
82                 gLogger.debug("zipLog - " + zfile);
83                 try {
84                     zip(s, zfile);
85                 }catch (Exception e) {
86                     gLogger.error("zipLog, error: " + e.getMessage());
87                     e.printStackTrace();
88                     return false;
89                 }
90
91                 sendMail(zfile);
92                 return true;
93             }
94             @Override
95             protected void onPostExecute(Boolean result){
96                 if(result) {
97                     gLogger.debug("Zip file completed");
98                     File zz = new File(zfile);
99                     zz.delete();
100                 } else {
101                     gLogger.error("zip file error");
102                 }
103             }
104         };
105         mailTask.execute();
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 subject = UserInfo.macAddr + " - " + BuildConfig.VERSION_NAME + " - " + BuildConfig.GIT_REVISION;
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(subject, "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 }