Skip to content

Commit 006a24f

Browse files
committed
Merge pull request phonegap#867 from ObjetDirect/master
[Android] Twitter plugin for Android
2 parents 1f82bfb + 39009fa commit 006a24f

File tree

3 files changed

+308
-0
lines changed

3 files changed

+308
-0
lines changed

Android/Twitter/README

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Twitter plugin for Phonegap #
2+
3+
This is inspired from the excellent iOS' plugin: https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/Twitter
4+
5+
For the moment, only two (major) functions are binding:
6+
1. isTwitterAvailable
7+
2. composeTweet
8+
9+
## Adding the Plugin to your project ##
10+
11+
Using this plugin requires [Android PhoneGap](http://github.com/phonegap/phonegap-android).
12+
13+
1. To install the plugin, move www/twitter.js to your project's www folder and include a reference to it in your html file after phonegap.js.
14+
15+
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
16+
<script type="text/javascript" charset="utf-8" src="twitter.js"></script>
17+
18+
2. Create a directory within your project called "src/com/phonegap/plugins/twitter" and move Twitter.java into it.
19+
20+
3. In your res/xml/plugins.xml file add the following line:
21+
22+
&lt;plugin name="Twitter" value="com.phonegap.plugins.twitter.Twitter"/&gt;
23+
24+
## Using the plugin ##
25+
26+
The plugin creates the object `window.plugins.twitter`. To use, call one of the following, available methods:
27+
28+
<pre>
29+
/**
30+
* Check if Twitter is installed
31+
*
32+
*/
33+
isTwitterAvailable(success, failure)
34+
</pre>
35+
36+
Sample use:
37+
38+
window.plugins.twitter.isTwitterAvailable(
39+
function(bool){
40+
if(bool){
41+
// Do something
42+
43+
} else {
44+
alert("Twitter is not available");
45+
}
46+
},
47+
function(){
48+
alert("We have a problem with the plugin");
49+
}
50+
);
51+
52+
<pre>
53+
/**
54+
* Compose tweet and open the Twitter application
55+
*/
56+
composeTweet(success, failure, message) {
57+
</pre>
58+
59+
Sample use:
60+
61+
window.plugins.twitter.composeTweet(
62+
function(){
63+
},
64+
function(){
65+
alert("We have a problem with the plugin");
66+
},
67+
"Base of the tweet"
68+
);
69+
70+
71+
## LICENSE ##
72+
73+
PhoneGap is available under *either* the terms of the modified BSD license *or* the
74+
MIT License (2008). As a recipient of PhonegGap, you may choose which
75+
license to receive this code under (except as noted in per-module LICENSE
76+
files). Some modules may not be the copyright of Nitobi. These
77+
modules contain explicit declarations of copyright in both the LICENSE files in
78+
the directories in which they reside and in the code itself. No external
79+
contributions are allowed under licenses which are fundamentally incompatible
80+
with the MIT or BSD licenses that PhoneGap is distributed under.
81+
82+
The text of the MIT and BSD licenses is reproduced below.
83+
84+
---
85+
86+
### The "New" BSD License
87+
88+
Copyright (c) 2005-2011, Nitobi Software Inc.
89+
All rights reserved.
90+
91+
Redistribution and use in source and binary forms, with or without
92+
modification, are permitted provided that the following conditions are met:
93+
94+
* Redistributions of source code must retain the above copyright notice, this
95+
list of conditions and the following disclaimer.
96+
* Redistributions in binary form must reproduce the above copyright notice,
97+
this list of conditions and the following disclaimer in the documentation
98+
and/or other materials provided with the distribution.
99+
* Neither the name of Phonegap/Nitobi nor the names of its contributors
100+
may be used to endorse or promote products derived from this software
101+
without specific prior written permission.
102+
103+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
104+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
105+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
106+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
107+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
108+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
109+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
110+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
111+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
112+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113+
114+
---
115+
116+
### The MIT License
117+
118+
Copyright (c) <2012> <Objet Direct>
119+
120+
Permission is hereby granted, free of charge, to any person obtaining a copy
121+
of this software and associated documentation files (the "Software"), to deal
122+
in the Software without restriction, including without limitation the rights
123+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
124+
copies of the Software, and to permit persons to whom the Software is
125+
furnished to do so, subject to the following conditions:
126+
127+
The above copyright notice and this permission notice shall be included in
128+
all copies or substantial portions of the Software.
129+
130+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
131+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
132+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
133+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
134+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
135+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
136+
THE SOFTWARE.
137+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.phonegap.plugins.twitter;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
import org.json.JSONArray;
8+
import org.json.JSONException;
9+
10+
import android.content.ComponentName;
11+
import android.content.Intent;
12+
import android.content.pm.ActivityInfo;
13+
import android.content.pm.PackageManager;
14+
import android.content.pm.ResolveInfo;
15+
16+
import com.phonegap.api.Plugin;
17+
import com.phonegap.api.PluginResult;
18+
19+
/**
20+
* Twitter plugin for Android
21+
* Inspired of the iOS plugin: https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/Twitter
22+
*
23+
* @see http://regis.decamps.info/blog/2011/06/intent-to-open-twitter-client-on-android/
24+
* @see http://blogrescue.com/2011/12/android-development-send-tweet-action/
25+
*
26+
* @author Julien Roche
27+
* @version 1.0
28+
*/
29+
public class Twitter extends Plugin {
30+
// Constants
31+
/** ComposeTweet method's name */
32+
private static final String METHOD_COMPOSE_TWEET = "composeTweet";
33+
34+
/** IsTwitterAvailable method's name */
35+
private static final String METHOD_IS_TWITTER_AVAILABLE = "isTwitterAvailable";
36+
37+
/** List of Twitter's applications with theirs linked send Activity */
38+
private static Map<String, String> TWITTER_APPS;
39+
40+
/** List of available methods */
41+
private static final String[] AVAILABLE_METHODS = new String[]{ METHOD_COMPOSE_TWEET, METHOD_IS_TWITTER_AVAILABLE };
42+
43+
static {
44+
TWITTER_APPS = new LinkedHashMap<String, String>();
45+
TWITTER_APPS.put("Twitter", "com.twitter.android.PostActivity");
46+
TWITTER_APPS.put("TweetCaster", "com.handmark.tweetcaster.NewTwitActivity");
47+
TWITTER_APPS.put("TweetCaster2", "com.handmark.tweetcaster.ShareSelectorActivity");
48+
TWITTER_APPS.put("UberSocial", "com.twidroid.activity.SendTweet");
49+
TWITTER_APPS.put("TweetDeck", "com.tweetdeck.compose.ComposeActivity");
50+
TWITTER_APPS.put("Seesmic", "com.seesmic.ui.Composer");
51+
TWITTER_APPS.put("Plume", "com.levelup.touiteur.appwidgets.TouiteurWidgetNewTweet");
52+
TWITTER_APPS.put("Twicca", "jp.r246.twicca.statuses.Send");
53+
}
54+
55+
/**
56+
* @param args
57+
* @param callbackId
58+
* @return A PluginResult object with a status and message.
59+
*/
60+
public PluginResult composeTweet(JSONArray args, String callbackId) {
61+
ResolveInfo resolveInfo = getTwitterResolveInfo();
62+
String message;
63+
64+
if(resolveInfo == null){
65+
return new PluginResult(PluginResult.Status.ERROR, "Twitter is not available");
66+
}
67+
68+
if(args.length() <= 0){
69+
return new PluginResult(PluginResult.Status.ERROR, "No parameter was specified");
70+
}
71+
72+
73+
try {
74+
message = args.getString(0);
75+
76+
} catch (JSONException e) {
77+
return new PluginResult(PluginResult.Status.ERROR, "Error with the message");
78+
}
79+
80+
final ActivityInfo activity = resolveInfo.activityInfo;
81+
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
82+
String p = resolveInfo.activityInfo.packageName;
83+
84+
Intent intent = new Intent(Intent.ACTION_SEND);
85+
intent.putExtra(Intent.EXTRA_TEXT, message);
86+
intent.setType(p != null && p.startsWith("com.twidroid") ? "application/twitter" : "text/plain");
87+
intent.addCategory(Intent.CATEGORY_LAUNCHER);
88+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
89+
intent.setComponent(name);
90+
this.ctx.startActivity(intent);
91+
92+
return new PluginResult(PluginResult.Status.OK);
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
* @see com.phonegap.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
98+
*/
99+
@Override
100+
public PluginResult execute(String action, JSONArray args, String callbackId) {
101+
if(action != null && Arrays.binarySearch(AVAILABLE_METHODS, action) >= 0){
102+
if(METHOD_IS_TWITTER_AVAILABLE.equals(action)){
103+
return new PluginResult(PluginResult.Status.OK, isTwitterAvailable());
104+
105+
}
106+
107+
if(METHOD_COMPOSE_TWEET.equals(action)) {
108+
return composeTweet(args, callbackId);
109+
}
110+
}
111+
112+
return new PluginResult(PluginResult.Status.ERROR, "This method is not available");
113+
}
114+
115+
/**
116+
* Get the Twitter's {@link ResolveInfo}
117+
* @return the Twitter's {@link ResolveInfo}
118+
*/
119+
public ResolveInfo getTwitterResolveInfo() {
120+
try{
121+
Intent intent = new Intent(Intent.ACTION_SEND);
122+
intent.putExtra(Intent.EXTRA_TEXT, "Test; please ignore");
123+
intent.setType("text/plain");
124+
125+
final PackageManager pm = this.ctx.getPackageManager();
126+
for(ResolveInfo resolveInfo: pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)){
127+
ActivityInfo activity = resolveInfo.activityInfo;
128+
if (TWITTER_APPS.containsValue(activity.name)) {
129+
return resolveInfo;
130+
}
131+
}
132+
133+
}
134+
finally {
135+
136+
}
137+
138+
139+
return null;
140+
}
141+
142+
/**
143+
* Check if the Twitter is available
144+
* @return true if a Twitter activity is detected
145+
*/
146+
public boolean isTwitterAvailable() {
147+
return getTwitterResolveInfo() != null;
148+
}
149+
}

Android/Twitter/www/twitter.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Base of the Android version for Twitter
3+
* @see https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/Twitter
4+
*/
5+
var Twitter =
6+
{
7+
/**
8+
* Check if the serrvice is available
9+
*/
10+
isTwitterAvailable: function( success, failure )
11+
{
12+
PhoneGap.exec( success, failure, "Twitter", "isTwitterAvailable", []);
13+
},
14+
15+
/**
16+
* Try to open Twitter for sending the message
17+
*/
18+
composeTweet: function( success, failure, tweetText, options)
19+
{
20+
PhoneGap.exec(success, failure, "Twitter", "composeTweet", [tweetText]);
21+
}
22+
};

0 commit comments

Comments
 (0)