Skip to content

Commit

Permalink
Initial android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
j3k0 committed Sep 26, 2017
1 parent 26e2e62 commit 7903c59
Show file tree
Hide file tree
Showing 13 changed files with 1,107 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "standard",
"plugins": [
"standard",
"mocha",
"promise"
],
"rules": {
"mocha/no-exclusive-tests": "error"
},
"env": {
"browser": true,
"node": true
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
> This plugin for [Apache Cordova](https://cordova.apache.org/) registers your app to open certain types of files.
## Overview
You'd like your app to be listed in the `Open With...` section for certain types of files, on both Android and iOS? This is THE plugin! No need to meddle into Android's Manifests and iOS's PList files, it's a no brainer one liner installation.
You'd like your app to be listed in the **Open With...** section for certain types of files, on both **Android** and **iOS**? This is THE plugin! No need to meddle into Android's manifests and iOS's plist files, it's all managed for you by a no brainer one liner installation command.

## Table of Contents

Expand All @@ -27,12 +27,16 @@ _If your module depends on important but not widely known abstractions or other

## Installation

cordova plugin add .. --variable MIME_TYPE="image/*"

## Configuration
_If your module requires configuration before developers can use it, explain it in this section._

## Usage
_Show developers what a module looks like in action so they can quickly determine whether the example meets their needs. This section should contain clear, runnable code examples._

Check out a [demo project](https://github.com/j3k0/cordova-plugin-openwith-demo) for an fully functional example.

## API
_The API section should detail the module's objects and functions, their signatures, return types, callbacks, and events in detail. Types should be included where they aren't obvious. Caveats should be made clear._

Expand Down
38 changes: 38 additions & 0 deletions install-pmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/sh

# Version of PMD to install
VERSION=5.8.1

set -e
COPYWD="$PWD"
cd "`dirname $0`"
INSTALL_DIR=$PWD/node_modules/pmd-bin-$VERSION
BIN_DIR=$PWD/node_modules/.bin

if test -e $INSTALL_DIR; then
# echo "PMD version $VERSION is already installed"
exit 0
fi

echo "Intalling PMD version $VERSION into node_modules"

curl -OL https://github.com/pmd/pmd/releases/download/pmd_releases%2F$VERSION/pmd-bin-$VERSION.zip
unzip pmd-bin-$VERSION.zip
rm -f pmd-bin-$VERSION.zip

mkdir -p node_modules
mv pmd-bin-$VERSION > $INSTALL_DIR

cat << EOF > $BIN_DIR/pmd
#!/bin/sh
echo
$INSTALL_DIR/bin/run.sh pmd "\$@"
EOF
chmod +x $BIN_DIR/pmd

cat << EOF > $INSTALL_DIR/package.json
{
"name": "pdm",
"version": "$VERSION"
}
EOF
53 changes: 53 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "cc.fovea.cordova.openwith",
"version": "1.0.0",
"description": "Cordova \"Open With\" plugin for iOS and Android",
"cordova": {
"id": "cc.fovea.cordova.openwith",
"platforms": [
"android",
"ios"
]
},
"repository": {
"type": "git",
"url": "https://github.com/j3k0/cordova-plugin-openwith.git"
},
"keywords": [
"ecosystem:cordova",
"cordova-android",
"cordova-ios",
"cordova",
"phonegap",
"openwith",
"ios",
"android"
],
"scripts": {
"test": "npm run js-lint && npm run js-test && npm run java-lint",
"install-dev": "./install-pmd",
"java-lint": "pmd -minimumpriority 3 -d src/android -R java-basic,java-android,java-braces,java-codesize,java-empty,java-finalizers,java-imports,java-naming,java-optimizations,java-strictexception,java-strings,java-sunsecure,java-typeresolution,java-unnecessary,java-unusedcode -f textcolor",
"js-lint": "eslint www",
"js-test": "mocha www",
"js-lint-watch": "esw --watch www",
"js-test-watch": "mocha --watch www"
},
"author": "Jean-Christophe Hoelt <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/j3k0/cordova-plugin-openwith/issues"
},
"homepage": "https://github.com/j3k0/cordova-plugin-openwith",
"devDependencies": {
"eslint": "^4.7.2",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-mocha": "^4.11.0",
"eslint-plugin-node": "^5.1.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"eslint-watch": "^3.1.2",
"expect.js": "^0.3.1",
"mocha": "^3.5.3"
}
}
102 changes: 102 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)
Copyright (c) 2013-2015 Jean-Christophe Hoelt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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="1.0.0">

<name>Open With</name>
<description>Cordova "Open With" plugin for iOS and Android</description>
<engines>
<engine name="cordova" version=">=2.4.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>
<keywords>cordova,phonegap,openwith,ios,android</keywords>

<!-- ios -->
<platform name="ios">
<js-module src="www/openwith.js" name="openwith">
<clobbers target="cordova.openwith" />
</js-module>

<!-- Cordova 2.2 -->
<plugins-plist key="OpenWith" string="OpenWith" />

<!-- Cordova 2.5+ -->
<config-file target="config.xml" parent="/*">
<feature name="OpenWith">
<param name="ios-package" value="OpenWith"/>
</feature>
</config-file>

<header-file src="src/ios/OpenWith.h" />
<source-file src="src/ios/OpenWith.m" />
</platform>

<!-- android -->
<platform name="android">
<preference name="MIME_TYPE" />

<js-module src="www/openwith.js" name="openwith">
<clobbers target="cordova.openwith" />
</js-module>

<config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
<!-- Open With -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- See https://developer.android.com/guide/topics/manifest/data-element.html -->
<intent-filter>
<!-- action android:name="android.intent.action.VIEW" / -->
<!-- category android:name="android.intent.category.BROWSABLE" / -->
<!-- action android:name="android.intent.action.SEND_MULTIPLE" / -->
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="$MIME_TYPE" />
</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>
<preference name="AndroidLaunchMode" value="singleTask"/>
</config-file>

<!-- cordova plugin src files -->
<source-file src="src/android/cc/fovea/openwith/OpenWithPlugin.java" target-dir="src/cc/fovea/openwith" />
<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" />
</platform>

<!--
vim: ts=4:sw=4:et
-->
</plugin>
4 changes: 4 additions & 0 deletions setenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NODE_VERSION=v6

nvm ls $NODE_VERSION || nvm install $NODE_VERSION
nvm use $NODE_VERSION
96 changes: 96 additions & 0 deletions src/android/cc/fovea/openwith/ByteStreams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cc.fovea.openwith;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

/**
* Convert an InputStream to a byte array.
*
* Sourced from Google guava classes.
*/
@SuppressWarnings("PMD.ShortVariable")
@SuppressWarnings("PMD.AvoidThrowingNullPointerException")
public final class ByteStreams {

/** Throws if the argument is null. */
public static <T> T checkNotNull(final T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}

/** Throws if the argument is false. */
public static void checkArgument(final boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}

/** Throws if the index isn't in [0..size]. */
public static int checkPositionIndex(final int index, final int size) {
// Carefully optimized for execution by hotspot (explanatory comment above)
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("invalid index");
}
return index;
}

/**
* Creates a new byte array for buffering reads or writes.
*/
static byte[] createBuffer() {
return new byte[8192];
}

private ByteStreams() {}

/**
* Copies all bytes from the input stream to the output stream. Does not close or flush either
* stream.
*
* @param from the input stream to read from
* @param to the output stream to write to
* @return the number of bytes copied
* @throws IOException if an I/O error occurs
*/
public static long copy(
final InputStream from,
final OutputStream to)
throws IOException {
checkNotNull(from);
checkNotNull(to);
final byte[] buf = createBuffer();
final long total = 0;
while (true) {
final int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
total += r;
}
return total;
}

/**
* Reads all bytes from an input stream into a byte array. Does not close the stream.
*
* @param in the input stream to read from
* @return a byte array containing all the bytes from the stream
* @throws IOException if an I/O error occurs
*/
public static byte[] toByteArray(final InputStream in) throws IOException {
// Presize the ByteArrayOutputStream since we know how large it will need
// to be, unless that value is less than the default ByteArrayOutputStream
// size (32).
final ByteArrayOutputStream out = new ByteArrayOutputStream(
Math.max(32, in.available()));
copy(in, out);
return out.toByteArray();
}
}
// vim: ts=4:sw=4:et
Loading

0 comments on commit 7903c59

Please sign in to comment.