From 3ee516599d271f433907321f54b1014bf3f10c12 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Mon, 7 Jun 2021 13:17:50 +0200 Subject: [PATCH 1/4] 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( From 3f5e131250ca121a211c41de7587913d09133762 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Tue, 8 Jun 2021 12:18:33 +0200 Subject: [PATCH 2/4] Use US locale for now --- .../briar/android/conversation/RemovableDriveViewModel.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index e1e3edb20..e9aff76b7 100644 --- 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 @@ -13,7 +13,6 @@ 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; @@ -25,6 +24,7 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; +import static java.util.Locale.US; import static java.util.logging.Logger.getLogger; import static org.briarproject.bramble.api.plugin.file.RemovableDriveConstants.PROP_URI; @@ -48,8 +48,7 @@ public class RemovableDriveViewModel extends AndroidViewModel { } String getFileName() { - SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", - Locale.getDefault()); + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS", US); return sdf.format(new Date()); } From fd810f5c165e05a1d1105ccec6b4467c608563e3 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Tue, 8 Jun 2021 12:24:45 +0200 Subject: [PATCH 3/4] Move to new removabledrive package --- .../briarproject/briar/android/AppModule.java | 2 ++ .../removabledrive/RemovableDriveModule.java | 18 ++++++++++++++++++ .../RemovableDriveViewModel.java | 2 +- .../android/viewmodel/ViewModelModule.java | 8 +------- 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveModule.java rename briar-android/src/main/java/org/briarproject/briar/android/{conversation => removabledrive}/RemovableDriveViewModel.java (98%) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java index 3b942cdb0..ec7e13f8a 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java @@ -25,6 +25,7 @@ import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory; import org.briarproject.bramble.api.reporting.DevConfig; import org.briarproject.bramble.plugin.bluetooth.AndroidBluetoothPluginFactory; import org.briarproject.bramble.plugin.file.AndroidRemovableDrivePluginFactory; +import org.briarproject.bramble.plugin.file.RemovableDriveModule; import org.briarproject.bramble.plugin.tcp.AndroidLanTcpPluginFactory; import org.briarproject.bramble.plugin.tor.AndroidTorPluginFactory; import org.briarproject.bramble.util.AndroidUtils; @@ -92,6 +93,7 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD; GroupListModule.class, GroupConversationModule.class, SharingModule.class, + RemovableDriveModule.class }) public class AppModule { diff --git a/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveModule.java b/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveModule.java new file mode 100644 index 000000000..44dd6e69a --- /dev/null +++ b/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveModule.java @@ -0,0 +1,18 @@ +package org.briarproject.briar.android.removabledrive; + +import org.briarproject.briar.android.viewmodel.ViewModelKey; + +import androidx.lifecycle.ViewModel; +import dagger.Binds; +import dagger.Module; +import dagger.multibindings.IntoMap; + +@Module +public interface RemovableDriveModule { + + @Binds + @IntoMap + @ViewModelKey(RemovableDriveViewModel.class) + ViewModel bindRemovableDriveViewModel(RemovableDriveViewModel removableDriveViewModel); + +} 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/removabledrive/RemovableDriveViewModel.java similarity index 98% rename from briar-android/src/main/java/org/briarproject/briar/android/conversation/RemovableDriveViewModel.java rename to briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveViewModel.java index e9aff76b7..a17fe7a3e 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/RemovableDriveViewModel.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveViewModel.java @@ -1,4 +1,4 @@ -package org.briarproject.briar.android.conversation; +package org.briarproject.briar.android.removabledrive; import android.app.Application; import android.net.Uri; 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 e75045eb4..6e8d295f1 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,7 +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 org.briarproject.briar.android.removabledrive.RemovableDriveViewModel; import javax.inject.Singleton; @@ -41,12 +41,6 @@ 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( From 1ddcd6cfffdd4ea5995128b6ae72b89ece2fa6ba Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Tue, 8 Jun 2021 20:31:23 +0200 Subject: [PATCH 4/4] Make pkg private --- .../briar/android/removabledrive/RemovableDriveViewModel.java | 2 +- .../briarproject/briar/android/viewmodel/ViewModelModule.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveViewModel.java b/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveViewModel.java index a17fe7a3e..d9be940d6 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveViewModel.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/removabledrive/RemovableDriveViewModel.java @@ -29,7 +29,7 @@ import static java.util.logging.Logger.getLogger; import static org.briarproject.bramble.api.plugin.file.RemovableDriveConstants.PROP_URI; @NotNullByDefault -public class RemovableDriveViewModel extends AndroidViewModel { +class RemovableDriveViewModel extends AndroidViewModel { private static final Logger LOG = getLogger(RemovableDriveViewModel.class.getName()); 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 6e8d295f1..ce7899915 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,7 +4,6 @@ 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.removabledrive.RemovableDriveViewModel; import javax.inject.Singleton;