From 2f37534bc2712cd11dcdc819f5b744caea19d857 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 2 May 2018 20:41:44 -0400 Subject: [PATCH] - Now the user configured received funds sounds gets played instead of woohoo default sound --- .../application/CrystalApplication.java | 2 +- .../notifiers/CrystalWalletNotifier.java | 46 +++++++++++++++++-- .../GeneralSettingListViewModel.java | 4 ++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java index 549aeda..b3d4504 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -86,7 +86,7 @@ public class CrystalApplication extends Application { } //The crystal notifier is initialized - CrystalWalletNotifier crystalWalletNotifier = new CrystalWalletNotifier(getApplicationContext()); + CrystalWalletNotifier crystalWalletNotifier = new CrystalWalletNotifier(this); CryptoNetEvents.getInstance().addListener(crystalWalletNotifier); //Next line is for use the bitshares main net diff --git a/app/src/main/java/cy/agorise/crystalwallet/notifiers/CrystalWalletNotifier.java b/app/src/main/java/cy/agorise/crystalwallet/notifiers/CrystalWalletNotifier.java index 0eeec88..fd85b20 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/notifiers/CrystalWalletNotifier.java +++ b/app/src/main/java/cy/agorise/crystalwallet/notifiers/CrystalWalletNotifier.java @@ -1,12 +1,18 @@ package cy.agorise.crystalwallet.notifiers; +import android.app.Application; import android.content.Context; import android.media.MediaPlayer; +import java.io.File; +import java.io.IOException; + import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.requestmanagers.CryptoNetEvent; import cy.agorise.crystalwallet.requestmanagers.CryptoNetEventsListener; import cy.agorise.crystalwallet.requestmanagers.ReceivedFundsCryptoNetEvent; +import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; /** * Created by Henry Varona on 29/4/2018. @@ -15,9 +21,42 @@ import cy.agorise.crystalwallet.requestmanagers.ReceivedFundsCryptoNetEvent; public class CrystalWalletNotifier implements CryptoNetEventsListener{ private Context context; + private MediaPlayer receivedFundsMediaPlayer; + private Application application; - public CrystalWalletNotifier(Context context){ - this.context = context; + public CrystalWalletNotifier(Application application){ + this.context = application.getApplicationContext(); + this.application = application; + loadReceivedFundsSound(); + } + + public void loadReceivedFundsSound(){ + GeneralSettingListViewModel generalSettingListViewModel = new GeneralSettingListViewModel(this.application); + GeneralSetting receivedFundsSoundGeneralSetting = generalSettingListViewModel.getGeneralSettingByName(GeneralSetting.SETTING_NAME_RECEIVED_FUNDS_SOUND_PATH); + + File receivedFundsSoundFile = null; + + if ((receivedFundsSoundGeneralSetting != null)){ + if (!receivedFundsSoundGeneralSetting.getValue().equals("")) { + receivedFundsSoundFile = new File(receivedFundsSoundGeneralSetting.getValue()); + + if (!receivedFundsSoundFile.exists()){ + receivedFundsSoundFile = null; + } + } + } + + if (receivedFundsSoundFile != null){ + receivedFundsMediaPlayer = new MediaPlayer(); + try { + receivedFundsMediaPlayer.setDataSource(receivedFundsSoundGeneralSetting.getValue()); + receivedFundsMediaPlayer.prepare(); + } catch (IOException e) { + receivedFundsMediaPlayer = MediaPlayer.create(this.context, R.raw.woohoo); + } + } else { + receivedFundsMediaPlayer = MediaPlayer.create(this.context, R.raw.woohoo); + } } public void onCryptoNetEvent(CryptoNetEvent event) { @@ -27,7 +66,6 @@ public class CrystalWalletNotifier implements CryptoNetEventsListener{ } private void playReceivedFundsSound() { - MediaPlayer defaultMediaPlayer = MediaPlayer.create(this.context, R.raw.woohoo); - defaultMediaPlayer.start(); + receivedFundsMediaPlayer.start(); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java index ccea8d7..22b2feb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java @@ -41,4 +41,8 @@ public class GeneralSettingListViewModel extends AndroidViewModel { public GeneralSetting getGeneralSettingByName(String name){ return this.db.generalSettingDao().getSettingByName(name); } + + public LiveData getGeneralSettingLiveDataByName(String name){ + return this.db.generalSettingDao().getByName(name); + } }