added file observer to get notified when files are deleted: fixes #396

This commit is contained in:
iNPUTmice 2014-10-16 02:39:02 +02:00
parent f5019ba966
commit 88d88ffb7b
2 changed files with 32 additions and 3 deletions

View File

@ -72,9 +72,7 @@ public class FileBackend {
public DownloadableFile getFile(Message message,
boolean decrypted) {
StringBuilder filename = new StringBuilder();
filename.append(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath());
filename.append("/Conversations/");
filename.append(getConversationsDirectory());
filename.append(message.getUuid());
if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) {
filename.append(".webp");
@ -88,6 +86,11 @@ public class FileBackend {
return new DownloadableFile(filename.toString());
}
public static String getConversationsDirectory() {
return Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath()+"/Conversations/";
}
public Bitmap resize(Bitmap originalBitmap, int size) {
int w = originalBitmap.getWidth();
int h = originalBitmap.getHeight();

View File

@ -76,6 +76,7 @@ import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.FileObserver;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
@ -144,6 +145,16 @@ public class XmppConnectionService extends Service {
}
};
private FileObserver fileObserver = new FileObserver(FileBackend.getConversationsDirectory()) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.DELETE) {
markFileDeleted(path.split("\\.")[0]);
}
}
};
private final IBinder mBinder = new XmppConnectionBinder();
private OnStatusChanged statusListener = new OnStatusChanged() {
@ -424,6 +435,7 @@ public class XmppConnectionService extends Service {
getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
this.fileObserver.startWatching();
this.pgpServiceConnection = new OpenPgpServiceConnection(
getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService();
@ -814,6 +826,20 @@ public class XmppConnectionService extends Service {
}
}
private void markFileDeleted(String uuid) {
for(Conversation conversation : getConversations()) {
for(Message message : conversation.getMessages()) {
if (message.getType() == Message.TYPE_IMAGE && message.getEncryption() != Message.ENCRYPTION_PGP && message.getUuid().equals(uuid)) {
if (!getFileBackend().isFileAvailable(message)) {
message.setDownloadable(new DeletedDownloadable());
updateConversationUi();
}
return;
}
}
}
}
public void populateWithOrderedConversations(List<Conversation> list) {
populateWithOrderedConversations(list, true);
}