clean up log exporting service. properly end service after exporting

This commit is contained in:
Daniel Gultsch 2015-10-06 14:13:07 +02:00
parent e6af502055
commit 1d2a24c9c0

View file

@ -4,18 +4,8 @@ import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.xmpp.jid.Jid;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
@ -25,6 +15,14 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.xmpp.jid.Jid;
public class ExportLogsService extends Service { public class ExportLogsService extends Service {
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@ -32,20 +30,34 @@ public class ExportLogsService extends Service {
private static final String MESSAGE_STRING_FORMAT = "(%s) %s: %s\n"; private static final String MESSAGE_STRING_FORMAT = "(%s) %s: %s\n";
private static final int NOTIFICATION_ID = 1; private static final int NOTIFICATION_ID = 1;
private static AtomicBoolean running = new AtomicBoolean(false); private static AtomicBoolean running = new AtomicBoolean(false);
private DatabaseBackend mDatabaseBackend;
private List<Account> mAccounts;
@Override
public void onCreate() {
mDatabaseBackend = DatabaseBackend.getInstance(getBaseContext());
mAccounts = mDatabaseBackend.getAccounts();
}
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (running.compareAndSet(false, true)) { if (running.compareAndSet(false, true)) {
new Thread( new Thread(new Runnable() {
new Runnable() {
DatabaseBackend databaseBackend = DatabaseBackend.getInstance(getBaseContext());
List<Account> accounts = databaseBackend.getAccounts();
@Override @Override
public void run() { public void run() {
List<Conversation> conversations = databaseBackend.getConversations(Conversation.STATUS_AVAILABLE); running.set(false);
conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_ARCHIVED)); export();
stopForeground(true);
stopSelf();
}
}).start();
}
return START_NOT_STICKY;
}
private void export() {
List<Conversation> conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE);
conversations.addAll(mDatabaseBackend.getConversations(Conversation.STATUS_ARCHIVED));
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext()); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
mBuilder.setContentTitle(getString(R.string.notification_export_logs_title)) mBuilder.setContentTitle(getString(R.string.notification_export_logs_title))
@ -60,22 +72,18 @@ public class ExportLogsService extends Service {
mBuilder.setProgress(conversations.size(), progress, false); mBuilder.setProgress(conversations.size(), progress, false);
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build()); mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
} }
running.set(false);
stopForeground(true);
} }
private void writeToFile(Conversation conversation) { private void writeToFile(Conversation conversation) {
Jid accountJid = resolveAccountUuid(conversation.getAccountUuid()); Jid accountJid = resolveAccountUuid(conversation.getAccountUuid());
Jid contactJid = conversation.getJid(); Jid contactJid = conversation.getJid();
File dir = new File(String.format(DIRECTORY_STRING_FORMAT, File dir = new File(String.format(DIRECTORY_STRING_FORMAT,accountJid.toBareJid().toString()));
accountJid.toBareJid().toString()));
dir.mkdirs(); dir.mkdirs();
BufferedWriter bw = null; BufferedWriter bw = null;
try { try {
for (Message message : databaseBackend.getMessagesIterable(conversation)) { for (Message message : mDatabaseBackend.getMessagesIterable(conversation)) {
if (message.getType() == Message.TYPE_TEXT || message.hasFileOnRemoteHost()) { if (message.getType() == Message.TYPE_TEXT || message.hasFileOnRemoteHost()) {
String date = simpleDateFormat.format(new Date(message.getTimeSent())); String date = simpleDateFormat.format(new Date(message.getTimeSent()));
if (bw == null) { if (bw == null) {
@ -114,7 +122,7 @@ public class ExportLogsService extends Service {
} }
private Jid resolveAccountUuid(String accountUuid) { private Jid resolveAccountUuid(String accountUuid) {
for (Account account : accounts) { for (Account account : mAccounts) {
if (account.getUuid().equals(accountUuid)) { if (account.getUuid().equals(accountUuid)) {
return account.getJid(); return account.getJid();
} }
@ -130,10 +138,6 @@ public class ExportLogsService extends Service {
return message.getCounterpart().toString(); return message.getCounterpart().toString();
} }
} }
}).start();
}
return START_STICKY;
}
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {