This is my ProcessMessage




/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package midletehs8sms; //----------------------------------------------------------------------------- // Extract_Incoming_Message_Format(String string) -> //----------------------------------------------------------------------------- // Extract all information contained in the sms: // Input will look somthing like this: // at+cmgr=index (given by cmti: +CMTI: "MT",15) mt is prefered message store // +CMGR: "REC UNREAD","+4526932050", // ,"15/05/03,13:37:58+08",145,0,0,0,"+4540390965",145,2 // <CR> // Message <- The message // <CR> // 0 //----------------------------------------------------------------------------- // Process_Incommig_Messages(String string) -> //----------------------------------------------------------------------------- // Split the message into readable items //----------------------------------------------------------------------------- public class Process_Message { /*****************************************************************************/ /*****************************************************************************/ public MidletEHS8SMS parent; public Notify_Incoming_Messages nim; public Reader reader; public Subrutine subrutine; public String_Manipulation sm; //--------------------------------------------------------- protected boolean exception = false; // exception = true; protected boolean exception_bool = false; protected boolean exception_in_query_ok = false; protected boolean message_read_OK = false; protected boolean deleted_message_ok = false; protected boolean breake = false; // breake = true; protected boolean boolean_incommig_messages = false; //--------------------------------------------------------- //AT+CSDH Show SMS text mode parameters public int CSDH = 0; //--------------------------------------------------------- //ATV_X Result code format mode protected String ATV_X = null;//0 or 1 protected String response = null; protected String get_the_response_code_as_a_string = null; protected String get_the_response_code = null; public String str_temp = null; //--------------------------------------------------------- protected String string_date = null; protected String string_time_stamp = null; protected String text_message = null; protected String[] arr = new String[128]; protected String[] event_array = new String[0]; protected String message_read = null; protected String [] extract_values = null; protected String [] array = null; //--------------------------------------------------------- protected String header = null; protected String substring_read = null; protected String[] splitted_header; protected String[] sms_array_read; protected String message = null; //--------------------------------------------------------- protected int sms_position_in_memory = 0; String [][] memory = null; protected int [] memory_1 = new int[2];; protected int [] memory_2 = new int[2];; protected int [] memory_3 = new int[2];; //--------------------------------------------------------- protected String memory_storage_1 = null; protected int memory_1_available = 0; protected int memory_1_in_use = 0; //--------------------------------------------------------- protected String memory_storage_2 = null; protected int memory_2_available = 0; protected int memory_2_in_use = 0; //--------------------------------------------------------- protected String memory_storage_3 = null; protected int memory_3_available = 0; protected int memory_3_in_use = 0; /*---------------------------------------------------------------------------*/ // Exporting non-public type through public API // Remember to declare all classes "PUBLIC" /*---------------------------------------------------------------------------*/ public void Process_Message( MidletEHS8SMS parent // ,File_Connections fc // Monitor mon ,Notify_Incoming_Messages nim ,Reader reader ,Subrutine subrutine ,String_Manipulation sm ){ //--------------------------------------------------------- try{ // this.fc = fc; // this.mon = mon; this.parent = parent; this.nim = nim; // this.pm = pm; this.reader = reader; this.subrutine = subrutine; this.sm = sm; } catch (Exception e) { breake = true; System.out.println("Exception in Process_Message(this)\n" + e); } //--------------------------------------------------------- // System.out.println("Process_Message Begin"); //----------------------------------------------------------------------------- }//End of "public void Process_Message(...)" /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ // System.out.println(""); /*****************************************************************************/ /****************** Extract_Incoming_Message_Format **************************/ /*****************************************************************************/ public String Extract_Incoming_Message_Format(String Event_Incomming_Sms) { // System.out.println("***** Extract_Incoming_Message_Format ****"); System.out.println("***** Read_SMS begin ****"); /*---------------------------------------------------------------------------*/ // Remember, this subroutine is called from ATEvent() // The event return with this line is: // ATEvent parsed to Reader. // From within Reader: event_read = +CMTI: "SM",1 // +CMTI: "MT",15 where the digit represent the number where the newly // arrived SMS is stored. // So by splitting up this information with: // Split_String_Into_Array( ',' , sms), we get the position from event_array[1] /*---------------------------------------------------------------------------*/ event_array = new String[0]; message_read = new String(); sms_position_in_memory = 0; /*---------------------------------------------------------------------------*/ // create an instance of ATCommand try { //----------------------------------------------------------------------------- /* if(parent.atcommand != null){ System.out.println("Extract_Incoming_Message_Format: To be continued(*1*)");} /*---------------------------------------------------------------------------*/ try { get_the_response_code_as_a_string = parent.SendCommand("ATV1\r"); System.out.println("ATV1 = " + get_the_response_code_as_a_string); // switch to the text mode get_the_response_code_as_a_string = parent.SendCommand("at+cmgf=1\r"); System.out.println("at+cmgf=1 = " + get_the_response_code_as_a_string); // activate the SMS URCs // SMS Event Reporting Configuration get_the_response_code_as_a_string = parent.SendCommand("at+cnmi=2,1\r"); System.out.println("at+cnmi=2,1 = " + get_the_response_code_as_a_string); // Show SMS text mode parameters get_the_response_code_as_a_string = parent.SendCommand("AT+CSDH=1\r"); System.out.println("AT+CSDH=1 = " + get_the_response_code_as_a_string); System.out.println("Extract_Incoming_Message_Format: To be continued(*2*)"); get_the_response_code_as_a_string = parent.SendCommand("AT&W\r"); //----------------------------------------------------------------------------- } catch (Exception e) { System.out.println("Exception in ProcessMessage(Line 200)\n" + e); return null; } //----------------------------------------------------------------------------- try{ event_array = sm.Split_String_Into_Array( ',' , Event_Incomming_Sms.trim()); System.out.println("event_array.lenght = " + event_array.length); sms_position_in_memory = Integer.parseInt(String.valueOf(event_array[1].trim())); System.out.println("Array[1]= " + event_array[1]); } catch (NumberFormatException e) { System.out.println("Exception in String_Manipulation: " + e); return null; } /*---------------------------------------------------------------------------*/ // get the SMS content message = new String(); //----------------------------------------------------------------------------- // Now we can read the message from the index position extracted from // event_array[1] wich is the same as "sms_position_in_memory" and an Integer type; //----------------------------------------------------------------------------- message_read = parent.SendCommand("AT+CMGR = " + sms_position_in_memory + "\r"); message_read_OK = subrutine.Query_OK(message_read.trim()); //----------------------------------------------------------------------------- // System.out.println("New Message read = " + message_read_OK ); //============================================================================= if( message_read_OK ) { //============================================================================= message_read = sm.GetRidOfSomeChar('"', message_read.trim()); /*****************************************************************************/ /*****************************************************************************/ // /*****************************************************************************/ /*****************************************************************************/ //----------------------------------------------------------------------------- // Input will look somthing like this: // +CMGR: "REC UNREAD","+4526932050", // ,"15/05/03,13:37:58+08",145,0,0,0,"+4540390965",145,2 // <CR> // Message <- The message // <CR> // 0 //----------------------------------------------------------------------------- if (message_read != null && message_read.length() > 0){ // First remove "+CMGR: " substring_read = sm.Get_Substring(':', message_read); System.out.println("SubString = " + substring_read); } //============================================================================= if (substring_read != null && substring_read.length() > 0) { //============================================================================= // Second remove <CR> sms_array_read = sm.Split_String_Into_Array( '\r' , substring_read); /*---------------------------------------------------------------------------*/ // Then split up header in order to extract information if( sms_array_read[0] != null){ header = String.valueOf( sms_array_read[0] ); System.out.println("Extract Header from incoming Message -> \n" + header); splitted_header = sm.Split_String_Into_Array( ',' , header); /*---------------------------------------------------------------------------*/ // splitted_header[0] = REC UNREAD //----------------------------------------------------------------------------- // Here pm the message parent.phone_number = splitted_header[1].toString().trim(); parent.phone_number_incoming = splitted_header[1].toString().trim(); System.out.println("Message phone_number: " + parent.phone_number); //----------------------------------------------------------------------------- // splitted_header[2] = is empty //----------------------------------------------------------------------------- string_date = splitted_header[3].toString().trim(); System.out.println("Message date: " + string_date); //----------------------------------------------------------------------------- string_time_stamp = splitted_header[4].toString().trim(); System.out.println("Message time: " + string_time_stamp); } //----------------------------------------------------------------------------- // sms_array_read[0] is the head extracted above //----------------------------------------------------------------------------- if(sms_array_read[1] != null){ text_message = sms_array_read[1].toString().trim(); // System.out.println("Message body(IF) = \n" + (text_message)); }else{ text_message = "null pointer"; // System.out.println("Message body(ELSE) = \n" + (text_message)); return null; } //----------------------------------------------------------------------------- text_message = text_message.trim(); /*---------------------------------------------------------------------------*/ /*****************************************************************************/ /*****************************************************************************/ // System.out.println(""); /*****************************************************************************/ /*****************************************************************************/ String GSM2Java_text_message = null; GSM2Java_text_message = sm.Convert_GSM_to_Java(text_message); System.out.println("Input:\n"+text_message); System.out.println("Output:\n" + GSM2Java_text_message); /*---------------------------------------------------------------------------*/ /*****************************************************************************/ /*****************************************************************************/ // System.out.println(""); /*****************************************************************************/ /*****************************************************************************/ // Now we Process the Message boolean_incommig_messages = Process_Incommig_Messages(GSM2Java_text_message.trim()); // System.out.println("Return from Process Message: "+ bool); //----------------------------------------------------------------------------- //============================================================================= }//if (substring_read != null && substring_read.length() > 0) //============================================================================= }else{ System.out.println("exception_in_extract_incoming_message_format = true"); return null; } //============================================================================= System.out.println("***** Extrac Incoming Messages: END *****"); //----------------------------------------------------------------------------- } catch (Exception e) { System.out.println("Exception in ProcessMessage(Line 334)\n" + e); return null; } /*****************************************************************************/ return null; /*****************************************************************************/ } /*****************************************************************************/ /*****************************************************************************/ /**************** THIS THE END OF ***************************/ /***************** Extract Incoming Message Format ***************************/ /*****************************************************************************/ /*****************************************************************************/ /* /* /* /* /*****************************************************************************/ // >>>>>>>>>>>>>> Split the message into readable items <<<<<<<<<<<<<<<<<<<< /*****************************************************************************/ public boolean Process_Incommig_Messages(String message_to_process) { //----------------------------------------------------------------------------- str_temp = new String(); if( message_to_process != null ) str_temp = message_to_process.trim().toUpperCase(); else return false; System.out.println("************ Process_Incommig_Messages ************"); System.out.println("************ Read_SMS begin ***********************"); System.out.println("************ " +parent.midlet_name+ " ***********************"); //----------------------------------------------------------------------------- // System.out.println(""); //----------------------------------------------------------------------------- // try{ /* ========================================================================= */ /* ========================================================================= */ // System.out.println("\nNo more hanky panky"); /* ========================================================================= */ /* ========================================================================= */ if( ( parent.phone_number != null) // Prevent NullPointerException && ( message_to_process.length() > 0 ) && ( subrutine.isValidPhoneNumber(parent.phone_number) ) // && ( phone_number.indexOf("26932050") != -1 ) // only my phone ) //----------------------------------------------------------------------------- { //----------------------------------------------------------------------------- if( message_to_process.indexOf("Exit") != -1 ) { Exit(message_to_process); }else //----------------------------------------------------------------------------- if( message_to_process.indexOf("Restart") != -1 || message_to_process.indexOf("Reset") != -1 ) { System.out.println("Restarting"); parent.Send_SMS("Restarting", parent.phone_number); //----------------------------------------------------------------------------- try{ parent.SendCommand("AT+CFUN=1,1\r"); } catch (Exception m) { return false; } //----------------------------------------------------------------------------- }else //----------------------------------------------------------------------------- if( message_to_process.indexOf("Delete") != -1 ) { System.out.println("Trying to delete all messages"); //----------------------------------------------------------------------------- try{ parent.SendCommand("AT+CMGD=1,4\r");//Delete all }catch (Exception e){ System.out.println("Exception in deleting all sms's"); } //----------------------------------------------------------------------------- }else //----------------------------------------------------------------------------- if( message_to_process.indexOf("Ping") != -1 ) { try{ System.out.println("Trying to Ping"); subrutine.Send_Back("I am alive", parent.phone_number); } catch (Exception m) { System.out.println("Exception in delete"); return false; } }else //----------------------------------------------------------------------------- //============================================================================= str_temp = new String(); str_temp = message_to_process.toUpperCase().trim(); message_to_process = new String(); message_to_process = str_temp; if( message_to_process.toUpperCase().indexOf("AT".trim() ) != -1) { try{ //----------------------------------------------------------------------------- if( (message_to_process.indexOf("SHD".trim()) !=-1)|| (message_to_process.indexOf("Sluk".trim()) !=-1) ){ // Switch Off EHS8 parent.SendCommand("AT^SMSO\r"); } //----------------------------------------------------------------------------- response = parent.SendCommand(message_to_process.toUpperCase().trim() + "\r"); System.out.println("" + "Result running AT-COMMAND(" + message_to_process.toUpperCase().trim() + ") = " + response); // Send_Back(response, phone_number); //----------------------------------------------------------------------------- }catch (Exception m){ System.out.println("Exception in running AT-COMMAND"); return false; } //----------------------------------------------------------------------------- }else if( message_to_process.toUpperCase().startsWith("AT".trim())) { try{ response = parent.SendCommand(message_to_process+"\r"); System.out.println("" + "Result running AT-COMMAND(" + message_to_process.toUpperCase().trim() + ") = " + response); }catch (Exception m){ return false; } }//End of message containing AT-command if(message_to_process.indexOf("SHD".trim()) !=-1){ // Switch Off EHS8 try{ parent.SendCommand("AT^SMSO\r"); }catch (Exception m){ return false; } }//if.. //----------------------------------------------------------------------------- //============================================================================= }// if(a lot . . . ) else try{ subrutine.Send_Back("Sms fail", parent.phone_number_incoming); }catch (Exception m){ return false; } //----------------------------------------------------------------------------- /*****************************************************************************/ return true; /*****************************************************************************/ } // Process_Incommig_Messages() end's here /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ // System.out.println(""); /*****************************************************************************/ /*****************************************************************************/ public void Exit(String todo){ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - System.out.println("Trying to Exit!"); //----------------------------------------------------------------------------- String midlet = parent.midlet_name; System.out.println("MIDlet-Name = " + midlet); String toarr[] = sm.Split_String_Into_Array('\r', todo); if( (toarr.length >1) && ( midlet.equalsIgnoreCase(toarr[0].trim()) && (toarr[1].indexOf("Delete") != -1) ) || ( midlet.equalsIgnoreCase(toarr[1].trim()) && (toarr[0].indexOf("Delete") != -1) ) ){ //----------------------------------------------------------------------------- try{ parent.SendCommand("AT+CMGD=1,4\r");//Delete all }catch (Exception e){ System.out.println("Exception in deleting all sms's"); } //----------------------------------------------------------------------------- parent.destroyApp(true); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } /*****************************************************************************/ /*****************************************************************************/ // System.out.println(""); /*****************************************************************************/ /*****************************************************************************/ public void nn(){ try{ }catch (Exception e) { System.out.println(""); } } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ }// public class Process_Message End /*****************************************************************************/ /*****************************************************************************/