|
| 1 | +package com.immutable.unity; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.ActivityNotFoundException; |
| 5 | +import android.content.ComponentName; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.pm.PackageManager; |
| 9 | +import android.content.pm.ResolveInfo; |
| 10 | +import android.graphics.Insets; |
| 11 | +import android.net.Uri; |
| 12 | +import android.os.Build; |
| 13 | +import android.util.DisplayMetrics; |
| 14 | +import android.view.WindowInsets; |
| 15 | +import android.view.WindowMetrics; |
| 16 | + |
| 17 | +import androidx.annotation.NonNull; |
| 18 | +import androidx.browser.customtabs.CustomTabsCallback; |
| 19 | +import androidx.browser.customtabs.CustomTabsClient; |
| 20 | +import androidx.browser.customtabs.CustomTabsIntent; |
| 21 | +import androidx.browser.customtabs.CustomTabsService; |
| 22 | +import androidx.browser.customtabs.CustomTabsServiceConnection; |
| 23 | +import androidx.browser.customtabs.CustomTabsSession; |
| 24 | + |
| 25 | +import java.lang.ref.WeakReference; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
| 31 | + |
| 32 | +public class CustomTabsController extends CustomTabsServiceConnection { |
| 33 | + private static final long MAX_WAIT_TIME_SECONDS = 1; |
| 34 | + |
| 35 | + private final WeakReference<Activity> context; |
| 36 | + private final AtomicReference<CustomTabsSession> session; |
| 37 | + private final CountDownLatch sessionLatch; |
| 38 | + private final String preferredPackage; |
| 39 | + private final CustomTabsCallback callback; |
| 40 | + |
| 41 | + private boolean didTryToBindService; |
| 42 | + |
| 43 | + public CustomTabsController(@NonNull Activity context, CustomTabsCallback callback) { |
| 44 | + this.context = new WeakReference<>(context); |
| 45 | + this.session = new AtomicReference<>(); |
| 46 | + this.sessionLatch = new CountDownLatch(1); |
| 47 | + this.callback = callback; |
| 48 | + this.preferredPackage = getPreferredCustomTabsPackage(context); |
| 49 | + } |
| 50 | + |
| 51 | + // Get all apps that can support Custom Tabs Service |
| 52 | + // i.e. services that can handle ACTION_CUSTOM_TABS_CONNECTION intents |
| 53 | + private String getPreferredCustomTabsPackage(@NonNull Activity context) { |
| 54 | + PackageManager packageManager = context.getPackageManager(); |
| 55 | + Intent serviceIntent = new Intent(); |
| 56 | + serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION); |
| 57 | + List<ResolveInfo> resolvedList = packageManager.queryIntentServices(serviceIntent, 0); |
| 58 | + List<String> packageNames = new ArrayList<>(); |
| 59 | + for (ResolveInfo info : resolvedList) { |
| 60 | + if (info.serviceInfo != null) { |
| 61 | + packageNames.add(info.serviceInfo.packageName); |
| 62 | + } |
| 63 | + } |
| 64 | + if (packageNames.size() > 0) { |
| 65 | + // Get the preferred Custom Tabs package |
| 66 | + return CustomTabsClient.getPackageName(context, packageNames); |
| 67 | + } else { |
| 68 | + return null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onCustomTabsServiceConnected(@NonNull ComponentName componentName, @NonNull CustomTabsClient client) { |
| 74 | + client.warmup(0L); |
| 75 | + CustomTabsSession customTabsSession = client.newSession(callback); |
| 76 | + session.set(customTabsSession); |
| 77 | + sessionLatch.countDown(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onServiceDisconnected(ComponentName componentName) { |
| 82 | + session.set(null); |
| 83 | + } |
| 84 | + |
| 85 | + public void bindService() { |
| 86 | + Context context = this.context.get(); |
| 87 | + didTryToBindService = false; |
| 88 | + if (context != null && preferredPackage != null) { |
| 89 | + didTryToBindService = true; |
| 90 | + CustomTabsClient.bindCustomTabsService(context, preferredPackage, this); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public void unbindService() { |
| 95 | + Context context = this.context.get(); |
| 96 | + if (didTryToBindService && context != null) { |
| 97 | + context.unbindService(this); |
| 98 | + didTryToBindService = false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void launch(@NonNull final Uri uri) { |
| 103 | + final Activity context = this.context.get(); |
| 104 | + if (context == null) { |
| 105 | + // Custom tabs context is no longer valid |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (preferredPackage == null) { |
| 110 | + // Could not get the preferred Custom Tab browser, so launch URL in any browser |
| 111 | + context.startActivity(new Intent(Intent.ACTION_VIEW, uri)); |
| 112 | + } else { |
| 113 | + // Running in a different thread to prevent doing too much work on main thread |
| 114 | + new Thread(() -> { |
| 115 | + try { |
| 116 | + launchCustomTabs(context, uri); |
| 117 | + } catch (ActivityNotFoundException ex) { |
| 118 | + // Failed to launch Custom Tab browser, so launch in browser |
| 119 | + context.startActivity(new Intent(Intent.ACTION_VIEW, uri)); |
| 120 | + } |
| 121 | + }).start(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void launchCustomTabs(Activity context, Uri uri) { |
| 126 | + bindService(); |
| 127 | + try { |
| 128 | + boolean ignored = sessionLatch.await(MAX_WAIT_TIME_SECONDS, TimeUnit.SECONDS); |
| 129 | + } catch (InterruptedException ignored) { |
| 130 | + } |
| 131 | + |
| 132 | + final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(session.get()) |
| 133 | + .setInitialActivityHeightPx(getCustomTabsHeight(context)) |
| 134 | + .setShareState(CustomTabsIntent.SHARE_STATE_OFF); |
| 135 | + final Intent intent = builder.build().intent; |
| 136 | + intent.setData(uri); |
| 137 | + context.startActivity(intent); |
| 138 | + } |
| 139 | + |
| 140 | + private int getCustomTabsHeight(Activity context) { |
| 141 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 142 | + WindowMetrics windowMetrics = context.getWindowManager().getCurrentWindowMetrics(); |
| 143 | + Insets insets = windowMetrics.getWindowInsets() |
| 144 | + .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()); |
| 145 | + return windowMetrics.getBounds().height() - insets.top - insets.bottom; |
| 146 | + } else { |
| 147 | + DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 148 | + context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); |
| 149 | + return displayMetrics.heightPixels; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments