package gps;
import java.lang.*;
import java.util.*;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
//import javax.bluetooth.*;
//import javax.obex.*;
//import de.avetana.javax.obex.*;
//import de.avetana.obexsolo.OBEXConnector;
//import javax.obex.*;
//import de.avetana.javax.obex.*;
//import de.avetana.obexsolo.*;
import gps.*;
import ui.Debug;
// TODO this one next
import vlkgps.MyCanvas;
public class WpExport implements Runnable {
public static final int FF_CSV = 0;
public static final int FF_WPT = 1;
public static final int FF_GPX = 2;
public static final int DEST_DISK = 0;
public static final int DEST_OBEX = 1;
public final static String[] extension = {
".csv",
".wpt",
".gpx",
""
};
private Thread t = null;
private Waypoint waypoint;
private WpSelect wpSelect;
private WpSearch wpSearch;
private String fileName = "";
private String obexAddr = "";
private int format;
private int dest;
public WpExport( Waypoint waypoint, int format, String fileName, int maxWp ) {
this.waypoint = waypoint;
wpSelect = new WpSelect( waypoint );
wpSearch = new WpSearch( waypoint, maxWp );
this.fileName = fileName;
this.format = format;
dest = DEST_DISK;
wpSearch.search( "", "", "", "", "", "", -1 );
t = new Thread( this );
t.setPriority( Thread.MIN_PRIORITY );
t.start();
}
public WpExport( Waypoint waypoint, int format, String fileName, String obexAddr, int maxWp ) {
this.waypoint = waypoint;
wpSelect = new WpSelect( waypoint );
wpSearch = new WpSearch( waypoint, maxWp );
this.fileName = fileName;
this.obexAddr = obexAddr;
this.format = format;
dest = DEST_OBEX;
wpSearch.search( "", "", "", "", "", "", -1 );
t = new Thread( this );
t.setPriority( Thread.MIN_PRIORITY );
t.start();
}
private String head() {
switch( format ) {
case FF_CSV :
return "IDNT,LATITUDE,LONGITUDE,ALTITUDE,PROXIMITY,NAME,DESCRIPTION,CITY,COUNTRY,DATE,SYMBOL\n";
case FF_WPT :
return "H SOFTWARE NAME & VERSION\n"
+ "I PCX5 2.09 vlkGPS 0.3\n\n"
+ "H R DATUM IDX DA DF DX DY DZ\n"
+ "M G WGS 84 121 +0.000000e+00 +0.000000e+00 +0.000000e+00 +0.000000e+00 +0.000000e+00\n\n"
+ "H COORDINATE SYSTEM\n"
+ "U LAT LON DM\n\n"
+ "H IDNT LATITUDE LONGITUDE ALT DATE TIME DESCRIPTION PROXIMITY NAME CITY CC SYMBOL\n"
;
case FF_GPX :
return "\n"
+ "\n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ " xmlns=\"http://www.topografix.com/GPX/1/1\"\n"
+ " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n"
// + "\n"
;
}
return "";
}
private String tail() {
switch( format ) {
case FF_CSV :
return "";
case FF_WPT :
return "";
case FF_GPX :
return "\n";
}
return "";
}
private String data( WpSelect wpSelect ) {
String dates = ( new Date( wpSelect.date ) ).toString();
switch( format ) {
case FF_CSV :
return "" + wpSelect.idnt + "," + wpSelect.lat + "," + wpSelect.lon + "," + wpSelect.alt + "," + wpSelect.prx + "," + wpSelect.name + "," + wpSelect.desc + "," + wpSelect.city + "," + wpSelect.ctry + "," + wpSelect.date + "," + wpSelect.sym + "\n";
case FF_WPT :
return "W " + setStrLen( wpSelect.idnt, 6, false, " " )
+ " " + setStrLen( toLatDM( wpSelect.lat ), 12, false, "0" )
+ " " + setStrLen( toLonDM( wpSelect.lon ), 12 ,false, "0" )
+ " " + setStrLen( Integer.toString( wpSelect.alt / 1000 ), 5, true, " " )
+ " " + setStrLen( dates.substring( 8, 10 ) + "-" + dates.substring( 4, 7 ) + "-" + dates.substring( dates.length() - 2, dates.length() ), 9, false, " " )
+ " " + setStrLen( dates.substring( 11, 19 ), 8 ,false, " " )
+ " " + setStrLen( wpSelect.desc, 40, false, " " )
+ " " + setStrLen( Integer.toString( wpSelect.prx ), 12, true, " " )
+ " " + setStrLen( wpSelect.name, 20, false, " " )
+ " " + setStrLen( wpSelect.city, 20, false, " " )
+ " " + setStrLen( wpSelect.ctry, 10, false, " " )
+ " " + setStrLen( Integer.toString( WpSymbol.getGarminCode( wpSelect.sym ) ), 5, true, " " )
+ "\n"
;
case FF_GPX :
return " \n"
+ " " + wpSelect.alt / 1000 + "\n"
// + " \n" // " + + "
+ " " + wpSelect.idnt + "\n"
+ " " + wpSelect.desc + "\n"
+ " " + wpSelect.name + "; " + wpSelect.city + "; " + wpSelect.ctry + "\n"
+ " " + WpSymbol.getName( wpSelect.sym ) + "\n"
+ " \n"
;
}
return "";
}
public void run() {
// Debug.print( "EXPORT start " + fileName + extension[ format ] );
switch( dest ) {
case DEST_DISK :
try {
FileConnection fc = (FileConnection)Connector.open( "file:///" + fileName + extension[ format ] );
if( ! fc.exists() ) {
fc.create();
} else {
fc.delete();
}
fc.setWritable( true );
DataOutputStream fdos = fc.openDataOutputStream();
fdos.write( head().getBytes() );
int searchIndex = 0;
while( wpSearch.getSearchStatus() || ( searchIndex != wpSearch.getNumResults() ) ) {
while( searchIndex < wpSearch.getNumResults() ) {
wpSelect.open( wpSearch.getResult( searchIndex ), false );
fdos.write( data( wpSelect ).getBytes() );
searchIndex++;
Thread.yield();
}
Thread.yield();
}
fdos.write( tail().getBytes() );
fc.close();
Debug.print( "EXPORT " + searchIndex + " waypoints exported" );
} catch( Exception e ) {
Debug.print( "EXPORT " + e );
}
break;
case DEST_OBEX :
try {
int searchIndex = 0;
boolean useAvetana = false;
Debug.print( obexAddr );
try {
// Class.forName("blabla");
Class.forName("javax.obex.ClientSession");
Debug.print("..using generic obex");
javax.obex.ClientSession cs = (javax.obex.ClientSession)Connector.open ( obexAddr );
javax.obex.HeaderSet hs = cs.connect(cs.createHeaderSet());
hs.setHeader( javax.obex.HeaderSet.NAME, fileName + extension[ format ] );
hs.setHeader( javax.obex.HeaderSet.TYPE, "text");
javax.obex.Operation po = cs.put(hs);
OutputStream os = po.openOutputStream();
os.write( head().getBytes() );
while( wpSearch.getSearchStatus() || ( searchIndex != wpSearch.getNumResults() ) ) {
while( searchIndex < wpSearch.getNumResults() ) {
wpSelect.open( wpSearch.getResult( searchIndex ), false );
os.write( data( wpSelect ).getBytes() );
searchIndex++;
Thread.yield();
}
Thread.yield();
}
os.write( tail().getBytes() );
os.flush();
po.close();
cs.disconnect(null);
cs.close();
} catch (ClassNotFoundException ex) {
Debug.print("..using avetan obex");
/* try {
clientSession = (de.avetana.javax.obex.ClientSession)Connector.open(url);
} catch (ConnectionNotFoundException ex) {
clientSession = (de.avetana.javax.obex.ClientSession)de.avetana.obexsolo.OBEXConnector.open(url);
}*/
de.avetana.javax.obex.ClientSession cs = (de.avetana.javax.obex.ClientSession)de.avetana.obexsolo.OBEXConnector.open ( obexAddr );
de.avetana.javax.obex.HeaderSet hs = cs.connect(cs.createHeaderSet());
hs.setHeader( de.avetana.javax.obex.HeaderSet.NAME, fileName + extension[ format ] );
hs.setHeader( de.avetana.javax.obex.HeaderSet.TYPE, "text");
de.avetana.javax.obex.Operation po = cs.put(hs);
OutputStream os = po.openOutputStream();
os.write( head().getBytes() );
while( wpSearch.getSearchStatus() || ( searchIndex != wpSearch.getNumResults() ) ) {
while( searchIndex < wpSearch.getNumResults() ) {
wpSelect.open( wpSearch.getResult( searchIndex ), false );
os.write( data( wpSelect ).getBytes() );
searchIndex++;
Thread.yield();
}
Thread.yield();
}
os.write( tail().getBytes() );
os.flush();
po.close();
cs.disconnect(null);
cs.close();
}
Debug.print( "EXPORT " + searchIndex + " waypoints exported" );
} catch( Exception e ) { // ???? java.io.IOException: Only OBEX Connections supported
Debug.print( "EXPORT " + e );
// Debug.print( "EXPORT " + ("" + e).substring( 20 ) );
// Debug.print( "EXPORT " + ("" + e).substring( 40 ) );
}
break;
}
}
public String setStrLen( String s, int n, boolean begin, String chr ) {
if( s.length() > n ) {
return s.substring( 0, n );
}
while( s.length() < n ) {
if( begin ) {
s = chr + s;
} else {
s += chr;
}
}
return s;
}
public String toLatDM( int lat ) {
String out = ( lat < 0 ) ? "W" : "E";
if( lat < 0 ) lat = -lat;
int tmp = lat / GpsMath.DEG;
if( tmp < 10 ) out += "0";
out += Double.toString( (double)( lat % GpsMath.DEG ) / (double)GpsMath.MIN + (double)( tmp * 100 ) );
return out;
}
public String toLonDM( int lon ) {
String out = ( lon < 0 ) ? "S" : "N";
if( lon < 0 ) lon = -lon;
int tmp = lon / GpsMath.DEG;
if( tmp < 100 ) out += "0";
if( tmp < 10 ) out += "0";
out += Double.toString( (double)( lon % GpsMath.DEG ) / (double)GpsMath.MIN + (double)( tmp * 100 ) );
return out;
}
}