-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitter.java
150 lines (131 loc) · 4.17 KB
/
Twitter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cc.lupine.quicksocial.shareutils;
import java.io.File;
import org.acra.ACRA;
import twitter4j.AsyncTwitter;
import twitter4j.AsyncTwitterFactory;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.TwitterAdapter;
import twitter4j.TwitterException;
import twitter4j.TwitterListener;
import twitter4j.conf.Configuration;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import cc.lupine.quicksocial.utils.ConfigurationBuilders;
import cc.lupine.quicksocial.utils.StaticUtilities;
public class Twitter extends SharingAdapter {
private Bundle params;
private OnShare event;
private Activity act;
/**
* Class constructor for Twitter sharing
*
* @param activity
* instance of a current {@link Activity}
* @param postParams
* {@link Bundle} containing all the post values
* @throws IllegalArgumentException
* when the postParams doesn't contain all required values
*/
public Twitter(Activity activity, Bundle postParams)
throws IllegalArgumentException {
if (postParams == null
|| (!postParams.containsKey("message")
&& !postParams.containsKey("image") && !postParams
.containsKey("link"))
|| !postParams.containsKey("user"))
throw new IllegalArgumentException(
"Bundle does not contain required keys");
params = postParams;
act = activity;
}
/**
* OnShare class instance containing listeners for common sharing events
*
* @param listener
* instance of the {@link OnShare} class with overriden abstract
* methods
* @see OnShare
*/
@Override
public void setOnShareListener(OnShare listener) {
event = listener;
}
/**
* Start asynchronous sharing in a new thread. No Exception can be thrown
* there, as the errors are now passed to the
* {@link OnShare#onError(String)} method.
*
* @see OnShare
*/
@Override
public void shareAsync() {
try {
if (params == null)
throw new IllegalStateException("No post bundle provided");
Configuration configuration = ConfigurationBuilders
.getTwitterConfig();
AsyncTwitterFactory factory = new AsyncTwitterFactory(configuration);
AsyncTwitter twitter = factory.getInstance();
twitter.setOAuthAccessToken(StaticUtilities
.getTwtTokensForUser(params.getString("user")));
String statusText = params.getString("message");
if (params.containsKey("link"))
statusText += " " + params.getString(params.getString("link"));
StatusUpdate status = new StatusUpdate(statusText);
if (params.containsKey("image"))
status.setMedia(new File(params.getString("image")));
twitter.addListener(getTwitterCallback());
event.onSharingStarted();
twitter.updateStatus(status);
} catch (IllegalStateException e) {
e.printStackTrace();
event.onError(e.getLocalizedMessage());
} catch (Exception e) {
e.printStackTrace();
event.onError("Unexpected exception: " + e.getLocalizedMessage());
ACRA.getErrorReporter().handleException(e);
}
}
/**
* Handle asynchronous response from twitter4j and call (in the UI thread)
* {@link OnShare#onShared(String)} if the post has been shared or
* {@link OnShare#onError(String)} when an error occurred.
*
* @return instance of a {@link com.facebook.Request.Callback} class
*/
private TwitterListener getTwitterCallback() {
TwitterListener listener = new TwitterAdapter() {
@Override
public void updatedStatus(final Status status) {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
event.onShared(String.valueOf(status.getId()));
}
});
}
@Override
public void onException(final TwitterException e,
final twitter4j.TwitterMethod method) {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
if (method == twitter4j.TwitterMethod.UPDATE_STATUS) {
e.printStackTrace();
event.onError(e.getLocalizedMessage());
} else {
e.printStackTrace();
event.onError("Unknown Twitter method, this should not happen: "
+ e.getLocalizedMessage());
ACRA.getErrorReporter().handleException(e);
}
}
});
}
};
return listener;
}
}