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