/*
 * OpenCellIdTest.java
 *
 * Created on 13 juin 2008, 00:17
 *  Thomas LANDSPURG
 * Check http://blog.8motions.com and http://www.opencellid.org for
 * more info and tutorials.
 */

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class OpenCellIdTest extends MIDlet implements  CommandListener , Runnable {
    Display m_disp;
    TextField cellInfo=new TextField("cellInfo:","",200,TextField.UNEDITABLE);
    TextField posInfo=new TextField("pos:","",200,TextField.UNEDITABLE);
    String url;
    Form test;
    public void startApp() {
        m_disp=Display.getDisplay(this);
        test=new Form("OpenIDSample");
        test.append(cellInfo);
        test.append(posInfo);
        test.addCommand(new Command("LocateMe",Command.OK,0));
        test.addCommand(new Command("Exit",Command.EXIT,0));
        test.setCommandListener(this);
        m_disp.setCurrent(test);
    }
    public void run(){
        try {
            HttpConnection cnx = (HttpConnection)Connector.open(url);
            InputStream is=cnx.openInputStream();
            StringBuffer b=new StringBuffer();
            int car;
            while( (car=is.read())!= -1){
                b.append((char)car);
            }
            is.close();
            cnx.close();
            String res=b.toString();
            if(res.startsWith("err")){
                posInfo.setString("Cell not found!");
            }else{
                int pos=res.indexOf(',');
                String lat=res.substring(0,pos);
                int pos2=res.indexOf(',',pos+1);
                String lon=res.substring(pos+1,pos2);
                posInfo.setString(lat+" "+lon);
//                String url="http://maps.google.com/staticmap?center="+lat+","+lon+"&zoom=10&size="+test.getWidth()+"x"+test.getHeight()+"&maptype=mobile&markers="+lat+","+lon+",blues&key=ABQIAAAAdq0rZQlDP0XqsRuObxTBphT8HGboXmX0ACadYXl8eRWIc88RsRTfXIQHiFUladry-qpzlGxU7tPGkQ";
//                platformRequest(url);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            posInfo.setString(ex.toString());
        }
        
    }
    public void commandAction(Command c, Displayable displayable) {
        if(c.getCommandType()==Command.EXIT){
            destroyApp(true);
            notifyDestroyed();
        }else{
            // These properties are implemented on latest SonyEricsson phones
            // This does not work on others (Nokia, etc...)
            String cellid=System.getProperty("com.sonyericsson.net.cellid");
            String mcc= System.getProperty("com.sonyericsson.net.cmcc");
            String mnc= System.getProperty("com.sonyericsson.net.cmnc");
            String lac= System.getProperty("com.sonyericsson.net.lac");
            String info="This phone does not support CellID";
//mcc="262";mnc="3";cellid="52DF";lac="0";
            if(cellid!=null){
                info="Cell:"+cellid+" mcc:"+mcc+" mnc:"+mnc+" lac:"+lac;
                url="http://www.opencellid.org/cell/get?cellid="+Integer.parseInt(cellid,16)+"&mcc="+mcc+"&mnc="+mnc+"&lac="+Integer.parseInt(lac,16)+"&fmt=txt";
                Thread t=new Thread(this);
                t.start();
                posInfo.setString("Requesting position...");
            }
            cellInfo.setString(info);            
        }
    }
    
    public void pauseApp() {}
    
    public void destroyApp(boolean unconditional) {    }
}
