Skip to content

Commit

Permalink
ADD [Android/iOS] Allow multiple mime types & multiple files share (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: lucca-dev <>
  • Loading branch information
shajz authored Apr 30, 2021
1 parent f3c8b80 commit a6bc493
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 605 deletions.
76 changes: 76 additions & 0 deletions hooks/androidIntentFilters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const fs = require("fs");
const path = require("path");
const et = require("elementtree");

// Parses a given file into an elementtree object
function parseElementtreeSync(filename) {
var contents = fs.readFileSync(filename, "utf-8");
if (contents) {
//Windows is the BOM. Skip the Byte Order Mark.
contents = contents.substring(contents.indexOf("<"));
}
return new et.ElementTree(et.XML(contents));
}

function updateMimeTypes(manifest, mimeTypes) {
const tempManifest = parseElementtreeSync(manifest);
const root = tempManifest.getroot();

const parent = "application/activity";

mimeTypes.forEach((mimeType) => {
const parentEl = root.find(parent);

const intentFilter = new et.Element("intent-filter");
intentFilter.append(
new et.Element("data", { "android:mimeType": mimeType })
);
intentFilter.append(
new et.Element("action", {
"android:name": "android.intent.action.SEND",
})
);
intentFilter.append(
new et.Element("action", {
"android:name": "android.intent.action.SEND_MULTIPLE",
})
);
intentFilter.append(
new et.Element("category", {
"android:name": "android.intent.category.DEFAULT",
})
);
parentEl.append(intentFilter);
});

fs.writeFileSync(manifest, tempManifest.write({ indent: 4 }), "utf-8");
}

module.exports = function (context) {
// Prevent double execution
if (
context.hook == "after_prepare" &&
!RegExp("\\s+prepare").test(context.cmdLine)
) {
return "";
}

const packageJson = require(path.join(
context.opts.projectRoot,
"package.json"
));

const manifestPath = path.join(
context.opts.projectRoot,
"platforms",
"android",
"app",
"src",
"main",
"AndroidManifest.xml"
);
const pluginProperties = packageJson.cordova.plugins["cc.fovea.cordova.openwith"];

const mimeTypes = pluginProperties["ANDROID_MIME_TYPES"].split(",");
updateMimeTypes(manifestPath, mimeTypes);
};
11 changes: 2 additions & 9 deletions hooks/iosAddTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@
// THE SOFTWARE.
//

const PLUGIN_ID = 'cc.fovea.cordova.openwith';
const { PLUGIN_ID, redError } = require("./utils");
const BUNDLE_SUFFIX = '.shareextension';

var fs = require('fs');
var path = require('path');
var packageJson;
var bundleIdentifier;

function redError(message) {
return new Error('"' + PLUGIN_ID + '" \x1b[1m\x1b[31m' + message + '\x1b[0m');
}

function replacePreferencesInFile(filePath, preferences) {
var content = fs.readFileSync(filePath, 'utf8');
for (var i = 0; i < preferences.length; i++) {
Expand Down Expand Up @@ -138,7 +134,7 @@ function getCordovaParameter(configXml, variableName) {
function getBundleId(context, configXml) {
var elementTree = require('elementtree');
var etree = elementTree.parse(configXml);
return etree.getroot().get('id');
return etree.getroot().get('ios-CFBundleIdentifier');
}

function parsePbxProject(context, pbxProjectPath) {
Expand Down Expand Up @@ -206,9 +202,6 @@ function getPreferences(context, configXml, projectName) {
}, {
key: '__URL_SCHEME__',
value: getCordovaParameter(configXml, 'IOS_URL_SCHEME')
}, {
key: '__UNIFORM_TYPE_IDENTIFIER__',
value: getCordovaParameter(configXml, 'IOS_UNIFORM_TYPE_IDENTIFIER')
}];
}

Expand Down
6 changes: 1 addition & 5 deletions hooks/iosCopyShareExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@
// THE SOFTWARE.
//

const { PLUGIN_ID, redError } = require("./utils");
var fs = require('fs');
var path = require('path');
const PLUGIN_ID = "cc.fovea.cordova.openwith";

function redError(message) {
return new Error('"' + PLUGIN_ID + '" \x1b[1m\x1b[31m' + message + '\x1b[0m');
}

function getPreferenceValue (config, name) {
var value = config.match(new RegExp('name="' + name + '" value="(.*?)"', "i"));
Expand Down
6 changes: 1 addition & 5 deletions hooks/iosRemoveTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
// THE SOFTWARE.
//

const PLUGIN_ID = "cc.fovea.cordova.openwith";
const { PLUGIN_ID, redError } = require("./utils");
const BUNDLE_SUFFIX = ".shareextension";

var fs = require('fs');
var path = require('path');

function redError(message) {
return new Error('"' + PLUGIN_ID + '" \x1b[1m\x1b[31m' + message + '\x1b[0m');
}

// Determine the full path to the app's xcode project file.
function findXCodeproject(context, callback) {
fs.readdir(iosFolder(context), function(err, data) {
Expand Down
30 changes: 16 additions & 14 deletions hooks/npmInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

const PLUGIN_ID = "cc.fovea.cordova.openwith";
const { PLUGIN_ID } = require("./utils");

module.exports = function (context) {
var child_process = require('child_process');
var deferral = require('q').defer();
var child_process = require("child_process");
var deferral = require("q").defer();

console.log('Installing "' + PLUGIN_ID + '" dependencies');
child_process.exec('npm install --production', {cwd:__dirname}, function (error) {
if (error !== null) {
console.log('exec error: ' + error);
deferral.reject('npm installation failed');
}
deferral.resolve();
});
console.log('Installing "' + PLUGIN_ID + '" dependencies');
child_process.exec(
"npm install --production",
{ cwd: __dirname },
function (error) {
if (error !== null) {
console.log("exec error: " + error);
deferral.reject("npm installation failed");
}
deferral.resolve();
}
);

return deferral.promise;
return deferral.promise;
};

// vim: ts=4:sw=4:et
10 changes: 10 additions & 0 deletions hooks/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const PLUGIN_ID = "cc.fovea.cordova.openwith";

function redError(message) {
return new Error(`"${PLUGIN_ID}" \x1b[1m\x1b[31m${message}\x1b[0m`);
}

module.exports = {
PLUGIN_ID,
redError,
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@
"src/ios/ShareExtension/ShareViewController.m",
"www/openwith.js",
"www/test-openwith.js",
"hooks/androidIntentFilters.js",
"hooks/iosAddTarget.js",
"hooks/iosRemoveTarget.js",
"hooks/iosCopyShareExtension.js",
"hooks/npmInstall.js",
"hooks/utils.js",
"install-pmd",
"plugin.xml",
"LICENSE",
Expand Down
32 changes: 8 additions & 24 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,22 @@ SOFTWARE.
-->

<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cc.fovea.cordova.openwith"
version="2.0.0">
xmlns:android="http://schemas.android.com/apk/res/android" id="cc.fovea.cordova.openwith" version="2.0.0">

<name>OpenWith</name>
<description>Cordova "Open With" plugin for iOS and Android</description>
<description>Cordova "Open With" plugin for iOS and Android</description>
<engines>
<engine name="cordova" version=">=6.0.0" />
</engines>
<repo>https://github.com/j3k0/cordova-plugin-openwith.git</repo>
<issue>https://github.com/j3k0/cordova-plugin-openwith/issues</issue>

<license>MIT</license>
<license>MIT</license>
<keywords>cordova,phonegap,openwith,ios,android</keywords>

<preference name="IOS_URL_SCHEME" />
<preference name="IOS_UNIFORM_TYPE_IDENTIFIER" />
<preference name="IOS_GROUP_IDENTIFIER" />
<preference name="ANDROID_MIME_TYPE" />
<preference name="ANDROID_EXTRA_ACTIONS" default=" " />
<preference name="ANDROID_MIME_TYPES" />

<!-- ios -->
<platform name="ios">
Expand Down Expand Up @@ -106,24 +102,11 @@ SOFTWARE.
<clobbers target="cordova.openwith" />
</js-module>

<config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
<intent-filter>
<!-- category android:name="android.intent.category.BROWSABLE" / -->
<!-- See https://developer.android.com/guide/topics/manifest/data-element.html -->
<data android:mimeType="$ANDROID_MIME_TYPE" />
<action android:name="android.intent.action.SEND" />
$ANDROID_EXTRA_ACTIONS
<!-- action android:name="android.intent.action.VIEW" / -->
<!-- action android:name="android.intent.action.SEND_MULTIPLE" / -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</config-file>

<!-- Cordova >= 3.0.0 -->
<config-file target="res/xml/config.xml" parent="/*">
<feature name="OpenWithPlugin">
<param name="android-package" value="cc.fovea.openwith.OpenWithPlugin"/>
</feature>
<param name="android-package" value="cc.fovea.openwith.OpenWithPlugin"/>
</feature>
<preference name="AndroidLaunchMode" value="singleTask"/>
</config-file>

Expand All @@ -132,9 +115,10 @@ SOFTWARE.
<source-file src="src/android/cc/fovea/openwith/PluginResultSender.java" target-dir="src/cc/fovea/openwith" />
<source-file src="src/android/cc/fovea/openwith/Serializer.java" target-dir="src/cc/fovea/openwith" />
<source-file src="src/android/cc/fovea/openwith/ByteStreams.java" target-dir="src/cc/fovea/openwith" />
<hook type="after_prepare" src="hooks/androidIntentFilters.js" />
</platform>

<!--
<!--
vim: ts=4:sw=4:et
-->
</plugin>
Loading

0 comments on commit a6bc493

Please sign in to comment.