Skip to content

Commit

Permalink
RMET-2378 Cloud Messaging Plugin - Hook for sound files (#52)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexgerardojacinto authored and OS-martacarlos committed May 21, 2024
1 parent 53afca8 commit a64e5a7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
45 changes: 45 additions & 0 deletions hooks/android/sound/unzipAndCopyConfigurations.js
Original file line number Diff line number Diff line change
@@ -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;
}
77 changes: 77 additions & 0 deletions hooks/android/sound/utilities.js
Original file line number Diff line number Diff line change
@@ -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
};
7 changes: 7 additions & 0 deletions hooks/android/sound/utils.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

<hook type="after_prepare" src="hooks/android/androidCopyChannelInfo.js" />

<hook type="before_plugin_install" src="hooks/android/sound/unzipAndCopyConfigurations.js" />

<config-file parent="/*" target="res/xml/config.xml">
<feature name="OSFirebaseCloudMessaging">
<param name="android-package" value="com.outsystems.firebase.cloudmessaging.OSFirebaseCloudMessaging"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a64e5a7

Please sign in to comment.