If you think the Android project saostar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package net.azyobuzi.azyotter.saostar.services;
/*www.java2s.com*/import java.util.ArrayList;
import java.util.HashMap;
import twitter4j.DirectMessage;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.TwitterStream;
import twitter4j.User;
import twitter4j.UserList;
import twitter4j.UserStreamListener;
import net.azyobuzi.azyotter.saostar.Twitter4JFactories;
import net.azyobuzi.azyotter.saostar.configuration.Account;
import net.azyobuzi.azyotter.saostar.system.Action;
import net.azyobuzi.azyotter.saostar.timeline_data.TimelineItemCollection;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
publicclass TimelineReceiveService extends Service {
public TimelineReceiveService() {
instance = this;
}
privatestatic TimelineReceiveService instance;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
publicint onStartCommand(Intent intent, int flags, int startId) {
for (TimelineReceiver receiver : receivers.values()) {
receiver.start();
}
for (Account a : addQueue) {
internalAdd(a);
}
addQueue.clear();
return START_STICKY;
}
@Override
publicvoid onDestroy() {
for (TimelineReceiver receiver : receivers.values()) {
receiver.stop();
}
}
publicfinal HashMap<Account, TimelineReceiver> receivers = new HashMap<Account, TimelineReceiver>();
privatestatic ArrayList<Account> addQueue = new ArrayList<Account>();
publicstaticvoid addAccount(Account a) {
if (instance != null) {
instance.internalAdd(a);
} else {
addQueue.add(a);
}
}
privatevoid internalAdd(Account a) {
TimelineReceiver receiver = new TimelineReceiver(a);
receivers.put(a, receiver);
receiver.start();
}
publicstaticvoid removeAccount(Account a) {
if (instance != null) {
instance.internalRemove(a);
} else {
addQueue.remove(a);
}
}
privatevoid internalRemove(Account a) {
receivers.remove(a).dispose();
}
privateclass TimelineReceiver {
public TimelineReceiver(Account a) {
account = a;
//twitter = Twitter4JFactories.twitterFactory.getInstance(a.toAccessToken());
stream = Twitter4JFactories.twitterStreamFactory.getInstance(a.toAccessToken());
stream.addListener(new UserStreamListener() {
@Override
publicvoid onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
//TimelineItemCollection.removeTweet(statusDeletionNotice.getStatusId());
}
@Override
publicvoid onScrubGeo(long userId, long upToStatusId) {
// TODO ????????????????????
}
@Override
publicvoid onStatus(Status status) {
TimelineItemCollection.addOrMerge(status, true);
}
@Override
publicvoid onTrackLimitationNotice(int numberOfLimitedStatuses) {
// TODO ????????????????????
}
@Override
publicvoid onException(Exception ex) {
// TODO ????????????????????
}
@Override
publicvoid onBlock(User source, User blockedUser) {
// TODO ????????????????????
}
@Override
publicvoid onDeletionNotice(long directMessageId, long userId) {
TimelineItemCollection.removeDirectMessage(directMessageId);
}
@Override
publicvoid onDirectMessage(DirectMessage directMessage) {
TimelineItemCollection.addOrMerge(directMessage);
}
@Override
publicvoid onFavorite(User source, User target, Status favoritedStatus) {
// TODO ????????????????????
}
@Override
publicvoid onFollow(User source, User followedUser) {
// TODO ????????????????????
}
@Override
publicvoid onFriendList(long[] friendIds) {
// TODO ????????????????????
}
@Override
publicvoid onRetweet(User source, User target, Status retweetedStatus) {
// TODO ????????????????????
}
@Override
publicvoid onUnblock(User source, User unblockedUser) {
// TODO ????????????????????
}
@Override
publicvoid onUnfavorite(User source, User target, Status unfavoritedStatus) {
// TODO ????????????????????
}
@Override
publicvoid onUserListCreation(User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserListDeletion(User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserListMemberAddition(User addedMember, User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserListMemberDeletion(User deletedMember, User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserListSubscription(User subscriber, User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserListUnsubscription(User subscriber, User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserListUpdate(User listOwner, UserList list) {
// TODO ????????????????????
}
@Override
publicvoid onUserProfileUpdate(User updatedUser) {
// TODO ????????????????????
}
});
}
private Account account;
//private Twitter twitter;
private TwitterStream stream;
publicvoid start() {
account.useUserStreamChangedHandler.add(useUserStreamChangedHandler);
useUserStreamChangedHandler.invoke();
}
publicvoid stop() {
account.useUserStreamChangedHandler.remove(useUserStreamChangedHandler);
stream.cleanUp();
}
publicvoid dispose() {
stop();
stream.shutdown();
}
privatefinal Action useUserStreamChangedHandler = new Action() {
@Override
publicvoid invoke() {
stream.cleanUp();
if (account.getUseUserStream()) {
stream.user();
}
}
};
}
}