mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-28 11:42:15 -05:00
clean up log exporting service. properly end service after exporting
This commit is contained in:
parent
e6af502055
commit
1d2a24c9c0
@ -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,107 +30,113 @@ 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() {
|
@Override
|
||||||
DatabaseBackend databaseBackend = DatabaseBackend.getInstance(getBaseContext());
|
public void run() {
|
||||||
List<Account> accounts = databaseBackend.getAccounts();
|
running.set(false);
|
||||||
|
export();
|
||||||
@Override
|
stopForeground(true);
|
||||||
public void run() {
|
stopSelf();
|
||||||
List<Conversation> conversations = databaseBackend.getConversations(Conversation.STATUS_AVAILABLE);
|
}
|
||||||
conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_ARCHIVED));
|
}).start();
|
||||||
|
}
|
||||||
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
return START_NOT_STICKY;
|
||||||
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
|
}
|
||||||
mBuilder.setContentTitle(getString(R.string.notification_export_logs_title))
|
|
||||||
.setSmallIcon(R.drawable.ic_import_export_white_24dp)
|
private void export() {
|
||||||
.setProgress(conversations.size(), 0, false);
|
List<Conversation> conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE);
|
||||||
startForeground(NOTIFICATION_ID, mBuilder.build());
|
conversations.addAll(mDatabaseBackend.getConversations(Conversation.STATUS_ARCHIVED));
|
||||||
|
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
int progress = 0;
|
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
|
||||||
for (Conversation conversation : conversations) {
|
mBuilder.setContentTitle(getString(R.string.notification_export_logs_title))
|
||||||
writeToFile(conversation);
|
.setSmallIcon(R.drawable.ic_import_export_white_24dp)
|
||||||
progress++;
|
.setProgress(conversations.size(), 0, false);
|
||||||
mBuilder.setProgress(conversations.size(), progress, false);
|
startForeground(NOTIFICATION_ID, mBuilder.build());
|
||||||
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
|
|
||||||
}
|
int progress = 0;
|
||||||
|
for (Conversation conversation : conversations) {
|
||||||
running.set(false);
|
writeToFile(conversation);
|
||||||
stopForeground(true);
|
progress++;
|
||||||
}
|
mBuilder.setProgress(conversations.size(), progress, false);
|
||||||
|
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
|
||||||
private void writeToFile(Conversation conversation) {
|
}
|
||||||
Jid accountJid = resolveAccountUuid(conversation.getAccountUuid());
|
}
|
||||||
Jid contactJid = conversation.getJid();
|
|
||||||
|
private void writeToFile(Conversation conversation) {
|
||||||
File dir = new File(String.format(DIRECTORY_STRING_FORMAT,
|
Jid accountJid = resolveAccountUuid(conversation.getAccountUuid());
|
||||||
accountJid.toBareJid().toString()));
|
Jid contactJid = conversation.getJid();
|
||||||
dir.mkdirs();
|
|
||||||
|
File dir = new File(String.format(DIRECTORY_STRING_FORMAT,accountJid.toBareJid().toString()));
|
||||||
BufferedWriter bw = null;
|
dir.mkdirs();
|
||||||
try {
|
|
||||||
for (Message message : databaseBackend.getMessagesIterable(conversation)) {
|
BufferedWriter bw = null;
|
||||||
if (message.getType() == Message.TYPE_TEXT || message.hasFileOnRemoteHost()) {
|
try {
|
||||||
String date = simpleDateFormat.format(new Date(message.getTimeSent()));
|
for (Message message : mDatabaseBackend.getMessagesIterable(conversation)) {
|
||||||
if (bw == null) {
|
if (message.getType() == Message.TYPE_TEXT || message.hasFileOnRemoteHost()) {
|
||||||
bw = new BufferedWriter(new FileWriter(
|
String date = simpleDateFormat.format(new Date(message.getTimeSent()));
|
||||||
new File(dir, contactJid.toBareJid().toString() + ".txt")));
|
if (bw == null) {
|
||||||
}
|
bw = new BufferedWriter(new FileWriter(
|
||||||
String jid = null;
|
new File(dir, contactJid.toBareJid().toString() + ".txt")));
|
||||||
switch (message.getStatus()) {
|
}
|
||||||
case Message.STATUS_RECEIVED:
|
String jid = null;
|
||||||
jid = getMessageCounterpart(message);
|
switch (message.getStatus()) {
|
||||||
break;
|
case Message.STATUS_RECEIVED:
|
||||||
case Message.STATUS_SEND:
|
jid = getMessageCounterpart(message);
|
||||||
case Message.STATUS_SEND_RECEIVED:
|
break;
|
||||||
case Message.STATUS_SEND_DISPLAYED:
|
case Message.STATUS_SEND:
|
||||||
jid = accountJid.toBareJid().toString();
|
case Message.STATUS_SEND_RECEIVED:
|
||||||
break;
|
case Message.STATUS_SEND_DISPLAYED:
|
||||||
}
|
jid = accountJid.toBareJid().toString();
|
||||||
if (jid != null) {
|
break;
|
||||||
String body = message.hasFileOnRemoteHost() ? message.getFileParams().url.toString() : message.getBody();
|
}
|
||||||
bw.write(String.format(MESSAGE_STRING_FORMAT, date, jid,
|
if (jid != null) {
|
||||||
body.replace("\\\n", "\\ \n").replace("\n", "\\ \n")));
|
String body = message.hasFileOnRemoteHost() ? message.getFileParams().url.toString() : message.getBody();
|
||||||
}
|
bw.write(String.format(MESSAGE_STRING_FORMAT, date, jid,
|
||||||
}
|
body.replace("\\\n", "\\ \n").replace("\n", "\\ \n")));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
e.printStackTrace();
|
}
|
||||||
} finally {
|
} catch (IOException e) {
|
||||||
try {
|
e.printStackTrace();
|
||||||
if (bw != null) {
|
} finally {
|
||||||
bw.close();
|
try {
|
||||||
}
|
if (bw != null) {
|
||||||
} catch (IOException e1) {
|
bw.close();
|
||||||
e1.printStackTrace();
|
}
|
||||||
}
|
} catch (IOException e1) {
|
||||||
}
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private Jid resolveAccountUuid(String accountUuid) {
|
}
|
||||||
for (Account account : accounts) {
|
|
||||||
if (account.getUuid().equals(accountUuid)) {
|
private Jid resolveAccountUuid(String accountUuid) {
|
||||||
return account.getJid();
|
for (Account account : mAccounts) {
|
||||||
}
|
if (account.getUuid().equals(accountUuid)) {
|
||||||
}
|
return account.getJid();
|
||||||
return null;
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
private String getMessageCounterpart(Message message) {
|
}
|
||||||
String trueCounterpart = (String) message.getContentValues().get(Message.TRUE_COUNTERPART);
|
|
||||||
if (trueCounterpart != null) {
|
private String getMessageCounterpart(Message message) {
|
||||||
return trueCounterpart;
|
String trueCounterpart = (String) message.getContentValues().get(Message.TRUE_COUNTERPART);
|
||||||
} else {
|
if (trueCounterpart != null) {
|
||||||
return message.getCounterpart().toString();
|
return trueCounterpart;
|
||||||
}
|
} else {
|
||||||
}
|
return message.getCounterpart().toString();
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
return START_STICKY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user