1 package ubc.midp.mobilephoto.sms;
2
3 import java.io.IOException;
4 import java.io.InterruptedIOException;
5
6 import javax.microedition.lcdui.Alert;
7 import javax.microedition.lcdui.Command;
8 import javax.wireless.messaging.Message;
9 import javax.wireless.messaging.MessageConnection;
10
11 import ubc.midp.mobilephoto.core.ui.MainUIMidlet;
12 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData;
13 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen;
14
15
16 /**
17 *
18 */
19 public class SmsReceiverThread implements Runnable { //extends BaseThread {
20
21 SmsReceiverController controller = null;
22
23
24 String[] connections; //Connections detected at start up.
25 String smsPort; //The port on which we listen for SMS messages
26 MessageConnection smsconn; //SMS message connection for inbound text messages.
27 Message msg; //Current message read from the network.
28 String senderAddress; //Address of the message's sender
29
30 Command acceptPhotoCommand = new Command("Accept Photo", Command.ITEM, 1);
31 Command rejectPhotoCommand = new Command("Reject Photo", Command.ITEM, 1);
32
33 Command errorNotice = new Command("Ok", Command.ITEM, 1);
34
35 /**
36 * Initialize the MIDlet with the current display object and graphical
37 * components.
38 */
39 public SmsReceiverThread(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen, SmsReceiverController controller) {
40 this.controller = controller;
41 //smsPort = getAppProperty("SMS-Port");
42 smsPort = "1000"; //getAppProperty("SMS-Port");
43 }
44
45 /**
46 * Initialize the MIDlet with the current display object and graphical
47 * components.
48 *
49 * Pass in the controller so we can notify it when a photo/message is received via SMS
50 */
51
52 /** Message reading thread. */
53 public void run() {
54 SmsMessaging smsMessenger = new SmsMessaging();
55
56 while(true){
57 System.out.println("Starting SMSReceiver::run()");
58
59 smsMessenger.setSmsReceivePort(smsPort);
60 byte[] receivedData = null;
61 try {
62 receivedData = smsMessenger.receiveImage();
63 } catch (InterruptedIOException e) {
64 Alert alert = new Alert("Error Incoming Photo");
65 alert.setString("" +
66 "You have just received a bad fragmentated photo which was not possible to recovery.");
67 alert.addCommand(errorNotice);
68 System.out.println("Error interreput");
69 alert.setCommandListener(controller);
70 controller.setCurrentScreen(alert);
71 smsMessenger.cleanUpReceiverConnections();
72 continue;
73
74 } catch (IOException e) {
75 Alert alert = new Alert("Error Incoming Photo");
76 alert.setString("" +
77 "You have just received a bad fragmentated photo which was not possible to recovery.");
78 alert.addCommand(errorNotice);
79 System.out.println("Bad fragmentation");
80 alert.setCommandListener(controller);
81 controller.setCurrentScreen(alert);
82 smsMessenger.cleanUpReceiverConnections();
83 continue;
84 }
85
86 System.out.println("BEFORE ALERT CODE");
87 Alert alert = new Alert("New Incoming Photo");
88 alert.setString("A MobilePhoto user has sent you a Photo. Do you want to accept it?");
89 alert.addCommand(acceptPhotoCommand);
90 alert.addCommand(rejectPhotoCommand);
91 controller.setIncommingData(receivedData);
92 alert.setCommandListener(controller);
93
94 controller.setCurrentScreen(alert);
95
96 System.out.println("Finishing SMSReceiver run()");
97 }
98
99 }
100 }
|