|
1 | 1 | package com.proyecto26.inappbrowser; |
2 | 2 |
|
3 | | -import android.R.anim; |
4 | | -import android.net.Uri; |
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; |
34 | 42 |
|
35 | 43 | public class RNInAppBrowser { |
36 | 44 | private final static String ERROR_CODE = "InAppBrowser"; |
@@ -63,6 +71,18 @@ public class RNInAppBrowser { |
63 | 71 | private Activity currentActivity; |
64 | 72 | private static final Pattern animationIdentifierPattern = Pattern.compile("^.+:.+/"); |
65 | 73 |
|
| 74 | + @Nullable |
| 75 | + private CustomTabsClient customTabsClient; |
| 76 | + |
| 77 | + private static RNInAppBrowser _inAppBrowser; |
| 78 | + |
| 79 | + public static RNInAppBrowser getInstance() { |
| 80 | + if (_inAppBrowser == null) { |
| 81 | + _inAppBrowser = new RNInAppBrowser(); |
| 82 | + } |
| 83 | + return _inAppBrowser; |
| 84 | + } |
| 85 | + |
66 | 86 | public Integer setColor(CustomTabsIntent.Builder builder, final ReadableMap options, String key, String method, String colorName) { |
67 | 87 | String colorString = null; |
68 | 88 | Integer color = null; |
@@ -303,4 +323,57 @@ private String getDefaultBrowser(Context context) { |
303 | 323 | } |
304 | 324 | return packageName; |
305 | 325 | } |
| 326 | + |
| 327 | + public void onStart(Activity activity) { |
| 328 | + Context applicationContext = activity.getApplicationContext(); |
| 329 | + CustomTabsServiceConnection connection = new CustomTabsServiceConnection() { |
| 330 | + @Override |
| 331 | + public void onCustomTabsServiceConnected(@NonNull ComponentName name, @NonNull CustomTabsClient client) { |
| 332 | + customTabsClient = client; |
| 333 | + if (!customTabsClient.warmup(0L)) { |
| 334 | + System.err.println("Couldn't warmup custom tabs client"); |
| 335 | + } |
| 336 | + applicationContext.unbindService(this); |
| 337 | + } |
| 338 | + |
| 339 | + @Override |
| 340 | + public void onServiceDisconnected(ComponentName name) { |
| 341 | + customTabsClient = null; |
| 342 | + } |
| 343 | + }; |
| 344 | + |
| 345 | + final String packageName = getDefaultBrowser(applicationContext); |
| 346 | + if (packageName != null) { |
| 347 | + CustomTabsClient.bindCustomTabsService(applicationContext, packageName, connection); |
| 348 | + } else { |
| 349 | + System.err.println("No browser supported to bind custom tab service"); |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + public void warmup(final Promise promise) { |
| 354 | + if (customTabsClient != null) { |
| 355 | + promise.resolve(customTabsClient.warmup(0L)); |
| 356 | + } |
| 357 | + promise.resolve(false); |
| 358 | + } |
| 359 | + |
| 360 | + public void mayLaunchUrl(String mostLikelyUrl, ReadableArray otherUrls) { |
| 361 | + if (customTabsClient != null) { |
| 362 | + final CustomTabsSession customTabsSession = customTabsClient.newSession(new CustomTabsCallback()); |
| 363 | + if (customTabsSession != null) { |
| 364 | + final ArrayList<Bundle> otherUrlBundles = new ArrayList<>(otherUrls.size()); |
| 365 | + |
| 366 | + for (int index = 0; index < otherUrls.size(); index++) { |
| 367 | + String link = otherUrls.getString(index); |
| 368 | + if (link != null) { |
| 369 | + final Bundle bundle = new Bundle(); |
| 370 | + bundle.putParcelable(CustomTabsService.KEY_URL, Uri.parse(link)); |
| 371 | + otherUrlBundles.add(bundle); |
| 372 | + } |
| 373 | + } |
| 374 | + |
| 375 | + customTabsSession.mayLaunchUrl(Uri.parse(mostLikelyUrl), null, otherUrlBundles); |
| 376 | + } |
| 377 | + } |
| 378 | + } |
306 | 379 | } |
0 commit comments