1+ package com .dvinfosys .widgets .SocialShare ;
2+
3+ import android .content .Context ;
4+ import android .content .Intent ;
5+ import android .content .pm .ResolveInfo ;
6+ import android .net .Uri ;
7+ import android .util .Log ;
8+
9+ import java .util .List ;
10+
11+ public class Shareable {
12+
13+ private final Context context ;
14+ private final int socialChannel ; //cup
15+ private final String url ; //cup
16+ private final Uri image ;
17+ private final String message ;
18+
19+ private Shareable (Builder builder ) {
20+ this .context = builder .c ;
21+ this .socialChannel = builder .socialChannel ;
22+ this .message = builder .message ;
23+ this .url = builder .url ;
24+ this .image = builder .image ;
25+ }
26+
27+ public Context getContext () {
28+ return context ;
29+ }
30+
31+ public void share () {
32+ Intent socialIntent = new Intent ();
33+ socialIntent .setAction (Intent .ACTION_SEND );
34+
35+ if (this .socialChannel != Builder .ANY ) {
36+ socialIntent .setType ("text/*" );
37+ String packageName = resolve (socialIntent , this .socialChannel );
38+ if (packageName != null ) {
39+ socialIntent .setPackage (packageName );
40+ }
41+ }
42+
43+ if (this .image != null ) {
44+ socialIntent .putExtra (Intent .EXTRA_STREAM , this .image );
45+ socialIntent .addFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
46+ socialIntent .putExtra (Intent .EXTRA_TEXT , this .message + "\n " + this .url );
47+ socialIntent .setType ("*/*" );
48+ } else {
49+ //Plain text post
50+ socialIntent .setType ("text/plain" );
51+ socialIntent .putExtra (Intent .EXTRA_TEXT , this .message + "\n " + this .url );
52+ }
53+
54+ this .getContext ().startActivity (Intent .createChooser (socialIntent , "Choose an application" ));
55+ }
56+
57+ private String resolve (Intent share , int channelType ) {
58+ String partialAppName = parseType (channelType );
59+ try {
60+ List <ResolveInfo > resInfo = getContext ().getPackageManager ().queryIntentActivities (share , 0 );
61+ if (!resInfo .isEmpty ()) {
62+ for (ResolveInfo info : resInfo ) {
63+ if (info .activityInfo .packageName .toLowerCase ().contains (partialAppName ) ||
64+ info .activityInfo .name .toLowerCase ().contains (partialAppName )) {
65+ return info .activityInfo .packageName ;
66+ }
67+ }
68+ }
69+ } catch (Exception e ) {
70+ Log .e ("Shareable" , "Failed to resolve packages for sharing.. defaulting to any. " + e .getMessage ());
71+ return null ;
72+ }
73+ return null ;
74+
75+ }
76+
77+ private String parseType (int type ) {
78+ switch (type ) {
79+ case Builder .FACEBOOK :
80+ return "faceb" ;
81+ case Builder .TWITTER :
82+ return "twi" ;
83+ case Builder .GOOGLE_PLUS :
84+ return "plus" ;
85+ case Builder .TUMBLR :
86+ return "tumblr" ;
87+ case Builder .LINKED_IN :
88+ return "linkedin" ;
89+ case Builder .EMAIL :
90+ return "mail" ;
91+ case Builder .MESSAGES :
92+ return "messag" ;
93+ case Builder .REDDIT :
94+ return "reddit" ;
95+ case Builder .WHATSAPP :
96+ return "whatsapp" ;
97+ default :
98+ return null ;
99+ }
100+ }
101+
102+ public static class Builder {
103+
104+ public static final int FACEBOOK = 1 ;
105+ public static final int TWITTER = 2 ;
106+ public static final int TUMBLR = 3 ;
107+ public static final int LINKED_IN = 4 ;
108+ public static final int GOOGLE_PLUS = 5 ;
109+ public static final int REDDIT = 6 ;
110+ public static final int MESSAGES = 7 ;
111+ public static final int EMAIL = 8 ;
112+ public static final int WHATSAPP = 9 ;
113+ public static final int ANY = 0 ;
114+
115+ private Context c = null ;
116+ private int socialChannel = ANY ;
117+ private String url = null ;
118+ private Uri image = null ; // If uses image, then it overrides text post
119+ private String message = null ; //spoon
120+
121+ public Builder (Context c ) {
122+ this .c = c ;
123+ }
124+
125+ public Builder socialChannel (int channel ) {
126+ this .socialChannel = channel ;
127+ return this ;
128+ }
129+
130+ public Builder url (String url ) {
131+ this .url = url ;
132+ return this ;
133+ }
134+
135+ public Builder image (Uri img ) {
136+ this .image = img ;
137+ return this ;
138+ }
139+
140+ public Builder message (String message ) {
141+ this .message = message ;
142+ return this ;
143+ }
144+
145+ public Shareable build () {
146+ return new Shareable (this );
147+ }
148+ }
149+ }
0 commit comments