Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[LIBRARY][GUIDE] DynamicShareActionProvider

OP nikwen

14th December 2013, 09:15 PM   |  #1  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
[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.
Last edited by nikwen; 20th December 2013 at 04:14 PM.
The Following 9 Users Say Thank You to nikwen For This Useful Post: [ View ]
14th December 2013, 09:15 PM   |  #2  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
[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.
Last edited by nikwen; 14th December 2013 at 10:09 PM.
The Following 2 Users Say Thank You to nikwen For This Useful Post: [ View ]
14th December 2013, 09:15 PM   |  #3  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
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.
Last edited by nikwen; 15th December 2013 at 09:07 PM.
The Following User Says Thank You to nikwen For This Useful Post: [ View ]
15th December 2013, 08:33 PM   |  #4  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
346 posts
Join Date:Joined: May 2013
More
Thumbs up
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: [ View ]
15th December 2013, 08:38 PM   |  #5  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
Quote:
Originally Posted by SimplicityApks

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: [ View ]
17th December 2013, 07:31 PM   |  #6  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
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.
Last edited by nikwen; 18th December 2013 at 09:01 PM.
The Following User Says Thank You to nikwen For This Useful Post: [ View ]
20th December 2013, 02:49 PM   |  #7  
Masrepus's Avatar
Senior Member
Thanks Meter: 79
 
702 posts
Join Date:Joined: Feb 2013
More
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: [ View ]
20th December 2013, 03:03 PM   |  #8  
lesiki's Avatar
Senior Member
Flag Frankfurt
Thanks Meter: 148
 
340 posts
Join Date:Joined: Oct 2011
More
Very nice for us devs! Thanks, I will look where to implement this
The Following User Says Thank You to lesiki For This Useful Post: [ View ]
20th December 2013, 03:20 PM   |  #9  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
Quote:
Originally Posted by Masrepus

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

Very nice for us devs! Thanks, I will look where to implement this

Big thanks to both of you.
14th February 2014, 10:07 PM   |  #10  
nikwen's Avatar
OP Recognized Contributor
Thanks Meter: 1,407
 
2,805 posts
Join Date:Joined: Feb 2013
More
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: [ View ]
Post Reply Subscribe to Thread

Tags
dynamic, dynamicshareactionprovider, library, share, shareactionprovider
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes