Skip to content

Instantly share code, notes, and snippets.

@brainail
Created November 22, 2015 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainail/4c1872a816e0bc208cbd to your computer and use it in GitHub Desktop.
Save brainail/4c1872a816e0bc208cbd to your computer and use it in GitHub Desktop.
public class CustomTabsSceneHelper {
/**
* A Callback for when the service is connected or disconnected. Use those callbacks to
* handle UI changes when the service is connected or disconnected
*/
public static interface ConnectionCallback {
public void onCustomTabsConnected ();
public void onCustomTabsDisconnected ();
}
private CustomTabsClient mClient;
private CustomTabsSession mCustomTabsSession;
private CustomTabsServiceConnection mConnection;
private ConnectionCallback mConnectionCallback;
public static void openCustomTab (
final Activity activity,
final CustomTabsIntent customTabsIntent,
final Uri uri) {
final String packageName = CustomTabsHelper.getPackageNameToUse (activity);
// if we can't find a package name, it means there's no browser that supports
// Custom Tabs installed. So, we fallback to a view intent (or custom WebView if you want)
if (null != packageName) {
customTabsIntent.intent.setPackage (packageName);
customTabsIntent.launchUrl (activity, uri);
} else {
activity.startActivity (new Intent (Intent.ACTION_VIEW, uri));
}
}
public void bindCustomTabsService (final Activity activity) {
final String packageName = CustomTabsHelper.getPackageNameToUse (activity);
if (null == packageName || null == mClient) return;
mConnection = new CustomTabsServiceConnection () {
@Override public void onCustomTabsServiceConnected (ComponentName name, CustomTabsClient client) {
(mClient = client).warmup (0L);
if (null != mConnectionCallback) {
mConnectionCallback.onCustomTabsConnected ();
}
// Initialize a session as soon as possible.
occupySession ();
}
@Override public void onServiceDisconnected (ComponentName name) {
mClient = null;
if (null != mConnectionCallback) {
mConnectionCallback.onCustomTabsDisconnected ();
}
}
};
CustomTabsClient.bindCustomTabsService (activity, packageName, mConnection);
}
public void unbindCustomTabsService (final Activity activity) {
if (null == mConnection) return;
activity.unbindService (mConnection);
mClient = null;
mCustomTabsSession = null;
}
public CustomTabsSession occupySession () {
if (null == mClient) {
// No session without client
mCustomTabsSession = null;
} else if (null == mCustomTabsSession) {
// Create a new one
mCustomTabsSession = mClient.newSession (null);
}
return mCustomTabsSession;
}
public void setConnectionCallback (ConnectionCallback connectionCallback) {
this.mConnectionCallback = connectionCallback;
}
public boolean mayLaunchUrl (Uri uri, Bundle extras, List<Bundle> otherLikelyBundles) {
if (null == mClient) return false;
final CustomTabsSession session = occupySession ();
if (null == session) return false;
return session.mayLaunchUrl (uri, extras, otherLikelyBundles);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment