use of twitter4j.Paging in project Talon-for-Twitter by klinker24.
the class CatchupPull method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
if (CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning || !MainActivity.canSwitch) {
return;
}
CatchupPull.isRunning = true;
Log.v("talon_pull", "catchup pull started");
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
final Context context = getApplicationContext();
int unreadNow = sharedPrefs.getInt("pull_unread", 0);
// stop it just in case
context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
AppSettings settings = AppSettings.getInstance(context);
if (settings.liveStreaming) {
Log.v("talon_pull", "into the try for catchup service");
Twitter twitter = Utils.getTwitter(context, settings);
HomeDataSource dataSource = HomeDataSource.getInstance(context);
int currentAccount = sharedPrefs.getInt("current_account", 1);
List<Status> statuses = new ArrayList<Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
long[] lastId;
long id;
try {
lastId = dataSource.getLastIds(currentAccount);
id = lastId[0];
} catch (Exception e) {
context.startService(new Intent(context, TalonPullNotificationService.class));
CatchupPull.isRunning = false;
return;
}
try {
paging.setSinceId(id);
} catch (Exception e) {
paging.setSinceId(1l);
}
for (int i = 0; i < settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getHomeTimeline(paging);
statuses.addAll(list);
if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
Log.v("talon_inserting", "found status");
foundStatus = true;
} else {
Log.v("talon_inserting", "haven't found status");
foundStatus = false;
}
}
} catch (Exception e) {
// the page doesn't exist
foundStatus = true;
e.printStackTrace();
} catch (OutOfMemoryError o) {
// don't know why...
o.printStackTrace();
}
}
Log.v("talon_pull", "got statuses, new = " + statuses.size());
// hash set to remove duplicates I guess
HashSet hs = new HashSet();
hs.addAll(statuses);
statuses.clear();
statuses.addAll(hs);
Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
lastId = dataSource.getLastIds(currentAccount);
int inserted = dataSource.insertTweets(statuses, currentAccount, lastId);
if (inserted > 0 && statuses.size() > 0) {
sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
unreadNow += statuses.size();
}
if (settings.preCacheImages) {
// delay it 15 seconds so that we can finish checking mentions first
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(context, PreCacheService.class));
}
}, 15000);
}
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
}
try {
Twitter twitter = Utils.getTwitter(context, settings);
int currentAccount = sharedPrefs.getInt("current_account", 1);
User user = twitter.verifyCredentials();
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 numNew = dataSource.insertTweets(statuses, currentAccount);
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
if (settings.notifications && settings.mentionsNot && numNew > 0) {
NotificationUtils.refreshNotification(context);
}
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
sharedPrefs.edit().putInt("pull_unread", unreadNow).commit();
context.startService(new Intent(context, TalonPullNotificationService.class));
context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
Log.v("talon_pull", "finished with the catchup service");
CatchupPull.isRunning = false;
}
use of twitter4j.Paging in project Talon-for-Twitter by klinker24.
the class DMFragment method onRefreshStarted.
@Override
public void onRefreshStarted() {
new AsyncTask<Void, Void, Void>() {
private boolean update;
private int numberNew;
@Override
protected void onPreExecute() {
DrawerActivity.canSwitch = false;
}
@Override
protected Void doInBackground(Void... params) {
try {
twitter = Utils.getTwitter(context, DrawerActivity.settings);
User user = twitter.verifyCredentials();
long lastId = sharedPrefs.getLong("last_direct_message_id_" + currentAccount, 0);
Paging paging;
if (lastId != 0) {
paging = new Paging(1).sinceId(lastId);
} else {
paging = new Paging(1, 500);
}
List<DirectMessage> dm = twitter.getDirectMessages(paging);
List<DirectMessage> sent = twitter.getSentDirectMessages(paging);
if (dm.size() != 0) {
sharedPrefs.edit().putLong("last_direct_message_id_" + currentAccount, dm.get(0).getId()).commit();
update = true;
numberNew = dm.size();
} else {
update = false;
numberNew = 0;
}
DMDataSource dataSource = DMDataSource.getInstance(context);
for (DirectMessage directMessage : dm) {
try {
dataSource.createDirectMessage(directMessage, currentAccount);
} catch (IllegalStateException e) {
dataSource = DMDataSource.getInstance(context);
dataSource.createDirectMessage(directMessage, currentAccount);
}
}
for (DirectMessage directMessage : sent) {
try {
dataSource.createDirectMessage(directMessage, currentAccount);
} catch (Exception e) {
dataSource = DMDataSource.getInstance(context);
dataSource.createDirectMessage(directMessage, currentAccount);
}
}
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long now = new Date().getTime();
long alarm = now + DrawerActivity.settings.dmRefresh;
Log.v("alarm_date", "direct message " + new Date(alarm).toString());
PendingIntent pendingIntent = PendingIntent.getService(context, DM_REFRESH_ID, new Intent(context, DirectMessageRefreshService.class), 0);
if (DrawerActivity.settings.dmRefresh != 0)
am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, DrawerActivity.settings.dmRefresh, pendingIntent);
else
am.cancel(pendingIntent);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (update) {
getCursorAdapter(false);
CharSequence text = numberNew == 1 ? numberNew + " " + getResources().getString(R.string.new_direct_message) : numberNew + " " + getResources().getString(R.string.new_direct_messages);
showToastBar(text + "", jumpToTop, 400, true, toTopListener);
int size = toDP(5) + mActionBarSize + (DrawerActivity.translucent ? DrawerActivity.statusBarHeight : 0);
listView.setSelectionFromTop(numberNew + (MainActivity.isPopup || landscape || MainActivity.settings.jumpingWorkaround ? 1 : 2), size);
} else {
getCursorAdapter(false);
CharSequence text = getResources().getString(R.string.no_new_direct_messages);
showToastBar(text + "", allRead, 400, true, toTopListener);
}
refreshLayout.setRefreshing(false);
} catch (IllegalStateException e) {
// fragment not attached to activity
}
DrawerActivity.canSwitch = true;
}
}.execute();
}
use of twitter4j.Paging in project Talon-for-Twitter by klinker24.
the class ListFragment method doRefresh.
public int doRefresh() {
int numberNew = 0;
try {
twitter = Utils.getTwitter(context, DrawerActivity.settings);
long[] lastId = ListDataSource.getInstance(context).getLastIds(listId);
final List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
if (lastId[0] > 0) {
paging.setSinceId(lastId[0]);
}
for (int i = 0; i < DrawerActivity.settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getUserListStatuses(listId, paging);
statuses.addAll(list);
}
} catch (Exception e) {
// the page doesn't exist
foundStatus = true;
} catch (OutOfMemoryError o) {
// don't know why...
}
}
manualRefresh = false;
ListDataSource dataSource = ListDataSource.getInstance(context);
numberNew = dataSource.insertTweets(statuses, listId);
return numberNew;
} catch (Exception e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
e.printStackTrace();
}
return 0;
}
Aggregations