Java tutorial
/* * Copyright 2014 Alban GRUIN * * This file is part of SenqmAndroid. * * SenqmAndroid 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. * * SenqmAndroid 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 SenqmAndroid. If not, see <http://www.gnu.org/licenses/>. */ package com.agrn.senqm.network; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.IBinder; import com.agrn.senqm.network.service.ConnectionManagerService; import com.agrn.senqm.network.service.ConnectionManagerServiceBoundListener; import com.agrn.senqm.network.service.ConnectionManagerServiceConnection; import org.json.JSONException; import org.json.JSONStringer; import java.util.HashMap; import java.util.Set; public class Message { private MessageContent content; private HashMap<String, Object> values; public Message() { } public Message(String content) { setContent(content); } public void addValue(String key, Object value) { if (!values.containsKey(key) && !key.equals("content")) values.put(key, value); } public MessageContent getContent() { return content; } public void removeValue(String key) { if (values.containsKey(key)) values.remove(key); } public void send(Context context) { if (!ConnectionManagerService.isRunning()) return; LocalServiceConnection serviceConnection = new LocalServiceConnection(context); Intent intent = new Intent(context, ConnectionManagerService.class); context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); } public void setContent(MessageContent content) { this.content = content; } public void setContent(String content) { this.content = MessageContent.fromString(content); } @Override public String toString() { JSONStringer stringer = new JSONStringer(); Set<String> keyValues = values.keySet(); try { stringer.object().key("content").value(content.toString()); for (String key : keyValues) { if (!key.equals("content")) stringer.key(key).value(values.get(key)); } return stringer.endObject().toString(); } catch (JSONException e) { return "{'content': '".concat(content.toString()).concat("'}"); } } public static enum MessageContent { CALL("call"), HOSTNAME("hostname"), LOGIN("login"), REGISTER("register"), SENDMESSAGE("send"), UNKNOWN( "unknown"); private String value; private MessageContent(String value) { this.value = value; } public static MessageContent fromString(String key) { if (key == null) return UNKNOWN; for (MessageContent messageContent : MessageContent.values()) { if (key.equalsIgnoreCase(messageContent.toString())) return messageContent; } return UNKNOWN; } @Override public String toString() { return value; } } private class LocalServiceConnection extends ConnectionManagerServiceConnection implements ConnectionManagerServiceBoundListener { private Context context; public LocalServiceConnection(Context context) { this.context = context; setListener(this); } @Override public void onServiceBound(ConnectionManagerService service) { service.broadcastMessage(Message.this); context.unbindService(this); } } }