ExportLogsService.java

  1package eu.siacs.conversations.services;
  2
  3import android.app.NotificationManager;
  4import android.app.Service;
  5import android.content.Context;
  6import android.content.Intent;
  7import android.os.IBinder;
  8import android.support.v4.app.NotificationCompat;
  9import java.io.BufferedWriter;
 10import java.io.File;
 11import java.io.FileWriter;
 12import java.io.IOException;
 13import java.text.SimpleDateFormat;
 14import java.util.Date;
 15import java.util.List;
 16import java.util.concurrent.atomic.AtomicBoolean;
 17
 18import eu.siacs.conversations.R;
 19import eu.siacs.conversations.entities.Account;
 20import eu.siacs.conversations.entities.Conversation;
 21import eu.siacs.conversations.entities.Message;
 22import eu.siacs.conversations.persistance.DatabaseBackend;
 23import eu.siacs.conversations.persistance.FileBackend;
 24import eu.siacs.conversations.xmpp.jid.Jid;
 25
 26public class ExportLogsService extends Service {
 27
 28	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
 29	private static final String DIRECTORY_STRING_FORMAT = FileBackend.getConversationsLogsDirectory() + "/logs/%s";
 30	private static final String MESSAGE_STRING_FORMAT = "(%s) %s: %s\n";
 31	private static final int NOTIFICATION_ID = 1;
 32	private static AtomicBoolean running = new AtomicBoolean(false);
 33	private DatabaseBackend mDatabaseBackend;
 34	private List<Account> mAccounts;
 35
 36	@Override
 37	public void onCreate() {
 38		mDatabaseBackend = DatabaseBackend.getInstance(getBaseContext());
 39		mAccounts = mDatabaseBackend.getAccounts();
 40	}
 41
 42	@Override
 43	public int onStartCommand(Intent intent, int flags, int startId) {
 44		if (running.compareAndSet(false, true)) {
 45			new Thread(new Runnable() {
 46				@Override
 47				public void run() {
 48					export();
 49					stopForeground(true);
 50					running.set(false);
 51					stopSelf();
 52				}
 53			}).start();
 54		}
 55		return START_NOT_STICKY;
 56	}
 57
 58	private void export() {
 59		List<Conversation> conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE);
 60		conversations.addAll(mDatabaseBackend.getConversations(Conversation.STATUS_ARCHIVED));
 61		NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 62		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
 63		mBuilder.setContentTitle(getString(R.string.notification_export_logs_title))
 64				.setSmallIcon(R.drawable.ic_import_export_white_24dp)
 65				.setProgress(conversations.size(), 0, false);
 66		startForeground(NOTIFICATION_ID, mBuilder.build());
 67
 68		int progress = 0;
 69		for (Conversation conversation : conversations) {
 70			writeToFile(conversation);
 71			progress++;
 72			mBuilder.setProgress(conversations.size(), progress, false);
 73			mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
 74		}
 75	}
 76
 77	private void writeToFile(Conversation conversation) {
 78		Jid accountJid = resolveAccountUuid(conversation.getAccountUuid());
 79		Jid contactJid = conversation.getJid();
 80
 81		File dir = new File(String.format(DIRECTORY_STRING_FORMAT,accountJid.toBareJid().toString()));
 82		dir.mkdirs();
 83
 84		BufferedWriter bw = null;
 85		try {
 86			for (Message message : mDatabaseBackend.getMessagesIterable(conversation)) {
 87				if (message.getType() == Message.TYPE_TEXT || message.hasFileOnRemoteHost()) {
 88					String date = simpleDateFormat.format(new Date(message.getTimeSent()));
 89					if (bw == null) {
 90						bw = new BufferedWriter(new FileWriter(
 91								new File(dir, contactJid.toBareJid().toString() + ".txt")));
 92					}
 93					String jid = null;
 94					switch (message.getStatus()) {
 95						case Message.STATUS_RECEIVED:
 96							jid = getMessageCounterpart(message);
 97							break;
 98						case Message.STATUS_SEND:
 99						case Message.STATUS_SEND_RECEIVED:
100						case Message.STATUS_SEND_DISPLAYED:
101							jid = accountJid.toBareJid().toString();
102							break;
103					}
104					if (jid != null) {
105						String body = message.hasFileOnRemoteHost() ? message.getFileParams().url.toString() : message.getBody();
106						bw.write(String.format(MESSAGE_STRING_FORMAT, date, jid,
107								body.replace("\\\n", "\\ \n").replace("\n", "\\ \n")));
108					}
109				}
110			}
111		} catch (IOException e) {
112			e.printStackTrace();
113		} finally {
114			try {
115				if (bw != null) {
116					bw.close();
117				}
118			} catch (IOException e1) {
119				e1.printStackTrace();
120			}
121		}
122	}
123
124	private Jid resolveAccountUuid(String accountUuid) {
125		for (Account account : mAccounts) {
126			if (account.getUuid().equals(accountUuid)) {
127				return account.getJid();
128			}
129		}
130		return null;
131	}
132
133	private String getMessageCounterpart(Message message) {
134		String trueCounterpart = (String) message.getContentValues().get(Message.TRUE_COUNTERPART);
135		if (trueCounterpart != null) {
136			return trueCounterpart;
137		} else {
138			return message.getCounterpart().toString();
139		}
140	}
141
142	@Override
143	public IBinder onBind(Intent intent) {
144		return null;
145	}
146}