Skip to content

Commit a3acf46

Browse files
authored
Merge pull request #208 from m1ga/closeEvent
parity: android close event
2 parents 4d9ec93 + d3ad207 commit a3acf46

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

README.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
# Titanium Web Dialog
22

3-
Use the native `SFSafariViewController` (iOS) and `Chrome Pages` (Android) within Axway Titanium.
3+
Use the native `SFSafariViewController` (iOS) and `Chrome Pages` (Android) within Appcelerator Titanium.
44

55
<img src="./fixtures/example-screens.jpg" width="890" alt="Titanium Web Dialog" />
66

77
## Requirements
88

9-
- Titanium SDK 7.0.0 or later (or use the [SDK-6-compatibility](https://github.com/appcelerator-modules/titanium-web-dialog/tree/SDK-6-compatibility) Titanium SDK 6.x)
9+
- Titanium SDK 9.0.0 or later
1010
- iOS 9+ and Android 4.1+
1111

1212
## iOS Note
1313

1414
The iOS part of this module is based on Ti.SafariDialog, which has been deprecated for a cross-platform solution. All API's of Ti.SafariDialog
1515
still work here and have been extended by more features over time.
1616

17-
## Android Legacy Support
18-
19-
This module is designed to work with the latest platform API's that are covered by the Titanium SDK 7.0.0 and later.
20-
If you want to use this module in Titanium SDK 6.x, please use the [this version](https://github.com/appcelerator-modules/titanium-web-dialog/raw/SDK-6-compatibility/android/legacy/ti.webdialog-android-1.0.0.zip).
17+
## Android Note
18+
In order to use the `close` event on Android it is recommended to have a short delay between `var WebDialog = require('ti.webdialog');` and `WebDialog.open({})`. Otherwise it might not fire the `close` event.
2119

2220
## API's
2321

@@ -38,7 +36,7 @@ If you want to use this module in Titanium SDK 6.x, please use the [this version
3836
* `fadeTransition` (Boolean, Android only)
3937
* `enableSharing` (Boolean, Android only) - Enable Share... menu item to share link
4038
* `closeIcon` (String, Android only) - image path to show as close-button icon
41-
39+
4240
* `isSupported()` -> Boolean
4341
* `isOpen()` (iOS only) -> Boolean
4442
* `close()` (iOS only)
@@ -52,7 +50,7 @@ If you want to use this module in Titanium SDK 6.x, please use the [this version
5250
#### Events
5351

5452
* `open` -> `success` (Boolean), `url` (String)
55-
* `close` -> `success` (Boolean), `url` (String) - iOS only
53+
* `close` -> `success` (Boolean), `url` (String)
5654
* `load` -> `success` (Boolean), `url` (String) - iOS only
5755
* `redirect` -> `url` (String) - iOS only
5856

@@ -62,7 +60,7 @@ If you want to use this module in Titanium SDK 6.x, please use the [this version
6260

6361
* `createAuthenticationSession(arguments)`
6462
* `url` (String)
65-
* `scheme` (String)
63+
* `scheme` (String)
6664

6765
#### Events
6866

android/manifest

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 2.1.0
5+
version: 2.2.0
66
apiversion: 4
77
architectures: arm64-v8a armeabi-v7a x86 x86_64
88
description: titanium-web-dialog

android/src/ti/webdialog/TitaniumWebDialogModule.java

+57-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
package ti.webdialog;
1010

11+
import android.content.ComponentName;
1112
import android.content.Context;
1213
import android.content.Intent;
1314
import android.content.pm.ResolveInfo;
@@ -16,10 +17,16 @@
1617
import android.graphics.drawable.BitmapDrawable;
1718
import android.graphics.drawable.Drawable;
1819
import android.net.Uri;
20+
import android.os.Bundle;
1921
import android.util.DisplayMetrics;
2022
import androidx.browser.customtabs.CustomTabColorSchemeParams;
23+
import androidx.browser.customtabs.CustomTabsCallback;
24+
import androidx.browser.customtabs.CustomTabsClient;
2125
import androidx.browser.customtabs.CustomTabsIntent;
2226
import androidx.browser.customtabs.CustomTabsService;
27+
import androidx.browser.customtabs.CustomTabsServiceConnection;
28+
import androidx.browser.customtabs.CustomTabsSession;
29+
2330
import java.util.ArrayList;
2431
import java.util.List;
2532
import org.appcelerator.kroll.KrollDict;
@@ -38,6 +45,52 @@ public class TitaniumWebDialogModule extends KrollModule
3845
{
3946
// Standard Debugging variables
4047
private static final String LCAT = "TiWebDialog";
48+
private CustomTabsSession mCustomTabsSession;
49+
private CustomTabsClient mCustomTabsClient;
50+
private CustomTabsServiceConnection mCustomTabsServiceConnection;
51+
private String url = "";
52+
53+
public TitaniumWebDialogModule()
54+
{
55+
Context context = TiApplication.getAppCurrentActivity();
56+
57+
mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
58+
@Override
59+
public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient)
60+
{
61+
mCustomTabsClient = customTabsClient;
62+
mCustomTabsClient.warmup(0L);
63+
mCustomTabsSession = mCustomTabsClient.newSession(new CustomTabsCallback() {
64+
@Override
65+
public void onNavigationEvent(int navigationEvent, Bundle extras)
66+
{
67+
super.onNavigationEvent(navigationEvent, extras);
68+
69+
KrollDict event = new KrollDict();
70+
if (navigationEvent == TAB_HIDDEN) {
71+
event.put("url", url);
72+
fireEvent("close", event);
73+
}
74+
}
75+
76+
@Override
77+
public void extraCallback(String callbackName, Bundle args)
78+
{
79+
super.extraCallback(callbackName, args);
80+
}
81+
});
82+
}
83+
84+
@Override
85+
public void onServiceDisconnected(ComponentName name)
86+
{
87+
Log.d(LCAT, "disconnected");
88+
mCustomTabsClient = null;
89+
}
90+
};
91+
92+
CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", mCustomTabsServiceConnection);
93+
}
4194

4295
private List<String> getCustomTabBrowsers(Context context, List<ResolveInfo> browsersList)
4396
{
@@ -60,8 +113,8 @@ private List<String> getCustomTabBrowsers(Context context, List<ResolveInfo> bro
60113

61114
private void openCustomTab(Context context, List<String> customTabBrowsers, KrollDict options)
62115
{
63-
String URL = options.getString(Params.URL);
64-
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
116+
url = options.getString(Params.URL);
117+
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(mCustomTabsSession);
65118
builder.setShowTitle(Utils.getBool(options, Params.SHOW_TITLE));
66119

67120
int barColor = Utils.getColor(options, Params.BAR_COLOR);
@@ -102,7 +155,7 @@ private void openCustomTab(Context context, List<String> customTabBrowsers, Krol
102155
tabIntent.intent.setPackage(s);
103156
}
104157

105-
tabIntent.launchUrl(context, Uri.parse(URL));
158+
tabIntent.launchUrl(context, Uri.parse(url));
106159
}
107160

108161
private Bitmap getIcon(String path)
@@ -157,7 +210,7 @@ public void open(KrollDict options)
157210
openCustomTab(context, customTabBrowsers, options);
158211

159212
} else {
160-
Log.i(Params.LCAT, "No browsers available in this device.");
213+
Log.d(Params.LCAT, "No browsers available in this device.");
161214
}
162215

163216
fireEvent("open", event);

example/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ win.add(btnOpenDialog);
1212

1313
btnOpenDialog.addEventListener('click', function () {
1414
WebDialog.open({
15-
url: 'https://appcelerator.com',
15+
url: 'https://tidev.io/',
1616
title: 'Hello World',
1717

1818
// iOS 10+

0 commit comments

Comments
 (0)