diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java index 7f83ce22f..428d8f721 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java @@ -42,10 +42,9 @@ import com.nextcloud.talk.activities.CallActivity; import com.nextcloud.talk.activities.MainActivity; import com.nextcloud.talk.api.NcApi; import com.nextcloud.talk.application.NextcloudTalkApplication; +import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager; import com.nextcloud.talk.data.user.model.User; import com.nextcloud.talk.models.SignatureVerification; -import com.nextcloud.talk.models.database.ArbitraryStorageEntity; -import com.nextcloud.talk.models.database.UserEntity; import com.nextcloud.talk.models.json.chat.ChatUtils; import com.nextcloud.talk.models.json.conversations.Conversation; import com.nextcloud.talk.models.json.conversations.RoomOverall; @@ -59,7 +58,6 @@ import com.nextcloud.talk.utils.DoNotDisturbUtils; import com.nextcloud.talk.utils.NotificationUtils; import com.nextcloud.talk.utils.PushUtils; import com.nextcloud.talk.utils.bundle.BundleKeys; -import com.nextcloud.talk.utils.database.arbitrarystorage.ArbitraryStorageUtils; import com.nextcloud.talk.utils.preferences.AppPreferences; import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder; @@ -91,6 +89,7 @@ import androidx.work.Data; import androidx.work.Worker; import androidx.work.WorkerParameters; import autodagger.AutoInjector; +import io.reactivex.Maybe; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import okhttp3.JavaNetCookieJar; @@ -107,7 +106,7 @@ public class NotificationWorker extends Worker { AppPreferences appPreferences; @Inject - ArbitraryStorageUtils arbitraryStorageUtils; + ArbitraryStorageManager arbitraryStorageManger; @Inject Retrofit retrofit; @@ -133,14 +132,19 @@ public class NotificationWorker extends Worker { private void showNotificationForCallWithNoPing(Intent intent) { User user = signatureVerification.getUser(); - ArbitraryStorageEntity arbitraryStorageEntity; - - if ((arbitraryStorageEntity = arbitraryStorageUtils.getStorageSetting( + importantConversation = arbitraryStorageManger.getStorageSetting( user.getId(), "important_conversation", - intent.getExtras().getString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN()))) != null) { - importantConversation = Boolean.parseBoolean(arbitraryStorageEntity.getValue()); - } + intent.getExtras().getString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN())) + .map(arbitraryStorage -> { + if (arbitraryStorage != null) { + return Boolean.parseBoolean(arbitraryStorage.getValue()); + } else { + return importantConversation; + } + }) + .switchIfEmpty(Maybe.just(importantConversation)) + .blockingGet(); int apiVersion = ApiUtils.getConversationApiVersion(user, new int[] {ApiUtils.APIv4, 1}); @@ -149,7 +153,7 @@ public class NotificationWorker extends Worker { .blockingSubscribe(new Observer() { @Override public void onSubscribe(Disposable d) { - + //unused atm } @Override @@ -179,11 +183,12 @@ public class NotificationWorker extends Worker { @Override public void onError(Throwable e) { + //unused atm } @Override public void onComplete() { - + //unused atm } }); } @@ -195,7 +200,7 @@ public class NotificationWorker extends Worker { .blockingSubscribe(new Observer() { @Override public void onSubscribe(Disposable d) { - + //unused atm } @Override @@ -258,11 +263,12 @@ public class NotificationWorker extends Worker { @Override public void onError(Throwable e) { - + //unused atm } @Override public void onComplete() { + //unused atm } }); } diff --git a/app/src/main/java/com/nextcloud/talk/utils/database/arbitrarystorage/ArbitraryStorageUtils.java b/app/src/main/java/com/nextcloud/talk/utils/database/arbitrarystorage/ArbitraryStorageUtils.java index 92abd42ec..14eb819c1 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/database/arbitrarystorage/ArbitraryStorageUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/database/arbitrarystorage/ArbitraryStorageUtils.java @@ -19,6 +19,8 @@ */ package com.nextcloud.talk.utils.database.arbitrarystorage; +import android.util.Log; + import androidx.annotation.Nullable; import com.nextcloud.talk.models.database.ArbitraryStorage; @@ -42,8 +44,8 @@ public class ArbitraryStorageUtils { this.dataStore = dataStore; } - public void storeStorageSetting(long accountIdentifier, String key, String value, String object) { + Log.e("ArbitraryStorageUtils", "storeStorageSetting: " + key + " / " + value + " / " + object); ArbitraryStorageEntity arbitraryStorageEntity = new ArbitraryStorageEntity(); arbitraryStorageEntity.setAccountIdentifier(accountIdentifier); arbitraryStorageEntity.setKey(key); @@ -57,15 +59,23 @@ public class ArbitraryStorageUtils { } public ArbitraryStorageEntity getStorageSetting(long accountIdentifier, String key, @Nullable String object) { + Log.e("ArbitraryStorageUtils", "getStorageSetting: " + accountIdentifier + " / " + key + " / " + object); Result findStorageQueryResult = dataStore.select(ArbitraryStorage.class) .where(ArbitraryStorageEntity.ACCOUNT_IDENTIFIER.eq(accountIdentifier) .and(ArbitraryStorageEntity.KEY.eq(key)).and(ArbitraryStorageEntity.OBJECT.eq(object))) .limit(1).get(); - return (ArbitraryStorageEntity) findStorageQueryResult.firstOrNull(); + ArbitraryStorageEntity arbitraryStorageEntity = (ArbitraryStorageEntity) findStorageQueryResult.firstOrNull(); + + if(arbitraryStorageEntity != null) { + Log.e("ArbitraryStorageUtils", "getStorageSetting: " + arbitraryStorageEntity.getKey() + " / " + arbitraryStorageEntity.getValue() + " / " + arbitraryStorageEntity.getObject()); + } + + return arbitraryStorageEntity; } public Observable deleteAllEntriesForAccountIdentifier(long accountIdentifier) { + Log.e("ArbitraryStorageUtils", "deleteAllEntriesForAccountIdentifier: " + accountIdentifier); ReactiveScalar deleteResult = dataStore.delete(ArbitraryStorage.class).where(ArbitraryStorageEntity.ACCOUNT_IDENTIFIER.eq(accountIdentifier)).get(); return deleteResult.single().toObservable() diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java index 851377466..cf37b49ca 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java @@ -28,11 +28,10 @@ import android.util.Log; import com.nextcloud.talk.api.NcApi; import com.nextcloud.talk.application.NextcloudTalkApplication; +import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager; import com.nextcloud.talk.data.user.model.User; -import com.nextcloud.talk.models.database.ArbitraryStorageEntity; import com.nextcloud.talk.models.json.generic.GenericOverall; import com.nextcloud.talk.utils.ApiUtils; -import com.nextcloud.talk.utils.database.arbitrarystorage.ArbitraryStorageUtils; import com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew; import com.yarolegovich.mp.io.StorageModule; @@ -43,6 +42,7 @@ import java.util.Set; import javax.inject.Inject; import autodagger.AutoInjector; +import io.reactivex.Maybe; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; @@ -51,13 +51,13 @@ import io.reactivex.schedulers.Schedulers; @AutoInjector(NextcloudTalkApplication.class) public class DatabaseStorageModule implements StorageModule { private static final String TAG = "DatabaseStorageModule"; + @Inject - ArbitraryStorageUtils arbitraryStorageUtils; + ArbitraryStorageManager arbitraryStorageManager; @Inject NcApi ncApi; - private User conversationUser; private String conversationToken; private long accountIdentifier; @@ -89,12 +89,12 @@ public class DatabaseStorageModule implements StorageModule { .subscribe(new Observer() { @Override public void onSubscribe(@NotNull Disposable d) { - + // unused atm } @Override public void onNext(@NotNull GenericOverall genericOverall) { - + // unused atm } @Override @@ -104,14 +104,17 @@ public class DatabaseStorageModule implements StorageModule { @Override public void onComplete() { - + // unused atm } } ); } if (!key.equals("conversation_lobby")) { - arbitraryStorageUtils.storeStorageSetting(accountIdentifier, key, Boolean.toString(value), conversationToken); + arbitraryStorageManager.storeStorageSetting(accountIdentifier, + key, + Boolean.toString(value), + conversationToken); } else { lobbyValue = value; } @@ -120,7 +123,7 @@ public class DatabaseStorageModule implements StorageModule { @Override public void saveString(String key, String value) { if (!key.equals("message_notification_level")) { - arbitraryStorageUtils.storeStorageSetting(accountIdentifier, key, value, conversationToken); + arbitraryStorageManager.storeStorageSetting(accountIdentifier, key, value, conversationToken); } else { if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "notification-levels")) { if (!TextUtils.isEmpty(messageNotificationLevel) && !messageNotificationLevel.equals(value)) { @@ -139,17 +142,20 @@ public class DatabaseStorageModule implements StorageModule { intValue = 0; } - int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[] {ApiUtils.APIv4, 1}); + int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, + new int[] {ApiUtils.APIv4, 1}); - ncApi.setNotificationLevel(ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken()), - ApiUtils.getUrlForRoomNotificationLevel(apiVersion, conversationUser.getBaseUrl(), + ncApi.setNotificationLevel(ApiUtils.getCredentials(conversationUser.getUsername(), + conversationUser.getToken()), + ApiUtils.getUrlForRoomNotificationLevel(apiVersion, + conversationUser.getBaseUrl(), conversationToken), intValue) .subscribeOn(Schedulers.io()) .subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { - + // unused atm } @Override @@ -159,11 +165,12 @@ public class DatabaseStorageModule implements StorageModule { @Override public void onError(Throwable e) { - + // unused atm } @Override public void onComplete() { + // unused atm } }); } else { @@ -175,12 +182,15 @@ public class DatabaseStorageModule implements StorageModule { @Override public void saveInt(String key, int value) { - arbitraryStorageUtils.storeStorageSetting(accountIdentifier, key, Integer.toString(value), conversationToken); + arbitraryStorageManager.storeStorageSetting(accountIdentifier, + key, + Integer.toString(value), + conversationToken); } @Override public void saveStringSet(String key, Set value) { - + // unused atm } @Override @@ -188,37 +198,52 @@ public class DatabaseStorageModule implements StorageModule { if (key.equals("conversation_lobby")) { return lobbyValue; } else { - ArbitraryStorageEntity valueFromDb = arbitraryStorageUtils.getStorageSetting(accountIdentifier, key, conversationToken); - if (valueFromDb == null) { - return defaultVal; - } else { - return Boolean.parseBoolean(valueFromDb.getValue()); - } + return arbitraryStorageManager + .getStorageSetting(accountIdentifier, key, conversationToken) + .map(arbitraryStorage -> { + if (arbitraryStorage != null) { + return Boolean.parseBoolean(arbitraryStorage.getValue()); + } else { + return defaultVal; + } + }) + .switchIfEmpty(Maybe.just(defaultVal)) + .blockingGet(); } } @Override public String getString(String key, String defaultVal) { - if (!key.equals("message_notification_level")) { - ArbitraryStorageEntity valueFromDb = arbitraryStorageUtils.getStorageSetting(accountIdentifier, key, conversationToken); - if (valueFromDb == null) { - return defaultVal; - } else { - return valueFromDb.getValue(); - } - } else { + if (key.equals("message_notification_level")) { return messageNotificationLevel; + } else { + return arbitraryStorageManager + .getStorageSetting(accountIdentifier, key, conversationToken) + .map(arbitraryStorage -> { + if (arbitraryStorage != null) { + return arbitraryStorage.getValue(); + } else { + return defaultVal; + } + }) + .switchIfEmpty(Maybe.just(defaultVal)) + .blockingGet(); } } @Override public int getInt(String key, int defaultVal) { - ArbitraryStorageEntity valueFromDb = arbitraryStorageUtils.getStorageSetting(accountIdentifier, key, conversationToken); - if (valueFromDb == null) { - return defaultVal; - } else { - return Integer.parseInt(valueFromDb.getValue()); - } + return arbitraryStorageManager + .getStorageSetting(accountIdentifier, key, conversationToken) + .map(arbitraryStorage -> { + if (arbitraryStorage != null && arbitraryStorage.getValue() != null) { + return Integer.parseInt(arbitraryStorage.getValue()); + } else { + return defaultVal; + } + }) + .switchIfEmpty(Maybe.just(defaultVal)) + .blockingGet(); } @Override @@ -228,11 +253,11 @@ public class DatabaseStorageModule implements StorageModule { @Override public void onSaveInstanceState(Bundle outState) { - + // unused atm } @Override public void onRestoreInstanceState(Bundle savedState) { - + // unused atm } }