Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added a process to launch the app independently on android+chrome83 or higher. #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ cordova plugin add cordova-plugin-openwith \

It shouldn't be too hard. But just in case, Jean-Christophe Hoelt [posted a screencast of it](https://youtu.be/eaE4m_xO1mg).

If you want to launch it as a task independent of chrome 83 or higher, please add the following.
```
<custom-preference name="android-manifest/application/activity/@android:name" value="OpenWithActivity" />
```

## Usage

```js
Expand Down
33 changes: 33 additions & 0 deletions hooks/androidEmbedPackageName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const ACTIVITY_PATH = './platforms/android/app/src/main/java/com/missiveapp/openwith/OpenWithActivity.java';

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

const {
redError,
} = require('./utils')

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

var parser = context.requireCordovaModule('cordova-common').ConfigParser;
var config = new parser("config.xml");
var packageName = config.android_packageName() || config.packageName();

fs.readFile(ACTIVITY_PATH, 'utf8', function (err,data) {
if (err) {
throw redError(err.message);
}

var result = data.replace(/##ANDROID_PACKAGE_NAME##/g, packageName);

fs.writeFile(ACTIVITY_PATH, result, 'utf8', function (err) {
if (err) {
throw redError(err.message);
}
deferral.resolve();
});
});

return deferral.promise;
};
4 changes: 4 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ SOFTWARE.
</config-file>

<!-- cordova plugin src files -->
<source-file src="src/android/com/missiveapp/openwith/OpenWithActivity.java" target-dir="src/com/missiveapp/openwith" />
<source-file src="src/android/com/missiveapp/openwith/OpenWithPlugin.java" target-dir="src/com/missiveapp/openwith" />
<source-file src="src/android/com/missiveapp/openwith/PluginResultSender.java" target-dir="src/com/missiveapp/openwith" />
<source-file src="src/android/com/missiveapp/openwith/Serializer.java" target-dir="src/com/missiveapp/openwith" />

<!-- Cordova hooks -->
<hook type="after_plugin_install" src="hooks/androidEmbedPackageName.js" />
</platform>
</plugin>
49 changes: 49 additions & 0 deletions src/android/com/missiveapp/openwith/OpenWithActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package ##ANDROID_PACKAGE_NAME##;

import android.content.Intent;
import android.os.Bundle;
import org.apache.cordova.*;

public class OpenWithActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

if (getIntent().getIntExtra("org.chromium.chrome.extra.TASK_ID", -1) == getTaskId()) {
this.finish();
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}

// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}