Twidere-App-Android-Twitter.../twidere/src/main/java/org/mariotaku/twidere/api/twitter/UserStreamCallback.java

208 lines
7.6 KiB
Java
Raw Normal View History

/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.api.twitter;
2015-05-27 18:31:48 +02:00
import android.util.Log;
import com.bluelinelabs.logansquare.LoganSquare;
2015-07-02 05:56:21 +02:00
import com.fasterxml.jackson.core.TreeNode;
2015-12-29 13:25:16 +01:00
import com.fasterxml.jackson.jr.tree.JacksonJrSimpleTreeCodec;
import org.mariotaku.restfu.callback.RawCallback;
2016-01-20 04:52:08 +01:00
import org.mariotaku.restfu.http.HttpResponse;
import org.mariotaku.twidere.api.twitter.model.DirectMessage;
import org.mariotaku.twidere.api.twitter.model.Status;
import org.mariotaku.twidere.api.twitter.model.StatusDeletionNotice;
import org.mariotaku.twidere.api.twitter.model.User;
import org.mariotaku.twidere.api.twitter.model.UserList;
import org.mariotaku.twidere.api.twitter.model.Warning;
import org.mariotaku.twidere.api.twitter.util.CRLFLineReader;
import org.mariotaku.twidere.api.twitter.util.JSONObjectType;
2016-02-19 08:09:32 +01:00
import org.mariotaku.twidere.util.LoganSquareMapperFinder;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by mariotaku on 15/5/26.
*/
public abstract class UserStreamCallback implements RawCallback {
2015-05-27 18:31:48 +02:00
private boolean connected;
private boolean disconnected;
@Override
2016-01-20 04:52:08 +01:00
public final void result(final HttpResponse response) throws IOException {
if (!response.isSuccessful()) {
final TwitterException cause = new TwitterException();
cause.setHttpResponse(response);
onException(cause);
return;
}
2015-12-29 13:25:16 +01:00
final JacksonJrSimpleTreeCodec mapper = new JacksonJrSimpleTreeCodec();
final CRLFLineReader reader = new CRLFLineReader(new InputStreamReader(response.getBody().stream(), "UTF-8"));
2015-05-27 18:31:48 +02:00
try {
for (String line; (line = reader.readLine()) != null && !disconnected; ) {
if (!connected) {
onConnected();
connected = true;
}
2015-05-27 18:31:48 +02:00
if (line.isEmpty()) continue;
2015-07-02 05:56:21 +02:00
TreeNode rootNode = mapper.readTree(LoganSquare.JSON_FACTORY.createParser(line));
2015-05-27 18:31:48 +02:00
switch (JSONObjectType.determine(rootNode)) {
case SENDER: {
break;
}
case STATUS: {
2016-02-19 08:09:32 +01:00
onStatus(LoganSquareMapperFinder.mapperFor(Status.class).parse(rootNode.traverse()));
2015-05-27 18:31:48 +02:00
break;
}
case DIRECT_MESSAGE: {
2016-02-19 08:09:32 +01:00
onDirectMessage(LoganSquareMapperFinder.mapperFor(DirectMessage.class).parse(rootNode.traverse()));
2015-05-27 18:31:48 +02:00
break;
}
case DELETE: {
break;
}
case LIMIT:
break;
case STALL_WARNING:
break;
case SCRUB_GEO:
break;
case FRIENDS:
break;
case FAVORITE: {
onFavorite(parse(User.class, rootNode.get("source")),
parse(User.class, rootNode.get("target")),
parse(Status.class, rootNode.get("target_object")));
break;
}
case UNFAVORITE: {
onUnfavorite(parse(User.class, rootNode.get("source")),
parse(User.class, rootNode.get("target")),
parse(Status.class, rootNode.get("target_object")));
break;
}
case FOLLOW:
break;
case UNFOLLOW:
break;
case USER_LIST_MEMBER_ADDED:
break;
case USER_LIST_MEMBER_DELETED:
break;
case USER_LIST_SUBSCRIBED:
break;
case USER_LIST_UNSUBSCRIBED:
break;
case USER_LIST_CREATED:
break;
case USER_LIST_UPDATED:
break;
case USER_LIST_DESTROYED:
break;
case USER_UPDATE:
break;
case USER_DELETE:
break;
case USER_SUSPEND:
break;
case BLOCK:
break;
case UNBLOCK:
break;
case DISCONNECTION:
break;
case UNKNOWN:
break;
}
}
2015-05-27 18:31:48 +02:00
} catch (IOException e) {
onException(e);
} finally {
Log.d("Twidere.Stream", "Cleaning up...");
reader.close();
response.close();
}
}
2015-05-27 18:31:48 +02:00
2015-07-02 05:56:21 +02:00
private static <T> T parse(final Class<T> cls, final TreeNode json) throws IOException {
2016-02-19 08:09:32 +01:00
return LoganSquareMapperFinder.mapperFor(cls).parse(json.traverse());
}
@Override
public final void error(final Throwable cause) {
onException(cause);
}
public void disconnect() {
disconnected = true;
}
2015-05-27 18:31:48 +02:00
public abstract void onConnected();
public abstract void onBlock(User source, User blockedUser);
public abstract void onDeletionNotice(long directMessageId, long userId);
public abstract void onDeletionNotice(StatusDeletionNotice statusDeletionNotice);
public abstract void onDirectMessage(DirectMessage directMessage);
public abstract void onException(Throwable ex);
public abstract void onFavorite(User source, User target, Status favoritedStatus);
public abstract void onFollow(User source, User followedUser);
public abstract void onFriendList(long[] friendIds);
public abstract void onScrubGeo(long userId, long upToStatusId);
public abstract void onStallWarning(Warning warn);
public abstract void onStatus(Status status);
public abstract void onTrackLimitationNotice(int numberOfLimitedStatuses);
public abstract void onUnblock(User source, User unblockedUser);
public abstract void onUnfavorite(User source, User target, Status unfavoritedStatus);
public abstract void onUserListCreation(User listOwner, UserList list);
public abstract void onUserListDeletion(User listOwner, UserList list);
public abstract void onUserListMemberAddition(User addedMember, User listOwner, UserList list);
public abstract void onUserListMemberDeletion(User deletedMember, User listOwner, UserList list);
public abstract void onUserListSubscription(User subscriber, User listOwner, UserList list);
public abstract void onUserListUnsubscription(User subscriber, User listOwner, UserList list);
public abstract void onUserListUpdate(User listOwner, UserList list);
public abstract void onUserProfileUpdate(User updatedUser);
}