

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.wireless.messaging.*;
import java.io.IOException;
import javax.microedition.io.*;


public class sms1 extends MIDlet implements CommandListener {
    // display manager
    Display display = null;

    // a menu with items
    List menu = null; // main menu

    // list of choices
    List choose = null;

   
    TextField input = null;
     TextField textbox;
     TextField Password;
     TextField Number;
     TextField textbox2;
    // ticker
    Ticker ticker = new Ticker("Sms Encryption");

    // form
    Form form = new Form("Form");

    // command
    static final Command backCommand = new Command("Back", Command.BACK, 0);
    static final Command sendCommand = new Command("Send", Command.OK, 1);
    static final Command exitCommand = new Command("Exit", Command.STOP, 2);
    static final Command send = new Command("Send", Command.SCREEN, 2);
     static final Command exit = new Command("Exit", Command.SCREEN, 2);
     static final Command encrypt1 = new Command ("Encrypt",Command.OK,2);
    String currentMenu = null;

    // constructor.
    public sms1() {
    }

    /**
     * Start the MIDlet by creating a list of items and associating the
     * exit command with it.
     */
    public void startApp() throws MIDletStateChangeException {
      display = Display.getDisplay(this);
      // open a db stock file

      menu = new List("Messages", Choice.IMPLICIT);
      menu.append("New Message", null);
      menu.append("Inbox", null);
      menu.append("Drafts", null);
      menu.append("Sent", null);
      menu.addCommand(exitCommand);
      menu.setCommandListener(this);
      menu.setTicker(ticker);

      mainMenu();
    }

    public void pauseApp() {
      display = null;
      choose = null;
      menu = null;
      ticker = null;
      form = null;
      input = null;

    }

    public void destroyApp(boolean unconditional) {
      notifyDestroyed();
    }

    // main menu
    void mainMenu() {
      display.setCurrent(menu);
      currentMenu = "Main";
    }

    /**
     * Test the TextBox component.
     */
    public void NewMessage() {
        form = new Form("");
        Password=new TextField("Password:","",10,TextField.PASSWORD);
        Number=new TextField("Number:","",10,TextField.PHONENUMBER);
       input = new TextField("Enter Some Text:", "", 160, TextField.ANY);
      form.setTicker(new Ticker(" Text message"));
      form.append(Password);
      form.append(Number);
      form.append(input);
      input.setString("");
      form.addCommand(backCommand);
      form.addCommand(send);
      form.addCommand(encrypt1);
      form.addCommand(exit);
      form.setCommandListener(this);
      display.setCurrent(form);
      currentMenu = "input";
    }

   
    public void Inbox() {

        form=new Form("");
        form.setTicker(new Ticker("Received messages"));
        form.addCommand(backCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
        currentMenu = "list";
   }

 
   public void Drafts() {
        form=new Form("");
        form.addCommand(backCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
        currentMenu = "drafts";

   }

   public void Sent() {

      Form f = new Form("");
       f.setTicker(new Ticker("Messages that have been sent"));
       f.append(textbox2);
      f.addCommand(backCommand);

      f.setCommandListener(this);
      display.setCurrent(f);
      currentMenu = "date";
   }

   

   /**
    * Handle events.
    */
   public void commandAction(Command c, Displayable d) {
      String label = c.getLabel();
      if (label.equals("Exit")) {
         destroyApp(true);
      } else if (label.equals("Back")) {
          if(currentMenu.equals("list") || currentMenu.equals("input") ||
               currentMenu.equals("date") || currentMenu.equals("drafts")) {
            // go back to menu
            mainMenu();
          }

      if (label.equals("send")){
          
          run();

}


      } else {
         List down = (List)display.getCurrent();
         switch(down.getSelectedIndex()) {
           case 0: NewMessage();break;
           case 1: Inbox();break;
           case 2: Drafts();break;
           case 3: Sent();break;

         }

      }
  }
    public void run(){
     double jumlah = input.getString().length();
     ticker.setString(Double.toString(jumlah));
     String s = "sms://"+Number.getString()+":50000";
     MessageConnection address = null;
                try    {
            
                    address = (MessageConnection)Connector.open(s);
                    try{
                    TextMessage tsms = (TextMessage)address.newMessage(MessageConnection.TEXT_MESSAGE);
                    tsms.setAddress(s);
                    tsms.setPayloadText(textbox2.getString());
                    address.send(tsms);
                    }
                    finally {
                    address.close();
                    }
                }
                catch (Throwable t) {
                System.out.println("run");
                t.printStackTrace();
                }


                if (address != null) {
            try {
                address.close();
            } catch (IOException ioe) {
                System.out.println("End call ");
                ioe.printStackTrace();
            }
        }

    }
}
