diff options
author | Andrew Comminos <andrew@morlunk.com> | 2015-04-17 11:04:38 +0300 |
---|---|---|
committer | Andrew Comminos <andrew@morlunk.com> | 2015-04-17 11:04:38 +0300 |
commit | 24f6e3d2c5662f38982b92e8148ed8088bd61e79 (patch) | |
tree | 39b3c5f223321c7894dbd2e1cc3058220d04eb9f /src/main/java/com/morlunk | |
parent | cef047a75db04ad8e3e8be154268c63ec3287aa4 (diff) |
WIP AIDL models.
Diffstat (limited to 'src/main/java/com/morlunk')
10 files changed, 227 insertions, 355 deletions
diff --git a/src/main/java/com/morlunk/jumble/JumbleService.java b/src/main/java/com/morlunk/jumble/JumbleService.java index fca2d76..a98425d 100644 --- a/src/main/java/com/morlunk/jumble/JumbleService.java +++ b/src/main/java/com/morlunk/jumble/JumbleService.java @@ -36,6 +36,8 @@ import android.util.Log; import com.morlunk.jumble.audio.AudioOutput; import com.morlunk.jumble.exception.AudioException; import com.morlunk.jumble.model.Channel; +import com.morlunk.jumble.model.IChannel; +import com.morlunk.jumble.model.IUser; import com.morlunk.jumble.model.Message; import com.morlunk.jumble.model.Server; import com.morlunk.jumble.model.User; @@ -175,7 +177,7 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon new AudioHandler.AudioEncodeListener() { @Override public void onAudioEncoded(byte[] data, int length) { - if(mConnection.isSynchronized()) { + if(mConnection != null && mConnection.isSynchronized()) { mConnection.sendUDPMessage(data, length, false); } } @@ -407,20 +409,19 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon @Override public void onConnectionWarning(String warning) { - log(Message.Type.WARNING, warning); + log(warning); } @Override - public void log(Message.Type type, String message) { - Message msg = new Message(type, message); + public void log(String message) { + Message msg = new Message(message); log(msg); } @Override public void log(Message message) { // Only log non-fatal (~INFO) messages post-connect. - if (mConnection != null && - (mConnection.isSynchronized() || message.getType() != Message.Type.INFO)) { + if (mConnection != null && mConnection.isSynchronized()) { mMessageLog.add(message); try { mCallbacks.onMessageLogged(message); @@ -654,15 +655,15 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon } @Override - public User getSessionUser() throws RemoteException { + public IUser getSessionUser() throws RemoteException { return mModelHandler != null ? mModelHandler.getUser(getSession()) : null; } @Override - public Channel getSessionChannel() throws RemoteException { - User user = getSessionUser(); + public IChannel getSessionChannel() throws RemoteException { + IUser user = getSessionUser(); if (user != null) { - return getChannel(user.getChannelId()); + return user.getChannel(); } return null; } @@ -673,21 +674,21 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon } @Override - public User getUser(int id) throws RemoteException { + public IUser getUser(int id) throws RemoteException { if (mModelHandler != null) return mModelHandler.getUser(id); return null; } @Override - public Channel getChannel(int id) throws RemoteException { + public IChannel getChannel(int id) throws RemoteException { if (mModelHandler != null) return mModelHandler.getChannel(id); return null; } @Override - public Channel getRootChannel() throws RemoteException { + public IChannel getRootChannel() throws RemoteException { return getChannel(0); } @@ -736,7 +737,7 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon try { mAudioHandler.setTalking(talking); } catch (AudioException e) { - log(Message.Type.WARNING, e.getMessage()); + log(e.getMessage()); } } @@ -837,8 +838,8 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon tmb.setMessage(message); mConnection.sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); - User self = getSessionUser(); - User user = getUser(session); + User self = mModelHandler.getUser(getSession()); + User user = mModelHandler.getUser(session); List<User> users = new ArrayList<User>(1); users.add(user); Message logMessage = new Message(getSession(), self.getName(), new ArrayList<Channel>(0), new ArrayList<Channel>(0), users, message); @@ -854,8 +855,8 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon tmb.setMessage(message); mConnection.sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); - User self = getSessionUser(); - Channel targetChannel = getChannel(channel); + User self = mModelHandler.getUser(getSession()); + Channel targetChannel = mModelHandler.getChannel(channel); List<Channel> targetChannels = new ArrayList<Channel>(); targetChannels.add(targetChannel); Message logMessage = new Message(getSession(), self.getName(), targetChannels, tree ? targetChannels : new ArrayList<Channel>(0), new ArrayList<User>(0), message); diff --git a/src/main/java/com/morlunk/jumble/audio/AudioUser.java b/src/main/java/com/morlunk/jumble/audio/AudioUser.java new file mode 100644 index 0000000..60fc7d8 --- /dev/null +++ b/src/main/java/com/morlunk/jumble/audio/AudioUser.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 Andrew Comminos + * + * 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 com.morlunk.jumble.audio; + +/** + * Created by andrew on 05/04/15. + */ +public class AudioUser { + private static final float AVERAGE_DECAY = 0.99f; + + private int mSession; + /** The average amount of packets in the jitter buffer. */ + private float mAverageAvailable; + + public AudioUser(int session) { + mSession = session; + mAverageAvailable = 0.f; + } + + public int getSession() { + return mSession; + } + + /** + * Use the given packet data to update the average available packets. + * If the number of packets present is greater than the accumulated average, update our average + * to it- otherwise, decay our average by a factor of {@link #AVERAGE_DECAY}. + * @param numPackets The number of packets present in this cycle of the jitter buffer. + */ + public void updateAveragePackets(int numPackets) { + mAverageAvailable = Math.max(numPackets, mAverageAvailable * AVERAGE_DECAY); + } + + public int getAveragePackets() { + return (int) Math.ceil(mAverageAvailable); + } +} diff --git a/src/main/java/com/morlunk/jumble/model/Channel.java b/src/main/java/com/morlunk/jumble/model/Channel.java index f0cca5e..47cc8f4 100644 --- a/src/main/java/com/morlunk/jumble/model/Channel.java +++ b/src/main/java/com/morlunk/jumble/model/Channel.java @@ -17,102 +17,53 @@ package com.morlunk.jumble.model; -import android.os.Parcel; -import android.os.Parcelable; - import java.util.ArrayList; +import java.util.Collections; import java.util.List; -public final class Channel implements Parcelable, Comparable<Channel> { +public final class Channel extends IChannel.Stub implements Comparable<Channel> { private int mId; private int mPosition; private int mLevel; private boolean mTemporary; - private int mParent = -1; + private Channel mParent; private String mName; private String mDescription; private byte[] mDescriptionHash; - private List<Integer> mSubchannels = new ArrayList<Integer>(); - private List<Integer> mUsers = new ArrayList<Integer>(); - private List<Integer> mLinks = new ArrayList<Integer>(); + private List<Channel> mSubchannels; + private List<User> mUsers; + private List<Channel> mLinks; private int mUserCount; private int mPermissions; - public static final Parcelable.Creator<Channel> CREATOR = new Parcelable.Creator<Channel>() { - - @Override - public Channel createFromParcel(Parcel parcel) { - return new Channel(parcel); - } - - @Override - public Channel[] newArray(int i) { - return new Channel[i]; - } - }; - public Channel() { + mSubchannels = new ArrayList<Channel>(); + mUsers = new ArrayList<User>(); + mLinks = new ArrayList<Channel>(); } public Channel(int id, boolean temporary) { + this(); mId = id; mTemporary = temporary; } - private Channel(Parcel in) { - readFromParcel(in); - } - - @Override - public void writeToParcel(Parcel out, int flags) { - out.writeInt(mId); - out.writeInt(mPosition); - out.writeInt(mLevel); - out.writeValue(mTemporary); - out.writeInt(mParent); - out.writeString(mName); - out.writeString(mDescription); - out.writeInt(mDescriptionHash.length); // Store length so we can re-initialize byte buffer on read - out.writeByteArray(mDescriptionHash); - out.writeList(mSubchannels); - out.writeList(mUsers); - out.writeList(mLinks); - out.writeInt(mUserCount); - out.writeInt(mPermissions); - } - - public void readFromParcel(Parcel in) { - mId = in.readInt(); - mPosition = in.readInt(); - mLevel = in.readInt(); - mTemporary = (Boolean)in.readValue(null); - mParent = in.readInt(); - mName = in.readString(); - mDescription = in.readString(); - mDescriptionHash = new byte[in.readInt()]; - in.readByteArray(mDescriptionHash); - mSubchannels = in.readArrayList(null); - mUsers = in.readArrayList(null); - mLinks = in.readArrayList(null); - mUserCount = in.readInt(); - mPermissions = in.readInt(); - } - - @Override - public int describeContents() { - return 0; - } - - public void addUser(int userId) { - mUsers.add(userId); + public void addUser(User user) { + for (int i = 0; i < mUsers.size() + 1; i++) { + User u = mUsers.get(i); + if (u == null || user.compareTo(u) <= 0) { + mUsers.add(i, user); + return; + } + } } - public void removeUser(int userId) { - mUsers.remove((Object)userId); + public void removeUser(User user) { + mUsers.remove(user); } - public List<Integer> getUsers() { - return mUsers; + public List<User> getUsers() { + return Collections.unmodifiableList(mUsers); } public int getId() { @@ -139,11 +90,11 @@ public final class Channel implements Parcelable, Comparable<Channel> { this.mTemporary = mTemporary; } - public int getParent() { + public Channel getParent() { return mParent; } - public void setParent(int mParent) { + public void setParent(Channel mParent) { this.mParent = mParent; } @@ -171,28 +122,40 @@ public final class Channel implements Parcelable, Comparable<Channel> { this.mDescriptionHash = mDescriptionHash; } - public List<Integer> getSubchannels() { - return mSubchannels; + public List<Channel> getSubchannels() { + return Collections.unmodifiableList(mSubchannels); } - public void addSubchannel(int channel) { - mSubchannels.add(channel); + public void addSubchannel(Channel channel) { + for (int i = 0; i < mSubchannels.size() + 1; i++) { + Channel sc = mSubchannels.get(i); + if (sc == null || channel.compareTo(sc) <= 0) { + mSubchannels.add(i, channel); + return; + } + } } - public void removeSubchannel(int channelId) { - mSubchannels.remove((Object)channelId); + public void removeSubchannel(Channel channel) { + mSubchannels.remove(channel); } - public List<Integer> getLinks() { - return mLinks; + public List<Channel> getLinks() { + return Collections.unmodifiableList(mLinks); } - public void addLink(int channelId) { - mLinks.add(channelId); + public void addLink(Channel channel) { + for (int i = 0; i < mLinks.size() + 1; i++) { + Channel sc = mLinks.get(i); + if (sc == null || channel.compareTo(sc) <= 0) { + mLinks.add(i, channel); + return; + } + } } - public void removeLink(int channelId) { - mLinks.remove((Object)channelId); + public void removeLink(Channel channel) { + mLinks.remove(channel); } public void clearLinks() { @@ -201,11 +164,15 @@ public final class Channel implements Parcelable, Comparable<Channel> { /** * @return The sum of users in this channel and its subchannels. + * @deprecated TODO: just recursively fetch user count */ public int getSubchannelUserCount() { return mUserCount; } + /** + * @deprecated TODO: just recursively fetch user count + */ public void setSubchannelUserCount(int userCount) { mUserCount = userCount; } diff --git a/src/main/java/com/morlunk/jumble/model/Message.java b/src/main/java/com/morlunk/jumble/model/Message.java index ee0247e..8aa97c4 100644 --- a/src/main/java/com/morlunk/jumble/model/Message.java +++ b/src/main/java/com/morlunk/jumble/model/Message.java @@ -21,83 +21,43 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.format.Time; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; import java.util.List; /** * A class encapsulating a text message from a Mumble server. - * NOTE: Always prefer using getActorName(). You CANNOT rely on getActor() to provide this info, as - * we might keep message history for a user that is no longer in the {@link UserManager}. + * NOTE: Always prefer using getActorName(). You CANNOT rely on getActor() to provide this info, + * as the actor may no longer be on the server. * Created by andrew on 03/12/13. */ -public class Message implements Parcelable { - - public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() { - - @Override - public Message createFromParcel(Parcel parcel) { - return new Message(parcel); - } - - @Override - public Message[] newArray(int i) { - return new Message[i]; - } - }; - - /** - * The type of message this object represents. - */ - public enum Type { - INFO, - WARNING, - TEXT_MESSAGE - } - - private Message.Type mType; - private int mActor = -1; +public class Message extends IMessage.Stub { + private int mActor; private String mActorName; private List<Channel> mChannels; private List<Channel> mTrees; private List<User> mUsers; private String mMessage; - private Time mReceivedTime; - - public Message() { - - } + private long mReceivedTime; - public Message(Message.Type messageType, String message) { - mType = messageType; + public Message(String message) { mMessage = message; - mReceivedTime = new Time(); - mReceivedTime.setToNow(); + mActor = -1; + mReceivedTime = new Date().getTime(); + mChannels = new ArrayList<Channel>(); + mTrees = new ArrayList<Channel>(); + mUsers = new ArrayList<User>(); } public Message(int actor, String actorName, List<Channel> channels, List<Channel> trees, List<User> users, String message) { - this(Type.TEXT_MESSAGE, message); + this(message); mActor = actor; mActorName = actorName; mChannels = channels; mTrees = trees; mUsers = users; } - - public Message(Parcel parcel) { - readFromParcel(parcel); - } - - public void readFromParcel(Parcel in) { - mType = Type.values()[in.readInt()]; - mActor = in.readInt(); - mActorName = in.readString(); - mChannels = in.readArrayList(null); - mTrees = in.readArrayList(null); - mUsers = in.readArrayList(null); - mMessage = in.readString(); - mReceivedTime = new Time(); - mReceivedTime.set(in.readLong()); - } - public int getActor() { return mActor; } @@ -106,47 +66,26 @@ public class Message implements Parcelable { return mActorName; } - public List<Channel> getChannels() { - return mChannels; + public List<Channel> getTargetChannels() { + return Collections.unmodifiableList(mChannels); } - public List<Channel> getTrees() { - return mTrees; + public List<Channel> getTargetTrees() { + return Collections.unmodifiableList(mTrees); } - public List<User> getUsers() { - return mUsers; + public List<User> getTargetUsers() { + return Collections.unmodifiableList(mUsers); } public String getMessage() { return mMessage; } - public Time getReceivedTime() { + public long getReceivedTime() { return mReceivedTime; } - public Type getType() { - return mType; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(mType.ordinal()); - dest.writeInt(mActor); - dest.writeString(mActorName); - dest.writeList(mChannels); - dest.writeList(mTrees); - dest.writeList(mUsers); - dest.writeString(mMessage); - dest.writeLong(mReceivedTime.toMillis(false)); - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -155,27 +94,28 @@ public class Message implements Parcelable { Message message = (Message) o; if (mActor != message.mActor) return false; - if (!mActorName.equals(message.mActorName)) return false; - if (!mChannels.equals(message.mChannels)) return false; - if (!mMessage.equals(message.mMessage)) return false; - if (!mReceivedTime.equals(message.mReceivedTime)) return false; - if (!mTrees.equals(message.mTrees)) return false; - if (mType != message.mType) return false; - if (!mUsers.equals(message.mUsers)) return false; + if (mReceivedTime != message.mReceivedTime) return false; + if (mActorName != null ? !mActorName.equals(message.mActorName) : message.mActorName != null) + return false; + if (mChannels != null ? !mChannels.equals(message.mChannels) : message.mChannels != null) + return false; + if (mMessage != null ? !mMessage.equals(message.mMessage) : message.mMessage != null) + return false; + if (mTrees != null ? !mTrees.equals(message.mTrees) : message.mTrees != null) return false; + if (mUsers != null ? !mUsers.equals(message.mUsers) : message.mUsers != null) return false; return true; } @Override public int hashCode() { - int result = mType.hashCode(); - result = 31 * result + mActor; - result = 31 * result + mActorName.hashCode(); - result = 31 * result + mChannels.hashCode(); - result = 31 * result + mTrees.hashCode(); - result = 31 * result + mUsers.hashCode(); - result = 31 * result + mMessage.hashCode(); - result = 31 * result + mReceivedTime.hashCode(); + int result = mActor; + result = 31 * result + (mActorName != null ? mActorName.hashCode() : 0); + result = 31 * result + (mChannels != null ? mChannels.hashCode() : 0); + result = 31 * result + (mTrees != null ? mTrees.hashCode() : 0); + result = 31 * result + (mUsers != null ? mUsers.hashCode() : 0); + result = 31 * result + (mMessage != null ? mMessage.hashCode() : 0); + result = 31 * result + (int) (mReceivedTime ^ (mReceivedTime >>> 32)); return result; } } diff --git a/src/main/java/com/morlunk/jumble/model/User.java b/src/main/java/com/morlunk/jumble/model/User.java index 09c097a..6a5e950 100644 --- a/src/main/java/com/morlunk/jumble/model/User.java +++ b/src/main/java/com/morlunk/jumble/model/User.java @@ -23,7 +23,7 @@ import android.os.Parcelable; import com.google.protobuf.ByteString; -public class User implements Parcelable { +public class User extends IUser.Stub implements Comparable<User> { public static enum TalkState { TALKING, @@ -37,7 +37,7 @@ public class User implements Parcelable { private String mName; private String mComment; private ByteString mCommentHash; - private Bitmap mTexture; + private ByteString mTexture; private ByteString mTextureHash; private String mHash; @@ -51,7 +51,7 @@ public class User implements Parcelable { private boolean mPrioritySpeaker; private boolean mRecording; - private int mChannel = -1; + private Channel mChannel; private TalkState mTalkState = TalkState.PASSIVE; @@ -62,19 +62,6 @@ public class User implements Parcelable { /** The number of samples normally available from the user. */ private float mAverageAvailable; - public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() { - - @Override - public User createFromParcel(Parcel parcel) { - return new User(parcel); - } - - @Override - public User[] newArray(int i) { - return new User[i]; - } - }; - public User() { } @@ -84,68 +71,15 @@ public class User implements Parcelable { mName = name; } - private User(Parcel in) { - readFromParcel(in); - } - - @Override - public void writeToParcel(Parcel out, int flags) { - out.writeInt(mSession); - out.writeInt(mId); - out.writeString(mName); - out.writeString(mComment); - out.writeInt(mCommentHash.size()); - out.writeByteArray(mCommentHash.toByteArray()); - out.writeString(mHash); - out.writeValue(mMuted); - out.writeValue(mDeafened); - out.writeValue(mSuppressed); - out.writeValue(mSelfMuted); - out.writeValue(mSelfDeafened); - out.writeValue(mPrioritySpeaker); - out.writeValue(mRecording); - out.writeInt(mChannel); - out.writeValue(mLocalMuted); - out.writeFloat(mAverageAvailable); - out.writeString(mTalkState.toString()); - } - - public void readFromParcel(Parcel in) { - mSession = in.readInt(); - mId = in.readInt(); - mName = in.readString(); - mComment = in.readString(); - byte[] commentHash = new byte[in.readInt()]; - in.readByteArray(commentHash); - mCommentHash = ByteString.copyFrom(commentHash); - mHash = in.readString(); - mMuted = (Boolean)in.readValue(null); - mDeafened = (Boolean)in.readValue(null); - mSuppressed = (Boolean)in.readValue(null); - mSelfMuted = (Boolean)in.readValue(null); - mSelfDeafened = (Boolean)in.readValue(null); - mPrioritySpeaker = (Boolean)in.readValue(null); - mRecording = (Boolean)in.readValue(null); - mChannel = in.readInt(); - mLocalMuted = (Boolean)in.readValue(null); - mAverageAvailable = in.readFloat(); - mTalkState = TalkState.valueOf(in.readString()); - } - - @Override - public int describeContents() { - return 0; - } - public int getSession() { return mSession; } - public int getChannelId() { + public Channel getChannel() { return mChannel; } - public void setChannelId(int channel) { + public void setChannel(Channel channel) { mChannel = channel; } @@ -173,24 +107,24 @@ public class User implements Parcelable { this.mComment = mComment; } - public ByteString getCommentHash() { - return mCommentHash; + public byte[] getCommentHash() { + return mCommentHash != null ? mCommentHash.toByteArray() : null; } public void setCommentHash(ByteString commentHash) { mCommentHash = commentHash; } - public Bitmap getTexture() { - return mTexture; + public byte[] getTexture() { + return mTexture.toByteArray(); } - public void setTexture(Bitmap texture) { + public void setTexture(ByteString texture) { mTexture = texture; } - public ByteString getTextureHash() { - return mTextureHash; + public byte[] getTextureHash() { + return mTextureHash != null ? mTextureHash.toByteArray() : null; } public void setTextureHash(ByteString textureHash) { @@ -277,8 +211,8 @@ public class User implements Parcelable { mLocalIgnored = localIgnored; } - public TalkState getTalkState() { - return mTalkState; + public int getTalkState() { + return mTalkState.ordinal(); // FIXME: ordinals are bad } public void setTalkState(TalkState mTalkState) { @@ -309,4 +243,9 @@ public class User implements Parcelable { public int hashCode() { return mId; } + + @Override + public int compareTo(User another) { + return getName().toLowerCase().compareTo(another.getName().toLowerCase()); + } } diff --git a/src/main/java/com/morlunk/jumble/protocol/AudioHandler.java b/src/main/java/com/morlunk/jumble/protocol/AudioHandler.java index 22003c7..cd0592a 100644 --- a/src/main/java/com/morlunk/jumble/protocol/AudioHandler.java +++ b/src/main/java/com/morlunk/jumble/protocol/AudioHandler.java @@ -112,7 +112,7 @@ public class AudioHandler extends JumbleNetworkListener implements AudioInput.Au mOutput.startPlaying(true); } catch (AudioInitializationException e) { e.printStackTrace(); - mLogger.log(Message.Type.WARNING, e.getLocalizedMessage()); + mLogger.log(e.getLocalizedMessage()); } break; case AudioManager.SCO_AUDIO_STATE_DISCONNECTED: @@ -124,7 +124,7 @@ public class AudioHandler extends JumbleNetworkListener implements AudioInput.Au mOutput.startPlaying(false); } catch (AudioInitializationException e) { e.printStackTrace(); - mLogger.log(Message.Type.WARNING, e.getLocalizedMessage()); + mLogger.log(e.getLocalizedMessage()); } mBluetoothOn = false; break; @@ -378,7 +378,7 @@ public class AudioHandler extends JumbleNetworkListener implements AudioInput.Au mBitrate = bitrate; mFramesPerPacket = framesPerPacket; - mLogger.log(Message.Type.INFO, mContext.getString(R.string.audio_max_bandwidth, + mLogger.log(mContext.getString(R.string.audio_max_bandwidth, maxBandwidth/1000, maxBandwidth/1000, framesPerPacket * 10)); } } diff --git a/src/main/java/com/morlunk/jumble/protocol/ModelHandler.java b/src/main/java/com/morlunk/jumble/protocol/ModelHandler.java index f3b83cf..7baeef2 100644 --- a/src/main/java/com/morlunk/jumble/protocol/ModelHandler.java +++ b/src/main/java/com/morlunk/jumble/protocol/ModelHandler.java @@ -30,6 +30,7 @@ import com.morlunk.jumble.model.Channel; import com.morlunk.jumble.model.Message; import com.morlunk.jumble.model.User; import com.morlunk.jumble.protobuf.Mumble; +import com.morlunk.jumble.protocol.JumbleTCPMessageListener; import com.morlunk.jumble.util.JumbleLogger; import com.morlunk.jumble.util.MessageFormatter; @@ -48,27 +49,6 @@ import java.util.Map; * Created by andrew on 18/07/13. */ public class ModelHandler extends JumbleTCPMessageListener.Stub { - - private Comparator<Integer> mChannelComparator = new Comparator<Integer>() { - @Override - public int compare(Integer lhs, Integer rhs) { - Channel clhs = getChannel(lhs); - Channel crhs = getChannel(rhs); - if(clhs.getPosition() != crhs.getPosition()) - return ((Integer)clhs.getPosition()).compareTo(crhs.getPosition()); - return clhs.getName().compareTo(crhs.getName()); - } - }; - - private Comparator<Integer> mUserComparator = new Comparator<Integer>() { - @Override - public int compare(Integer lhs, Integer rhs) { - User ulhs = mUsers.get(lhs); - User urhs = mUsers.get(rhs); - return ulhs.getName().toLowerCase().compareTo(urhs.getName().toLowerCase()); - } - }; - private final Context mContext; private final Map<Integer, Channel> mChannels; private final Map<Integer, User> mUsers; @@ -134,18 +114,9 @@ public class ModelHandler extends JumbleTCPMessageListener.Stub { */ private void changeSubchannelUsers(Channel channel, int change) { channel.setSubchannelUserCount(channel.getSubchannelUserCount() + change); - int parent = channel.getParent(); - Channel parentChannel = mChannels.get(parent); - if(parentChannel != null) - changeSubchannelUsers(parentChannel, change); - } - - /** - * Sorts the users in the provided channel alphabetically. - * @param channel The channel containing the users to sort. - */ - private void sortUsers(Channel channel) { - Collections.sort(channel.getUsers(), mUserComparator); + Channel parent = channel.getParent(); + if(parent != null) + changeSubchannelUsers(parent, change); } public void clear() { @@ -175,13 +146,12 @@ public class ModelHandler extends JumbleTCPMessageListener.Stub { channel.setPosition(msg.getPosition()); if(msg.hasParent()) { - Channel oldParent = mChannels.get(channel.getParent()); - channel.setParent(parent.getId()); - parent.addSubchannel(channel.getId()); + Channel oldParent = channel.getParent(); + channel.setParent(parent); + parent.addSubchannel(channel); changeSubchannelUsers(parent, channel.getSubchannelUserCount()); - Collections.sort(parent.getSubchannels(), mChannelComparator); // Re-sort after subchannel addition if(oldParent != null) { - oldParent.removeSubchannel(channel.getId()); + oldParent.removeSubchannel(channel); changeSubchannelUsers(oldParent, -channel.getSubchannelUserCount()); } } @@ -194,18 +164,19 @@ public class ModelHandler extends JumbleTCPMessageListener.Stub { if(msg.getLinksCount() > 0) { channel.clearLinks(); - for(int link : msg.getLinksList()) - channel.addLink(link); + for(int link : msg.getLinksList()) { + channel.addLink(mChannels.get(link)); + } } if(msg.getLinksRemoveCount() > 0) { for(int link : msg.getLinksRemoveList()) - channel.removeLink(link); + channel.removeLink(mChannels.get(link)); } if(msg.getLinksAddCount() > 0) { for(int link : msg.getLinksAddList()) - channel.addLink(link); + channel.addLink(mChannels.get(link)); } final Channel finalChannel = channel; @@ -224,9 +195,9 @@ public class ModelHandler extends JumbleTCPMessageListener.Stub { final Channel channel = mChannels.get(msg.getChannelId()); if(channel != null && channel.getId() != 0) { mChannels.remove(channel.getId()); - Channel parent = mChannels.get(channel.getParent()); + Channel parent = channel.getParent(); if(parent != null) { - parent.removeSubchannel(msg.getChannelId()); + parent.removeSubchannel(channel); changeSubchannelUsers(parent, -channel.getUsers().size()); } try { @@ -271,10 +242,9 @@ public class ModelHandler extends JumbleTCPMessageListener.Stub { // Add user to root channel by default. This works because for some reason, we don't get a channel ID when the user joins into root. Channel root = mChannels.get(0); if(root == null) root = createStubChannel(0); - user.setChannelId(0); - root.addUser(user.getSession()); + user.setChannel(root); + root.addUser(user); root.setSubchannelUserCount(root.getSubchannelUserCount()+1); - sortUsers(root); } else return; diff --git a/src/main/java/com/morlunk/jumble/util/JumbleCallbacks.java b/src/main/java/com/morlunk/jumble/util/JumbleCallbacks.java index 16c04da..404a3f5 100644 --- a/src/main/java/com/morlunk/jumble/util/JumbleCallbacks.java +++ b/src/main/java/com/morlunk/jumble/util/JumbleCallbacks.java @@ -21,9 +21,9 @@ import android.os.RemoteCallbackList; import android.os.RemoteException; import com.morlunk.jumble.IJumbleObserver; -import com.morlunk.jumble.model.Channel; -import com.morlunk.jumble.model.Message; -import com.morlunk.jumble.model.User; +import com.morlunk.jumble.model.IChannel; +import com.morlunk.jumble.model.IMessage; +import com.morlunk.jumble.model.IUser; /** * A composite wrapper around Jumble observers to easily broadcast to each observer. @@ -89,7 +89,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onChannelAdded(Channel channel) throws RemoteException { + public void onChannelAdded(IChannel channel) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -99,7 +99,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onChannelStateUpdated(Channel channel) throws RemoteException { + public void onChannelStateUpdated(IChannel channel) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -109,7 +109,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onChannelRemoved(Channel channel) throws RemoteException { + public void onChannelRemoved(IChannel channel) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -119,7 +119,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onChannelPermissionsUpdated(Channel channel) throws RemoteException { + public void onChannelPermissionsUpdated(IChannel channel) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -129,7 +129,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onUserConnected(User user) throws RemoteException { + public void onUserConnected(IUser user) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -139,7 +139,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onUserStateUpdated(User user) throws RemoteException { + public void onUserStateUpdated(IUser user) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -149,7 +149,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onUserTalkStateUpdated(User user) throws RemoteException { + public void onUserTalkStateUpdated(IUser user) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -159,7 +159,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onUserJoinedChannel(User user, Channel newChannel, Channel oldChannel) throws RemoteException { + public void onUserJoinedChannel(IUser user, IChannel newChannel, IChannel oldChannel) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -169,7 +169,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onUserRemoved(User user, String reason) throws RemoteException { + public void onUserRemoved(IUser user, String reason) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; @@ -189,7 +189,7 @@ public class JumbleCallbacks extends JumbleObserver.Stub { } @Override - public void onMessageLogged(Message message) throws RemoteException { + public void onMessageLogged(IMessage message) throws RemoteException { int i = mCallbacks.beginBroadcast(); while(i > 0) { i--; diff --git a/src/main/java/com/morlunk/jumble/util/JumbleLogger.java b/src/main/java/com/morlunk/jumble/util/JumbleLogger.java index c946e52..5570241 100644 --- a/src/main/java/com/morlunk/jumble/util/JumbleLogger.java +++ b/src/main/java/com/morlunk/jumble/util/JumbleLogger.java @@ -23,6 +23,6 @@ import com.morlunk.jumble.model.Message; * Created by andrew on 12/07/14. */ public interface JumbleLogger { - public void log(Message.Type type, String message); + public void log(String message); public void log(Message message); } diff --git a/src/main/java/com/morlunk/jumble/util/JumbleObserver.java b/src/main/java/com/morlunk/jumble/util/JumbleObserver.java index ca61e4b..92a94ed 100644 --- a/src/main/java/com/morlunk/jumble/util/JumbleObserver.java +++ b/src/main/java/com/morlunk/jumble/util/JumbleObserver.java @@ -21,6 +21,9 @@ import android.os.RemoteException; import com.morlunk.jumble.IJumbleObserver; import com.morlunk.jumble.model.Channel; +import com.morlunk.jumble.model.IChannel; +import com.morlunk.jumble.model.IMessage; +import com.morlunk.jumble.model.IUser; import com.morlunk.jumble.model.Message; import com.morlunk.jumble.model.User; @@ -50,47 +53,47 @@ public class JumbleObserver extends IJumbleObserver.Stub { } @Override - public void onChannelAdded(Channel channel) throws RemoteException { + public void onChannelAdded(IChannel channel) throws RemoteException { } @Override - public void onChannelStateUpdated(Channel channel) throws RemoteException { + public void onChannelStateUpdated(IChannel channel) throws RemoteException { } @Override - public void onChannelRemoved(Channel channel) throws RemoteException { + public void onChannelRemoved(IChannel channel) throws RemoteException { } @Override - public void onChannelPermissionsUpdated(Channel channel) throws RemoteException { + public void onChannelPermissionsUpdated(IChannel channel) throws RemoteException { } @Override - public void onUserConnected(User user) throws RemoteException { + public void onUserConnected(IUser user) throws RemoteException { } @Override - public void onUserStateUpdated(User user) throws RemoteException { + public void onUserStateUpdated(IUser user) throws RemoteException { } @Override - public void onUserTalkStateUpdated(User user) throws RemoteException { + public void onUserTalkStateUpdated(IUser user) throws RemoteException { } @Override - public void onUserJoinedChannel(User user, Channel newChannel, Channel oldChannel) throws RemoteException { + public void onUserJoinedChannel(IUser user, IChannel newChannel, IChannel oldChannel) throws RemoteException { } @Override - public void onUserRemoved(User user, String reason) throws RemoteException { + public void onUserRemoved(IUser user, String reason) throws RemoteException { } @@ -100,7 +103,7 @@ public class JumbleObserver extends IJumbleObserver.Stub { } @Override - public void onMessageLogged(Message message) throws RemoteException { + public void onMessageLogged(IMessage message) throws RemoteException { } |