Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,731,426 Members 53,432 Now Online
XDA Developers Android and Mobile Development Forum

[LIBRARY][GUIDE] DynamicShareActionProvider

Tip us?
 
nikwen
Old
(Last edited by nikwen; 20th December 2013 at 03:14 PM.)
#1  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
Info 2 [LIBRARY][GUIDE] DynamicShareActionProvider

[LIBRARY][GUIDE] DynamicShareActionProvider

Some time ago I had a conversation with @SimplicityApks about Google's ShareActionProvider. We found out that it's not flexible enough as it doesn't allow dynamic generation of the data which should be shared.

So here I release my open-source DynamicShareActionProvider library. Its code can be found on Github.

The differences:
  • Sharing is done dynamically now!
    This means that your app specifies the type of the shared data first and generates the data when an app is chosen for sharing. No more need to define what to share when the Activity is created. The content can now be set dynamically.
    There are two types of listeners so that you can also generate the data in an AsyncTask.

  • There is no app icon of the most often used app appearing next to the share icon.
    Other icons often do not match the app theme.

  • The shown app list is not limited to three apps and a "See all" entry first.
    Why should the user not see all apps for that action?

  • You can adjust/must set the icon manually.
    Don't worry.

Screenshots:



(The second screenshot is from the app FunctionCapture by @SimplicityApks)


Big thanks to:
This was featured on the XDA portal on December 20, 2013.
The Following 9 Users Say Thank You to nikwen For This Useful Post: [ Click to Expand ]
 
nikwen
Old
(Last edited by nikwen; 14th December 2013 at 09:09 PM.)
#2  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
[GUIDE] Adding this to your app

As indicated in the first post, the implementation is a little bit different than with Google's ShareActionProvider.
I assume that you are using Google's ActionBarCompat library.

Add the ActionBarCompat and the DynamicShareActionProvider library to your project first.

Afterwards, there are two ways of sharing your data using the DynamicShareActionProvider:
  • You can pass the data to the library when an app is selected and it will do the sharing part.
  • Or the library gives you an Intent which you just need to add the data to. This allows using an AsyncTask for data generation.

Adding the provider to the menu file

The first steps are the same for both ways. Add this to your menu resource file:

Code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:library="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_item_share"
          library:showAsAction="always"
          android:title="Share"
          android:icon="@drawable/abc_ic_menu_share_holo_dark"
          library:actionProviderClass="de.nikwen.dynamicshareactionprovider.library.DynamicShareActionProvider" />

    <!-- Your other entries here -->

</menu>
I saved the menu file as "res/menu/main.xml".

What's different from Google's Provider here, is that you need to set the icon for the menu entry. That shouldn't be a problem though because we can use the one from the ActionBarCompat library.

Then inflate the menu resource in the onCreateOptionsMenu() method of your Activity and get the provider from the menu:

Code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    DynamicShareActionProvider provider = (DynamicShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.menu_item_share));

    return true;
}
Set the data type

To be able to filter your installed apps for those that can handle the data which should be shared, the provider needs to know which type of data you want to share. Set it like this:

Code:
provider.setShareDataType("text/plain");
(Add this to your onCreateOptionsMenu() method.)

Two different types of listeners

Now you need to choose the way you want to do the sharing:

Quote:
  • If the generation of the data which should be shared takes much time so that you want to use an AsyncTask or a Service for that, use the DynamicShareActionProvider.OnShareLaterListener.
  • Otherwise use the DynamicShareActionProvider.OnShareIntentUpdateList ener.

Using the OnShareIntentUpdateListener - Let the library do the sharing for you

Set the listener using the setOnShareIntentUpdateListener() method:

Code:
provider.setOnShareIntentUpdateListener(new DynamicShareActionProvider.OnShareIntentUpdateListener() {

    @Override
    public Bundle onShareIntentExtrasUpdate() {
        Bundle extras = new Bundle();
        
        //Generate your data here and add it to the Bundle
        
        return extras;
    }

});
(Add this to your onCreateOptionsMenu() method as well.)

We override the onShareIntentExtrasUpdate() method here and pass a Bundle as the return value. The data you add to the bundle should have the same format that you would use, if you added it to the Intent directly using the Intent#putExtras(Bundle extras) method (that's what is used internally).

This is an example for passing the data from an EditText:

Code:
@Override
public Bundle onShareIntentExtrasUpdate() {
    Bundle extras = new Bundle();
    EditText shareEdit = (EditText) findViewById(R.id.share_edit);
    extras.putString(android.content.Intent.EXTRA_TEXT, shareEdit.getText().toString());
    return extras;
}
You're done.

Using the OnShareLaterListener - Do the sharing yourself

This time we override the onShareClick() method of the OnShareLaterListener:

Code:
provider.setOnShareLaterListener(new DynamicShareActionProvider.OnShareLaterListener() {

    @Override
    public void onShareClick(Intent shareIntent) {
        MyShareAsyncTask task = new MyShareAsyncTask();
        task.execute(shareIntent);
    }

});
We get the share Intent as an argument and don't have to pass a return value. Instead, we have to do the sharing part manually.

The example code uses an AsyncTask for that:

Code:
private class MyShareAsyncTask extends AsyncTask<Intent, Void, Intent> {

    @Override
    protected void onPreExecute() {
        Toast.makeText(MainActivity.this, R.string.asynctask, Toast.LENGTH_LONG).show();
    }

    @Override
    protected Intent doInBackground(Intent... intents) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        EditText shareEdit = (EditText) findViewById(R.id.share_edit);
        intents[0].putExtra(android.content.Intent.EXTRA_TEXT, "Shared from an AsyncTask: " + shareEdit.getText().toString());

        return intents[0];
    }

    @Override
    protected void onPostExecute(Intent intent) {
        startActivity(intent);
    }

}
However, you're free to do it the way you like.

You're done.
The Following 2 Users Say Thank You to nikwen For This Useful Post: [ Click to Expand ]
 
nikwen
Old
(Last edited by nikwen; 15th December 2013 at 08:07 PM.)
#3  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
Get the sample code

It's on Github: Link to the sample code

Other stuff

See (and maybe share) my Google Plus post on the library.

Let me know if you add this to your app. I will really appreciate it.
The Following User Says Thank You to nikwen For This Useful Post: [ Click to Expand ]
 
SimplicityApks
Old
#4  
SimplicityApks's Avatar
Senior Member
Thanks Meter 307
Posts: 287
Join Date: May 2013
Location: Aachen
Well, it's time to say thank you again^^, without your lib sharing in my app wouldn't be nearly as useful... Thanks soo much!
The Following User Says Thank You to SimplicityApks For This Useful Post: [ Click to Expand ]
 
nikwen
Old
#5  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
Quote:
Originally Posted by SimplicityApks View Post
Well, it's time to say thank you again^^, without your lib sharing in my app wouldn't be nearly as useful... Thanks soo much!
Thank you very much for the compliments.
And welcome.
The Following User Says Thank You to nikwen For This Useful Post: [ Click to Expand ]
 
nikwen
Old
(Last edited by nikwen; 18th December 2013 at 08:01 PM.)
#6  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
It's among the top trending Java repos on Github today. Currently, it's at position #14.

Screenshot:
 


(I know that the screenshot still shows #16. Old one. )


EDIT: The best I saw was position #11.
The Following User Says Thank You to nikwen For This Useful Post: [ Click to Expand ]
 
Masrepus
Old
#7  
Masrepus's Avatar
Senior Member
Thanks Meter 67
Posts: 632
Join Date: Feb 2013
Hey congrats for the portal post about ur lib


---------------------------------
Phone : Samsung Galaxy Mini S5570
OS:
•AOSP ICS Touchwiz V5 by A_U
•Android 4.0.4
•Baseband XWKS2
•Kernel: 2.6.37.6 Badass v1.9 by alin.p
•Recovery CWM v4.0.0.5
Mods:
PureAudio, Mounts2SD, ODEX through Universal ODEX script, AdBlock by AdAway
---------------------------------

Gesendet von Tapatalk
The Following User Says Thank You to Masrepus For This Useful Post: [ Click to Expand ]
 
lesiki
Old
#8  
lesiki's Avatar
Senior Member
Thanks Meter 147
Posts: 330
Join Date: Oct 2011
Location: Frankfurt
Very nice for us devs! Thanks, I will look where to implement this
.
Thanks and sorry for my english
__________________



 
People 1950:
People 2012:

My xda-Dropbox
The Following User Says Thank You to lesiki For This Useful Post: [ Click to Expand ]
 
nikwen
Old
#9  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
Quote:
Originally Posted by Masrepus View Post
Hey congrats for the portal post about ur lib


---------------------------------
Phone : Samsung Galaxy Mini S5570
OS:
•AOSP ICS Touchwiz V5 by A_U
•Android 4.0.4
•Baseband XWKS2
•Kernel: 2.6.37.6 Badass v1.9 by alin.p
•Recovery CWM v4.0.0.5
Mods:
PureAudio, Mounts2SD, ODEX through Universal ODEX script, AdBlock by AdAway
---------------------------------

Gesendet von Tapatalk
Quote:
Originally Posted by lesiki View Post
Very nice for us devs! Thanks, I will look where to implement this
Big thanks to both of you.
 
nikwen
Old
#10  
nikwen's Avatar
Recognized Contributor - OP
Thanks Meter 1295
Posts: 2,690
Join Date: Feb 2013
Just pushed an update to the library which adds support for ActionBarSherlock and the native ActionBar. I haven't changed the readme-file yet as I'm currently looking for someone to test those versions in his app. Would be great if someone (maybe you?) did it.

If you're interested in the update, have a look at the issue on Github: Issue #2

The Following User Says Thank You to nikwen For This Useful Post: [ Click to Expand ]
Tags
dynamic, dynamicshareactionprovider, library, share, shareactionprovider
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes