package ai.suanzi.rtmpclient;
-import android.content.Intent;
-
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import de.mindpipe.android.logging.log4j.LogConfigurator;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
+import javax.mail.BodyPart;
+import javax.mail.Multipart;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMultipart;
+
import java.util.Date;
+
+import android.content.Context;
+import android.os.AsyncTask;
import android.os.StrictMode;
import java.io.File;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+import java.io.FileOutputStream;
+import java.io.FileInputStream;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
public class LogUtil {
private static final String FILE_NAME = "log.txt";
private static final long MAX_SIZE = 1024 * 1024 * 10; // 10M
private static String logFile = "";
+ //private static File DIR;
+ private static Context mContext;
- public static void config(File dir) {
+ public static void config(Context context) {
try {
final LogConfigurator logConfigurator = new LogConfigurator();
- //String fname = getExternalFilesDir(null) + File.separator + "log.txt";
- logFile = dir + File.separator + FILE_NAME;
+ mContext = context;
+ logFile = context.getExternalFilesDir(null) + File.separator + FILE_NAME;
logConfigurator.setFileName(logFile);
logConfigurator.setRootLevel(Level.DEBUG);
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.setMaxFileSize(MAX_SIZE);
+ logConfigurator.setFilePattern("%d [%-6.6t] %-5p [%c{2}]-[%L] %m%n");
logConfigurator.configure();
gLogger = Logger.getLogger("LogUtil");
- gLogger.error("#######################################");
- gLogger.debug("Log file is located at: " + logFile);
+ gLogger.error("");
+ gLogger.error("##################################################");
+ gLogger.error("### RtmpClient for Android by " + BuildConfig.COMPANY);
+ gLogger.error("### Git Revision: " + BuildConfig.GIT_REVISION);
+ gLogger.error("### Version : " + BuildConfig.VERSION_NAME);
+ gLogger.debug("### Log file is located at: " + logFile);
+ gLogger.debug("");
} catch (Exception e){
+ gLogger.error("LogUtil.config error: " + e.getMessage());
e.printStackTrace();
}
- try {
+ }
+
+ public static void sendLogs (){
- int SDK_INT = android.os.Build.VERSION.SDK_INT;
- if (SDK_INT > 8)
- {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
- .permitAll().build();
- StrictMode.setThreadPolicy(policy);
- //your codes here
- MailUtil.sendMail();
+ AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
+ //private String zfile = DIR + File.separator + "log.zip";
+ private String zfile = mContext.getExternalCacheDir() + File.separator + "log.zip";
+
+
+ @Override
+ protected Boolean doInBackground(Void... voids) {
+
+ String[] s = new String[1];
+ s[0] = logFile;
+ gLogger.debug("zipLog - " + zfile);
+ try {
+ zip(s, zfile);
+ }catch (Exception e) {
+ gLogger.error("zipLog, error: " + e.getMessage());
+ e.printStackTrace();
+ return false;
+ }
+
+ sendMail(zfile);
+ return true;
+ }
+ @Override
+ protected void onPostExecute(Boolean result){
+ if(result) {
+ gLogger.debug("Zip file completed");
+ File zz = new File(zfile);
+ zz.delete();
+ } else {
+ gLogger.error("zip file error");
+ }
}
- } catch (Exception e) {
+ };
+ mailTask.execute();
+
+ }
+
+ private static final String SMTP_SERVER = "smtp.exmail.qq.com";
+ private static final String USER = "support@suanzi.ai";
+ private static final String SENT_MAIL = "support@suanzi.ai";
+ private static final String PASSWORD = "oqiDX8fcWa58CmNf";
+ private static final String RECV_MAIL = "support@suanzi.ai";
+
+ private static boolean sendMail(String attachment){
+
+ String account = UserInfo.user;
+ String subject = UserInfo.macAddr + " - " + BuildConfig.VERSION_NAME + " - " + BuildConfig.GIT_REVISION;
+ StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
+ StrictMode.setThreadPolicy(policy);
+
+ Properties props = new Properties();
+ props.setProperty("mail.transport.protocol", "smtp");
+ props.setProperty("mail.smtp.host", SMTP_SERVER);
+ props.setProperty("mail.smtp.auth", "true");
+ /*final String smtpPort = "465";
+ props.setProperty("mail.smtp.port", smtpPort);
+ props.setProperty("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
+ props.setProperty("mail.smtp.socketFactory.fallback", "false");
+ props.setProperty("mail.smtp.socketFactory.port", smtpPort);*/
+ Session session = Session.getDefaultInstance(props);
+ session.setDebug(true);
+
+ MimeMessage message = new MimeMessage(session);
+ try {
+ message.setFrom(new InternetAddress(SENT_MAIL, account, "UTF-8"));
+ message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECV_MAIL, "support", "UTF-8"));
+ message.setSubject(subject, "UTF-8");
+ //message.setContent("hahaha", "text/html;charset=UTF-8");
+ message.setSentDate(new Date());
+
+
+ BodyPart messageBodyPart = new MimeBodyPart();
+ messageBodyPart.setText("This is message body");
+ Multipart multipart = new MimeMultipart();
+ multipart.addBodyPart(messageBodyPart);
+ messageBodyPart = new MimeBodyPart();
+ DataSource source = new FileDataSource(attachment);
+ messageBodyPart.setDataHandler(new DataHandler(source));
+
+ String fname = attachment.substring(attachment.lastIndexOf("/") + 1);
+ messageBodyPart.setFileName(fname);
+ multipart.addBodyPart(messageBodyPart);
+
+ // Send the complete message parts
+ message.setContent(multipart);
+ message.saveChanges();
+
+ Transport transport = session.getTransport();
+ transport.connect(USER, PASSWORD);
+ transport.sendMessage(message, message.getAllRecipients());
+ transport.close();
+ } catch (Exception e){
+ gLogger.error("send mail, error: " + e.getMessage());
e.printStackTrace();
+ return false;
}
+ return true;
}
+ private static final int BUFFER_SIZE = 8192;
+ private static void zip(String[] files, String zipFile) throws IOException {
+ BufferedInputStream origin = null;
+ ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
-
-
-}
-
-
-//
-//
-// public void sendEmail()
-// {
-//
-// private static final int PICK_FROM_GALLERY = 101;
-// int columnIndex;
-// try
-// {
-//
-//
-//
-// final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
-// emailIntent.setType("plain/text");
-// emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "Email" });
-// emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
-// if (URI != null) {
-// emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
-// }
-// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a message");
-// this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));
-// }
-// catch (Throwable t)
-// {
-// Toast.makeText(this, "Request failed try again: " + t.toString(),Toast.LENGTH_LONG).show();
-// }
-// }
-// public void openFolder()
-// {
-// Intent intent = new Intent();
-// intent.setType("image/*");
-// intent.setAction(Intent.ACTION_GET_CONTENT);
-// intent.putExtra("return-data", true);
-// startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
-// }
-//
-// public static sendLogs(){
-//
-// }
-//
-// public void SendLoagcatMail(){
-//
-// // save logcat in file
-// /*File outputFile = new File(Environment.getExternalStorageDirectory(),
-// "logcat.txt");
-// try {
-// Runtime.getRuntime().exec(
-// "logcat -f " + outputFile.getAbsolutePath());
-// } catch (IOException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }*/
-// String fname = getExternalFilesDir(null) + File.separator + "log.txt";
-//
-// File logfile = new File(fname);
-//
-// //send file using email
-// Intent emailIntent = new Intent(Intent.ACTION_SEND);
-// emailIntent.setType("vnd.android.cursor.dir/email");
-// String to[] = {"seudut@gmail.com"};
-// emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
-// // the attachment
-// emailIntent.putExtra(Intent.EXTRA_STREAM, logfile.getAbsolutePath());
-// // the mail subject
-// emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
-// startActivity(Intent.createChooser(emailIntent , "Send email..."));
-// }
-
-//}
+ try {
+ byte data[] = new byte[BUFFER_SIZE];
+
+ for (int i = 0; i < files.length; i++) {
+ FileInputStream fi = new FileInputStream(files[i]);
+ origin = new BufferedInputStream(fi, BUFFER_SIZE);
+ try {
+ ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
+ out.putNextEntry(entry);
+ int count;
+ while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
+ out.write(data, 0, count);
+ }
+ }
+ finally {
+ origin.close();
+ }
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ } finally {
+ out.close();
+ }
+ }
+}
\ No newline at end of file