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).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 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 > 140 && settings.twitlonger) {
// twitlonger goes here
TwitLongerHelper helper = new TwitLongerHelper(message, twitter);
return helper.createPost() != 0;
} else if (size <= 140) {
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 SendTweet method sendTweet.
public boolean sendTweet(AppSettings settings, Context context) {
try {
Twitter twitter = getTwitter();
if (remainingChars < 0 && !pwiccer) {
// twitlonger goes here
TwitLongerHelper helper = new TwitLongerHelper(message, twitter);
helper.setInReplyToStatusId(tweetId);
return helper.createPost() != 0;
} else {
twitter4j.StatusUpdate reply = new twitter4j.StatusUpdate(message);
reply.setInReplyToStatusId(tweetId);
if (!attachedUri.equals("")) {
// context being the Activity pointer
File outputDir = context.getCacheDir();
File f = File.createTempFile("compose", "picture", outputDir);
Bitmap bitmap = getBitmapToSend(Uri.parse(attachedUri), context);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
if (!settings.twitpic) {
reply.setMedia(f);
twitter.updateStatus(reply);
return true;
} else {
TwitPicHelper helper = new TwitPicHelper(twitter, message, f, context);
helper.setInReplyToStatusId(tweetId);
return helper.createPost() != 0;
}
} else {
// no picture
twitter.updateStatus(reply);
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class MentionsRefreshService method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
Context context = getApplicationContext();
AppSettings settings = AppSettings.getInstance(context);
// if they have mobile data on and don't want to sync over mobile data
if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
return;
}
try {
Twitter twitter = Utils.getTwitter(context, settings);
int currentAccount = sharedPrefs.getInt("current_account", 1);
MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
long[] lastId = dataSource.getLastIds(currentAccount);
Paging paging;
paging = new Paging(1, 200);
if (lastId[0] > 0) {
paging.sinceId(lastId[0]);
}
List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);
int inserted = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
if (settings.notifications && settings.mentionsNot && inserted > 0) {
if (intent.getBooleanExtra("from_launcher", false)) {
NotificationUtils.refreshNotification(context, true);
} else {
NotificationUtils.refreshNotification(context);
}
}
if (settings.syncSecondMentions) {
startService(new Intent(context, SecondMentionsRefreshService.class));
}
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
}
use of twitter4j.Twitter in project twitter4j by yusuke.
the class GetRateLimitStatus method main.
/**
* Usage: java twitter4j.examples.account.GetRateLimitStatus
*
* @param args message
*/
public static void main(String[] args) {
try {
Twitter twitter = new TwitterFactory().getInstance();
Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus();
for (String endpoint : rateLimitStatus.keySet()) {
RateLimitStatus status = rateLimitStatus.get(endpoint);
System.out.println("Endpoint: " + endpoint);
System.out.println(" Limit: " + status.getLimit());
System.out.println(" Remaining: " + status.getRemaining());
System.out.println(" ResetTimeInSeconds: " + status.getResetTimeInSeconds());
System.out.println(" SecondsUntilReset: " + status.getSecondsUntilReset());
}
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get rate limit status: " + te.getMessage());
System.exit(-1);
}
}
Aggregations