Skip to content
Merged
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
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"cordova-plugin-sftp": {},
"cordova-plugin-system": {},
"com.foxdebug.acode.rk.exec.proot": {},
"com.foxdebug.acode.rk.exec.terminal": {}
"com.foxdebug.acode.rk.exec.terminal": {},
"com.foxdebug.acode.rk.customtabs": {}
},
"platforms": [
"android"
Expand All @@ -63,6 +64,7 @@
"@types/url-parse": "^1.4.11",
"autoprefixer": "^10.4.21",
"babel-loader": "^10.0.0",
"com.foxdebug.acode.rk.customtabs": "file:src/plugins/custom-tabs",
"com.foxdebug.acode.rk.exec.proot": "file:src/plugins/proot",
"com.foxdebug.acode.rk.exec.terminal": "file:src/plugins/terminal",
"cordova-android": "^14.0.1",
Expand Down
17 changes: 17 additions & 0 deletions src/plugins/custom-tabs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "com.foxdebug.acode.rk.customtabs",
"version": "1.0.0",
"description": "Custom tabs api",
"cordova": {
"id": "com.foxdebug.acode.rk.customtabs",
"platforms": [
"android"
]
},
"keywords": [
"ecosystem:cordova",
"cordova-android"
],
"author": "@RohitKushvaha01",
"license": "MIT"
}
36 changes: 36 additions & 0 deletions src/plugins/custom-tabs/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="com.foxdebug.acode.rk.customtabs" version="1.0.0">
<name>Custom Tabs</name>


<js-module name="CustomTabs" src="www/customtabs.js">
<clobbers target="CustomTabs"/>
</js-module>


<platform name="android">

<config-file parent="/*" target="res/xml/config.xml">
<feature name="CustomTabs">
<param name="android-package" value="com.foxdebug.acode.rk.customtabs.CustomTabsPlugin" />
</feature>

</config-file>

<config-file parent="/*" target="AndroidManifest.xml">
<queries>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
</config-file>

<framework src="androidx.browser:browser:1.8.0"/>

<source-file
src="src/CustomTabsPlugin.java"
target-dir="src/com/foxdebug/acode/rk/customtabs"/>


</platform>
</plugin>
79 changes: 79 additions & 0 deletions src/plugins/custom-tabs/src/CustomTabsPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.foxdebug.acode.rk.customtabs;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.graphics.Color;

import androidx.browser.customtabs.CustomTabsIntent;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;

public class CustomTabsPlugin extends CordovaPlugin {

@Override
public boolean execute(
String action,
JSONArray args,
CallbackContext callbackContext
) {

if ("open".equals(action)) {
try {
final String url = args.getString(0);
final JSONObject options = args.optJSONObject(1) != null
? args.optJSONObject(1)
: new JSONObject();

cordova.getActivity().runOnUiThread(() -> {
try {
openCustomTab(url, options);
callbackContext.success();
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
});

return true;

} catch (Exception e) {
callbackContext.error(e.getMessage());
return true;
}
}

return false;
}

private void openCustomTab(String url, JSONObject options) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();

String toolbarColor = options.optString("toolbarColor", null);
if (toolbarColor != null && !toolbarColor.isEmpty()) {
builder.setToolbarColor(Color.parseColor(toolbarColor));
}

CustomTabsIntent customTabsIntent = builder.build();

if (options.optBoolean("showTitle", true)) {
customTabsIntent.intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.SHOW_PAGE_TITLE);
}

try {
customTabsIntent.launchUrl(
cordova.getActivity(),
Uri.parse(url)
);
} catch (ActivityNotFoundException e) {
// Fallback to default browser
Intent fallback = new Intent(
Intent.ACTION_VIEW,
Uri.parse(url)
);
cordova.getActivity().startActivity(fallback);
}
}
}
11 changes: 11 additions & 0 deletions src/plugins/custom-tabs/www/customtabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var exec = require('cordova/exec');

exports.open = function (url, options, success, error) {
exec(
success,
error,
'CustomTabs',
'open',
[url, options || {}]
);
};