5

I would like to share a simple text via Facebook and other social media (Google+, Twitter, ...)

My initial code looked like:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Test Subject");
shareIntent.putExtra(Intent.EXTRA_TITLE, "Test Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Only link visible in FB: http://www.google.com/");
startActivity(Intent.createChooser(shareIntent, "Test Intent"));

If I am not mistaken, Facebook allows me to share only a link (or some kind of an extract from a website) specified via EXTRA_TEXT.

While sharing a link is nice I want to share a custom text with it as well, hence I linked my project to facebook SDK and managed to create more custom sharing experience. However, since sharing to Facebook it not done via Intent anymore, I lost my Intent.createChooser which shows users the well known list of sharing options.

My question is can I have the default Intent.createChooser but use custom sharing on Facebook icon selection? If not then what are the other options?
Thanks!

Blo
  • 11,903
  • 5
  • 45
  • 99
Jana
  • 5,516
  • 5
  • 23
  • 29
  • I found a [post](http://stackoverflow.com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-package-name) which allowed me to create custom Intent per social media. However, still do not know how to use something else than intent. – Jana Mar 24 '14 at 04:02

1 Answers1

0

I found a solution.

As I already mentioned in my original question I encapsulated my Facebook custom sharing (by using Facebook SDK) into an Activity class ShareFacebook by following instructions on FB.

Then I used this ShareFacebook Activity as Intent and replaced the original Intent (which is normally created by Intent.createChooser) like this:

String myPackageName = getPackageName();
String[] blacklist = new String[]{"com.pinterest", "com.tunewiki.lyricplayer.android", "com.dropbox.android", "com.google.zxing.client.android", "com.adobe.reader", "com.google.android.apps.docs", "flipboard.app"};
Set<Integer> usedSharingApps = new HashSet<Integer>();
int facebookHash1 = "com.facebook.katana".hashCode();
int facebookHash2 = "com.facebook.wakizashi".hashCode();

// share result
// By default intent only link is possible to share on facebook                         

    List<Intent> targetedShareIntents = new ArrayList<Intent>();

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;

        Intent targetShareIntent;
        int appId = packageName.hashCode();
        if (packageName.equals(myPackageName) 
            || Arrays.asList(blacklist).contains(packageName) /* skip some other packages */
            || usedSharingApps.contains(appId)) {
            continue;
        } else if (packageName.equals("com.facebook.katana") || packageName.equals("com.facebook.wakizashi")) {
            usedSharingApps.add(appId);
            Intent targetShareIntentTmp = new Intent(getApplicationContext(), ShareFacebook.class);
            targetShareIntentTmp.addCategory("android.intent.category.ALTERNATIVE");

            targetShareIntentTmp.setAction(Intent.ACTION_SEND);
            targetShareIntentTmp.setType("text/plain");
            targetShareIntentTmp.setPackage(myPackageName);
            targetShareIntentTmp.setComponent(new ComponentName(getPackageName(), ShareFacebook.class.getName()));
            targetShareIntentTmp.putExtra(Intent.EXTRA_SUBJECT, "FacebookDialog Description");
            targetShareIntentTmp.putExtra(Intent.EXTRA_TITLE, "FacebookDialog Name");
            targetShareIntentTmp.putExtra(Intent.EXTRA_TEXT, "http://stackoverflow.com");

            targetShareIntent = new LabeledIntent(targetShareIntentTmp, myPackageName, R.string.facebook, R.drawable.fb_logo_blue);

        } else {
            usedSharingApps.add(appId);
            targetShareIntent = new Intent();   

            targetShareIntent.setAction(Intent.ACTION_SEND);
            targetShareIntent.setType("text/plain");
            targetShareIntent.setPackage(packageName);

            targetShareIntent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT visible by mail or sms app");
            targetShareIntent.putExtra(Intent.EXTRA_TITLE, "TITLE - visible anywhere?");
            targetShareIntent.putExtra(Intent.EXTRA_TEXT, "All this text visible in every intent http://www.google.com/");
        }
        targetedShareIntents.add(targetShareIntent);
    }
}               

if (!usedSharingApps.contains(facebookHash1) && !usedSharingApps.contains(facebookHash2)) {
    // user does not have facebook app but we still want to share since it took us such effort to use facebook sharing!

    Intent targetShareIntentTmp = new Intent(getApplicationContext(), ShareFacebook.class);
    targetShareIntentTmp.addCategory("android.intent.category.ALTERNATIVE");

    targetShareIntentTmp.setAction(Intent.ACTION_SEND);
    targetShareIntentTmp.setType("text/plain");
    targetShareIntentTmp.setPackage(myPackageName);
    targetShareIntentTmp.setComponent(new ComponentName(getPackageName(), ShareFacebook.class.getName()));
    targetShareIntentTmp.putExtra(Intent.EXTRA_SUBJECT, "FacebookDialog Description");
    targetShareIntentTmp.putExtra(Intent.EXTRA_TITLE, "FacebookDialog Name");
    targetShareIntentTmp.putExtra(Intent.EXTRA_TEXT, "http://stackoverflow.com");

    Intent targetShareIntent = new LabeledIntent(targetShareIntentTmp, myPackageName, R.string.facebook, R.drawable.fb_logo_blue);
    targetedShareIntents.add(targetShareIntent);
}

Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);           

Into the manifest file I added:

    <activity
        android:name="your.package.ShareFacebook" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />                
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>

Modifying the manifest was quite important, otherwise I was getting this error: "No Activity found for Intent"

I have also downloaded Facebook logo and copied it into my project resources under name "fb_logo_blue.png" (filename must contain only a-z0-9_).

For these two extra lines (FacebookDialog Description + FacebookDialog Name) I had to link to FB SDK and kill so many hours! Well, at least now I can share on phones without Facebook app installed.

My result on Facebook:

enter image description here

Jana
  • 5,516
  • 5
  • 23
  • 29
  • if desired, the website address can be "covered" by using .setCaption("Cover") in the FeedDialog – Jana Mar 26 '14 at 02:25