From 3ee516599d271f433907321f54b1014bf3f10c12 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Mon, 7 Jun 2021 13:17:50 +0200 Subject: [PATCH] Add initial RemovableDriveViewModel --- .../conversation/RemovableDriveViewModel.java | 102 ++++++++++++++++++ .../android/viewmodel/ViewModelModule.java | 7 ++ 2 files changed, 109 insertions(+) create mode 100644 briar-android/src/main/java/org/briarproject/briar/android/conversation/RemovableDriveViewModel.java diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/RemovableDriveViewModel.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/RemovableDriveViewModel.java new file mode 100644 index 000000000..e1e3edb20 --- /dev/null +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/RemovableDriveViewModel.java @@ -0,0 +1,102 @@ +package org.briarproject.briar.android.conversation; + +import android.app.Application; +import android.net.Uri; + +import org.briarproject.bramble.api.Consumer; +import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; +import org.briarproject.bramble.api.plugin.file.RemovableDriveManager; +import org.briarproject.bramble.api.plugin.file.RemovableDriveTask; +import org.briarproject.bramble.api.plugin.file.RemovableDriveTask.State; +import org.briarproject.bramble.api.properties.TransportProperties; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +import javax.annotation.Nullable; +import javax.inject.Inject; + +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; + +import static java.util.logging.Logger.getLogger; +import static org.briarproject.bramble.api.plugin.file.RemovableDriveConstants.PROP_URI; + +@NotNullByDefault +public class RemovableDriveViewModel extends AndroidViewModel { + + private static final Logger LOG = + getLogger(RemovableDriveViewModel.class.getName()); + + private final RemovableDriveManager manager; + + private final ConcurrentHashMap, RemovableDriveTask> + observers = new ConcurrentHashMap<>(); + + @Inject + RemovableDriveViewModel(Application app, + RemovableDriveManager removableDriveManager) { + super(app); + + this.manager = removableDriveManager; + } + + String getFileName() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", + Locale.getDefault()); + return sdf.format(new Date()); + } + + + LiveData write(ContactId contactId, Uri uri) { + TransportProperties p = new TransportProperties(); + p.put(PROP_URI, uri.toString()); + return observe(manager.startWriterTask(contactId, p)); + } + + LiveData read(ContactId contactId, Uri uri) { + TransportProperties p = new TransportProperties(); + p.put(PROP_URI, uri.toString()); + return observe(manager.startReaderTask(contactId, p)); + } + + @Nullable + LiveData ongoingWrite(ContactId contactId) { + RemovableDriveTask task = manager.getCurrentWriterTask(contactId); + if (task == null) { + return null; + } + return observe(task); + } + + @Nullable + LiveData ongoingRead(ContactId contactId) { + RemovableDriveTask task = manager.getCurrentReaderTask(contactId); + if (task == null) { + return null; + } + return observe(task); + } + + private LiveData observe(RemovableDriveTask task) { + MutableLiveData state = new MutableLiveData<>(); + Consumer observer = state::postValue; + task.addObserver(observer); + observers.put(observer, task); + return state; + } + + @Override + protected void onCleared() { + for (Map.Entry, RemovableDriveTask> entry + : observers.entrySet()) { + entry.getValue().removeObserver(entry.getKey()); + } + } +} diff --git a/briar-android/src/main/java/org/briarproject/briar/android/viewmodel/ViewModelModule.java b/briar-android/src/main/java/org/briarproject/briar/android/viewmodel/ViewModelModule.java index ce7899915..e75045eb4 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/viewmodel/ViewModelModule.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/viewmodel/ViewModelModule.java @@ -4,6 +4,7 @@ import org.briarproject.briar.android.contact.add.remote.AddContactViewModel; import org.briarproject.briar.android.contact.add.remote.PendingContactListViewModel; import org.briarproject.briar.android.conversation.ConversationViewModel; import org.briarproject.briar.android.conversation.ImageViewModel; +import org.briarproject.briar.android.conversation.RemovableDriveViewModel; import javax.inject.Singleton; @@ -40,6 +41,12 @@ public abstract class ViewModelModule { abstract ViewModel bindPendingRequestsViewModel( PendingContactListViewModel pendingContactListViewModel); + @Binds + @IntoMap + @ViewModelKey(RemovableDriveViewModel.class) + abstract ViewModel bindRemovableDriveViewModel( + RemovableDriveViewModel removableDriveViewModel); + @Binds @Singleton abstract ViewModelProvider.Factory bindViewModelFactory(