Skip to content

Instantly share code, notes, and snippets.

@YeWang0
Last active March 24, 2018 03:55
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 YeWang0/d344618bf8ac269dc3c39fa45214cf8b to your computer and use it in GitHub Desktop.
Save YeWang0/d344618bf8ac269dc3c39fa45214cf8b to your computer and use it in GitHub Desktop.
Reference of Custom Chrome Tab implementation (For save page implementation)

Custom Chrome Tab implementation reference

Reference link:

Introduction: https://developer.chrome.com/multidevice/android/customtabs#whentouse

Customize chrome tab: https://segunfamisa.com/posts/chrome-custom-tabs

Interface: https://developer.android.com/reference/android/support/customtabs/CustomTabsIntent.html https://developer.android.com/reference/android/support/customtabs/CustomTabsIntent.Builder.html

Source code: https://github.com/GoogleChrome/custom-tabs-client/blob/master/customtabs/src/android/support/customtabs/CustomTabsIntent.java

Reference project

Github demos of chrome tab features: https://github.com/GoogleChrome/custom-tabs-client.git There are multiple apps inside.(list related features)

  1. Application/MainActivity
    • bind CustomTabs Service (initialization and finalization) :
      • Locate : MainActivity.java/bindCustomTabsService()
    • Customize bottom tool bar (a mini music player) :
      • Locate : BottomBarManager.java
      • Feature : Can hide and show the bar by click or scroll
    • Add a share button to top tool bar :
      • Locate : MainActivity.java/prepareActionButton()
    • Add menu item :
      • Locate : MainActivity.java/prepareMenuItems()
  2. demos/CustomUIActivity
    • Add Action Buttion
    • Add default share
    • Add toolbar item
    • Show title
    • Custom back button
    • Auto-hide AppBar
    • Set top and bottom toolbar color

Reference code:

Customize the bottom action bar

// Set toolbar color
intentBuilder.setSecondaryToolbarColor(getResources().getColor(android.R.color.holo_red_light));

// add multiple tool bar
// Example of creating 5 buttons at bottom tool bar.
// i == 0 will create button at top.

Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back);

for (int i = 1; i < 6; i++) {

	PendingIntent pendingIntent = createPendingIntent(i);

	intentBuilder.addToolbarItem(i, icon, actionLabel, pendingIntent);

}

private PendingIntent createPendingIntent(int actionSourceId) {

	Intent actionIntent = new Intent(

	this.getApplicationContext(), ActionBroadcastReceiver.class);

	actionIntent.putExtra(ActionBroadcastReceiver.KEY_ACTION_SOURCE, actionSourceId);

	return PendingIntent.getBroadcast(getApplicationContext(), actionSourceId, actionIntent, 0);
}

Handle actions using a BroadcastReceiver

public class ActionBroadcastReceiver extends BroadcastReceiver {

	public static final String KEY_ACTION_SOURCE = "org.chromium.customtabsdemos.ACTION_SOURCE";

	@Override

	public void onReceive(Context context, Intent intent) {

		// get the link of current url
		String url = intent.getDataString();
		// get action id
		int actionId = intent.getIntExtra(KEY\_ACTION\_SOURCE, -1);

		handleAction(actionId);

	}
	public void handleAction(int actionId) {

		switch (actionId) {

			// Handle actions here

			1.  Save page
			    
			2.  Share link
			    
			3.  ...
		    
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment