diff options
author | Andrew Comminos <andrewcomminos@gmail.com> | 2013-12-06 10:47:22 +0400 |
---|---|---|
committer | Andrew Comminos <andrewcomminos@gmail.com> | 2013-12-06 10:47:22 +0400 |
commit | 2d851e3951caada8a6e5375760fd9c7849777eaf (patch) | |
tree | e57965e7c6bf7367962ce827842987c9baf93cf3 /src/main/java | |
parent | 0e549c360c3bea4d335c97cdd10a939816e145f7 (diff) |
Implemented new message framework for more client customizability.
Diffstat (limited to 'src/main/java')
5 files changed, 158 insertions, 62 deletions
diff --git a/src/main/java/com/morlunk/jumble/JumbleService.java b/src/main/java/com/morlunk/jumble/JumbleService.java index 85d30ee..d73d71d 100644 --- a/src/main/java/com/morlunk/jumble/JumbleService.java +++ b/src/main/java/com/morlunk/jumble/JumbleService.java @@ -29,6 +29,7 @@ import android.util.Log; import com.morlunk.jumble.audio.AudioInput; import com.morlunk.jumble.audio.AudioOutput; import com.morlunk.jumble.model.Channel; +import com.morlunk.jumble.model.Message; import com.morlunk.jumble.model.Server; import com.morlunk.jumble.model.User; import com.morlunk.jumble.protocol.ChannelHandler; @@ -127,10 +128,6 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon } }; - // Logging - private List<String> mLogHistory = new ArrayList<String>(); - private SimpleDateFormat mChatDateFormat = new SimpleDateFormat("[h:mm a] "); - private int mPermissions; private RemoteCallbackList<IJumbleObserver> mObservers = new RemoteCallbackList<IJumbleObserver>(); @@ -233,11 +230,6 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon } @Override - public List getLogHistory() throws RemoteException { - return mLogHistory; - } - - @Override public int getPermissions() throws RemoteException { return mPermissions; } @@ -370,11 +362,6 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon tmb.addSession(session); tmb.setMessage(message); mConnection.sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); - - // Log message to chat - User target = getUser(session); - String formattedMessage = getString(R.string.chat_message_to, MessageFormatter.highlightString(target.getName()), message); - logMessage(getSessionUser(), formattedMessage); } @Override @@ -386,11 +373,6 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon tmb.addChannelId(channel); tmb.setMessage(message); mConnection.sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); - - // Log message to chat - Channel target = getChannel(channel); - String formattedMessage = getString(R.string.chat_message_to, MessageFormatter.highlightString(target.getName()), message); - logMessage(getSessionUser(), formattedMessage); } @Override @@ -508,7 +490,6 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon return; } - mLogHistory.clear(); mPermissions = 0; mChannelHandler = new ChannelHandler(this); @@ -614,15 +595,14 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon /** * Logs a warning message to the client. - * @param warning An HTML warning string to be messagxz ed to the client. + * @param warning An HTML warning string to be messaged to the client. */ public void logWarning(final String warning) { - Log.w(Constants.TAG, warning); - mLogHistory.add(warning); + final Message message = new Message(Message.Type.WARNING, warning); notifyObservers(new ObserverRunnable() { @Override public void run(IJumbleObserver observer) throws RemoteException { - observer.onLogWarning(warning); + observer.onMessageLogged(message); } }); } @@ -635,29 +615,24 @@ public class JumbleService extends Service implements JumbleConnection.JumbleCon if(!mConnection.isSynchronized()) return; // Don't log messages while synchronizing. - final String formatInfo = mChatDateFormat.format(new Date())+info; - Log.v(Constants.TAG, formatInfo); - mLogHistory.add(formatInfo); + final Message message = new Message(Message.Type.INFO, info); notifyObservers(new ObserverRunnable() { @Override public void run(IJumbleObserver observer) throws RemoteException { - observer.onLogInfo(formatInfo); + observer.onMessageLogged(message); } }); } /** - * Logs a text message to the client. - * @param user The user that sent the message. - * @param message An HTML message to send to the client. + * Logs a message to the client. + * @param message A message to log to the client. */ - public void logMessage(final User user, final String message) { - final String formatMessage = mChatDateFormat.format(new Date())+message; - mLogHistory.add(formatMessage); + public void logMessage(final Message message) { notifyObservers(new ObserverRunnable() { @Override public void run(IJumbleObserver observer) throws RemoteException { - observer.onMessageReceived(user, formatMessage); + observer.onMessageLogged(message); } }); } diff --git a/src/main/java/com/morlunk/jumble/model/Message.java b/src/main/java/com/morlunk/jumble/model/Message.java new file mode 100644 index 0000000..40f7ee1 --- /dev/null +++ b/src/main/java/com/morlunk/jumble/model/Message.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2013 Andrew Comminos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.morlunk.jumble.model; + +import android.os.Parcel; +import android.os.Parcelable; +import android.text.format.Time; + +import java.util.List; + +/** + * 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; + private List<Integer> mChannelIds; + private List<Integer> mTreeIds; + private List<Integer> mUserIds; + private String mMessage; + private Time mReceivedTime; + + public Message() { + + } + + public Message(Message.Type messageType, String message) { + mType = messageType; + mMessage = message; + mReceivedTime = new Time(); + mReceivedTime.setToNow(); + } + + public Message(int actor, List<Integer> channelIds, List<Integer> treeIds, List<Integer> userIds, String message) { + this(Type.TEXT_MESSAGE, message); + mActor = actor; + mChannelIds = channelIds; + mTreeIds = treeIds; + mUserIds = userIds; + } + + public Message(Parcel parcel) { + readFromParcel(parcel); + } + + public void readFromParcel(Parcel in) { + mType = Type.values()[in.readInt()]; + mActor = in.readInt(); + mChannelIds = in.readArrayList(null); + mTreeIds = in.readArrayList(null); + mUserIds = in.readArrayList(null); + mMessage = in.readString(); + mReceivedTime = new Time(); + mReceivedTime.set(in.readLong()); + } + + public int getActor() { + return mActor; + } + + public List<Integer> getChannelIds() { + return mChannelIds; + } + + public List<Integer> getTreeIds() { + return mTreeIds; + } + + public List<Integer> getUserIds() { + return mUserIds; + } + + public String getMessage() { + return mMessage; + } + + public Time 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.writeList(mChannelIds); + dest.writeList(mTreeIds); + dest.writeList(mUserIds); + dest.writeString(mMessage); + dest.writeLong(mReceivedTime.toMillis(false)); + } +} diff --git a/src/main/java/com/morlunk/jumble/net/JumbleObserver.java b/src/main/java/com/morlunk/jumble/net/JumbleObserver.java index d328c2f..f0f73bc 100644 --- a/src/main/java/com/morlunk/jumble/net/JumbleObserver.java +++ b/src/main/java/com/morlunk/jumble/net/JumbleObserver.java @@ -20,6 +20,7 @@ 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; /** @@ -93,17 +94,8 @@ public class JumbleObserver extends IJumbleObserver.Stub { } @Override - public void onLogInfo(String message) throws RemoteException { + public void onMessageLogged(Message message) throws RemoteException { } - @Override - public void onLogWarning(String message) throws RemoteException { - - } - - @Override - public void onMessageReceived(User user, String message) throws RemoteException { - - } } diff --git a/src/main/java/com/morlunk/jumble/protobuf/Mumble.java b/src/main/java/com/morlunk/jumble/protobuf/Mumble.java index 9f933ee..12b8507 100644 --- a/src/main/java/com/morlunk/jumble/protobuf/Mumble.java +++ b/src/main/java/com/morlunk/jumble/protobuf/Mumble.java @@ -12606,12 +12606,12 @@ public final class Mumble { getMessageBytes(); } /** - * Protobuf type {@code MumbleProto.TextMessage} + * Protobuf type {@code MumbleProto.Message} */ public static final class TextMessage extends com.google.protobuf.GeneratedMessage implements TextMessageOrBuilder { - // Use TextMessage.newBuilder() to construct. + // Use Message.newBuilder() to construct. private TextMessage(com.google.protobuf.GeneratedMessage.Builder<?> builder) { super(builder); this.unknownFields = builder.getUnknownFields(); @@ -13067,7 +13067,7 @@ public final class Mumble { return builder; } /** - * Protobuf type {@code MumbleProto.TextMessage} + * Protobuf type {@code MumbleProto.Message} */ public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder> @@ -13084,7 +13084,7 @@ public final class Mumble { com.morlunk.jumble.protobuf.Mumble.TextMessage.class, com.morlunk.jumble.protobuf.Mumble.TextMessage.Builder.class); } - // Construct using com.morlunk.jumble.protobuf.Mumble.TextMessage.newBuilder() + // Construct using com.morlunk.jumble.protobuf.Mumble.Message.newBuilder() private Builder() { maybeForceBuilderInitialization(); } @@ -13555,7 +13555,7 @@ public final class Mumble { return this; } - // @@protoc_insertion_point(builder_scope:MumbleProto.TextMessage) + // @@protoc_insertion_point(builder_scope:MumbleProto.Message) } static { @@ -13563,7 +13563,7 @@ public final class Mumble { defaultInstance.initFields(); } - // @@protoc_insertion_point(class_scope:MumbleProto.TextMessage) + // @@protoc_insertion_point(class_scope:MumbleProto.Message) } public interface PermissionDeniedOrBuilder diff --git a/src/main/java/com/morlunk/jumble/protocol/TextMessageHandler.java b/src/main/java/com/morlunk/jumble/protocol/TextMessageHandler.java index 97db785..8a80360 100644 --- a/src/main/java/com/morlunk/jumble/protocol/TextMessageHandler.java +++ b/src/main/java/com/morlunk/jumble/protocol/TextMessageHandler.java @@ -17,6 +17,7 @@ package com.morlunk.jumble.protocol; import com.morlunk.jumble.JumbleService; +import com.morlunk.jumble.model.Message; import com.morlunk.jumble.model.User; import com.morlunk.jumble.protobuf.Mumble; import com.morlunk.jumble.util.MessageFormatter; @@ -33,21 +34,12 @@ public class TextMessageHandler extends ProtocolHandler { @Override public void messageTextMessage(Mumble.TextMessage msg) { - // TODO format user colors User sender = getService().getUserHandler().getUser(msg.getActor()); if(sender != null && sender.isLocalIgnored()) return; - // TODO use more localized strings here - String senderName = sender != null ? MessageFormatter.highlightString(sender.getName()) : "Server"; - String senderTarget = ""; - - if(msg.getTreeIdCount() > 0) - senderTarget = "(Tree) "; - else if(msg.getChannelIdCount() > 0) - senderTarget = "(Channel) "; - - getService().logMessage(sender, String.format("%s%s: %s", senderTarget, senderName, msg.getMessage())); + Message message = new Message(msg.getActor(), msg.getChannelIdList(), msg.getTreeIdList(), msg.getSessionList(), msg.getMessage()); + getService().logMessage(message); } } |