use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class ConversationFragment method getReplies.
public void getReplies(final ListView listView, final long tweetId, final LinearLayout progressSpinner, final HoloTextView none) {
Thread getReplies = new Thread(new Runnable() {
@Override
public void run() {
if (!isRunning) {
return;
}
Twitter twitter = Utils.getTwitter(context, settings);
replies = new ArrayList<twitter4j.Status>();
try {
status = twitter.showStatus(tweetId);
if (status.isRetweet()) {
status = status.getRetweetedStatus();
}
twitter4j.Status replyStatus = twitter.showStatus(status.getInReplyToStatusId());
try {
while (!replyStatus.getText().equals("")) {
if (!isRunning) {
return;
}
replies.add(replyStatus);
Log.v("reply_status", replyStatus.getText());
replyStatus = twitter.showStatus(replyStatus.getInReplyToStatusId());
}
} catch (Exception e) {
// the list of replies has ended, but we dont want to go to null
}
} catch (TwitterException e) {
e.printStackTrace();
}
if (status != null && replies.size() > 0) {
replies.add(0, status);
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (replies.size() > 0) {
ArrayList<twitter4j.Status> reversed = new ArrayList<twitter4j.Status>();
for (int i = replies.size() - 1; i >= 0; i--) {
reversed.add(replies.get(i));
}
replies = reversed;
adapter = new TimelineArrayAdapter(context, replies);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
progressSpinner.setVisibility(View.GONE);
} else {
}
} catch (Exception e) {
// none and it got the null object
}
if (status != null) {
// everything here worked, so get the discussion on the tweet
getDiscussion(listView, tweetId, progressSpinner, none, status);
}
}
});
}
});
getReplies.setPriority(7);
getReplies.start();
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class ConversationFragment method getDiscussion.
public void getDiscussion(final ListView listView, final long tweetId, final LinearLayout progressBar, final HoloTextView none, final Status status) {
Log.v("talon_replies", "getting discussion");
if (replies.size() == 0) {
replies.add(status);
}
Thread getReplies = new Thread(new Runnable() {
@Override
public void run() {
if (!isRunning) {
return;
}
ArrayList<twitter4j.Status> all = null;
Twitter twitter = Utils.getTwitter(context, settings);
try {
Log.v("talon_replies", "looking for discussion");
long id = status.getId();
String screenname = status.getUser().getScreenName();
query = new Query("@" + screenname + " since_id:" + id);
Log.v("talon_replies", "query string: " + query.getQuery());
try {
query.setCount(100);
} catch (Throwable e) {
// enlarge buffer error?
query.setCount(30);
}
QueryResult result = twitter.search(query);
Log.v("talon_replies", "result: " + result.getTweets().size());
all = new ArrayList<twitter4j.Status>();
do {
Log.v("talon_replies", "do loop repetition");
if (!isRunning) {
return;
}
List<Status> tweets = result.getTweets();
for (twitter4j.Status tweet : tweets) {
if (tweet.getInReplyToStatusId() == id) {
all.add(tweet);
Log.v("talon_replies", tweet.getText());
}
}
if (all.size() > 0) {
for (int i = all.size() - 1; i >= 0; i--) {
Log.v("talon_replies", "inserting into arraylist:" + all.get(i).getText());
replies.add(all.get(i));
}
all.clear();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
try {
if (replies.size() > 0) {
if (adapter == null || adapter.getCount() == 0) {
adapter = new TimelineArrayAdapter(context, replies);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
} else {
Log.v("talon_replies", "notifying adapter change");
adapter.notifyDataSetChanged();
}
} else {
none.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
// none and it got the null object
e.printStackTrace();
listView.setVisibility(View.GONE);
none.setVisibility(View.VISIBLE);
}
}
});
}
try {
Thread.sleep(250);
} catch (Exception e) {
// since we are changing the arraylist for the adapter in the background, we need to make sure it
// gets updated before continuing
}
query = result.nextQuery();
if (query != null)
result = twitter.search(query);
} while (query != null);
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
if (replies.size() < 2) {
// nothing to show, so tell them that
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
listView.setVisibility(View.GONE);
none.setVisibility(View.VISIBLE);
}
});
}
}
});
getReplies.setPriority(8);
getReplies.start();
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class SendScheduledTweet method sendTweet.
public boolean sendTweet(AppSettings settings, Context context, String message, int account) {
try {
Twitter twitter;
if (account == settings.currentAccount) {
twitter = Utils.getTwitter(context, settings);
} else {
twitter = Utils.getSecondTwitter(context);
}
int size = getCount(message);
Log.v("talon_queued", "sending: " + message);
if (size > AppSettings.getInstance(this).tweetCharacterCount && settings.twitlonger) {
// twitlonger goes here
TwitLongerHelper helper = new TwitLongerHelper(message, twitter);
return helper.createPost() != 0;
} else if (size <= AppSettings.getInstance(this).tweetCharacterCount) {
twitter4j.StatusUpdate reply = new twitter4j.StatusUpdate(message);
twitter.updateStatus(reply);
} else {
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class TalonPullNotificationService method onCreate.
@Override
public void onCreate() {
super.onCreate();
if (TalonPullNotificationService.isRunning) {
stopSelf();
return;
}
TalonPullNotificationService.isRunning = true;
settings = AppSettings.getInstance(this);
mCache = App.getInstance(this).getBitmapCache();
sharedPreferences = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
showNotification = sharedPreferences.getBoolean("show_pull_notification", true);
pullUnread = sharedPreferences.getInt("pull_unread", 0);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent stop = new Intent(this, StopPull.class);
PendingIntent stopPending = PendingIntent.getService(this, 0, stop, 0);
Intent popup = new Intent(this, RedirectToPopup.class);
popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
popup.putExtra("from_notification", true);
PendingIntent popupPending = PendingIntent.getActivity(this, 0, popup, 0);
Intent compose = new Intent(this, WidgetCompose.class);
popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent composePending = PendingIntent.getActivity(this, 0, compose, 0);
String text;
int count = 0;
if (sharedPreferences.getBoolean("is_logged_in_1", false)) {
count++;
}
if (sharedPreferences.getBoolean("is_logged_in_2", false)) {
count++;
}
boolean multAcc = false;
if (count == 2) {
multAcc = true;
}
if (settings.liveStreaming && settings.timelineNot) {
text = getResources().getString(R.string.new_tweets_upper) + ": " + pullUnread;
} else {
text = getResources().getString(R.string.listening_for_mentions) + "...";
}
mBuilder = new NotificationCompat.Builder(this, TALON_SERVICE_CHANNEL_ID).setSmallIcon(android.R.color.transparent).setContentTitle(getResources().getString(R.string.talon_pull) + (multAcc ? " - @" + settings.myScreenName : "")).setContentText(text).setOngoing(true).setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_stat_icon));
if (getApplicationContext().getResources().getBoolean(R.bool.expNotifications)) {
mBuilder.addAction(R.drawable.ic_cancel_dark, getApplicationContext().getResources().getString(R.string.stop), stopPending);
mBuilder.addAction(R.drawable.ic_popup, getResources().getString(R.string.popup), popupPending);
mBuilder.addAction(R.drawable.ic_send_dark, getResources().getString(R.string.tweet), composePending);
}
try {
mBuilder.setWhen(0);
} catch (Exception e) {
}
mBuilder.setContentIntent(pendingIntent);
// priority flag is only available on api level 16 and above
if (getResources().getBoolean(R.bool.expNotifications)) {
mBuilder.setPriority(Notification.PRIORITY_MIN);
}
mContext = getApplicationContext();
IntentFilter filter = new IntentFilter();
filter.addAction("com.klinker.android.twitter.STOP_PUSH");
registerReceiver(stopPush, filter);
filter = new IntentFilter();
filter.addAction("com.klinker.android.twitter.START_PUSH");
registerReceiver(startPush, filter);
filter = new IntentFilter();
filter.addAction("com.klinker.android.twitter.STOP_PUSH_SERVICE");
registerReceiver(stopService, filter);
if (settings.liveStreaming && settings.timelineNot) {
filter = new IntentFilter();
filter.addAction("com.klinker.android.twitter.UPDATE_NOTIF");
registerReceiver(updateNotification, filter);
filter = new IntentFilter();
filter.addAction("com.klinker.android.twitter.NEW_TWEET");
registerReceiver(updateNotification, filter);
filter = new IntentFilter();
filter.addAction("com.klinker.android.twitter.CLEAR_PULL_UNREAD");
registerReceiver(clearPullUnread, filter);
}
Thread start = new Thread(new Runnable() {
@Override
public void run() {
// get the ids of everyone you follow
try {
Log.v("getting_ids", "started getting ids, mine: " + settings.myId);
Twitter twitter = Utils.getTwitter(mContext, settings);
long currCursor = -1;
IDs idObject;
ids = new ArrayList<Long>();
do {
idObject = twitter.getFriendsIDs(settings.myId, currCursor);
long[] lIds = idObject.getIDs();
for (int i = 0; i < lIds.length; i++) {
ids.add(lIds[i]);
}
} while ((currCursor = idObject.getNextCursor()) != 0);
ids.add(settings.myId);
currCursor = -1;
blockedIds = new ArrayList<Long>();
do {
idObject = twitter.getBlocksIDs(currCursor);
long[] lIds = idObject.getIDs();
for (int i = 0; i < lIds.length; i++) {
blockedIds.add(lIds[i]);
}
} while ((currCursor = idObject.getNextCursor()) != 0);
idsLoaded = true;
if (showNotification)
startForeground(FOREGROUND_SERVICE_ID, mBuilder.build());
mContext.sendBroadcast(new Intent("com.klinker.android.twitter.START_PUSH"));
} catch (Exception e) {
e.printStackTrace();
TalonPullNotificationService.isRunning = false;
pullUnread = 0;
Thread stop = new Thread(new Runnable() {
@Override
public void run() {
TalonPullNotificationService.shuttingDown = true;
try {
// pushStream.removeListener(userStream);
} catch (Exception x) {
}
try {
pushStream.cleanUp();
pushStream.shutdown();
Log.v("twitter_stream_push", "stopping push notifications");
} catch (Exception e) {
// it isn't running
e.printStackTrace();
// try twice to shut it down i guess
try {
Thread.sleep(2000);
pushStream.cleanUp();
pushStream.shutdown();
Log.v("twitter_stream_push", "stopping push notifications");
} catch (Exception x) {
// it isn't running
x.printStackTrace();
}
}
TalonPullNotificationService.shuttingDown = false;
}
});
stop.setPriority(Thread.MAX_PRIORITY);
stop.start();
stopSelf();
} catch (OutOfMemoryError e) {
TalonPullNotificationService.isRunning = false;
Thread stop = new Thread(new Runnable() {
@Override
public void run() {
TalonPullNotificationService.shuttingDown = true;
try {
// pushStream.removeListener(userStream);
} catch (Exception x) {
}
try {
pushStream.cleanUp();
pushStream.shutdown();
Log.v("twitter_stream_push", "stopping push notifications");
} catch (Exception e) {
// it isn't running
e.printStackTrace();
// try twice to shut it down i guess
try {
Thread.sleep(2000);
pushStream.cleanUp();
pushStream.shutdown();
Log.v("twitter_stream_push", "stopping push notifications");
} catch (Exception x) {
// it isn't running
x.printStackTrace();
}
}
TalonPullNotificationService.shuttingDown = false;
}
});
stop.setPriority(Thread.MAX_PRIORITY);
stop.start();
pullUnread = 0;
stopSelf();
}
}
});
start.setPriority(Thread.MAX_PRIORITY - 1);
start.start();
}
use of twitter4j.Twitter in project twicalico by moko256.
the class ShowUserActivity method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ThrowableFunc throwableFunc = null;
Twitter twitter = GlobalApplication.twitter;
switch(item.getItemId()) {
case R.id.action_share:
startActivity(Intent.createChooser(new Intent().setAction(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, getShareUrl()), getString(R.string.share)));
break;
case R.id.action_open_in_browser:
AppCustomTabsKt.launchChromeCustomTabs(this, getShareUrl());
break;
case R.id.action_create_follow:
throwableFunc = () -> twitter.createFriendship(user.getId());
break;
case R.id.action_destroy_follow:
throwableFunc = () -> twitter.destroyFriendship(user.getId());
break;
case R.id.action_create_mute:
throwableFunc = () -> twitter.createMute(user.getId());
break;
case R.id.action_destroy_mute:
throwableFunc = () -> twitter.destroyMute(user.getId());
break;
case R.id.action_create_block:
throwableFunc = () -> twitter.createBlock(user.getId());
break;
case R.id.action_destroy_block:
throwableFunc = () -> twitter.destroyBlock(user.getId());
break;
case R.id.action_destroy_follow_follower:
throwableFunc = () -> {
twitter.createBlock(user.getId());
twitter.destroyBlock(user.getId());
};
break;
case R.id.action_spam_report:
throwableFunc = () -> GlobalApplication.twitter.reportSpam(user.getId());
break;
}
if (throwableFunc != null) {
ThrowableFunc finalThrowableFunc = throwableFunc;
confirmDialog(item.getTitle(), getString(R.string.confirm_message), () -> runAsWorkerThread(finalThrowableFunc));
}
return super.onOptionsItemSelected(item);
}
Aggregations