From de58b4666438046547458a314ea6c7f5dd9c89d5 Mon Sep 17 00:00:00 2001 From: Alexandre Jacinto Date: Wed, 5 Apr 2023 09:17:56 +0100 Subject: [PATCH] RMET-2378 Cloud Messaging Plugin - Hook for sound files (#52) * feat: first version of hook to copy sound files References: https://outsystemsrd.atlassian.net/browse/RMET-2378 * chore: save current progress * refactor: remove comment * refactor: remove unnecessary code from hook References: https://outsystemsrd.atlassian.net/browse/RMET-2378 * refactor: remove unnecessary functions References: https://outsystemsrd.atlassian.net/browse/RMET-2378 * chore: update libs References: https://outsystemsrd.atlassian.net/browse/RMET-2378 * chore: update changelog * refactor: replace var with let * refactor: replace var with let * refactor: use let instead of var * chore: update lib version --- CHANGELOG.md | 3 + .../sound/unzipAndCopyConfigurations.js | 45 +++++++++++ hooks/android/sound/utilities.js | 77 +++++++++++++++++++ hooks/android/sound/utils.js | 7 ++ plugin.xml | 2 + .../firebase/cloudmessaging/build.gradle | 4 +- 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 hooks/android/sound/unzipAndCopyConfigurations.js create mode 100644 hooks/android/sound/utilities.js create mode 100644 hooks/android/sound/utils.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 664b51c3..ffc63771 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository. ## [Unreleased] +## 04-04-2023 +- Feat: [Android] Add hook to copy sound files (https://outsystemsrd.atlassian.net/browse/RMET-2378). + ## 31-03-2023 - Feat: [iOS] React to a triggered notification with custom sound enabled (https://outsystemsrd.atlassian.net/browse/RMET-2381). diff --git a/hooks/android/sound/unzipAndCopyConfigurations.js b/hooks/android/sound/unzipAndCopyConfigurations.js new file mode 100644 index 00000000..f4c0190f --- /dev/null +++ b/hooks/android/sound/unzipAndCopyConfigurations.js @@ -0,0 +1,45 @@ +"use strict"; + +var path = require("path"); +var utils = require("./utilities"); + +module.exports = function(context) { + let cordovaAbove8 = utils.isCordovaAbove(context, 8); + let defer; + if (cordovaAbove8) { + defer = require("q").defer(); + } else { + defer = context.requireCordovaModule("q").defer(); + } + + let platform = context.opts.plugin.platform; + let platformConfig = utils.getPlatformConfigs(platform); + if (!platformConfig) { + utils.handleError("Invalid platform", defer); + } + + let sourceFolderPath = platformConfig.getSoundSourceFolder() + let destFolderPath = platformConfig.getSoundDestinationFolder() + + if(!utils.checkIfFolderExists(destFolderPath)) { + utils.createOrCheckIfFolderExists(destFolderPath) + } + + let files = utils.getFilesFromPath(sourceFolderPath); + if (!files) { + utils.handleError("No directory found", defer); + } + else { + let filteredFiles = files.filter(function(file){ + return file.endsWith(platformConfig.soundFileExtension) == true; + }); + + filteredFiles.forEach(function (f) { + let filePath = path.join(sourceFolderPath, f); + let destFilePath = path.join(destFolderPath, f); + utils.copyFromSourceToDestPath(defer, filePath, destFilePath); + }); + } + + return defer.promise; +} diff --git a/hooks/android/sound/utilities.js b/hooks/android/sound/utilities.js new file mode 100644 index 00000000..82aee8e2 --- /dev/null +++ b/hooks/android/sound/utilities.js @@ -0,0 +1,77 @@ +"use strict" + +var path = require("path"); +var fs = require("fs"); + +var utils = require("./utils"); + +var constants = { + platforms: "platforms", + android: { + platform: "android", + wwwFolder: "assets/www", + soundFileExtension: ".wav", + getSoundDestinationFolder: function() { + return "platforms/android/app/src/main/res/raw"; + }, + getSoundSourceFolder: function() { + return "platforms/android/app/src/main/assets/www"; + } + } +}; + +function handleError(errorMessage, defer) { + console.log(errorMessage); + defer.reject(); +} + +function checkIfFolderExists(path) { + return fs.existsSync(path); +} + +function getFilesFromPath(path) { + return fs.readdirSync(path); +} + +function createOrCheckIfFolderExists(path) { + if (!fs.existsSync(path)) { + fs.mkdirSync(path); + } +} + +function getPlatformConfigs(platform) { + if (platform === constants.android.platform) { + return constants.android; + } else if (platform === constants.ios.platform) { + return constants.ios; + } +} + +function isCordovaAbove(context, version) { + let cordovaVersion = context.opts.cordova.version; + console.log(cordovaVersion); + let sp = cordovaVersion.split('.'); + return parseInt(sp[0]) >= version; +} + + +function copyFromSourceToDestPath(defer, sourcePath, destPath) { + fs.createReadStream(sourcePath).pipe(fs.createWriteStream(destPath)) + .on("close", function (err) { + defer.resolve(); + }) + .on("error", function (err) { + console.log(err); + defer.reject(); + }); +} + +module.exports = { + isCordovaAbove, + handleError, + getPlatformConfigs, + copyFromSourceToDestPath, + getFilesFromPath, + createOrCheckIfFolderExists, + checkIfFolderExists +}; \ No newline at end of file diff --git a/hooks/android/sound/utils.js b/hooks/android/sound/utils.js new file mode 100644 index 00000000..dee7bf7a --- /dev/null +++ b/hooks/android/sound/utils.js @@ -0,0 +1,7 @@ +module.exports = { + getAppName: function (context) { + let ConfigParser = context.requireCordovaModule("cordova-lib").configparser; + let config = new ConfigParser("config.xml"); + return config.name(); + } +}; \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 5ac3c8a1..0d755094 100644 --- a/plugin.xml +++ b/plugin.xml @@ -59,6 +59,8 @@ + + diff --git a/src/android/com/outsystems/firebase/cloudmessaging/build.gradle b/src/android/com/outsystems/firebase/cloudmessaging/build.gradle index fc484cca..d38a8c0b 100644 --- a/src/android/com/outsystems/firebase/cloudmessaging/build.gradle +++ b/src/android/com/outsystems/firebase/cloudmessaging/build.gradle @@ -23,8 +23,8 @@ apply plugin: 'kotlin-kapt' dependencies { implementation("com.github.outsystems:oscore-android:1.2.0@aar") implementation("com.github.outsystems:oscordova-android:1.2.0@aar") - implementation("com.github.outsystems:osfirebasemessaging-android:1.0.2.1@aar") - implementation("com.github.outsystems:oslocalnotifications-android:0.0.2@aar") + implementation("com.github.outsystems:osfirebasemessaging-android:1.0.4@aar") + implementation("com.github.outsystems:oslocalnotifications-android:0.0.3@aar") implementation("com.github.outsystems:osnotificationpermissions-android:0.0.4@aar") implementation("com.google.code.gson:gson:2.8.9")