use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class ChoosenListActivity method getLists.
public void getLists() {
canRefresh = false;
new Thread(new Runnable() {
@Override
public void run() {
try {
Twitter twitter = Utils.getTwitter(context, settings);
ResponseList<twitter4j.Status> lists = twitter.getUserListStatuses(listId, paging);
paging.setPage(paging.getPage() + 1);
for (Status status : lists) {
statuses.add(status);
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
if (adapter == null) {
adapter = new TimelineArrayAdapter(context, statuses);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
} else {
adapter.notifyDataSetChanged();
}
spinner.setVisibility(View.GONE);
canRefresh = true;
}
});
} catch (Exception e) {
e.printStackTrace();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
spinner.setVisibility(View.GONE);
canRefresh = false;
}
});
} catch (OutOfMemoryError e) {
e.printStackTrace();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
spinner.setVisibility(View.GONE);
canRefresh = false;
}
});
}
}
}).start();
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class ChoosenListActivity method onRefreshStarted.
public void onRefreshStarted() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Twitter twitter = Utils.getTwitter(context, settings);
paging.setPage(1);
ResponseList<twitter4j.Status> lists = twitter.getUserListStatuses(listId, paging);
statuses.clear();
for (Status status : lists) {
statuses.add(status);
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new TimelineArrayAdapter(context, statuses);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
}
});
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
mPullToRefreshLayout.setRefreshing(false);
}
});
}
}).start();
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class SavedSearchFragment method getCursorAdapter.
@Override
public void getCursorAdapter(boolean showSpinner) {
if (showSpinner) {
listView.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
}
new Thread(new Runnable() {
@Override
public void run() {
final long topId;
if (tweets.size() > 0) {
topId = tweets.get(0).getId();
} else {
topId = 0;
}
try {
Twitter twitter = Utils.getTwitter(context, settings);
query = new Query(search);
QueryResult result = twitter.search(query);
tweets.clear();
for (twitter4j.Status status : result.getTweets()) {
tweets.add(status);
}
if (result.hasNext()) {
query = result.nextQuery();
hasMore = true;
} else {
hasMore = false;
}
try {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
int top = 0;
for (int i = 0; i < tweets.size(); i++) {
if (tweets.get(i).getId() == topId) {
top = i;
break;
}
}
adapter = new TimelineArrayAdapter(context, tweets);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
listView.setSelection(top);
spinner.setVisibility(View.GONE);
refreshLayout.setRefreshing(false);
}
});
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
try {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
spinner.setVisibility(View.GONE);
refreshLayout.setRefreshing(false);
}
});
} catch (Exception x) {
e.printStackTrace();
}
}
}
}).start();
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class SavedSearchFragment method getMore.
public void getMore() {
if (hasMore) {
canRefresh = false;
refreshLayout.setRefreshing(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Twitter twitter = Utils.getTwitter(context, settings);
QueryResult result = twitter.search(query);
for (twitter4j.Status status : result.getTweets()) {
tweets.add(status);
}
if (result.hasNext()) {
query = result.nextQuery();
hasMore = true;
} else {
hasMore = false;
}
try {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
canRefresh = true;
}
});
} catch (Exception e) {
}
} catch (Exception e) {
e.printStackTrace();
try {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
refreshLayout.setRefreshing(false);
canRefresh = true;
}
});
} catch (Exception x) {
}
}
}
}).start();
}
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class TimelineRefreshService method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
if (!MainActivity.canSwitch || CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning) {
return;
}
if (MainActivity.canSwitch) {
TimelineRefreshService.isRunning = true;
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
Context context = getApplicationContext();
int numberNew = 0;
AppSettings settings = AppSettings.getInstance(context);
// if they have mobile data on and don't want to sync over mobile data
if (intent.getBooleanExtra("on_start_refresh", false)) {
} else if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
return;
}
Twitter twitter = Utils.getTwitter(context, settings);
HomeDataSource dataSource = HomeDataSource.getInstance(context);
int currentAccount = sharedPrefs.getInt("current_account", 1);
List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
long[] lastId = null;
long id;
try {
lastId = dataSource.getLastIds(currentAccount);
id = lastId[1];
} catch (Exception e) {
try {
Thread.sleep(5000);
} catch (InterruptedException i) {
}
TimelineRefreshService.isRunning = false;
return;
}
if (id == 0) {
id = 1;
}
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;
} catch (OutOfMemoryError o) {
// don't know why...
}
}
Log.v("talon_pull", "got statuses, new = " + statuses.size());
// hash set to check for 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);
Long currentTime = Calendar.getInstance().getTimeInMillis();
if (currentTime - sharedPrefs.getLong("last_timeline_insert", 0l) < 10000) {
Log.v("talon_refresh", "don't insert the tweets on refresh");
sendBroadcast(new Intent("com.klinker.android.twitter.TIMELINE_REFRESHED").putExtra("number_new", 0));
TimelineRefreshService.isRunning = false;
context.getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
return;
} else {
sharedPrefs.edit().putLong("last_timeline_insert", currentTime).commit();
}
int inserted = HomeDataSource.getInstance(context).insertTweets(statuses, currentAccount, lastId);
if (inserted > 0 && statuses.size() > 0) {
sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
}
if (!intent.getBooleanExtra("on_start_refresh", false)) {
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
if (settings.notifications && (settings.timelineNot || settings.favoriteUserNotifications) && inserted > 0 && !intent.getBooleanExtra("from_launcher", false)) {
NotificationUtils.refreshNotification(context, !settings.timelineNot);
}
if (settings.preCacheImages) {
startService(new Intent(this, PreCacheService.class));
}
} else {
Log.v("talon_refresh", "sending broadcast to fragment");
sendBroadcast(new Intent("com.klinker.android.twitter.TIMELINE_REFRESHED").putExtra("number_new", inserted));
}
sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
TimelineRefreshService.isRunning = false;
}
}
Aggregations