/*
 * Copyright 2018 Nikita Shakarun
 *
 * 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 org.microemu.cldc.sms;

import org.microemu.microedition.io.ConnectionImplementation;

import java.io.IOException;
import java.io.InterruptedIOException;

import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;

public class Connection implements MessageConnection, ConnectionImplementation {

	private MessageListener listener;

	@Override
	public javax.microedition.io.Connection openConnection(String name, int mode, boolean timeouts) throws IOException {
		return this;
	}

	@Override
	public Message newMessage(String type) {
		return new MessageImpl(type, null);
	}

	@Override
	public Message newMessage(String type, String address) {
		return new MessageImpl(type, address);
	}

	@Override
	public int numberOfSegments(Message message) {
		return 0;
	}

	@Override
	public Message receive() throws IOException, InterruptedIOException {
		MessageImpl message = new MessageImpl(MessageConnection.TEXT_MESSAGE, "addr");
		message.setPayloadText("sms");
		return message;
	}

	@Override
	public void send(Message message) throws IOException, InterruptedIOException {
		if (listener != null) {
			Connection connection = (Connection) openConnection(null, 0, false);
			listener.notifyIncomingMessage(connection);
		}
	}

	@Override
	public void setMessageListener(MessageListener listener) throws IOException {
		this.listener = listener;
	}

	@Override
	public void close() throws IOException {
	}
}

@@ -1014,16 +1014,92 @@
 istener;
+%0A%09private String name;%0A%09private boolean noMessages;%0A%09private boolean closed;
 %0A%0A%09@Over
@@ -1221,16 +1221,36 @@
 ption %7B%0A
+%09%09this.name = name;%0A
 %09%09return
@@ -1345,19 +1345,19 @@
 (type, n
-ull
+ame
 );%0A%09%7D%0A%0A%09
@@ -1537,17 +1537,17 @@
 %09return 
-0
+1
 ;%0A%09%7D%0A%0A%09@
@@ -1626,16 +1626,156 @@
 ption %7B%0A
+%09%09while (noMessages && !closed) %7B%0A%09%09%09try %7B%0A%09%09%09%09Thread.sleep(60000);%0A%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09Messag
@@ -1842,14 +1842,12 @@
 GE, 
-%22addr%22
+name
 );%0A%09
@@ -1878,16 +1878,37 @@
 %22sms%22);%0A
+%09%09noMessages = true;%0A
 %09%09return
@@ -2094,19 +2094,19 @@
 ection(n
-ull
+ame
 , 0, fal
@@ -2290,32 +2290,32 @@
 %0A%09%7D%0A%0A%09@Override%0A
-
 %09public void clo
@@ -2340,13 +2340,30 @@
 ption %7B%0A
+%09%09closed = true;%0A
 %09%7D%0A%7D%0A

Improve SMS connection