forked from BenjaminPoncet/cordova-plugin-openwith
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD [Android/iOS] Allow multiple mime types & multiple files share (#2)
Co-authored-by: lucca-dev <>
- Loading branch information
Showing
13 changed files
with
698 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.