Skip to content

Commit 8748d9b

Browse files
CustomTabs web api for plugins (#1785)
* feat: custom tab api * feat: undo delete proot plugin * feat: added suggestions of gemini
1 parent 715d50d commit 8748d9b

File tree

6 files changed

+162
-17
lines changed

6 files changed

+162
-17
lines changed

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"cordova-plugin-sftp": {},
3939
"cordova-plugin-system": {},
4040
"com.foxdebug.acode.rk.exec.proot": {},
41-
"com.foxdebug.acode.rk.exec.terminal": {}
41+
"com.foxdebug.acode.rk.exec.terminal": {},
42+
"com.foxdebug.acode.rk.customtabs": {}
4243
},
4344
"platforms": [
4445
"android"
@@ -63,6 +64,7 @@
6364
"@types/url-parse": "^1.4.11",
6465
"autoprefixer": "^10.4.21",
6566
"babel-loader": "^10.0.0",
67+
"com.foxdebug.acode.rk.customtabs": "file:src/plugins/custom-tabs",
6668
"com.foxdebug.acode.rk.exec.proot": "file:src/plugins/proot",
6769
"com.foxdebug.acode.rk.exec.terminal": "file:src/plugins/terminal",
6870
"cordova-android": "^14.0.1",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "com.foxdebug.acode.rk.customtabs",
3+
"version": "1.0.0",
4+
"description": "Custom tabs api",
5+
"cordova": {
6+
"id": "com.foxdebug.acode.rk.customtabs",
7+
"platforms": [
8+
"android"
9+
]
10+
},
11+
"keywords": [
12+
"ecosystem:cordova",
13+
"cordova-android"
14+
],
15+
"author": "@RohitKushvaha01",
16+
"license": "MIT"
17+
}

src/plugins/custom-tabs/plugin.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<name>Custom Tabs</name>
4+
5+
6+
<js-module name="CustomTabs" src="www/customtabs.js">
7+
<clobbers target="CustomTabs"/>
8+
</js-module>
9+
10+
11+
<platform name="android">
12+
13+
<config-file parent="/*" target="res/xml/config.xml">
14+
<feature name="CustomTabs">
15+
<param name="android-package" value="com.foxdebug.acode.rk.customtabs.CustomTabsPlugin" />
16+
</feature>
17+
18+
</config-file>
19+
20+
<config-file parent="/*" target="AndroidManifest.xml">
21+
<queries>
22+
<intent>
23+
<action android:name="android.support.customtabs.action.CustomTabsService" />
24+
</intent>
25+
</queries>
26+
</config-file>
27+
28+
<framework src="androidx.browser:browser:1.8.0"/>
29+
30+
<source-file
31+
src="src/CustomTabsPlugin.java"
32+
target-dir="src/com/foxdebug/acode/rk/customtabs"/>
33+
34+
35+
</platform>
36+
</plugin>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.foxdebug.acode.rk.customtabs;
2+
3+
import android.content.ActivityNotFoundException;
4+
import android.content.Intent;
5+
import android.net.Uri;
6+
import android.graphics.Color;
7+
8+
import androidx.browser.customtabs.CustomTabsIntent;
9+
10+
import org.apache.cordova.CallbackContext;
11+
import org.apache.cordova.CordovaPlugin;
12+
import org.json.JSONArray;
13+
import org.json.JSONObject;
14+
15+
public class CustomTabsPlugin extends CordovaPlugin {
16+
17+
@Override
18+
public boolean execute(
19+
String action,
20+
JSONArray args,
21+
CallbackContext callbackContext
22+
) {
23+
24+
if ("open".equals(action)) {
25+
try {
26+
final String url = args.getString(0);
27+
final JSONObject options = args.optJSONObject(1) != null
28+
? args.optJSONObject(1)
29+
: new JSONObject();
30+
31+
cordova.getActivity().runOnUiThread(() -> {
32+
try {
33+
openCustomTab(url, options);
34+
callbackContext.success();
35+
} catch (Exception e) {
36+
callbackContext.error(e.getMessage());
37+
}
38+
});
39+
40+
return true;
41+
42+
} catch (Exception e) {
43+
callbackContext.error(e.getMessage());
44+
return true;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
51+
private void openCustomTab(String url, JSONObject options) {
52+
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
53+
54+
String toolbarColor = options.optString("toolbarColor", null);
55+
if (toolbarColor != null && !toolbarColor.isEmpty()) {
56+
builder.setToolbarColor(Color.parseColor(toolbarColor));
57+
}
58+
59+
CustomTabsIntent customTabsIntent = builder.build();
60+
61+
if (options.optBoolean("showTitle", true)) {
62+
customTabsIntent.intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.SHOW_PAGE_TITLE);
63+
}
64+
65+
try {
66+
customTabsIntent.launchUrl(
67+
cordova.getActivity(),
68+
Uri.parse(url)
69+
);
70+
} catch (ActivityNotFoundException e) {
71+
// Fallback to default browser
72+
Intent fallback = new Intent(
73+
Intent.ACTION_VIEW,
74+
Uri.parse(url)
75+
);
76+
cordova.getActivity().startActivity(fallback);
77+
}
78+
}
79+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var exec = require('cordova/exec');
2+
3+
exports.open = function (url, options, success, error) {
4+
exec(
5+
success,
6+
error,
7+
'CustomTabs',
8+
'open',
9+
[url, options || {}]
10+
);
11+
};

0 commit comments

Comments
 (0)