Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #24 from eduardb/master
Browse files Browse the repository at this point in the history
Provide share functionality
  • Loading branch information
juankysoriano committed May 25, 2016
2 parents 92bd3c2 + cfe2c2a commit acfb21e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
5 changes: 5 additions & 0 deletions demo-extended/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
</intent-filter>
</activity>

<receiver
android:name="com.novoda.simplechromecustomtabs.demo.ShareBroadcastReceiver"
android:enabled="true"
android:exported="false" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ExtendedDemoActivity extends AppCompatActivity {

private static final Uri WEB_URL = Uri.parse("http://www.novoda.com");
private static final int REQUEST_CODE_VIEW_SOURCE = 1;
private static final int REQUEST_CODE_NOVODA_LONDON = 2;
private static final int REQUEST_CODE_SHARE_URL = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -72,24 +72,25 @@ public SimpleChromeCustomTabsIntentBuilder onCustomiseIntent(SimpleChromeCustomT
.showingTitle()
.withUrlBarHiding()
.withCloseButtonIcon(decodeCloseBitmap())
.withActionButton(decodeMapBitmap(), getString(R.string.novoda_london), navigateToNovodaLondon(), false)
.withDefaultShareMenuItem()
.withActionButton(decodeShareBitmap(), getString(R.string.share), shareUrl(), false)
.withMenuItem(getString(R.string.view_demo_source_code), viewSourceCode())
.withExitAnimations(getApplicationContext(), android.R.anim.slide_in_left, android.R.anim.fade_out)
.withStartAnimations(getApplicationContext(), android.R.anim.fade_in, android.R.anim.slide_out_right);
}
};

private Bitmap decodeMapBitmap() {
return BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_mapmode);
private Bitmap decodeShareBitmap() {
return BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_share);
}

private Bitmap decodeCloseBitmap() {
return BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back);
}

private PendingIntent navigateToNovodaLondon() {
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:51.5411671,-0.0947801"));
return PendingIntent.getActivity(ExtendedDemoActivity.this, REQUEST_CODE_NOVODA_LONDON, viewIntent, 0);
private PendingIntent shareUrl() {
Intent shareIntent = new Intent(ExtendedDemoActivity.this, ShareBroadcastReceiver.class);
return PendingIntent.getBroadcast(ExtendedDemoActivity.this, REQUEST_CODE_SHARE_URL, shareIntent, 0);
}

private PendingIntent viewSourceCode() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.novoda.simplechromecustomtabs.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ShareBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getDataString();
if (url == null) {
return;
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
Intent chooserIntent = Intent.createChooser(shareIntent, context.getString(R.string.share));
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooserIntent);
}
}
2 changes: 1 addition & 1 deletion demo-extended/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="application_found">The website will open on a Chrome Custom Tab! (custom theme).</string>
<string name="application_not_found">You don\'t have an appropriate version of Chrome.\nWebsite will be open on default browser.</string>
<string name="view_demo_source_code">View demo source code</string>
<string name="novoda_london">Novoda London</string>
<string name="share">Share</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.novoda.simplechromecustomtabs.navigation;

import android.support.customtabs.CustomTabsIntent;

class DefaultShareMenuItemComposer implements Composer {

@Override
public CustomTabsIntent.Builder compose(CustomTabsIntent.Builder builder) {
return builder.addDefaultShareMenuItem();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public SimpleChromeCustomTabsIntentBuilder withMenuItem(String label, PendingInt
return this;
}

public SimpleChromeCustomTabsIntentBuilder withDefaultShareMenuItem() {
composers.add(new DefaultShareMenuItemComposer());
return this;
}

public SimpleChromeCustomTabsIntentBuilder withActionButton(Bitmap icon, String description, PendingIntent pendingIntent, boolean shouldTint) {
composers.add(new ActionButtonComposer(icon, description, pendingIntent, shouldTint));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public void givenMenuItemIsSet_thenMenuItemComposerIsAdded() {
verify(mockComposers).add(any(MenuItemComposer.class));
}

@Test
public void givenDefaultShareMenuItemIsSet_thenDefaultShareMenuItemComposerIsAdded() {
simpleChromeCustomTabsIntentBuilder.withDefaultShareMenuItem();

verify(mockComposers).add(any(DefaultShareMenuItemComposer.class));
}

@Test
public void givenActionButtonIsSet_thenActionButtonComposerIsAdded() {
simpleChromeCustomTabsIntentBuilder.withActionButton(ANY_ICON, ANY_DESCRIPTION, ANY_PENDING_INTENT, false);
Expand Down

0 comments on commit acfb21e

Please sign in to comment.