create notification channel for export service
This commit is contained in:
parent
c141f16065
commit
3624d11824
|
@ -6,6 +6,7 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
|
|
||||||
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,121 +26,123 @@ import rocks.xmpp.addr.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");
|
||||||
private static final String DIRECTORY_STRING_FORMAT = FileBackend.getConversationsLogsDirectory() + "/logs/%s";
|
private static final String DIRECTORY_STRING_FORMAT = FileBackend.getConversationsLogsDirectory() + "/logs/%s";
|
||||||
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 DatabaseBackend mDatabaseBackend;
|
||||||
private List<Account> mAccounts;
|
private List<Account> mAccounts;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
mDatabaseBackend = DatabaseBackend.getInstance(getBaseContext());
|
mDatabaseBackend = DatabaseBackend.getInstance(getBaseContext());
|
||||||
mAccounts = mDatabaseBackend.getAccounts();
|
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(() -> {
|
||||||
export();
|
export();
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
running.set(false);
|
running.set(false);
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void export() {
|
private void export() {
|
||||||
List<Conversation> conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE);
|
List<Conversation> conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE);
|
||||||
conversations.addAll(mDatabaseBackend.getConversations(Conversation.STATUS_ARCHIVED));
|
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(), "export");
|
||||||
mBuilder.setContentTitle(getString(R.string.notification_export_logs_title))
|
mBuilder.setContentTitle(getString(R.string.notification_export_logs_title))
|
||||||
.setSmallIcon(R.drawable.ic_import_export_white_24dp)
|
.setSmallIcon(R.drawable.ic_import_export_white_24dp)
|
||||||
.setProgress(conversations.size(), 0, false);
|
.setProgress(conversations.size(), 0, false);
|
||||||
startForeground(NOTIFICATION_ID, mBuilder.build());
|
startForeground(NOTIFICATION_ID, mBuilder.build());
|
||||||
|
|
||||||
int progress = 0;
|
int progress = 0;
|
||||||
for (Conversation conversation : conversations) {
|
for (Conversation conversation : conversations) {
|
||||||
writeToFile(conversation);
|
writeToFile(conversation);
|
||||||
progress++;
|
progress++;
|
||||||
mBuilder.setProgress(conversations.size(), progress, false);
|
mBuilder.setProgress(conversations.size(), progress, false);
|
||||||
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
|
if (mNotifyManager != null) {
|
||||||
}
|
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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,accountJid.asBareJid().toString()));
|
File dir = new File(String.format(DIRECTORY_STRING_FORMAT, accountJid.asBareJid().toString()));
|
||||||
dir.mkdirs();
|
dir.mkdirs();
|
||||||
|
|
||||||
BufferedWriter bw = null;
|
BufferedWriter bw = null;
|
||||||
try {
|
try {
|
||||||
for (Message message : mDatabaseBackend.getMessagesIterable(conversation)) {
|
for (Message message : mDatabaseBackend.getMessagesIterable(conversation)) {
|
||||||
if (message == null)
|
if (message == null)
|
||||||
continue;
|
continue;
|
||||||
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) {
|
||||||
bw = new BufferedWriter(new FileWriter(
|
bw = new BufferedWriter(new FileWriter(
|
||||||
new File(dir, contactJid.asBareJid().toString() + ".txt")));
|
new File(dir, contactJid.asBareJid().toString() + ".txt")));
|
||||||
}
|
}
|
||||||
String jid = null;
|
String jid = null;
|
||||||
switch (message.getStatus()) {
|
switch (message.getStatus()) {
|
||||||
case Message.STATUS_RECEIVED:
|
case Message.STATUS_RECEIVED:
|
||||||
jid = getMessageCounterpart(message);
|
jid = getMessageCounterpart(message);
|
||||||
break;
|
break;
|
||||||
case Message.STATUS_SEND:
|
case Message.STATUS_SEND:
|
||||||
case Message.STATUS_SEND_RECEIVED:
|
case Message.STATUS_SEND_RECEIVED:
|
||||||
case Message.STATUS_SEND_DISPLAYED:
|
case Message.STATUS_SEND_DISPLAYED:
|
||||||
jid = accountJid.asBareJid().toString();
|
jid = accountJid.asBareJid().toString();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (jid != null) {
|
if (jid != null) {
|
||||||
String body = message.hasFileOnRemoteHost() ? message.getFileParams().url.toString() : message.getBody();
|
String body = message.hasFileOnRemoteHost() ? message.getFileParams().url.toString() : message.getBody();
|
||||||
bw.write(String.format(MESSAGE_STRING_FORMAT, date, jid,
|
bw.write(String.format(MESSAGE_STRING_FORMAT, date, jid,
|
||||||
body.replace("\\\n", "\\ \n").replace("\n", "\\ \n")));
|
body.replace("\\\n", "\\ \n").replace("\n", "\\ \n")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (bw != null) {
|
if (bw != null) {
|
||||||
bw.close();
|
bw.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Jid resolveAccountUuid(String accountUuid) {
|
private Jid resolveAccountUuid(String accountUuid) {
|
||||||
for (Account account : mAccounts) {
|
for (Account account : mAccounts) {
|
||||||
if (account.getUuid().equals(accountUuid)) {
|
if (account.getUuid().equals(accountUuid)) {
|
||||||
return account.getJid();
|
return account.getJid();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getMessageCounterpart(Message message) {
|
private String getMessageCounterpart(Message message) {
|
||||||
String trueCounterpart = (String) message.getContentValues().get(Message.TRUE_COUNTERPART);
|
String trueCounterpart = (String) message.getContentValues().get(Message.TRUE_COUNTERPART);
|
||||||
if (trueCounterpart != null) {
|
if (trueCounterpart != null) {
|
||||||
return trueCounterpart;
|
return trueCounterpart;
|
||||||
} else {
|
} else {
|
||||||
return message.getCounterpart().toString();
|
return message.getCounterpart().toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -100,7 +100,7 @@ public class NotificationService {
|
||||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
public void initializeChannels() {
|
public void initializeChannels() {
|
||||||
final Context c = mXmppConnectionService;
|
final Context c = mXmppConnectionService;
|
||||||
NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
|
final NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
|
||||||
if (notificationManager == null) {
|
if (notificationManager == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -129,6 +129,13 @@ public class NotificationService {
|
||||||
videoCompressionChannel.setGroup("status");
|
videoCompressionChannel.setGroup("status");
|
||||||
notificationManager.createNotificationChannel(videoCompressionChannel);
|
notificationManager.createNotificationChannel(videoCompressionChannel);
|
||||||
|
|
||||||
|
final NotificationChannel exportChannel = new NotificationChannel("export",
|
||||||
|
c.getString(R.string.export_channel_name),
|
||||||
|
NotificationManager.IMPORTANCE_LOW);
|
||||||
|
exportChannel.setShowBadge(false);
|
||||||
|
exportChannel.setGroup("status");
|
||||||
|
notificationManager.createNotificationChannel(exportChannel);
|
||||||
|
|
||||||
final NotificationChannel messagesChannel = new NotificationChannel("messages",
|
final NotificationChannel messagesChannel = new NotificationChannel("messages",
|
||||||
c.getString(R.string.messages_channel_name),
|
c.getString(R.string.messages_channel_name),
|
||||||
NotificationManager.IMPORTANCE_HIGH);
|
NotificationManager.IMPORTANCE_HIGH);
|
||||||
|
|
|
@ -736,4 +736,5 @@
|
||||||
<string name="video_compression_channel_name">Video compression</string>
|
<string name="video_compression_channel_name">Video compression</string>
|
||||||
<string name="view_media">View media</string>
|
<string name="view_media">View media</string>
|
||||||
<string name="media_browser">Media browser</string>
|
<string name="media_browser">Media browser</string>
|
||||||
|
<string name="export_channel_name">History export</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue