|
1 | 1 | package com.proyecto26.inappbrowser; |
2 | 2 |
|
3 | | -import android.net.Uri; |
4 | | -import android.os.Build; |
5 | | -import android.os.Bundle; |
6 | | -import android.text.TextUtils; |
7 | 3 | import android.app.Activity; |
| 4 | +import android.content.ComponentName; |
8 | 5 | import android.content.Context; |
9 | 6 | import android.content.Intent; |
10 | 7 | import android.content.pm.ResolveInfo; |
11 | | -import android.graphics.Color; |
12 | 8 | import android.graphics.BitmapFactory; |
| 9 | +import android.graphics.Color; |
| 10 | +import android.net.Uri; |
| 11 | +import android.os.Bundle; |
13 | 12 | import android.provider.Browser; |
| 13 | +import android.text.TextUtils; |
| 14 | + |
| 15 | +import androidx.annotation.NonNull; |
14 | 16 | import androidx.annotation.Nullable; |
| 17 | +import androidx.browser.customtabs.CustomTabsCallback; |
15 | 18 | import androidx.browser.customtabs.CustomTabsClient; |
16 | 19 | import androidx.browser.customtabs.CustomTabsIntent; |
| 20 | +import androidx.browser.customtabs.CustomTabsService; |
| 21 | +import androidx.browser.customtabs.CustomTabsServiceConnection; |
| 22 | +import androidx.browser.customtabs.CustomTabsSession; |
17 | 23 | import androidx.core.graphics.ColorUtils; |
18 | 24 |
|
19 | 25 | import com.facebook.react.bridge.Arguments; |
| 26 | +import com.facebook.react.bridge.JSApplicationIllegalArgumentException; |
20 | 27 | import com.facebook.react.bridge.Promise; |
21 | | -import com.facebook.react.bridge.WritableMap; |
| 28 | +import com.facebook.react.bridge.ReadableArray; |
22 | 29 | import com.facebook.react.bridge.ReadableMap; |
23 | | -import com.facebook.react.bridge.ReadableType; |
24 | 30 | import com.facebook.react.bridge.ReadableMapKeySetIterator; |
25 | | -import com.facebook.react.bridge.JSApplicationIllegalArgumentException; |
| 31 | +import com.facebook.react.bridge.ReadableType; |
| 32 | +import com.facebook.react.bridge.WritableMap; |
26 | 33 |
|
27 | 34 | import org.greenrobot.eventbus.EventBus; |
28 | 35 | import org.greenrobot.eventbus.Subscribe; |
29 | 36 |
|
30 | 37 | import java.lang.reflect.Method; |
| 38 | +import java.util.ArrayList; |
31 | 39 | import java.util.Arrays; |
32 | | -import java.util.regex.Pattern; |
33 | 40 | import java.util.List; |
| 41 | +import java.util.regex.Pattern; |
| 42 | + |
34 | 43 | public class RNInAppBrowser { |
35 | 44 | private final static String ERROR_CODE = "InAppBrowser"; |
36 | 45 | private static final String KEY_TOOLBAR_COLOR = "toolbarColor"; |
@@ -63,6 +72,18 @@ public class RNInAppBrowser { |
63 | 72 | private Activity currentActivity; |
64 | 73 | private static final Pattern animationIdentifierPattern = Pattern.compile("^.+:.+/"); |
65 | 74 |
|
| 75 | + @Nullable |
| 76 | + private CustomTabsClient customTabsClient; |
| 77 | + |
| 78 | + private static RNInAppBrowser _inAppBrowser; |
| 79 | + |
| 80 | + public static RNInAppBrowser getInstance() { |
| 81 | + if (_inAppBrowser == null) { |
| 82 | + _inAppBrowser = new RNInAppBrowser(); |
| 83 | + } |
| 84 | + return _inAppBrowser; |
| 85 | + } |
| 86 | + |
66 | 87 | public Integer setColor(CustomTabsIntent.Builder builder, final ReadableMap options, String key, String method, String colorName) { |
67 | 88 | String colorString = null; |
68 | 89 | Integer color = null; |
@@ -309,4 +330,57 @@ private String getDefaultBrowser(Context context) { |
309 | 330 | } |
310 | 331 | return packageName; |
311 | 332 | } |
| 333 | + |
| 334 | + public void onStart(Activity activity) { |
| 335 | + Context applicationContext = activity.getApplicationContext(); |
| 336 | + CustomTabsServiceConnection connection = new CustomTabsServiceConnection() { |
| 337 | + @Override |
| 338 | + public void onCustomTabsServiceConnected(@NonNull ComponentName name, @NonNull CustomTabsClient client) { |
| 339 | + customTabsClient = client; |
| 340 | + if (!customTabsClient.warmup(0L)) { |
| 341 | + System.err.println("Couldn't warmup custom tabs client"); |
| 342 | + } |
| 343 | + applicationContext.unbindService(this); |
| 344 | + } |
| 345 | + |
| 346 | + @Override |
| 347 | + public void onServiceDisconnected(ComponentName name) { |
| 348 | + customTabsClient = null; |
| 349 | + } |
| 350 | + }; |
| 351 | + |
| 352 | + final String packageName = getDefaultBrowser(applicationContext); |
| 353 | + if (packageName != null) { |
| 354 | + CustomTabsClient.bindCustomTabsService(applicationContext, packageName, connection); |
| 355 | + } else { |
| 356 | + System.err.println("No browser supported to bind custom tab service"); |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + public void warmup(final Promise promise) { |
| 361 | + if (customTabsClient != null) { |
| 362 | + promise.resolve(customTabsClient.warmup(0L)); |
| 363 | + } |
| 364 | + promise.resolve(false); |
| 365 | + } |
| 366 | + |
| 367 | + public void mayLaunchUrl(String mostLikelyUrl, ReadableArray otherUrls) { |
| 368 | + if (customTabsClient != null) { |
| 369 | + final CustomTabsSession customTabsSession = customTabsClient.newSession(new CustomTabsCallback()); |
| 370 | + if (customTabsSession != null) { |
| 371 | + final ArrayList<Bundle> otherUrlBundles = new ArrayList<>(otherUrls.size()); |
| 372 | + |
| 373 | + for (int index = 0; index < otherUrls.size(); index++) { |
| 374 | + String link = otherUrls.getString(index); |
| 375 | + if (link != null) { |
| 376 | + final Bundle bundle = new Bundle(); |
| 377 | + bundle.putParcelable(CustomTabsService.KEY_URL, Uri.parse(link)); |
| 378 | + otherUrlBundles.add(bundle); |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + customTabsSession.mayLaunchUrl(Uri.parse(mostLikelyUrl), null, otherUrlBundles); |
| 383 | + } |
| 384 | + } |
| 385 | + } |
312 | 386 | } |
0 commit comments