Created
September 9, 2015 19:31
-
-
Save tomredman/ae2d2185879793d10ad2 to your computer and use it in GitHub Desktop.
A simple, reusable implementation of Chrome custom tabs for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.buffer.android.helpers.customtabs; | |
import android.app.Activity; | |
import android.content.ComponentName; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.customtabs.CustomTabsCallback; | |
import android.support.customtabs.CustomTabsClient; | |
import android.support.customtabs.CustomTabsIntent; | |
import android.support.customtabs.CustomTabsServiceConnection; | |
import android.support.customtabs.CustomTabsSession; | |
import android.util.Log; | |
import org.buffer.android.R; | |
/** | |
* ----------------------- | |
* ChromeCustomTab | |
* ----------------------- | |
* | |
* @author Tom Redman | |
* 15-09-09 | |
*/ | |
public class ChromeCustomTab { | |
public static final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome"; | |
private Activity activity; | |
private String url; | |
private CustomTabsSession customTabsSession; | |
private CustomTabsClient client; | |
private CustomTabsServiceConnection connection; | |
private boolean warmupWhenReady = false; | |
private boolean mayLaunchWhenReady = false; | |
public ChromeCustomTab(Activity activity, String url) { | |
this.activity = activity; | |
this.url = url; | |
bindCustomTabsService(); | |
} | |
public void warmup() { | |
if (client != null) { | |
warmupWhenReady = false; | |
client.warmup(0); | |
} | |
else { | |
warmupWhenReady = true; | |
} | |
} | |
public void mayLaunch() { | |
CustomTabsSession session = getSession(); | |
if (client != null) { | |
mayLaunchWhenReady = false; | |
session.mayLaunchUrl(Uri.parse(url), null, null); | |
} | |
else { | |
mayLaunchWhenReady = true; | |
} | |
} | |
public void show() { | |
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(getSession()); | |
builder.setToolbarColor(activity.getResources().getColor(R.color.buffer_blue)).setShowTitle(true); | |
builder.setStartAnimations(activity, R.anim.slide_in_left, R.anim.slide_out_right); | |
builder.setExitAnimations(activity, R.anim.slide_in_right, R.anim.slide_out_left); | |
CustomTabsIntent customTabsIntent = builder.build(); | |
customTabsIntent.launchUrl(activity, Uri.parse(url)); | |
} | |
private CustomTabsSession getSession() { | |
if (client == null) { | |
customTabsSession = null; | |
} else if (customTabsSession == null) { | |
customTabsSession = client.newSession(new CustomTabsCallback() { | |
@Override | |
public void onNavigationEvent(int navigationEvent, Bundle extras) { | |
Log.w("CustomTabs", "onNavigationEvent: Code = " + navigationEvent); | |
} | |
}); | |
} | |
return customTabsSession; | |
} | |
private void bindCustomTabsService() { | |
if (client != null) { | |
return; | |
} | |
connection = new CustomTabsServiceConnection() { | |
@Override | |
public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) { | |
ChromeCustomTab.this.client = client; | |
if (warmupWhenReady) { | |
warmup(); | |
} | |
if (mayLaunchWhenReady) { | |
mayLaunch(); | |
} | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
client = null; | |
} | |
}; | |
boolean ok = CustomTabsClient.bindCustomTabsService(activity, CUSTOM_TAB_PACKAGE_NAME, connection); | |
if (!ok) { | |
connection = null; | |
} | |
} | |
public void unbindCustomTabsService() { | |
if (connection == null) { | |
return; | |
} | |
activity.unbindService(connection); | |
client = null; | |
customTabsSession = null; | |
} | |
} |
let me try it , any thanks so much
i need some doubt.. how to create instant url open in our app. that means if i click the link ,that can be open in same app without popup chorme or anythiing, and without showing url .. how can we create ...plz help
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great, thanks a lot :)