Android Share to Social Media Using SocialAuth Library, Facebook, Twitter

SocialAuth-Android ApplicationAndroid Share to Social Media Using SocialAuth Library, Facebook, Twitter

Download library here

ShareButtonActivity.java :

/*
 ===========================================================================
 Copyright (c) 2012 Three Pillar Global Inc. http://threepillarglobal.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ===========================================================================
 */

package org.brickred.socialshare;

import java.io.File;
import java.util.List;

import org.brickred.socialauth.Photo;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.android.DialogListener;
import org.brickred.socialauth.android.SocialAuthAdapter;
import org.brickred.socialauth.android.SocialAuthAdapter.Provider;
import org.brickred.socialauth.android.SocialAuthError;
import org.brickred.socialauth.android.SocialAuthListener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * Main class of the ShareButton Example for SocialAuth Android SDK. <br>
 * 
 * The main objective of this example is to access social media providers
 * Facebook, Twitter and others by clicking a single button "Share".On Clicking
 * the button the api will open dialog of providers. User can access the
 * provider from dialog and can update the status.
 * 
 * The class first creates a button in main.xml. It then adds button to
 * SocialAuth Android Library <br>
 * 
 * Then it adds providers Facebook, Twitter and others to library object by
 * addProvider method and finally enables the providers by calling enable method<br>
 * 
 * After successful authentication of provider, it receives the response in
 * responseListener and then update status by updatestatus() method
 * 
 * <br>
 * 
 * @author vineet.aggarwal@3pillarglobal.com
 * 
 */

public class ShareButtonActivity extends Activity {

	// SocialAuth Component
	SocialAuthAdapter adapter;
	Profile profileMap;
	List<Photo> photosList;

	// Android Components
	Button update;
	EditText edit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		// Welcome Message
		TextView textview = (TextView) findViewById(R.id.text);
		textview.setText("Welcome to SocialAuth Demo. Connect any provider and then press Update button to Share Update.");

		// Create Your Own Share Button
		Button share = (Button) findViewById(R.id.sharebutton);
		share.setText("Share");
		share.setTextColor(Color.WHITE);
		share.setBackgroundResource(R.drawable.button_gradient);

		// Add it to Library
		adapter = new SocialAuthAdapter(new ResponseListener());

		// Add providers
		adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
		adapter.addProvider(Provider.TWITTER, R.drawable.twitter);
		adapter.addProvider(Provider.LINKEDIN, R.drawable.linkedin);
		adapter.addProvider(Provider.YAHOO, R.drawable.yahoo);
		adapter.addProvider(Provider.YAMMER, R.drawable.yammer);
		adapter.addProvider(Provider.EMAIL, R.drawable.email);
		adapter.addProvider(Provider.MMS, R.drawable.mms);

		// Providers require setting user call Back url
		adapter.addCallBack(Provider.TWITTER, "http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");
		adapter.addCallBack(Provider.YAMMER, "http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");

		// Enable Provider
		adapter.enable(share);

	}

	/**
	 * Listens Response from Library
	 * 
	 */

	private final class ResponseListener implements DialogListener {
		@Override
		public void onComplete(Bundle values) {

			Log.d("ShareButton", "Authentication Successful");

			// Get name of provider after authentication
			final String providerName = values.getString(SocialAuthAdapter.PROVIDER);
			Log.d("ShareButton", "Provider Name = " + providerName);
			Toast.makeText(ShareButtonActivity.this, providerName + " connected", Toast.LENGTH_LONG).show();

			update = (Button) findViewById(R.id.update);
			edit = (EditText) findViewById(R.id.editTxt);

			// Please avoid sending duplicate message. Social Media Providers
			// block duplicate messages.

			update.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					adapter.updateStatus(edit.getText().toString(), new MessageListener(), false);
				}
			});

			// Share via Email Intent
			if (providerName.equalsIgnoreCase("share_mail")) {
				// Use your own code here
				Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
						"vineet.aggarwal@3pillarglobal.com", null));
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
				File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
						"image5964402.png");
				Uri uri = Uri.fromFile(file);
				emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
				startActivity(Intent.createChooser(emailIntent, "Test"));
			}

			// Share via mms intent
			if (providerName.equalsIgnoreCase("share_mms")) {

				// Use your own code here
				File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
						"image5964402.png");
				Uri uri = Uri.fromFile(file);

				Intent mmsIntent = new Intent(Intent.ACTION_SEND, uri);
				mmsIntent.putExtra("sms_body", "Test");
				mmsIntent.putExtra(Intent.EXTRA_STREAM, uri);
				mmsIntent.setType("image/png");
				startActivity(mmsIntent);
			}

		}

		@Override
		public void onError(SocialAuthError error) {
			Log.d("ShareButton", "Authentication Error: " + error.getMessage());
		}

		@Override
		public void onCancel() {
			Log.d("ShareButton", "Authentication Cancelled");
		}

		@Override
		public void onBack() {
			Log.d("Share-Button", "Dialog Closed by pressing Back Key");
		}

	}

	// To get status of message after authentication
	private final class MessageListener implements SocialAuthListener<Integer> {
		@Override
		public void onExecute(String provider, Integer t) {
			Integer status = t;
			if (status.intValue() == 200 || status.intValue() == 201 || status.intValue() == 204)
				Toast.makeText(ShareButtonActivity.this, "Message posted on " + provider, Toast.LENGTH_LONG).show();
			else
				Toast.makeText(ShareButtonActivity.this, "Message not posted on " + provider, Toast.LENGTH_LONG).show();
		}

		@Override
		public void onError(SocialAuthError e) {

		}
	}

}

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<!--
===========================================================================
 Copyright (c) 2012 Three Pillar Global Inc. http://threepillarglobal.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ===========================================================================
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    >
    <LinearLayout 
    android:orientation="horizontal"
    android:id="@+id/topbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="7dp"
    android:background="@drawable/blue_gradient"
    >   
	    <TextView 
			android:id="@+id/textview"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:layout_gravity="center_vertical"
			android:textColor="#ffffff"
			android:textSize="14sp"
			android:text="Share Button"
			android:textStyle="bold"
		/>
	</LinearLayout>
	<View
        android:id="@+id/view01"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/topbar"
        android:background="#313437" />      
     <TextView 
		android:id="@+id/text"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:textSize="14sp"
		android:textStyle="bold"
		android:layout_marginLeft="10dp"
		android:layout_marginRight="10dp"
		android:layout_marginTop="20dp"
		android:textColor="#ffffff"
		android:layout_below="@+id/view01"
	/>
    <TextView 
		android:id="@+id/dialogTitle"
		android:layout_height="wrap_content"
		android:layout_width="fill_parent"
		android:textColor="#ffffff"
		android:textSize="14sp"
		android:text="Share Update"
		android:layout_marginTop="30dp"
		android:layout_marginLeft="10dp"
		android:layout_marginRight="10dp"
		android:textStyle="bold"
		android:layout_below="@+id/text"	
	/>  
	<EditText 
	    android:id="@+id/editTxt"
		android:layout_height="wrap_content"
		android:layout_width="fill_parent"
		android:textColor="#000000"
		android:textSize="12sp"
		android:layout_margin="5dp"
		android:hint="Enter Message"
		android:layout_marginLeft="10dp"
		android:layout_marginRight="10dp"
		android:layout_below="@+id/dialogTitle"
	/>
	<Button 
		android:id="@+id/update"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:background="#147af8"
		android:textColor="#ffffff"
		android:padding="5dp"
		android:textSize="12sp"
		android:text="Update"
		android:layout_alignRight="@+id/editTxt"
		android:layout_marginRight="10dp"
		android:layout_below="@+id/editTxt"
	/>
    
	<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_gradient"
    android:layout_alignParentBottom="true"
    >    
    <Button 
		android:id="@+id/sharebutton"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:layout_centerInParent="true"
		android:layout_margin="20dp"
	/>     
</RelativeLayout>
</RelativeLayout>

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
===========================================================================
 Copyright (c) 2012 Three Pillar Global Inc. http://threepillarglobal.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ===========================================================================
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.brickred.socialshare"
      android:versionCode="7"
      android:versionName="3.2">

    <uses-sdk android:minSdkVersion="8"/>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    
    <application android:icon="@drawable/ic_launcher" 
        		 android:theme="@android:style/Theme.NoTitleBar" 
        		 android:label="@string/app_name">
        <activity android:name=".ShareButtonActivity"
                  android:configChanges="keyboardHidden|orientation"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

assets/oauth_consumer.properties :

#google
www.google.com.consumer_key = socialauth.in
www.google.com.consumer_secret = Mlt6oDI0iIoOiylxxxxx

#yahoo 
api.login.yahoo.com.consumer_key = dj0yJmk9Nlpqd0pIMENnb1N4JmQ9WVdrOWJqbEhURXA0TTJVbWNHbzlNVEk0TXpNeE5EYzJNZy0tJnM9Y29uc3VtZXJzZWNyxxxxx--
api.login.yahoo.com.consumer_secret = c04675a9274fb556f9e817b1ad23f0c91xxxxx

#twitter
twitter.com.consumer_key = g8jpcV9GThl4BBcxxxxx
twitter.com.consumer_secret = 3j5SVAayCMxRbUHk40hB5Vhk0ysOXRcYbpxxxxx

#facebook
graph.facebook.com.consumer_key = 101913803xxxxx
graph.facebook.com.consumer_secret = 8e59608d0e5925d6e3ca4c27xxxxx

#LinkedIn
api.linkedin.com.consumer_key = bh82t5xxxxx
api.linkedin.com.consumer_secret = zQ1LLrGbhDxxxxx

#FourSquare
foursquare.com.consumer_key = VJT0YKBEQAQOMPWZPTGF1V5WOHZQMWNNJEBBQBSDNxxxxx
foursquare.com.consumer_secret = HCVBO1W5MNWIPAOJABI3LILMLTHTPR52RU3P0GJxxxxx

#MySpace
api.myspace.com.consumer_key = 67cc035edf0f4387a2488a0fxxxxx
api.myspace.com.consumer_secret = 80b58c4873ac4d2e8edb6257d85c79e71692c39d5f0f4b3a90acc1dfxxxxx

#Runkeeper
runkeeper.com.consumer_key=b1320c16082d48e2a7a94f5bxxxxx
runkeeper.com.consumer_secret=7cbf7688519d4e8cb5343a4cxxxxx

#Yammer
www.yammer.com.consumer_key=wLR96V3qBPHPcxxxxx
www.yammer.com.consumer_secret=4hhUOP6tYiHNOUz6dMhWBVHgcsFq3xxxxx

#Salesforce
login.salesforce.com.consumer_key = 3MVG9Y6d_Btp4xp4yFMR0tPSndvoapjV0sIbnVSNLg_l1vkeDgLelQhr6iYwlTgCL98VSBjU_XvQxxxxx
login.salesforce.com.consumer_secret = 496220226393xxxxx

#Google Plus
googleapis.com.consumer_key=505071246058-6i3hf3rhd1s0qst492e23gxxxxx.apps.googleusercontent.com
googleapis.com.consumer_secret=xrNQTx9ELsxmyUxxxxx

#Flickr
www.flickr.com.consumer_key = c519f64dbddc50f3e25e8cxxxxx
www.flickr.com.consumer_secret = bf1a181c4xxxxx

#Instagram
api.instagram.com.consumer_key = c28ba7d08da64462a0db848cxxxxx
api.instagram.com.consumer_secret = c5063d4e5f1248a19d2fa3exxxxx

Result :

SocialAuth-Android Application

 

Android Share to Facebook

Android Share to Twitter

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>