-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebBrowserModule.java
202 lines (169 loc) · 7.48 KB
/
WebBrowserModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package expo.modules.webbrowser;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.browser.customtabs.CustomTabsIntent;
import android.text.TextUtils;
import org.unimodules.core.ExportedModule;
import org.unimodules.core.ModuleRegistry;
import org.unimodules.core.Promise;
import org.unimodules.core.arguments.ReadableArguments;
import org.unimodules.core.errors.CurrentActivityNotFoundException;
import org.unimodules.core.interfaces.ExpoMethod;
import java.util.ArrayList;
import java.util.List;
import expo.modules.webbrowser.error.NoPreferredPackageFound;
import expo.modules.webbrowser.error.PackageManagerNotFoundException;
public class WebBrowserModule extends ExportedModule {
private final static String BROWSER_PACKAGE_KEY = "browserPackage";
private final static String SERVICE_PACKAGE_KEY = "servicePackage";
private final static String BROWSER_PACKAGES_KEY = "browserPackages";
private final static String SERVICE_PACKAGES_KEY = "servicePackages";
private final static String PREFERRED_BROWSER_PACKAGE = "preferredBrowserPackage";
private final static String DEFAULT_BROWSER_PACKAGE = "defaultBrowserPackage";
private final static String SHOW_IN_RECENTS = "showInRecents";
private final static String DEFAULT_SHARE_MENU_ITEM_KEY = "enableDefaultShare";
private final static String TOOLBAR_COLOR_KEY = "toolbarColor";
private final static String ERROR_CODE = "EXWebBrowser";
private static final String TAG = "ExpoWebBrowser";
private static final String SHOW_TITLE_KEY = "showTitle";
private static final String ENABLE_BAR_COLLAPSING_KEY = "enableBarCollapsing";
private final static String NO_PREFERRED_PACKAGE_MSG = "Cannot determine preferred package without satisfying it.";
private CustomTabsActivitiesHelper mCustomTabsResolver;
private CustomTabsConnectionHelper mConnectionHelper;
public WebBrowserModule(Context context) {
super(context);
}
@Override
public String getName() {
return TAG;
}
@Override
public void onCreate(ModuleRegistry moduleRegistry) {
mCustomTabsResolver = new CustomTabsActivitiesHelper(moduleRegistry);
mConnectionHelper = new CustomTabsConnectionHelper(getContext());
}
@ExpoMethod
public void warmUpAsync(@Nullable String packageName, final Promise promise) {
try {
packageName = givenOfPreferredPackageName(packageName);
mConnectionHelper.warmUp(packageName);
Bundle result = new Bundle();
result.putString(SERVICE_PACKAGE_KEY, packageName);
promise.resolve(result);
} catch (NoPreferredPackageFound ex) {
promise.reject(ex);
}
}
@ExpoMethod
public void coolDownAsync(@Nullable String packageName, final Promise promise) {
try {
packageName = givenOfPreferredPackageName(packageName);
if (mConnectionHelper.coolDown(packageName)) {
Bundle result = new Bundle();
result.putString(SERVICE_PACKAGE_KEY, packageName);
promise.resolve(result);
} else {
promise.resolve(new Bundle());
}
} catch (NoPreferredPackageFound ex) {
promise.reject(ex);
}
}
@ExpoMethod
public void mayInitWithUrlAsync(@Nullable final String url, String packageName, final Promise promise) {
try {
packageName = givenOfPreferredPackageName(packageName);
mConnectionHelper.mayInitWithUrl(packageName, Uri.parse(url));
Bundle result = new Bundle();
result.putString(SERVICE_PACKAGE_KEY, packageName);
promise.resolve(result);
} catch (NoPreferredPackageFound ex) {
promise.reject(ex);
}
}
@ExpoMethod
public void getCustomTabsSupportingBrowsersAsync(final Promise promise) {
try {
ArrayList<String> activities = mCustomTabsResolver.getCustomTabsResolvingActivities();
ArrayList<String> services = mCustomTabsResolver.getCustomTabsResolvingServices();
String preferredPackage = mCustomTabsResolver.getPreferredCustomTabsResolvingActivity(activities);
String defaultPackage = mCustomTabsResolver.getDefaultCustomTabsResolvingActivity();
String defaultCustomTabsPackage = null;
if (activities.contains(defaultPackage)) { // It might happen, that default activity does not support Chrome Tabs. Then it will be ResolvingActivity and we don't want to return it as a result.
defaultCustomTabsPackage = defaultPackage;
}
Bundle result = new Bundle();
result.putStringArrayList(BROWSER_PACKAGES_KEY, activities);
result.putStringArrayList(SERVICE_PACKAGES_KEY, services);
result.putString(PREFERRED_BROWSER_PACKAGE, preferredPackage);
result.putString(DEFAULT_BROWSER_PACKAGE, defaultCustomTabsPackage);
promise.resolve(result);
} catch (CurrentActivityNotFoundException | PackageManagerNotFoundException ex) {
promise.reject(ex);
}
}
@ExpoMethod
public void openBrowserAsync(final String url, ReadableArguments arguments, final Promise promise) {
Intent intent = createCustomTabsIntent(arguments);
intent.setData(Uri.parse(url));
try {
List<ResolveInfo> activities = mCustomTabsResolver.getResolvingActivities(intent);
if (activities.size() > 0) {
mCustomTabsResolver.startCustomTabs(intent);
Bundle result = new Bundle();
result.putString("type", "opened");
promise.resolve(result);
} else {
promise.reject(ERROR_CODE, "No matching activity!");
}
} catch (CurrentActivityNotFoundException | PackageManagerNotFoundException ex) {
promise.reject(ex);
}
}
private Intent createCustomTabsIntent(ReadableArguments arguments) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
String color = arguments.getString(TOOLBAR_COLOR_KEY);
String packageName = arguments.getString(BROWSER_PACKAGE_KEY);
try {
if (!TextUtils.isEmpty(color)) {
int intColor = Color.parseColor(color);
builder.setToolbarColor(intColor);
}
} catch (IllegalArgumentException ignored) {
}
builder.setShowTitle(arguments.getBoolean(SHOW_TITLE_KEY, false));
if (arguments.hasKey(DEFAULT_SHARE_MENU_ITEM_KEY) && options.getBoolean(KEY_DEFAULT_SHARE_MENU_ITEM)) {
builder.addDefaultShareMenuItem();
}
Intent intent = builder.build().intent;
// We cannot use builder's method enableUrlBarHiding, because there is no corresponding disable method and some browsers enables it by default.
intent.putExtra(CustomTabsIntent.EXTRA_ENABLE_URLBAR_HIDING, arguments.getBoolean(ENABLE_BAR_COLLAPSING_KEY, false));
if (!TextUtils.isEmpty(packageName)) {
intent.setPackage(packageName);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (!arguments.getBoolean(SHOW_IN_RECENTS, false)) {
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
}
return intent;
}
private String givenOfPreferredPackageName(@Nullable String packageName) throws NoPreferredPackageFound {
try {
if (TextUtils.isEmpty(packageName)) {
packageName = mCustomTabsResolver.getPreferredCustomTabsResolvingActivity(null);
}
} catch (CurrentActivityNotFoundException | PackageManagerNotFoundException ex) {
throw new NoPreferredPackageFound(NO_PREFERRED_PACKAGE_MSG);
}
if (TextUtils.isEmpty(packageName)) {
throw new NoPreferredPackageFound(NO_PREFERRED_PACKAGE_MSG);
}
return packageName;
}
}