diff --git a/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java b/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java index 9e84b8f..6c86f33 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java @@ -13,70 +13,58 @@ import android.os.Environment; /** * static logger interface to be used in the android app */ -public class AppLogger -{ - private static FileWriter debugLogWriter = null; - private static boolean initDone = false; - - private static void init() - { - try - { - // open logfile if existing - File sd = Environment.getExternalStorageDirectory(); - if ( sd == null ) return; - File debugLog = new File( sd, "Android/media/btools.routingapp/brouter/brouterapp.txt" ); - if ( debugLog.exists() ) - { - debugLogWriter = new FileWriter( debugLog, true ); - } +public class AppLogger { + private static FileWriter debugLogWriter = null; + private static boolean initDone = false; + + private static void init() { + try { + // open logfile if existing + File sd = Environment.getExternalStorageDirectory(); + if (sd == null) return; + File debugLog = new File(sd, "Android/media/btools.routingapp/brouter/brouterapp.txt"); + if (debugLog.exists()) { + debugLogWriter = new FileWriter(debugLog, true); } - catch( IOException ioe ) {} + } catch (IOException ioe) { } + } - /** - * log an info trace to the app log file, if any - */ - public static boolean isLogging() - { - if ( !initDone ) - { - initDone = true; - init(); - log( "logging started at " + new Date() ); - } - return debugLogWriter != null; + /** + * log an info trace to the app log file, if any + */ + public static boolean isLogging() { + if (!initDone) { + initDone = true; + init(); + log("logging started at " + new Date()); } + return debugLogWriter != null; + } - /** - * log an info trace to the app log file, if any - */ - public static void log( String msg ) - { - if ( isLogging() ) - { - try - { - debugLogWriter.write( msg ); - debugLogWriter.write( '\n' ); - debugLogWriter.flush(); - } - catch( IOException e ) - { - throw new RuntimeException( "cannot write brouterapp.txt: " + e ); - } + /** + * log an info trace to the app log file, if any + */ + public static void log(String msg) { + if (isLogging()) { + try { + debugLogWriter.write(msg); + debugLogWriter.write('\n'); + debugLogWriter.flush(); + } catch (IOException e) { + throw new RuntimeException("cannot write brouterapp.txt: " + e); } } + } - /** - * Format an exception using - */ - public static String formatThrowable( Throwable t ) - { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter( sw ); - t.printStackTrace( pw ); - return sw.toString(); - } + /** + * Format an exception using + */ + public static String formatThrowable(Throwable t) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + return sw.toString(); + } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java index 8014d37..9fdbc76 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java @@ -22,144 +22,136 @@ import android.util.Log; public class BInstallerActivity extends Activity { - public static final String DOWNLOAD_ACTION = "btools.routingapp.download"; + public static final String DOWNLOAD_ACTION = "btools.routingapp.download"; - private static final int DIALOG_CONFIRM_DELETE_ID = 1; + private static final int DIALOG_CONFIRM_DELETE_ID = 1; - private BInstallerView mBInstallerView; - private PowerManager mPowerManager; - private WakeLock mWakeLock; - private DownloadReceiver myReceiver; + private BInstallerView mBInstallerView; + private PowerManager mPowerManager; + private WakeLock mWakeLock; + private DownloadReceiver myReceiver; - public class DownloadReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - if (intent.hasExtra("txt")) { - String txt = intent.getStringExtra("txt"); - boolean ready = intent.getBooleanExtra("ready", false); - mBInstallerView.setState(txt, ready); - } - } - } - - - /** Called when the activity is first created. */ - @Override - @SuppressWarnings("deprecation") - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get an instance of the PowerManager - mPowerManager = (PowerManager) getSystemService(POWER_SERVICE); - - // Create a bright wake lock - mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass() - .getName()); - - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - - // instantiate our simulation view and set it as the activity's content - mBInstallerView = new BInstallerView(this); - setContentView(mBInstallerView); - } + public class DownloadReceiver extends BroadcastReceiver { @Override - protected void onResume() { - super.onResume(); - /* - * when the activity is resumed, we acquire a wake-lock so that the - * screen stays on, since the user will likely not be fiddling with the - * screen or buttons. - */ - mWakeLock.acquire(); - - IntentFilter filter = new IntentFilter(); - filter.addAction(DOWNLOAD_ACTION); - - myReceiver = new DownloadReceiver(); - registerReceiver(myReceiver, filter); - - // Start the download manager - mBInstallerView.startInstaller(); + public void onReceive(Context context, Intent intent) { + if (intent.hasExtra("txt")) { + String txt = intent.getStringExtra("txt"); + boolean ready = intent.getBooleanExtra("ready", false); + mBInstallerView.setState(txt, ready); + } } - - @Override - protected void onPause() { - super.onPause(); + } - super.onPause(); + /** + * Called when the activity is first created. + */ + @Override + @SuppressWarnings("deprecation") + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - mWakeLock.release(); + // Get an instance of the PowerManager + mPowerManager = (PowerManager) getSystemService(POWER_SERVICE); - } + // Create a bright wake lock + mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass() + .getName()); - @Override - public void onDestroy() { - super.onDestroy(); - if (myReceiver != null) unregisterReceiver(myReceiver); - System.exit(0); - } + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + + // instantiate our simulation view and set it as the activity's content + mBInstallerView = new BInstallerView(this); + setContentView(mBInstallerView); + } + + @Override + protected void onResume() { + super.onResume(); + /* + * when the activity is resumed, we acquire a wake-lock so that the + * screen stays on, since the user will likely not be fiddling with the + * screen or buttons. + */ + mWakeLock.acquire(); + + IntentFilter filter = new IntentFilter(); + filter.addAction(DOWNLOAD_ACTION); + + myReceiver = new DownloadReceiver(); + registerReceiver(myReceiver, filter); + + // Start the download manager + mBInstallerView.startInstaller(); + } + + @Override + protected void onPause() { + super.onPause(); + + + super.onPause(); + + mWakeLock.release(); + + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (myReceiver != null) unregisterReceiver(myReceiver); + System.exit(0); + } @Override @SuppressWarnings("deprecation") - protected Dialog onCreateDialog( int id ) - { + protected Dialog onCreateDialog(int id) { AlertDialog.Builder builder; - switch ( id ) - { - case DIALOG_CONFIRM_DELETE_ID: - builder = new AlertDialog.Builder( this ); - builder - .setTitle( "Confirm Delete" ) - .setMessage( "Really delete?" ).setPositiveButton( "Yes", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - mBInstallerView.deleteSelectedTiles(); - } - } ).setNegativeButton( "No", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - } - } ); - return builder.create(); + switch (id) { + case DIALOG_CONFIRM_DELETE_ID: + builder = new AlertDialog.Builder(this); + builder + .setTitle("Confirm Delete") + .setMessage("Really delete?").setPositiveButton("Yes", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + mBInstallerView.deleteSelectedTiles(); + } + }).setNegativeButton("No", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + } + }); + return builder.create(); - default: - return null; + default: + return null; } } - + @SuppressWarnings("deprecation") - public void showConfirmDelete() - { - showDialog( DIALOG_CONFIRM_DELETE_ID ); + public void showConfirmDelete() { + showDialog(DIALOG_CONFIRM_DELETE_ID); } private Set dialogIds = new HashSet(); - private void showNewDialog( int id ) - { - if ( dialogIds.contains( Integer.valueOf( id ) ) ) - { - removeDialog( id ); + private void showNewDialog(int id) { + if (dialogIds.contains(Integer.valueOf(id))) { + removeDialog(id); } - dialogIds.add( Integer.valueOf( id ) ); - showDialog( id ); + dialogIds.add(Integer.valueOf(id)); + showDialog(id); } - static public long getAvailableSpace (String baseDir) { + static public long getAvailableSpace(String baseDir) { StatFs stat = new StatFs(baseDir); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - return stat.getAvailableBlocksLong()*stat.getBlockSizeLong(); - } - else { - return stat.getAvailableBlocks()*stat.getBlockSize(); + return stat.getAvailableBlocksLong() * stat.getBlockSizeLong(); + } else { + return stat.getAvailableBlocks() * stat.getBlockSize(); } } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java index a610a63..f262cfc 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java @@ -29,672 +29,604 @@ import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; + import btools.mapaccess.PhysicalFile; import btools.mapaccess.Rd5DiffManager; import btools.mapaccess.Rd5DiffTool; import btools.router.RoutingHelper; import btools.util.ProgressListener; -public class BInstallerView extends View -{ +public class BInstallerView extends View { private static final int MASK_SELECTED_RD5 = 1; private static final int MASK_DELETED_RD5 = 2; private static final int MASK_INSTALLED_RD5 = 4; private static final int MASK_CURRENT_RD5 = 8; - - private int imgwOrig; - private int imghOrig; - private float scaleOrig; - private int imgw; - private int imgh; + private int imgwOrig; + private int imghOrig; + private float scaleOrig; - private float lastDownX; - private float lastDownY; - - private Bitmap bmp; - - private float viewscale; - - private float[] testVector = new float[2]; + private int imgw; + private int imgh; - private int[] tileStatus; + private float lastDownX; + private float lastDownY; - private boolean tilesVisible = false; - - private long availableSize; - private File baseDir; - private File segmentDir; - - private boolean isDownloading = false; - public static boolean downloadCanceled = false; + private Bitmap bmp; - private long currentDownloadSize; - private String currentDownloadFile = ""; - private volatile String currentDownloadOperation = ""; - private String downloadAction = ""; - private volatile String newDownloadAction = ""; + private float viewscale; - private long totalSize = 0; - private long rd5Tiles = 0; - private long delTiles = 0; + private float[] testVector = new float[2]; - Paint pnt_1 = new Paint(); - Paint pnt_2 = new Paint(); - Paint paint = new Paint(); - - Activity mActivity; + private int[] tileStatus; + + private boolean tilesVisible = false; + + private long availableSize; + private File baseDir; + private File segmentDir; + + private boolean isDownloading = false; + public static boolean downloadCanceled = false; + + private long currentDownloadSize; + private String currentDownloadFile = ""; + private volatile String currentDownloadOperation = ""; + private String downloadAction = ""; + private volatile String newDownloadAction = ""; + + private long totalSize = 0; + private long rd5Tiles = 0; + private long delTiles = 0; + + Paint pnt_1 = new Paint(); + Paint pnt_2 = new Paint(); + Paint paint = new Paint(); + + Activity mActivity; - protected String baseNameForTile( int tileIndex ) - { - int lon = (tileIndex % 72 ) * 5 - 180; - int lat = (tileIndex / 72 ) * 5 - 90; - String slon = lon < 0 ? "W" + (-lon) : "E" + lon; - String slat = lat < 0 ? "S" + (-lat) : "N" + lat; - return slon + "_" + slat; - } - - private int gridPos2Tileindex( int ix, int iy ) - { - return (35-iy)*72 + ( ix >= 70 ? ix-70: ix+2 ); - } - - private int tileForBaseName( String basename ) - { - String uname = basename.toUpperCase(Locale.ROOT); - int idx = uname.indexOf( "_" ); - if ( idx < 0 ) return -1; - String slon = uname.substring( 0, idx ); - String slat = uname.substring( idx+1 ); - int ilon = slon.charAt(0) == 'W' ? -Integer.parseInt( slon.substring(1) ) : - ( slon.charAt(0) == 'E' ? Integer.parseInt( slon.substring(1) ) : -1 ); - int ilat = slat.charAt(0) == 'S' ? -Integer.parseInt( slat.substring(1) ) : - ( slat.charAt(0) == 'N' ? Integer.parseInt( slat.substring(1) ) : -1 ); - if ( ilon < -180 || ilon >= 180 || ilon % 5 != 0 ) return -1; - if ( ilat < - 90 || ilat >= 90 || ilat % 5 != 0 ) return -1; - return (ilon+180) / 5 + 72*((ilat+90)/5); - } - - - public boolean isDownloadCanceled() - { - return downloadCanceled; - } - - private void toggleDownload() - { - if ( isDownloading ) - { - downloadCanceled = true; - downloadAction = "Canceling..."; - return; - } - - if ( delTiles > 0 ) - { - ( (BInstallerActivity) getContext() ).showConfirmDelete(); - return; - } - - int tidx_min = -1; - int min_size = Integer.MAX_VALUE; - - ArrayList downloadList = new ArrayList<>(); - // prepare download list - for( int ix=0; ix<72; ix++ ) - { - for( int iy=0; iy<36; iy++ ) - { - int tidx = gridPos2Tileindex( ix, iy ); - if ( ( tileStatus[tidx] & MASK_SELECTED_RD5 ) != 0 ) - { - int tilesize = BInstallerSizes.getRd5Size(tidx); - downloadList.add(tidx); - if ( tilesize > 0 && tilesize < min_size ) - { - tidx_min = tidx; - min_size = tilesize; - } - } - } - } - - if (downloadList.size()>0) { - isDownloading = true; - downloadAll(downloadList); - for (Integer i : downloadList) { - tileStatus[i.intValue()] ^= tileStatus[i.intValue()] & MASK_SELECTED_RD5; - } - downloadList.clear(); - } - } - - private void downloadAll(ArrayList downloadList) { - ArrayList urlparts = new ArrayList<>(); - for (Integer i: downloadList) { - urlparts.add(baseNameForTile( i.intValue() )); - } - - currentDownloadOperation = "Start download ..."; - downloadAction = ""; - downloadCanceled = false; - isDownloading = true; - - //final DownloadBackground downloadTask = new DownloadBackground(getContext(), urlparts, baseDir); - //downloadTask.execute( ); - Intent intent = new Intent(mActivity, DownloadService.class); - intent.putExtra("dir", baseDir.getAbsolutePath()+"/brouter/"); - intent.putExtra("urlparts", urlparts); - mActivity.startService(intent); - - deleteRawTracks(); // invalidate raw-tracks after data update - } - - - public void downloadDone( boolean success ) - { - isDownloading = false; - if ( success ) - { - scanExistingFiles(); - toggleDownload(); // keep on if no error - } - invalidate(); - } - - public void setState(String txt, boolean b) { - currentDownloadOperation = txt; - downloadAction = ""; - isDownloading = b; - if (!b) { - scanExistingFiles(); - } - invalidate(); - } - - private int tileIndex( float x, float y ) - { - int ix = (int)(72.f * x / bmp.getWidth()); - int iy = (int)(36.f * y / bmp.getHeight()); - if ( ix >= 0 && ix < 72 && iy >= 0 && iy < 36 ) return gridPos2Tileindex(ix, iy); - return -1; - } - - private void clearTileSelection( int mask ) - { - // clear selection if zooming out - for( int ix=0; ix<72; ix++ ) - for( int iy=0; iy<36; iy++ ) - { - int tidx = gridPos2Tileindex( ix, iy ); - tileStatus[tidx] ^= tileStatus[tidx] & mask; - } - } - - // get back the current image scale - private float currentScale() - { - testVector[1] = 1.f; - mat.mapVectors(testVector); - return testVector[1] / viewscale; - } - - private void deleteRawTracks() - { - File modeDir = new File( baseDir, "brouter/modes" ); - String[] fileNames = modeDir.list(); - if ( fileNames == null ) return; - for( String fileName : fileNames ) - { - if ( fileName.endsWith( "_rawtrack.dat" ) ) - { - File f = new File( modeDir, fileName ); - f.delete(); - } - } - } - - private void scanExistingFiles() - { - clearTileSelection( MASK_INSTALLED_RD5 | MASK_CURRENT_RD5 ); - - scanExistingFiles( new File( baseDir, "brouter/segments4" ) ); - - File secondary = RoutingHelper.getSecondarySegmentDir( new File ( baseDir, "brouter/segments4" ) ); - if ( secondary != null ) - { - scanExistingFiles( secondary ); - } - - availableSize = -1; - try - { - availableSize = (long)((BInstallerActivity)getContext()).getAvailableSpace(baseDir.getAbsolutePath ()); - //StatFs stat = new StatFs(baseDir.getAbsolutePath ()); - //availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong(); - } - catch (Exception e) { /* ignore */ } - } - - private void scanExistingFiles( File dir ) - { - String[] fileNames = dir.list(); - if ( fileNames == null ) return; - String suffix = ".rd5"; - for( String fileName : fileNames ) - { - if ( fileName.endsWith( suffix ) ) - { - String basename = fileName.substring( 0, fileName.length() - suffix.length() ); - int tidx = tileForBaseName( basename ); - tileStatus[tidx] |= MASK_INSTALLED_RD5; - - long age = System.currentTimeMillis() - new File( dir, fileName ).lastModified(); - if ( age < 10800000 ) tileStatus[tidx] |= MASK_CURRENT_RD5; // 3 hours - } - } - } - - private Matrix mat; - private Matrix matText; - - public void startInstaller() { - - baseDir = ConfigHelper.getBaseDir( getContext() ); - segmentDir = new File( baseDir, "brouter/segments4"); - try - { - AssetManager assetManager = getContext().getAssets(); - InputStream istr = assetManager.open("world.png"); - bmp = BitmapFactory.decodeStream(istr); - istr.close(); - } - catch( IOException io ) - { - throw new RuntimeException( "cannot read world.png from assets" ); - } - - tileStatus = new int[72*36]; - scanExistingFiles(); - - float scaleX = imgwOrig / ((float)bmp.getWidth()); - float scaley = imghOrig / ((float)bmp.getHeight()); - - viewscale = scaleX < scaley ? scaleX : scaley; - - mat = new Matrix(); - mat.postScale( viewscale, viewscale ); - tilesVisible = false; - } - - public BInstallerView(Context context) { - super(context); - mActivity = (Activity) context; - - DisplayMetrics metrics = new DisplayMetrics(); - ((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics); - imgwOrig = metrics.widthPixels; - imghOrig = metrics.heightPixels; - int im = imgwOrig > imghOrig ? imgwOrig : imghOrig; - - scaleOrig = im / 480.f; - - matText = new Matrix(); - matText.preScale( scaleOrig, scaleOrig ); - - imgw = (int)(imgwOrig / scaleOrig); - imgh = (int)(imghOrig / scaleOrig); - - totalSize = 0; - rd5Tiles = 0; - delTiles = 0; - } - - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w,h,oldw,oldh); - } - - private void toast( String msg ) - { - Toast.makeText(getContext(), msg, Toast.LENGTH_LONG ).show(); - } - - @Override - protected void onDraw(Canvas canvas) - { - if ( !isDownloading ) - { - canvas.setMatrix( mat ); - canvas.drawBitmap(bmp, 0,0, null); - } - // draw 5*5 lattice starting at scale=3 - - int iw = bmp.getWidth(); - int ih = bmp.getHeight(); - float fw = iw / 72.f; - float fh = ih / 36.f; - - boolean drawGrid = tilesVisible && !isDownloading; - - if ( drawGrid ) - { - - pnt_1.setColor(Color.GREEN); - - for( int ix=1; ix<72; ix++ ) - { - float fx = fw*ix; - canvas.drawLine( fx, 0, fx, ih, pnt_1); - } - for( int iy=1; iy<36; iy++ ) - { - float fy = fh*iy; - canvas.drawLine( 0, fy, iw, fy, pnt_1); - } - } - rd5Tiles = 0; - delTiles = 0; - totalSize = 0; - int mask2 = MASK_SELECTED_RD5 | MASK_DELETED_RD5 | MASK_INSTALLED_RD5; - int mask3 = mask2 | MASK_CURRENT_RD5; - - pnt_2.setColor(Color.GRAY); - pnt_2.setStrokeWidth(1); - drawSelectedTiles( canvas, pnt_2, fw, fh, MASK_INSTALLED_RD5, mask3, false, false, drawGrid ); - pnt_2.setColor(Color.BLUE); - pnt_2.setStrokeWidth(1); - drawSelectedTiles( canvas, pnt_2, fw, fh, MASK_INSTALLED_RD5 | MASK_CURRENT_RD5, mask3, false, false, drawGrid ); - pnt_2.setColor(Color.GREEN); - pnt_2.setStrokeWidth(2); - drawSelectedTiles( canvas, pnt_2, fw, fh, MASK_SELECTED_RD5, mask2, true, false, drawGrid ); - pnt_2.setColor(Color.YELLOW); - pnt_2.setStrokeWidth(2); - drawSelectedTiles( canvas, pnt_2, fw, fh, MASK_SELECTED_RD5 | MASK_INSTALLED_RD5, mask2, true, false, drawGrid ); - pnt_2.setColor(Color.RED); - pnt_2.setStrokeWidth(2); - drawSelectedTiles( canvas, pnt_2, fw, fh, MASK_DELETED_RD5 | MASK_INSTALLED_RD5, mask2, false, true, drawGrid ); - - canvas.setMatrix( matText ); - - paint.setColor(Color.RED); - - long mb = 1024*1024; - - if ( isDownloading ) - { - String sizeHint = currentDownloadSize > 0 ? " (" + ((currentDownloadSize + mb-1)/mb) + " MB)" : ""; - paint.setTextSize(30); - canvas.drawText( currentDownloadOperation, 30, (imgh/3)*2-30, paint); - // canvas.drawText( currentDownloadOperation + " " + currentDownloadFile + sizeHint, 30, (imgh/3)*2-30, paint); - canvas.drawText( downloadAction, 30, (imgh/3)*2, paint); - } - if ( !tilesVisible && !isDownloading) - { - paint.setTextSize(35); - canvas.drawText( "Touch region to zoom in!", 30, (imgh/3)*2, paint); - } - paint.setTextSize(20); - - - - String totmb = ((totalSize + mb-1)/mb) + " MB"; - String freemb = availableSize >= 0 ? ((availableSize + mb-1)/mb) + " MB" : "?"; - canvas.drawText( "Selected segments=" + rd5Tiles, 10, 25, paint ); - canvas.drawText( "Size=" + totmb + " Free=" + freemb , 10, 45, paint ); - - - String btnText = null; - if ( isDownloading ) btnText = "Cancel Download"; - else if ( delTiles > 0 ) btnText = "Delete " + delTiles + " tiles"; - else if ( rd5Tiles > 0 ) btnText = "Start Download"; - else if ( tilesVisible && - rd5Tiles == 0 && - RoutingHelper.hasDirectoryAnyDatafiles( segmentDir )) btnText = "Update all"; - - if ( btnText != null ) - { - canvas.drawLine( imgw-btnw, imgh-btnh, imgw-btnw, imgh-2, paint); - canvas.drawLine( imgw-btnw, imgh-btnh, imgw-2, imgh-btnh, paint); - canvas.drawLine( imgw-btnw, imgh-btnh, imgw-btnw, imgh-2, paint); - canvas.drawLine( imgw-2, imgh-btnh, imgw-2, imgh-2, paint); - canvas.drawLine( imgw-btnw, imgh-2, imgw-2, imgh-2, paint); - canvas.drawText( btnText , imgw-btnw+5, imgh-10, paint ); - } - } - - int btnh = 40; - int btnw = 160; - - -float tx, ty; - - private void drawSelectedTiles( Canvas canvas, Paint pnt, float fw, float fh, int status, int mask, boolean doCount, boolean cntDel, boolean doDraw ) - { - for ( int ix = 0; ix < 72; ix++ ) - for ( int iy = 0; iy < 36; iy++ ) - { - int tidx = gridPos2Tileindex( ix, iy ); - if ( ( tileStatus[tidx] & mask ) == status ) - { - int tilesize = BInstallerSizes.getRd5Size( tidx ); - if ( tilesize > 0 ) - { - if ( doCount ) - { - rd5Tiles++; - totalSize += BInstallerSizes.getRd5Size( tidx ); - } - if ( cntDel ) - { - delTiles++; - totalSize += BInstallerSizes.getRd5Size( tidx ); - } - if ( !doDraw ) - continue; - // draw cross - canvas.drawLine( fw * ix, fh * iy, fw * ( ix + 1 ), fh * ( iy + 1 ), pnt ); - canvas.drawLine( fw * ix, fh * ( iy + 1 ), fw * ( ix + 1 ), fh * iy, pnt ); - - // draw frame - canvas.drawLine( fw * ix, fh * iy, fw * ( ix + 1 ), fh * iy, pnt ); - canvas.drawLine( fw * ix, fh * ( iy + 1 ), fw * ( ix + 1 ), fh * ( iy + 1 ), pnt ); - canvas.drawLine( fw * ix, fh * iy, fw * ix, fh * ( iy + 1 ), pnt ); - canvas.drawLine( fw * ( ix + 1 ), fh * iy, fw * ( ix + 1 ), fh * ( iy + 1 ), pnt ); - } - } - } + protected String baseNameForTile(int tileIndex) { + int lon = (tileIndex % 72) * 5 - 180; + int lat = (tileIndex / 72) * 5 - 90; + String slon = lon < 0 ? "W" + (-lon) : "E" + lon; + String slat = lat < 0 ? "S" + (-lat) : "N" + lat; + return slon + "_" + slat; } - public void deleteSelectedTiles() - { - for ( int ix = 0; ix < 72; ix++ ) - { - for ( int iy = 0; iy < 36; iy++ ) - { - int tidx = gridPos2Tileindex( ix, iy ); - if ( ( tileStatus[tidx] & MASK_DELETED_RD5 ) != 0 ) - { - new File( baseDir, "brouter/segments4/" + baseNameForTile( tidx ) + ".rd5").delete(); + private int gridPos2Tileindex(int ix, int iy) { + return (35 - iy) * 72 + (ix >= 70 ? ix - 70 : ix + 2); + } + + private int tileForBaseName(String basename) { + String uname = basename.toUpperCase(Locale.ROOT); + int idx = uname.indexOf("_"); + if (idx < 0) return -1; + String slon = uname.substring(0, idx); + String slat = uname.substring(idx + 1); + int ilon = slon.charAt(0) == 'W' ? -Integer.parseInt(slon.substring(1)) : + (slon.charAt(0) == 'E' ? Integer.parseInt(slon.substring(1)) : -1); + int ilat = slat.charAt(0) == 'S' ? -Integer.parseInt(slat.substring(1)) : + (slat.charAt(0) == 'N' ? Integer.parseInt(slat.substring(1)) : -1); + if (ilon < -180 || ilon >= 180 || ilon % 5 != 0) return -1; + if (ilat < -90 || ilat >= 90 || ilat % 5 != 0) return -1; + return (ilon + 180) / 5 + 72 * ((ilat + 90) / 5); + } + + + public boolean isDownloadCanceled() { + return downloadCanceled; + } + + private void toggleDownload() { + if (isDownloading) { + downloadCanceled = true; + downloadAction = "Canceling..."; + return; + } + + if (delTiles > 0) { + ((BInstallerActivity) getContext()).showConfirmDelete(); + return; + } + + int tidx_min = -1; + int min_size = Integer.MAX_VALUE; + + ArrayList downloadList = new ArrayList<>(); + // prepare download list + for (int ix = 0; ix < 72; ix++) { + for (int iy = 0; iy < 36; iy++) { + int tidx = gridPos2Tileindex(ix, iy); + if ((tileStatus[tidx] & MASK_SELECTED_RD5) != 0) { + int tilesize = BInstallerSizes.getRd5Size(tidx); + downloadList.add(tidx); + if (tilesize > 0 && tilesize < min_size) { + tidx_min = tidx; + min_size = tilesize; + } } } } - scanExistingFiles(); + + if (downloadList.size() > 0) { + isDownloading = true; + downloadAll(downloadList); + for (Integer i : downloadList) { + tileStatus[i.intValue()] ^= tileStatus[i.intValue()] & MASK_SELECTED_RD5; + } + downloadList.clear(); + } + } + + private void downloadAll(ArrayList downloadList) { + ArrayList urlparts = new ArrayList<>(); + for (Integer i : downloadList) { + urlparts.add(baseNameForTile(i.intValue())); + } + + currentDownloadOperation = "Start download ..."; + downloadAction = ""; + downloadCanceled = false; + isDownloading = true; + + //final DownloadBackground downloadTask = new DownloadBackground(getContext(), urlparts, baseDir); + //downloadTask.execute( ); + Intent intent = new Intent(mActivity, DownloadService.class); + intent.putExtra("dir", baseDir.getAbsolutePath() + "/brouter/"); + intent.putExtra("urlparts", urlparts); + mActivity.startService(intent); + + deleteRawTracks(); // invalidate raw-tracks after data update + } + + + public void downloadDone(boolean success) { + isDownloading = false; + if (success) { + scanExistingFiles(); + toggleDownload(); // keep on if no error + } invalidate(); } - @Override - public boolean onTouchEvent(MotionEvent event) { + public void setState(String txt, boolean b) { + currentDownloadOperation = txt; + downloadAction = ""; + isDownloading = b; + if (!b) { + scanExistingFiles(); + } + invalidate(); + } - // get pointer index from the event object - int pointerIndex = event.getActionIndex(); + private int tileIndex(float x, float y) { + int ix = (int) (72.f * x / bmp.getWidth()); + int iy = (int) (36.f * y / bmp.getHeight()); + if (ix >= 0 && ix < 72 && iy >= 0 && iy < 36) return gridPos2Tileindex(ix, iy); + return -1; + } - // get pointer ID - int pointerId = event.getPointerId(pointerIndex); + private void clearTileSelection(int mask) { + // clear selection if zooming out + for (int ix = 0; ix < 72; ix++) + for (int iy = 0; iy < 36; iy++) { + int tidx = gridPos2Tileindex(ix, iy); + tileStatus[tidx] ^= tileStatus[tidx] & mask; + } + } - // get masked (not specific to a pointer) action - int maskedAction = event.getActionMasked(); + // get back the current image scale + private float currentScale() { + testVector[1] = 1.f; + mat.mapVectors(testVector); + return testVector[1] / viewscale; + } - switch (maskedAction) { + private void deleteRawTracks() { + File modeDir = new File(baseDir, "brouter/modes"); + String[] fileNames = modeDir.list(); + if (fileNames == null) return; + for (String fileName : fileNames) { + if (fileName.endsWith("_rawtrack.dat")) { + File f = new File(modeDir, fileName); + f.delete(); + } + } + } - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_POINTER_DOWN: { - lastDownX = event.getX(); - lastDownY = event.getY(); - - break; + private void scanExistingFiles() { + clearTileSelection(MASK_INSTALLED_RD5 | MASK_CURRENT_RD5); + + scanExistingFiles(new File(baseDir, "brouter/segments4")); + + File secondary = RoutingHelper.getSecondarySegmentDir(new File(baseDir, "brouter/segments4")); + if (secondary != null) { + scanExistingFiles(secondary); + } + + availableSize = -1; + try { + availableSize = (long) ((BInstallerActivity) getContext()).getAvailableSpace(baseDir.getAbsolutePath()); + //StatFs stat = new StatFs(baseDir.getAbsolutePath ()); + //availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong(); + } catch (Exception e) { /* ignore */ } + } + + private void scanExistingFiles(File dir) { + String[] fileNames = dir.list(); + if (fileNames == null) return; + String suffix = ".rd5"; + for (String fileName : fileNames) { + if (fileName.endsWith(suffix)) { + String basename = fileName.substring(0, fileName.length() - suffix.length()); + int tidx = tileForBaseName(basename); + tileStatus[tidx] |= MASK_INSTALLED_RD5; + + long age = System.currentTimeMillis() - new File(dir, fileName).lastModified(); + if (age < 10800000) tileStatus[tidx] |= MASK_CURRENT_RD5; // 3 hours + } + } + } + + private Matrix mat; + private Matrix matText; + + public void startInstaller() { + + baseDir = ConfigHelper.getBaseDir(getContext()); + segmentDir = new File(baseDir, "brouter/segments4"); + try { + AssetManager assetManager = getContext().getAssets(); + InputStream istr = assetManager.open("world.png"); + bmp = BitmapFactory.decodeStream(istr); + istr.close(); + } catch (IOException io) { + throw new RuntimeException("cannot read world.png from assets"); + } + + tileStatus = new int[72 * 36]; + scanExistingFiles(); + + float scaleX = imgwOrig / ((float) bmp.getWidth()); + float scaley = imghOrig / ((float) bmp.getHeight()); + + viewscale = scaleX < scaley ? scaleX : scaley; + + mat = new Matrix(); + mat.postScale(viewscale, viewscale); + tilesVisible = false; + } + + public BInstallerView(Context context) { + super(context); + mActivity = (Activity) context; + + DisplayMetrics metrics = new DisplayMetrics(); + ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics); + imgwOrig = metrics.widthPixels; + imghOrig = metrics.heightPixels; + int im = imgwOrig > imghOrig ? imgwOrig : imghOrig; + + scaleOrig = im / 480.f; + + matText = new Matrix(); + matText.preScale(scaleOrig, scaleOrig); + + imgw = (int) (imgwOrig / scaleOrig); + imgh = (int) (imghOrig / scaleOrig); + + totalSize = 0; + rd5Tiles = 0; + delTiles = 0; + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + } + + private void toast(String msg) { + Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); + } + + @Override + protected void onDraw(Canvas canvas) { + if (!isDownloading) { + canvas.setMatrix(mat); + canvas.drawBitmap(bmp, 0, 0, null); + } + // draw 5*5 lattice starting at scale=3 + + int iw = bmp.getWidth(); + int ih = bmp.getHeight(); + float fw = iw / 72.f; + float fh = ih / 36.f; + + boolean drawGrid = tilesVisible && !isDownloading; + + if (drawGrid) { + + pnt_1.setColor(Color.GREEN); + + for (int ix = 1; ix < 72; ix++) { + float fx = fw * ix; + canvas.drawLine(fx, 0, fx, ih, pnt_1); + } + for (int iy = 1; iy < 36; iy++) { + float fy = fh * iy; + canvas.drawLine(0, fy, iw, fy, pnt_1); + } + } + rd5Tiles = 0; + delTiles = 0; + totalSize = 0; + int mask2 = MASK_SELECTED_RD5 | MASK_DELETED_RD5 | MASK_INSTALLED_RD5; + int mask3 = mask2 | MASK_CURRENT_RD5; + + pnt_2.setColor(Color.GRAY); + pnt_2.setStrokeWidth(1); + drawSelectedTiles(canvas, pnt_2, fw, fh, MASK_INSTALLED_RD5, mask3, false, false, drawGrid); + pnt_2.setColor(Color.BLUE); + pnt_2.setStrokeWidth(1); + drawSelectedTiles(canvas, pnt_2, fw, fh, MASK_INSTALLED_RD5 | MASK_CURRENT_RD5, mask3, false, false, drawGrid); + pnt_2.setColor(Color.GREEN); + pnt_2.setStrokeWidth(2); + drawSelectedTiles(canvas, pnt_2, fw, fh, MASK_SELECTED_RD5, mask2, true, false, drawGrid); + pnt_2.setColor(Color.YELLOW); + pnt_2.setStrokeWidth(2); + drawSelectedTiles(canvas, pnt_2, fw, fh, MASK_SELECTED_RD5 | MASK_INSTALLED_RD5, mask2, true, false, drawGrid); + pnt_2.setColor(Color.RED); + pnt_2.setStrokeWidth(2); + drawSelectedTiles(canvas, pnt_2, fw, fh, MASK_DELETED_RD5 | MASK_INSTALLED_RD5, mask2, false, true, drawGrid); + + canvas.setMatrix(matText); + + paint.setColor(Color.RED); + + long mb = 1024 * 1024; + + if (isDownloading) { + String sizeHint = currentDownloadSize > 0 ? " (" + ((currentDownloadSize + mb - 1) / mb) + " MB)" : ""; + paint.setTextSize(30); + canvas.drawText(currentDownloadOperation, 30, (imgh / 3) * 2 - 30, paint); + // canvas.drawText( currentDownloadOperation + " " + currentDownloadFile + sizeHint, 30, (imgh/3)*2-30, paint); + canvas.drawText(downloadAction, 30, (imgh / 3) * 2, paint); + } + if (!tilesVisible && !isDownloading) { + paint.setTextSize(35); + canvas.drawText("Touch region to zoom in!", 30, (imgh / 3) * 2, paint); + } + paint.setTextSize(20); + + + String totmb = ((totalSize + mb - 1) / mb) + " MB"; + String freemb = availableSize >= 0 ? ((availableSize + mb - 1) / mb) + " MB" : "?"; + canvas.drawText("Selected segments=" + rd5Tiles, 10, 25, paint); + canvas.drawText("Size=" + totmb + " Free=" + freemb, 10, 45, paint); + + + String btnText = null; + if (isDownloading) btnText = "Cancel Download"; + else if (delTiles > 0) btnText = "Delete " + delTiles + " tiles"; + else if (rd5Tiles > 0) btnText = "Start Download"; + else if (tilesVisible && + rd5Tiles == 0 && + RoutingHelper.hasDirectoryAnyDatafiles(segmentDir)) btnText = "Update all"; + + if (btnText != null) { + canvas.drawLine(imgw - btnw, imgh - btnh, imgw - btnw, imgh - 2, paint); + canvas.drawLine(imgw - btnw, imgh - btnh, imgw - 2, imgh - btnh, paint); + canvas.drawLine(imgw - btnw, imgh - btnh, imgw - btnw, imgh - 2, paint); + canvas.drawLine(imgw - 2, imgh - btnh, imgw - 2, imgh - 2, paint); + canvas.drawLine(imgw - btnw, imgh - 2, imgw - 2, imgh - 2, paint); + canvas.drawText(btnText, imgw - btnw + 5, imgh - 10, paint); + } + } + + int btnh = 40; + int btnw = 160; + + + float tx, ty; + + private void drawSelectedTiles(Canvas canvas, Paint pnt, float fw, float fh, int status, int mask, boolean doCount, boolean cntDel, boolean doDraw) { + for (int ix = 0; ix < 72; ix++) + for (int iy = 0; iy < 36; iy++) { + int tidx = gridPos2Tileindex(ix, iy); + if ((tileStatus[tidx] & mask) == status) { + int tilesize = BInstallerSizes.getRd5Size(tidx); + if (tilesize > 0) { + if (doCount) { + rd5Tiles++; + totalSize += BInstallerSizes.getRd5Size(tidx); } - case MotionEvent.ACTION_MOVE: { // a pointer was moved - - if ( isDownloading ) break; - int np = event.getPointerCount(); - int nh = event.getHistorySize(); - if ( nh == 0 ) break; - - float x0 = event.getX( 0 ); - float y0 = event.getY( 0 ); - float hx0 = event.getHistoricalX(0,0); - float hy0 = event.getHistoricalY(0,0); - - if ( np > 1 ) // multi-touch - { - float x1 = event.getX( 1 ); - float y1 = event.getY( 1 ); - float hx1 = event.getHistoricalX(1,0); - float hy1 = event.getHistoricalY(1,0); - - float r = (float)Math.sqrt( (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0) ); - float hr = (float)Math.sqrt( (hx1-hx0)*(hx1-hx0) + (hy1-hy0)*(hy1-hy0) ); - - if ( hr > 0. ) - { - float ratio = r/hr; - - float mx = (x1+x0)/2.f; - float my = (y1+y0)/2.f; - - float scale = currentScale(); - float newscale = scale*ratio; - - if ( newscale > 10.f ) ratio *= (10.f / newscale ); - if ( newscale < 0.5f ) ratio *= (0.5f / newscale ); - - mat.postScale( ratio, ratio, mx, my ); - - mat.postScale( ratio, ratio, mx, my ); - - boolean tilesv = currentScale() >= 3.f; - if ( tilesVisible && !tilesv ) - { - clearTileSelection( MASK_SELECTED_RD5 | MASK_DELETED_RD5 ); - } - tilesVisible = tilesv; - } - - break; - } - mat.postTranslate( x0-hx0, y0-hy0 ); - - break; + if (cntDel) { + delTiles++; + totalSize += BInstallerSizes.getRd5Size(tidx); } - case MotionEvent.ACTION_UP: - - long downTime = event.getEventTime() - event.getDownTime(); - - if ( downTime < 5 || downTime > 500 ) - { - break; - } + if (!doDraw) + continue; + // draw cross + canvas.drawLine(fw * ix, fh * iy, fw * (ix + 1), fh * (iy + 1), pnt); + canvas.drawLine(fw * ix, fh * (iy + 1), fw * (ix + 1), fh * iy, pnt); - if ( Math.abs(lastDownX - event.getX() ) > 10 || Math.abs(lastDownY - event.getY() ) > 10 ) - { - break; - } - - // download button? - if ( ( delTiles > 0 || rd5Tiles >= 0 || isDownloading ) && event.getX() > imgwOrig - btnw*scaleOrig && event.getY() > imghOrig-btnh*scaleOrig ) - { - if (rd5Tiles == 0) { - for ( int ix = 0; ix < 72; ix++ ) - { - for ( int iy = 0; iy < 36; iy++ ) - { - int tidx = gridPos2Tileindex( ix, iy ); - if (tidx != -1) { - if ( ( tileStatus[tidx] & MASK_INSTALLED_RD5 ) != 0 ) { - tileStatus[tidx] |= MASK_SELECTED_RD5; - } - } + // draw frame + canvas.drawLine(fw * ix, fh * iy, fw * (ix + 1), fh * iy, pnt); + canvas.drawLine(fw * ix, fh * (iy + 1), fw * (ix + 1), fh * (iy + 1), pnt); + canvas.drawLine(fw * ix, fh * iy, fw * ix, fh * (iy + 1), pnt); + canvas.drawLine(fw * (ix + 1), fh * iy, fw * (ix + 1), fh * (iy + 1), pnt); + } + } + } + } - } - } - } - toggleDownload(); - invalidate(); - break; - } + public void deleteSelectedTiles() { + for (int ix = 0; ix < 72; ix++) { + for (int iy = 0; iy < 36; iy++) { + int tidx = gridPos2Tileindex(ix, iy); + if ((tileStatus[tidx] & MASK_DELETED_RD5) != 0) { + new File(baseDir, "brouter/segments4/" + baseNameForTile(tidx) + ".rd5").delete(); + } + } + } + scanExistingFiles(); + invalidate(); + } - if ( !tilesVisible ) - { - float scale = currentScale(); - if ( scale > 0f && scale < 5f ) - { - float ratio = 5f / scale; - mat.postScale( ratio, ratio, event.getX(), event.getY() ); - tilesVisible = true; - } - break; - } + @Override + public boolean onTouchEvent(MotionEvent event) { - if ( isDownloading ) break; + // get pointer index from the event object + int pointerIndex = event.getActionIndex(); - Matrix imat = new Matrix(); - if ( mat.invert(imat) ) - { - float[] touchpoint = new float[2]; - touchpoint[0] = event.getX(); - touchpoint[1] = event.getY(); - imat.mapPoints(touchpoint); - - int tidx = tileIndex( touchpoint[0], touchpoint[1] ); - if ( tidx != -1 ) - { - if ( ( tileStatus[tidx] & MASK_SELECTED_RD5 ) != 0 ) - { - tileStatus[tidx] ^= MASK_SELECTED_RD5; - if ( ( tileStatus[tidx] & MASK_INSTALLED_RD5 ) != 0 ) - { - tileStatus[tidx] |= MASK_DELETED_RD5; - } - } - else if ( ( tileStatus[tidx] & MASK_DELETED_RD5 ) != 0 ) - { - tileStatus[tidx] ^= MASK_DELETED_RD5; + // get pointer ID + int pointerId = event.getPointerId(pointerIndex); + + // get masked (not specific to a pointer) action + int maskedAction = event.getActionMasked(); + + switch (maskedAction) { + + case MotionEvent.ACTION_DOWN: + case MotionEvent.ACTION_POINTER_DOWN: { + lastDownX = event.getX(); + lastDownY = event.getY(); + + break; + } + case MotionEvent.ACTION_MOVE: { // a pointer was moved + + if (isDownloading) break; + int np = event.getPointerCount(); + int nh = event.getHistorySize(); + if (nh == 0) break; + + float x0 = event.getX(0); + float y0 = event.getY(0); + float hx0 = event.getHistoricalX(0, 0); + float hy0 = event.getHistoricalY(0, 0); + + if (np > 1) // multi-touch + { + float x1 = event.getX(1); + float y1 = event.getY(1); + float hx1 = event.getHistoricalX(1, 0); + float hy1 = event.getHistoricalY(1, 0); + + float r = (float) Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); + float hr = (float) Math.sqrt((hx1 - hx0) * (hx1 - hx0) + (hy1 - hy0) * (hy1 - hy0)); + + if (hr > 0.) { + float ratio = r / hr; + + float mx = (x1 + x0) / 2.f; + float my = (y1 + y0) / 2.f; + + float scale = currentScale(); + float newscale = scale * ratio; + + if (newscale > 10.f) ratio *= (10.f / newscale); + if (newscale < 0.5f) ratio *= (0.5f / newscale); + + mat.postScale(ratio, ratio, mx, my); + + mat.postScale(ratio, ratio, mx, my); + + boolean tilesv = currentScale() >= 3.f; + if (tilesVisible && !tilesv) { + clearTileSelection(MASK_SELECTED_RD5 | MASK_DELETED_RD5); + } + tilesVisible = tilesv; + } + + break; + } + mat.postTranslate(x0 - hx0, y0 - hy0); + + break; + } + case MotionEvent.ACTION_UP: + + long downTime = event.getEventTime() - event.getDownTime(); + + if (downTime < 5 || downTime > 500) { + break; + } + + if (Math.abs(lastDownX - event.getX()) > 10 || Math.abs(lastDownY - event.getY()) > 10) { + break; + } + + // download button? + if ((delTiles > 0 || rd5Tiles >= 0 || isDownloading) && event.getX() > imgwOrig - btnw * scaleOrig && event.getY() > imghOrig - btnh * scaleOrig) { + if (rd5Tiles == 0) { + for (int ix = 0; ix < 72; ix++) { + for (int iy = 0; iy < 36; iy++) { + int tidx = gridPos2Tileindex(ix, iy); + if (tidx != -1) { + if ((tileStatus[tidx] & MASK_INSTALLED_RD5) != 0) { + tileStatus[tidx] |= MASK_SELECTED_RD5; } - else - { - tileStatus[tidx] ^= MASK_SELECTED_RD5; - } - } - - tx = touchpoint[0]; - ty = touchpoint[1]; - } - - - break; - case MotionEvent.ACTION_POINTER_UP: - case MotionEvent.ACTION_CANCEL: { - // TODO use data - break; - } - } - invalidate(); + } - return true; + } + } + } + toggleDownload(); + invalidate(); + break; + } + + if (!tilesVisible) { + float scale = currentScale(); + if (scale > 0f && scale < 5f) { + float ratio = 5f / scale; + mat.postScale(ratio, ratio, event.getX(), event.getY()); + tilesVisible = true; + } + break; + } + + if (isDownloading) break; + + Matrix imat = new Matrix(); + if (mat.invert(imat)) { + float[] touchpoint = new float[2]; + touchpoint[0] = event.getX(); + touchpoint[1] = event.getY(); + imat.mapPoints(touchpoint); + + int tidx = tileIndex(touchpoint[0], touchpoint[1]); + if (tidx != -1) { + if ((tileStatus[tidx] & MASK_SELECTED_RD5) != 0) { + tileStatus[tidx] ^= MASK_SELECTED_RD5; + if ((tileStatus[tidx] & MASK_INSTALLED_RD5) != 0) { + tileStatus[tidx] |= MASK_DELETED_RD5; + } + } else if ((tileStatus[tidx] & MASK_DELETED_RD5) != 0) { + tileStatus[tidx] ^= MASK_DELETED_RD5; + } else { + tileStatus[tidx] ^= MASK_SELECTED_RD5; + } + } + + tx = touchpoint[0]; + ty = touchpoint[1]; } + break; + case MotionEvent.ACTION_POINTER_UP: + case MotionEvent.ACTION_CANCEL: { + // TODO use data + break; + } + } + invalidate(); + + return true; + } + } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index 647a0ff..40bcb5f 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -63,379 +63,309 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques private PowerManager mPowerManager; private WakeLock mWakeLock; - /** Called when the activity is first created. */ + /** + * Called when the activity is first created. + */ @Override @SuppressWarnings("deprecation") - public void onCreate( Bundle savedInstanceState ) - { - super.onCreate( savedInstanceState ); + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); // Get an instance of the PowerManager - mPowerManager = (PowerManager) getSystemService( POWER_SERVICE ); + mPowerManager = (PowerManager) getSystemService(POWER_SERVICE); // Create a bright wake lock - mWakeLock = mPowerManager.newWakeLock( PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName() ); + mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName()); ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); int memoryClass = am.getMemoryClass(); // instantiate our simulation view and set it as the activity's content - mBRouterView = new BRouterView( this, memoryClass ); + mBRouterView = new BRouterView(this, memoryClass); mBRouterView.init(); - setContentView( mBRouterView ); + setContentView(mBRouterView); } @Override @SuppressWarnings("deprecation") - protected Dialog onCreateDialog( int id ) - { + protected Dialog onCreateDialog(int id) { AlertDialog.Builder builder; - builder = new AlertDialog.Builder( this ); + builder = new AlertDialog.Builder(this); builder.setCancelable(false); - switch ( id ) - { - case DIALOG_SELECTPROFILE_ID: - builder.setTitle( "Select a routing profile" ); - builder.setItems( availableProfiles, new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int item ) - { - selectedProfile = availableProfiles[item]; - mBRouterView.startProcessing( selectedProfile ); - } - } ); - return builder.create(); - case DIALOG_MAINACTION_ID: - builder.setTitle( "Select Main Action" ); - builder - .setItems( new String[] - { "Download Manager", "BRouter App" }, new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int item ) - { - if ( item == 0 ) - startDownloadManager(); - else - showDialog( DIALOG_SELECTPROFILE_ID ); - } - } - ) - .setNegativeButton( "Close", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } - ); - return builder.create(); - case DIALOG_SHOW_DM_INFO_ID: - builder - .setTitle( "BRouter Download Manager" ) - .setMessage( - "*** Attention: ***\n\n" + "The Download Manager is used to download routing-data " - + "files which can be up to 170MB each. Do not start the Download Manager " + "on a cellular data connection without a data plan! " - + "Download speed is restricted to 4 MBit/s." ).setPositiveButton( "I know", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - Intent intent = new Intent( BRouterActivity.this, BInstallerActivity.class ); - startActivity( intent ); - // finish(); - } - } ).setNegativeButton( "Cancel", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_SHOW_WP_HELP_ID: - builder - .setTitle( "No Waypoint Database found" ) - .setMessage( - "The simple scan did not find any map-tool directory including a waypoint database. " - + "Reason could be there is no map-tool installed (osmand, locus or oruxmaps), or at an " - + "unusual path, or it contains no waypoints yet. That's o.k. if you want to use BRouter " - + "in server-mode only - in that case you can still use the 'Server-Mode' button to " - + "configure the profile mapping. But you will not be able to use nogo-points or do " - + "long distance calculations. If you know the path to your map-tool, you can manually " - + "configure it in 'storageconfig.txt'. Or I can do an extended scan searching " - + "your sd-card for a valid waypoint database" ).setPositiveButton( "Scan", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - mBRouterView.startWpDatabaseScan(); - } - } ).setNegativeButton( "Exit", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_SHOW_API23_HELP_ID: - builder - .setTitle( "Android >=6 limitations" ) - .setMessage( - "You are using the BRouter APP on Android >= 6, where classic mode is no longer supported. " - + "Reason is that security policy does not permit any longer to read the waypoint databases of other apps. " - + "That's o.k. if you want to use BRouter in server-mode only, where the apps actively send the waypoints " - + "via a remote procedure call to BRouter (And Locus can also send nogo areas). " - + "So the only functions you need to start the BRouter App are 1) to initially define the base directory " - + "2) to download routing data files and 3) to configure the profile mapping via the 'Server-Mode' button. " - + "You will eventually not be able to define nogo-areas (OsmAnd, Orux) or to do " - + "very long distance calculations. If you want to get classic mode back, you can manually install " - + "the APK of the BRouter App from the release page ( http://brouter.de/brouter/revisions.html ), which " - + "is still built against Android API 10, and does not have these limitations. " - ).setNegativeButton( "Exit", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID: - builder - .setTitle( "Successfully prepared a timeout-free calculation" ) - .setMessage( - "You successfully repeated a calculation that previously run into a timeout " - + "when started from your map-tool. If you repeat the same request from your " - + "maptool, with the exact same destination point and a close-by starting point, " - + "this request is guaranteed not to time out." ).setNegativeButton( "Exit", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_SHOW_WP_SCANRESULT_ID: - builder - .setTitle( "Waypoint Database " ) - .setMessage( "Found Waypoint-Database(s) for maptool-dir: " + maptoolDirCandidate - + " Configure that?" ).setPositiveButton( "Yes", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - mBRouterView.saveMaptoolDir( maptoolDirCandidate ); - } - } ).setNegativeButton( "No", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_OLDDATAHINT_ID: - builder - .setTitle( "Local setup needs reset" ) - .setMessage( - "You are currently using an old version of the lookup-table " + "together with routing data made for this old table. " - + "Before downloading new datafiles made for the new table, " - + "you have to reset your local setup by 'moving away' (or deleting) " - + "your /brouter directory and start a new setup by calling the " + "BRouter App again." ) - .setPositiveButton( "OK", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_ROUTINGMODES_ID: - builder.setTitle( message ); - builder.setMultiChoiceItems( routingModes, routingModesChecked, new DialogInterface.OnMultiChoiceClickListener() - { - @Override - public void onClick( DialogInterface dialog, int which, boolean isChecked ) - { - routingModesChecked[which] = isChecked; - } - } ); - builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int whichButton ) - { - mBRouterView.configureService( routingModes, routingModesChecked ); - } - } ); - return builder.create(); - case DIALOG_EXCEPTION_ID: - builder.setTitle( "An Error occured" ).setMessage( errorMessage ).setPositiveButton( "OK", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - mBRouterView.continueProcessing(); - } - } ); - return builder.create(); - case DIALOG_TEXTENTRY_ID: - builder.setTitle( "Enter SDCARD base dir:" ); - builder.setMessage( message ); - final EditText input = new EditText( this ); - input.setText( defaultbasedir ); - builder.setView( input ); - builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int whichButton ) - { - String basedir = input.getText().toString(); - mBRouterView.startSetup( new File(basedir), true ); - } - } ); - return builder.create(); - case DIALOG_SELECTBASEDIR_ID: - builder.setTitle( "Choose brouter data base dir:" ); - // builder.setMessage( message ); - builder.setSingleChoiceItems( basedirOptions, 0, new DialogInterface.OnClickListener() - { - @Override - public void onClick( DialogInterface dialog, int item ) - { - selectedBasedir = item; - } - } ); - builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int whichButton ) - { - if ( selectedBasedir < availableBasedirs.size() ) - { - mBRouterView.startSetup( availableBasedirs.get( selectedBasedir ), true ); + switch (id) { + case DIALOG_SELECTPROFILE_ID: + builder.setTitle("Select a routing profile"); + builder.setItems(availableProfiles, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int item) { + selectedProfile = availableProfiles[item]; + mBRouterView.startProcessing(selectedProfile); } - else - { - showDialog( DIALOG_TEXTENTRY_ID ); + }); + return builder.create(); + case DIALOG_MAINACTION_ID: + builder.setTitle("Select Main Action"); + builder + .setItems(new String[] + {"Download Manager", "BRouter App"}, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int item) { + if (item == 0) + startDownloadManager(); + else + showDialog(DIALOG_SELECTPROFILE_ID); + } + } + ) + .setNegativeButton("Close", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); + } + } + ); + return builder.create(); + case DIALOG_SHOW_DM_INFO_ID: + builder + .setTitle("BRouter Download Manager") + .setMessage( + "*** Attention: ***\n\n" + "The Download Manager is used to download routing-data " + + "files which can be up to 170MB each. Do not start the Download Manager " + "on a cellular data connection without a data plan! " + + "Download speed is restricted to 4 MBit/s.").setPositiveButton("I know", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Intent intent = new Intent(BRouterActivity.this, BInstallerActivity.class); + startActivity(intent); + // finish(); } - } - } ); - return builder.create(); - case DIALOG_VIASELECT_ID: - builder.setTitle( "Check VIA Selection:" ); - builder.setMultiChoiceItems( availableVias, getCheckedBooleanArray( availableVias.length ), new DialogInterface.OnMultiChoiceClickListener() - { - @Override - public void onClick( DialogInterface dialog, int which, boolean isChecked ) - { - if ( isChecked ) - { - selectedVias.add( availableVias[which] ); - } - else - { - selectedVias.remove( availableVias[which] ); - } - } - } ); - builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int whichButton ) - { - mBRouterView.updateViaList( selectedVias ); - mBRouterView.startProcessing( selectedProfile ); - } - } ); - return builder.create(); - case DIALOG_NOGOSELECT_ID: - builder.setTitle( "Check NoGo Selection:" ); - String[] nogoNames = new String[nogoList.size()]; - for ( int i = 0; i < nogoList.size(); i++ ) - nogoNames[i] = nogoList.get( i ).name; - final boolean[] nogoEnabled = getCheckedBooleanArray( nogoList.size() ); - builder.setMultiChoiceItems( nogoNames, getCheckedBooleanArray( nogoNames.length ), new DialogInterface.OnMultiChoiceClickListener() - { - @Override - public void onClick( DialogInterface dialog, int which, boolean isChecked ) - { - nogoEnabled[which] = isChecked; - } - } ); - builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int whichButton ) - { - mBRouterView.updateNogoList( nogoEnabled ); - mBRouterView.startProcessing( selectedProfile ); - } - } ); - return builder.create(); - case DIALOG_SHOWRESULT_ID: - String leftLabel = wpCount < 0 ? ( wpCount != -2 ? "Exit" : "Help") : ( wpCount == 0 ? "Select from" : "Select to/via" ); - String rightLabel = wpCount < 2 ? ( wpCount == -3 ? "Help" : "Server-Mode" ) : "Calc Route"; - - builder.setTitle( title ).setMessage( errorMessage ).setPositiveButton( leftLabel, new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - if ( wpCount == -2 ) - { - showWaypointDatabaseHelp(); - } - else if ( wpCount == -1 || wpCount == -3 ) - { + }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { finish(); } - else - { - mBRouterView.pickWaypoints(); + }); + return builder.create(); + case DIALOG_SHOW_WP_HELP_ID: + builder + .setTitle("No Waypoint Database found") + .setMessage( + "The simple scan did not find any map-tool directory including a waypoint database. " + + "Reason could be there is no map-tool installed (osmand, locus or oruxmaps), or at an " + + "unusual path, or it contains no waypoints yet. That's o.k. if you want to use BRouter " + + "in server-mode only - in that case you can still use the 'Server-Mode' button to " + + "configure the profile mapping. But you will not be able to use nogo-points or do " + + "long distance calculations. If you know the path to your map-tool, you can manually " + + "configure it in 'storageconfig.txt'. Or I can do an extended scan searching " + + "your sd-card for a valid waypoint database").setPositiveButton("Scan", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + mBRouterView.startWpDatabaseScan(); } - } - } ).setNegativeButton( rightLabel, new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - if ( wpCount == -3 ) - { - showRepeatTimeoutHelp(); + }).setNegativeButton("Exit", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); } - else if ( wpCount < 2 ) - { - mBRouterView.startConfigureService(); + }); + return builder.create(); + case DIALOG_SHOW_API23_HELP_ID: + builder + .setTitle("Android >=6 limitations") + .setMessage( + "You are using the BRouter APP on Android >= 6, where classic mode is no longer supported. " + + "Reason is that security policy does not permit any longer to read the waypoint databases of other apps. " + + "That's o.k. if you want to use BRouter in server-mode only, where the apps actively send the waypoints " + + "via a remote procedure call to BRouter (And Locus can also send nogo areas). " + + "So the only functions you need to start the BRouter App are 1) to initially define the base directory " + + "2) to download routing data files and 3) to configure the profile mapping via the 'Server-Mode' button. " + + "You will eventually not be able to define nogo-areas (OsmAnd, Orux) or to do " + + "very long distance calculations. If you want to get classic mode back, you can manually install " + + "the APK of the BRouter App from the release page ( http://brouter.de/brouter/revisions.html ), which " + + "is still built against Android API 10, and does not have these limitations. " + ).setNegativeButton("Exit", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); } - else - { - mBRouterView.finishWaypointSelection(); - mBRouterView.startProcessing( selectedProfile ); + }); + return builder.create(); + case DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID: + builder + .setTitle("Successfully prepared a timeout-free calculation") + .setMessage( + "You successfully repeated a calculation that previously run into a timeout " + + "when started from your map-tool. If you repeat the same request from your " + + "maptool, with the exact same destination point and a close-by starting point, " + + "this request is guaranteed not to time out.").setNegativeButton("Exit", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); } - } - } ); - return builder.create(); - case DIALOG_MODECONFIGOVERVIEW_ID: - builder.setTitle( "Success" ).setMessage( message ).setPositiveButton( "Exit", new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int id ) - { - finish(); - } - } ); - return builder.create(); - case DIALOG_PICKWAYPOINT_ID: - builder.setTitle( wpCount > 0 ? "Select to/via" : "Select from" ); - builder.setItems( availableWaypoints, new DialogInterface.OnClickListener() - { - public void onClick( DialogInterface dialog, int item ) - { - mBRouterView.updateWaypointList( availableWaypoints[item] ); - mBRouterView.startProcessing( selectedProfile ); - } - } ); - return builder.create(); + }); + return builder.create(); + case DIALOG_SHOW_WP_SCANRESULT_ID: + builder + .setTitle("Waypoint Database ") + .setMessage("Found Waypoint-Database(s) for maptool-dir: " + maptoolDirCandidate + + " Configure that?").setPositiveButton("Yes", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + mBRouterView.saveMaptoolDir(maptoolDirCandidate); + } + }).setNegativeButton("No", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }); + return builder.create(); + case DIALOG_OLDDATAHINT_ID: + builder + .setTitle("Local setup needs reset") + .setMessage( + "You are currently using an old version of the lookup-table " + "together with routing data made for this old table. " + + "Before downloading new datafiles made for the new table, " + + "you have to reset your local setup by 'moving away' (or deleting) " + + "your /brouter directory and start a new setup by calling the " + "BRouter App again.") + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }); + return builder.create(); + case DIALOG_ROUTINGMODES_ID: + builder.setTitle(message); + builder.setMultiChoiceItems(routingModes, routingModesChecked, new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + routingModesChecked[which] = isChecked; + } + }); + builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + mBRouterView.configureService(routingModes, routingModesChecked); + } + }); + return builder.create(); + case DIALOG_EXCEPTION_ID: + builder.setTitle("An Error occured").setMessage(errorMessage).setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + mBRouterView.continueProcessing(); + } + }); + return builder.create(); + case DIALOG_TEXTENTRY_ID: + builder.setTitle("Enter SDCARD base dir:"); + builder.setMessage(message); + final EditText input = new EditText(this); + input.setText(defaultbasedir); + builder.setView(input); + builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + String basedir = input.getText().toString(); + mBRouterView.startSetup(new File(basedir), true); + } + }); + return builder.create(); + case DIALOG_SELECTBASEDIR_ID: + builder.setTitle("Choose brouter data base dir:"); + // builder.setMessage( message ); + builder.setSingleChoiceItems(basedirOptions, 0, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int item) { + selectedBasedir = item; + } + }); + builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + if (selectedBasedir < availableBasedirs.size()) { + mBRouterView.startSetup(availableBasedirs.get(selectedBasedir), true); + } else { + showDialog(DIALOG_TEXTENTRY_ID); + } + } + }); + return builder.create(); + case DIALOG_VIASELECT_ID: + builder.setTitle("Check VIA Selection:"); + builder.setMultiChoiceItems(availableVias, getCheckedBooleanArray(availableVias.length), new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + if (isChecked) { + selectedVias.add(availableVias[which]); + } else { + selectedVias.remove(availableVias[which]); + } + } + }); + builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + mBRouterView.updateViaList(selectedVias); + mBRouterView.startProcessing(selectedProfile); + } + }); + return builder.create(); + case DIALOG_NOGOSELECT_ID: + builder.setTitle("Check NoGo Selection:"); + String[] nogoNames = new String[nogoList.size()]; + for (int i = 0; i < nogoList.size(); i++) + nogoNames[i] = nogoList.get(i).name; + final boolean[] nogoEnabled = getCheckedBooleanArray(nogoList.size()); + builder.setMultiChoiceItems(nogoNames, getCheckedBooleanArray(nogoNames.length), new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + nogoEnabled[which] = isChecked; + } + }); + builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + mBRouterView.updateNogoList(nogoEnabled); + mBRouterView.startProcessing(selectedProfile); + } + }); + return builder.create(); + case DIALOG_SHOWRESULT_ID: + String leftLabel = wpCount < 0 ? (wpCount != -2 ? "Exit" : "Help") : (wpCount == 0 ? "Select from" : "Select to/via"); + String rightLabel = wpCount < 2 ? (wpCount == -3 ? "Help" : "Server-Mode") : "Calc Route"; - default: - return null; + builder.setTitle(title).setMessage(errorMessage).setPositiveButton(leftLabel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + if (wpCount == -2) { + showWaypointDatabaseHelp(); + } else if (wpCount == -1 || wpCount == -3) { + finish(); + } else { + mBRouterView.pickWaypoints(); + } + } + }).setNegativeButton(rightLabel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + if (wpCount == -3) { + showRepeatTimeoutHelp(); + } else if (wpCount < 2) { + mBRouterView.startConfigureService(); + } else { + mBRouterView.finishWaypointSelection(); + mBRouterView.startProcessing(selectedProfile); + } + } + }); + return builder.create(); + case DIALOG_MODECONFIGOVERVIEW_ID: + builder.setTitle("Success").setMessage(message).setPositiveButton("Exit", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }); + return builder.create(); + case DIALOG_PICKWAYPOINT_ID: + builder.setTitle(wpCount > 0 ? "Select to/via" : "Select from"); + builder.setItems(availableWaypoints, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int item) { + mBRouterView.updateWaypointList(availableWaypoints[item]); + mBRouterView.startProcessing(selectedProfile); + } + }); + return builder.create(); + + default: + return null; } } - private boolean[] getCheckedBooleanArray( int size ) - { + private boolean[] getCheckedBooleanArray(int size) { boolean[] checked = new boolean[size]; - for ( int i = 0; i < checked.length; i++ ) checked[i] = true; + for (int i = 0; i < checked.length; i++) checked[i] = true; return checked; } @@ -461,76 +391,63 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques private String maptoolDirCandidate; - public boolean isOnline(Context context) - { + public boolean isOnline(Context context) { boolean result = false; - ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Network nw = connectivityManager.getActiveNetwork(); if (nw == null) return false; NetworkCapabilities nwc = connectivityManager.getNetworkCapabilities(nw); - if (nwc == null)return false; + if (nwc == null) return false; result = nwc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) | - nwc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) | - nwc.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) ; + nwc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) | + nwc.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET); } else { NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); if (ni == null) return false; result = ni.getType() == ConnectivityManager.TYPE_WIFI || - ni.getType() == ConnectivityManager.TYPE_MOBILE || - ni.getType() == ConnectivityManager.TYPE_ETHERNET; + ni.getType() == ConnectivityManager.TYPE_MOBILE || + ni.getType() == ConnectivityManager.TYPE_ETHERNET; } return result; } @SuppressWarnings("deprecation") - public void selectProfile( String[] items ) - { + public void selectProfile(String[] items) { availableProfiles = items; // if we have internet access, first show the main action dialog - if ( isOnline(this) ) - { - showDialog( DIALOG_MAINACTION_ID ); - } - else - { - showDialog( DIALOG_SELECTPROFILE_ID ); + if (isOnline(this)) { + showDialog(DIALOG_MAINACTION_ID); + } else { + showDialog(DIALOG_SELECTPROFILE_ID); } } @SuppressWarnings("deprecation") - public void startDownloadManager() - { - if ( !mBRouterView.hasUpToDateLookups() ) - { - showDialog( DIALOG_OLDDATAHINT_ID ); - } - else - { - showDialog( DIALOG_SHOW_DM_INFO_ID ); + public void startDownloadManager() { + if (!mBRouterView.hasUpToDateLookups()) { + showDialog(DIALOG_OLDDATAHINT_ID); + } else { + showDialog(DIALOG_SHOW_DM_INFO_ID); } } @SuppressWarnings("deprecation") - public void selectBasedir( ArrayList items, String defaultBasedir, String message ) - { + public void selectBasedir(ArrayList items, String defaultBasedir, String message) { this.defaultbasedir = defaultBasedir; this.message = message; availableBasedirs = items; ArrayList dirFreeSizes = new ArrayList(); - for ( File f : items ) - { + for (File f : items) { long size = 0L; - try - { - StatFs stat = new StatFs( f.getAbsolutePath() ); + try { + StatFs stat = new StatFs(f.getAbsolutePath()); size = (long) stat.getAvailableBlocks() * stat.getBlockSize(); - } - catch (Exception e) { /* ignore */ } - dirFreeSizes.add( Long.valueOf( size ) ); + } catch (Exception e) { /* ignore */ } + dirFreeSizes.add(Long.valueOf(size)); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -540,94 +457,80 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques } int bdidx = 0; - DecimalFormat df = new DecimalFormat( "###0.00" ); - for ( int idx = 0; idx < availableBasedirs.size(); idx++ ) - { - basedirOptions[bdidx++] = availableBasedirs.get( idx ) + " (" + df.format( dirFreeSizes.get( idx ) / 1024. / 1024. / 1024. ) + " GB free)"; + DecimalFormat df = new DecimalFormat("###0.00"); + for (int idx = 0; idx < availableBasedirs.size(); idx++) { + basedirOptions[bdidx++] = availableBasedirs.get(idx) + " (" + df.format(dirFreeSizes.get(idx) / 1024. / 1024. / 1024.) + " GB free)"; } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { basedirOptions[bdidx] = "Enter path manually"; } - showDialog( DIALOG_SELECTBASEDIR_ID ); + showDialog(DIALOG_SELECTBASEDIR_ID); } @SuppressWarnings("deprecation") - public void selectRoutingModes( String[] modes, boolean[] modesChecked, String message ) - { + public void selectRoutingModes(String[] modes, boolean[] modesChecked, String message) { routingModes = modes; routingModesChecked = modesChecked; this.message = message; - showDialog( DIALOG_ROUTINGMODES_ID ); + showDialog(DIALOG_ROUTINGMODES_ID); } @SuppressWarnings("deprecation") - public void showModeConfigOverview( String message ) - { + public void showModeConfigOverview(String message) { this.message = message; - showDialog( DIALOG_MODECONFIGOVERVIEW_ID ); + showDialog(DIALOG_MODECONFIGOVERVIEW_ID); } @SuppressWarnings("deprecation") - public void selectVias( String[] items ) - { + public void selectVias(String[] items) { availableVias = items; - selectedVias = new HashSet( availableVias.length ); - for ( String via : items ) - selectedVias.add( via ); - showDialog( DIALOG_VIASELECT_ID ); + selectedVias = new HashSet(availableVias.length); + for (String via : items) + selectedVias.add(via); + showDialog(DIALOG_VIASELECT_ID); } @SuppressWarnings("deprecation") - public void selectWaypoint( String[] items ) - { + public void selectWaypoint(String[] items) { availableWaypoints = items; - showNewDialog( DIALOG_PICKWAYPOINT_ID ); + showNewDialog(DIALOG_PICKWAYPOINT_ID); } @SuppressWarnings("deprecation") - public void showWaypointDatabaseHelp() - { - if ( mBRouterView.canAccessSdCard ) - { - showNewDialog( DIALOG_SHOW_WP_HELP_ID ); - } - else - { - showNewDialog( DIALOG_SHOW_API23_HELP_ID ); + public void showWaypointDatabaseHelp() { + if (mBRouterView.canAccessSdCard) { + showNewDialog(DIALOG_SHOW_WP_HELP_ID); + } else { + showNewDialog(DIALOG_SHOW_API23_HELP_ID); } } @SuppressWarnings("deprecation") - public void showRepeatTimeoutHelp() - { - showNewDialog( DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID ); + public void showRepeatTimeoutHelp() { + showNewDialog(DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID); } @SuppressWarnings("deprecation") - public void showWpDatabaseScanSuccess( String bestGuess ) - { + public void showWpDatabaseScanSuccess(String bestGuess) { maptoolDirCandidate = bestGuess; - showNewDialog( DIALOG_SHOW_WP_SCANRESULT_ID ); + showNewDialog(DIALOG_SHOW_WP_SCANRESULT_ID); } @SuppressWarnings("deprecation") - public void selectNogos( List nogoList ) - { + public void selectNogos(List nogoList) { this.nogoList = nogoList; - showDialog( DIALOG_NOGOSELECT_ID ); + showDialog(DIALOG_NOGOSELECT_ID); } private Set dialogIds = new HashSet(); - private void showNewDialog( int id ) - { - if ( dialogIds.contains( Integer.valueOf( id ) ) ) - { - removeDialog( id ); + private void showNewDialog(int id) { + if (dialogIds.contains(Integer.valueOf(id))) { + removeDialog(id); } - dialogIds.add( Integer.valueOf( id ) ); - showDialog( id ); + dialogIds.add(Integer.valueOf(id)); + showDialog(id); } private String errorMessage; @@ -635,24 +538,21 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques private int wpCount; @SuppressWarnings("deprecation") - public void showErrorMessage( String msg ) - { + public void showErrorMessage(String msg) { errorMessage = msg; - showNewDialog( DIALOG_EXCEPTION_ID ); + showNewDialog(DIALOG_EXCEPTION_ID); } @SuppressWarnings("deprecation") - public void showResultMessage( String title, String msg, int wpCount ) - { + public void showResultMessage(String title, String msg, int wpCount) { errorMessage = msg; this.title = title; this.wpCount = wpCount; - showNewDialog( DIALOG_SHOWRESULT_ID ); + showNewDialog(DIALOG_SHOWRESULT_ID); } @Override - protected void onResume() - { + protected void onResume() { super.onResume(); /* * when the activity is resumed, we acquire a wake-lock so that the screen @@ -663,8 +563,7 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques } @Override - protected void onPause() - { + protected void onPause() { super.onPause(); /* * When the activity is paused, we make sure to stop the router @@ -681,7 +580,7 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques return EnvironmentCompat.getStorageState(f); //Environment.MEDIA_MOUNTED } - public ArrayList getStorageDirectories () { + public ArrayList getStorageDirectories() { ArrayList list = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { list = new ArrayList(Arrays.asList(getExternalMediaDirs())); @@ -693,7 +592,7 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques for (File f : list) { if (f != null) { if (getStorageState(f).equals(Environment.MEDIA_MOUNTED)) - res.add (f); + res.add(f); } } @@ -708,27 +607,25 @@ public class BRouterActivity extends Activity implements ActivityCompat.OnReques boolean isWritable = false; try { File sd = Environment.getExternalStorageDirectory(); - File testDir = new File( sd, "brouter" ); + File testDir = new File(sd, "brouter"); boolean didExist = testDir.isDirectory(); - if ( !didExist ) - { + if (!didExist) { testDir.mkdir(); } - File testFile = new File( testDir, "test" + System.currentTimeMillis() ); + File testFile = new File(testDir, "test" + System.currentTimeMillis()); testFile.createNewFile(); - if ( testFile.exists() ) { + if (testFile.exists()) { testFile.delete(); isWritable = true; } - } - catch (Throwable t) { + } catch (Throwable t) { // ignore } return isWritable; } @Override - public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == 0) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { mBRouterView.startSetup(null, true); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java index 52cade2..f7c96b6 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java @@ -1,342 +1,302 @@ -package btools.routingapp; - - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.ByteArrayOutputStream; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.zip.GZIPOutputStream; -import java.util.ArrayList; - -import android.Manifest; -import android.app.Service; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Build; -import android.os.Bundle; -import android.os.Environment; -import android.os.IBinder; -import android.os.RemoteException; -import android.util.Log; -import android.util.Base64; - -import androidx.core.content.ContextCompat; - -import btools.router.OsmNodeNamed; - -public class BRouterService extends Service -{ - - @Override - public IBinder onBind(Intent arg0) { - Log.d(getClass().getSimpleName(), "onBind()"); - return myBRouterServiceStub; - } - - private IBRouterService.Stub myBRouterServiceStub = new IBRouterService.Stub() - { - @Override - public String getTrackFromParams( Bundle params ) throws RemoteException - { - logBundle( params ); - - BRouterWorker worker = new BRouterWorker(); - - // get base dir from private file - String baseDir = null; - InputStream configInput = null; - try - { - configInput = openFileInput( "config15.dat" ); - BufferedReader br = new BufferedReader( new InputStreamReader( configInput ) ); - baseDir = br.readLine(); - } - catch (Exception e) - { - } - finally - { - if ( configInput != null ) try { configInput.close(); } catch (Exception ee) {} - } - worker.baseDir = baseDir; - worker.segmentDir = new File (baseDir, "brouter/segments4" ); - - String remoteProfile = params.getString( "remoteProfile" ); - - if ( remoteProfile == null ) - { - remoteProfile = checkForTestDummy( baseDir ); - } - - String errMsg = null; - if (remoteProfile != null ) { - errMsg = getConfigForRemoteProfile(worker, baseDir, remoteProfile); - } else if (params.containsKey("profile")) { - String profile = params.getString( "profile" ); - worker.profileName = profile; - worker.profilePath = baseDir + "/brouter/profiles2/" + profile + ".brf"; - worker.rawTrackPath = baseDir + "/brouter/modes/" + profile + "_rawtrack.dat"; - if (!new File(worker.profilePath).exists()) errMsg = "Profile " + profile + " does not exists"; - try { - readNogos(worker, baseDir); - } catch (Exception e) { - errMsg = e.getLocalizedMessage(); - } - } - else { - errMsg = getConfigFromMode(worker, baseDir, params.getString("v"), params.getString("fast")); - } - - if ( errMsg != null ) - { - return errMsg; - } - - boolean canCompress = "true".equals( params.getString( "acceptCompressedResult" ) ); - try - { - String gpxMessage = worker.getTrackFromParams( params ); - if ( canCompress && gpxMessage.startsWith( "<" ) ) - { - try - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - baos.write( "z64".getBytes(Charset.forName("UTF-8")) ); // marker prefix - OutputStream os = new GZIPOutputStream( baos ); - byte[] ab = gpxMessage.getBytes(Charset.forName("UTF-8")); //StandardCharsets.UTF_8 - gpxMessage = null; - os.write( ab ); - ab = null; - os.close(); - gpxMessage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); - return gpxMessage; - } - catch( Exception e ) - { - return "error compressing result"; - } - } - return gpxMessage; - } - catch (IllegalArgumentException iae) - { - return iae.getMessage(); - } - } - - private String getConfigFromMode( BRouterWorker worker, String baseDir, String mode, String fast ) - { - boolean isFast = "1".equals( fast ) || "true".equals( fast ) || "yes".equals( fast ); - String mode_key = mode + "_" + ( isFast ? "fast" : "short" ); - - BufferedReader br = null; - try - { - String modesFile = baseDir + "/brouter/modes/serviceconfig.dat"; - br = new BufferedReader( new FileReader( modesFile ) ); - for ( ;; ) - { - String line = br.readLine(); - if ( line == null ) - break; - ServiceModeConfig smc = new ServiceModeConfig( line ); - if ( !smc.mode.equals( mode_key ) ) - continue; - worker.profileName = smc.profile; - worker.profilePath = baseDir + "/brouter/profiles2/" + smc.profile + ".brf"; - worker.rawTrackPath = baseDir + "/brouter/modes/" + mode_key + "_rawtrack.dat"; - - readNogos(worker, baseDir); - - return null; - } - } - catch (Exception e) - { - return "no brouter service config found, mode " + mode_key; - } - finally - { - if ( br != null ) try { br.close(); } catch( Exception ee ) {} - } - return "no brouter service config found for mode " + mode_key; - } - - private String getConfigForRemoteProfile( BRouterWorker worker, String baseDir, String remoteProfile ) - { - worker.profileName = "remote"; - worker.profilePath = baseDir + "/brouter/profiles2/remote.brf"; - worker.rawTrackPath = baseDir + "/brouter/modes/remote_rawtrack.dat"; - - // store profile only if not identical (to preserve timestamp) - byte[] profileBytes = remoteProfile.getBytes(); - File profileFile = new File( worker.profilePath ); - - try - { - readNogos(worker, baseDir); - - if ( !fileEqual( profileBytes, profileFile ) ) - { - OutputStream os = null; - try - { - os = new FileOutputStream( profileFile ); - os.write( profileBytes ); - } - finally - { - if ( os != null ) try { os.close(); } catch( IOException io ) {} - } - } - } - catch( Exception e ) - { - return "error caching remote profile: " + e; - } - return null; - } - - private void readNogos(BRouterWorker worker, String baseDir) throws Exception { - // add nogos from waypoint database - int deviceLevel = android.os.Build.VERSION.SDK_INT; - int targetSdkVersion = getApplicationInfo().targetSdkVersion; - boolean canAccessSdCard = true; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) { - canAccessSdCard = false; - } - if (ContextCompat.checkSelfPermission(BRouterService.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - canAccessSdCard = false; - } - AppLogger.log( "dev/target=" + deviceLevel + "/" + targetSdkVersion + " canAccessSdCard=" + canAccessSdCard ); - - CoordinateReader cor = CoordinateReader.obtainValidReader( baseDir, worker.segmentDir, canAccessSdCard, true ); - worker.nogoList = new ArrayList( cor.nogopoints ); - worker.nogoPolygonsList = new ArrayList(); - } - - - private boolean fileEqual( byte[] fileBytes, File file ) throws Exception - { - if ( !file.exists() ) - { - return false; - } - int nbytes = fileBytes.length; - int pos = 0; - int blen = 8192; - byte[] buf = new byte[blen]; - InputStream is = null; - try - { - is = new FileInputStream( file ); - while( pos < nbytes ) - { - int len = is.read( buf, 0, blen ); - if ( len <= 0 ) return false; - if ( pos + len > nbytes ) return false; - for( int j=0; j" : params.get(k); - String desc = "key=" + k + (val == null ? "" : " class=" + val.getClass() + " val=" + val.toString() ); - AppLogger.log( desc ); - } - } - } - - }; - - @Override - public void onCreate() - { - super.onCreate(); - Log.d(getClass().getSimpleName(),"onCreate()"); - } - - @Override - public void onDestroy() - { - super.onDestroy(); - Log.d(getClass().getSimpleName(),"onDestroy()"); - } - - - // This is the old onStart method that will be called on the pre-2.0 - // platform. On 2.0 or later we override onStartCommand() so this - // method will not be called. - @Override - @SuppressWarnings("deprecation") - public void onStart(Intent intent, int startId) - { - Log.d(getClass().getSimpleName(), "onStart()"); - handleStart(intent, startId); - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) - { - handleStart(intent, startId); - return START_STICKY; - } - - void handleStart(Intent intent, int startId) - { - } -} +package btools.routingapp; + + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.zip.GZIPOutputStream; +import java.util.ArrayList; + +import android.Manifest; +import android.app.Service; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.Bundle; +import android.os.Environment; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Log; +import android.util.Base64; + +import androidx.core.content.ContextCompat; + +import btools.router.OsmNodeNamed; + +public class BRouterService extends Service { + + @Override + public IBinder onBind(Intent arg0) { + Log.d(getClass().getSimpleName(), "onBind()"); + return myBRouterServiceStub; + } + + private IBRouterService.Stub myBRouterServiceStub = new IBRouterService.Stub() { + @Override + public String getTrackFromParams(Bundle params) throws RemoteException { + logBundle(params); + + BRouterWorker worker = new BRouterWorker(); + + // get base dir from private file + String baseDir = null; + InputStream configInput = null; + try { + configInput = openFileInput("config15.dat"); + BufferedReader br = new BufferedReader(new InputStreamReader(configInput)); + baseDir = br.readLine(); + } catch (Exception e) { + } finally { + if (configInput != null) try { + configInput.close(); + } catch (Exception ee) { + } + } + worker.baseDir = baseDir; + worker.segmentDir = new File(baseDir, "brouter/segments4"); + + String remoteProfile = params.getString("remoteProfile"); + + if (remoteProfile == null) { + remoteProfile = checkForTestDummy(baseDir); + } + + String errMsg = null; + if (remoteProfile != null) { + errMsg = getConfigForRemoteProfile(worker, baseDir, remoteProfile); + } else if (params.containsKey("profile")) { + String profile = params.getString("profile"); + worker.profileName = profile; + worker.profilePath = baseDir + "/brouter/profiles2/" + profile + ".brf"; + worker.rawTrackPath = baseDir + "/brouter/modes/" + profile + "_rawtrack.dat"; + if (!new File(worker.profilePath).exists()) + errMsg = "Profile " + profile + " does not exists"; + try { + readNogos(worker, baseDir); + } catch (Exception e) { + errMsg = e.getLocalizedMessage(); + } + } else { + errMsg = getConfigFromMode(worker, baseDir, params.getString("v"), params.getString("fast")); + } + + if (errMsg != null) { + return errMsg; + } + + boolean canCompress = "true".equals(params.getString("acceptCompressedResult")); + try { + String gpxMessage = worker.getTrackFromParams(params); + if (canCompress && gpxMessage.startsWith("<")) { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write("z64".getBytes(Charset.forName("UTF-8"))); // marker prefix + OutputStream os = new GZIPOutputStream(baos); + byte[] ab = gpxMessage.getBytes(Charset.forName("UTF-8")); //StandardCharsets.UTF_8 + gpxMessage = null; + os.write(ab); + ab = null; + os.close(); + gpxMessage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); + return gpxMessage; + } catch (Exception e) { + return "error compressing result"; + } + } + return gpxMessage; + } catch (IllegalArgumentException iae) { + return iae.getMessage(); + } + } + + private String getConfigFromMode(BRouterWorker worker, String baseDir, String mode, String fast) { + boolean isFast = "1".equals(fast) || "true".equals(fast) || "yes".equals(fast); + String mode_key = mode + "_" + (isFast ? "fast" : "short"); + + BufferedReader br = null; + try { + String modesFile = baseDir + "/brouter/modes/serviceconfig.dat"; + br = new BufferedReader(new FileReader(modesFile)); + for (; ; ) { + String line = br.readLine(); + if (line == null) + break; + ServiceModeConfig smc = new ServiceModeConfig(line); + if (!smc.mode.equals(mode_key)) + continue; + worker.profileName = smc.profile; + worker.profilePath = baseDir + "/brouter/profiles2/" + smc.profile + ".brf"; + worker.rawTrackPath = baseDir + "/brouter/modes/" + mode_key + "_rawtrack.dat"; + + readNogos(worker, baseDir); + + return null; + } + } catch (Exception e) { + return "no brouter service config found, mode " + mode_key; + } finally { + if (br != null) try { + br.close(); + } catch (Exception ee) { + } + } + return "no brouter service config found for mode " + mode_key; + } + + private String getConfigForRemoteProfile(BRouterWorker worker, String baseDir, String remoteProfile) { + worker.profileName = "remote"; + worker.profilePath = baseDir + "/brouter/profiles2/remote.brf"; + worker.rawTrackPath = baseDir + "/brouter/modes/remote_rawtrack.dat"; + + // store profile only if not identical (to preserve timestamp) + byte[] profileBytes = remoteProfile.getBytes(); + File profileFile = new File(worker.profilePath); + + try { + readNogos(worker, baseDir); + + if (!fileEqual(profileBytes, profileFile)) { + OutputStream os = null; + try { + os = new FileOutputStream(profileFile); + os.write(profileBytes); + } finally { + if (os != null) try { + os.close(); + } catch (IOException io) { + } + } + } + } catch (Exception e) { + return "error caching remote profile: " + e; + } + return null; + } + + private void readNogos(BRouterWorker worker, String baseDir) throws Exception { + // add nogos from waypoint database + int deviceLevel = android.os.Build.VERSION.SDK_INT; + int targetSdkVersion = getApplicationInfo().targetSdkVersion; + boolean canAccessSdCard = true; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) { + canAccessSdCard = false; + } + if (ContextCompat.checkSelfPermission(BRouterService.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + canAccessSdCard = false; + } + AppLogger.log("dev/target=" + deviceLevel + "/" + targetSdkVersion + " canAccessSdCard=" + canAccessSdCard); + + CoordinateReader cor = CoordinateReader.obtainValidReader(baseDir, worker.segmentDir, canAccessSdCard, true); + worker.nogoList = new ArrayList(cor.nogopoints); + worker.nogoPolygonsList = new ArrayList(); + } + + + private boolean fileEqual(byte[] fileBytes, File file) throws Exception { + if (!file.exists()) { + return false; + } + int nbytes = fileBytes.length; + int pos = 0; + int blen = 8192; + byte[] buf = new byte[blen]; + InputStream is = null; + try { + is = new FileInputStream(file); + while (pos < nbytes) { + int len = is.read(buf, 0, blen); + if (len <= 0) return false; + if (pos + len > nbytes) return false; + for (int j = 0; j < len; j++) { + if (fileBytes[pos++] != buf[j]) { + return false; + } + } + } + return true; + } finally { + if (is != null) try { + is.close(); + } catch (IOException io) { + } + } + } + + private String checkForTestDummy(String baseDir) { + File testdummy = new File(baseDir + "/brouter/profiles2/remotetestdummy.brf"); + if (!testdummy.exists()) return null; + BufferedReader br = null; + StringBuilder sb = new StringBuilder(); + try { + br = new BufferedReader(new FileReader(testdummy)); + for (; ; ) { + String line = br.readLine(); + if (line == null) + break; + sb.append(line).append('\n'); + } + return sb.toString(); + } catch (Exception e) { + throw new RuntimeException("error reading " + testdummy); + } finally { + if (br != null) try { + br.close(); + } catch (Exception ee) { + } + } + } + + private void logBundle(Bundle params) { + if (AppLogger.isLogging()) { + for (String k : params.keySet()) { + Object val = "remoteProfile".equals(k) ? "<..cut..>" : params.get(k); + String desc = "key=" + k + (val == null ? "" : " class=" + val.getClass() + " val=" + val.toString()); + AppLogger.log(desc); + } + } + } + + }; + + @Override + public void onCreate() { + super.onCreate(); + Log.d(getClass().getSimpleName(), "onCreate()"); + } + + @Override + public void onDestroy() { + super.onDestroy(); + Log.d(getClass().getSimpleName(), "onDestroy()"); + } + + + // This is the old onStart method that will be called on the pre-2.0 + // platform. On 2.0 or later we override onStartCommand() so this + // method will not be called. + @Override + @SuppressWarnings("deprecation") + public void onStart(Intent intent, int startId) { + Log.d(getClass().getSimpleName(), "onStart()"); + handleStart(intent, startId); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + handleStart(intent, startId); + return START_STICKY; + } + + void handleStart(Intent intent, int startId) { + } +} diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 82ac3e9..9b04dba 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -53,8 +53,7 @@ import btools.router.RoutingEngine; import btools.router.RoutingHelper; import btools.util.CheapRuler; -public class BRouterView extends View -{ +public class BRouterView extends View { RoutingEngine cr; private int imgw; private int imgh; @@ -95,134 +94,120 @@ public class BRouterView extends View private int[] imgPixels; private int memoryClass; - + public boolean canAccessSdCard; - public void stopRouting() - { - if ( cr != null ) cr.terminate(); + public void stopRouting() { + if (cr != null) cr.terminate(); } - public BRouterView( Context context, int memoryClass ) - { - super( context ); + public BRouterView(Context context, int memoryClass) { + super(context); this.memoryClass = memoryClass; } - public void init() - { - try - { + public void init() { + try { DisplayMetrics metrics = new DisplayMetrics(); - ( (Activity) getContext() ).getWindowManager().getDefaultDisplay().getMetrics( metrics ); + ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics); imgw = metrics.widthPixels; imgh = metrics.heightPixels; // get base dir from private file - File baseDir = ConfigHelper.getBaseDir( getContext() ); + File baseDir = ConfigHelper.getBaseDir(getContext()); // check if valid boolean bdValid = false; - if ( baseDir != null ) - { + if (baseDir != null) { bdValid = baseDir.isDirectory(); - File brd = new File( baseDir, "brouter" ); - if ( brd.isDirectory() ) - { - if ( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q && - !brd.getAbsolutePath().contains("/Android/media/btools.routingapp")) { - String message = "(previous basedir " + baseDir + " has to migrate )" ; + File brd = new File(baseDir, "brouter"); + if (brd.isDirectory()) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q && + !brd.getAbsolutePath().contains("/Android/media/btools.routingapp")) { + String message = "(previous basedir " + baseDir + " has to migrate )"; - ( (BRouterActivity) getContext() ).selectBasedir( ( (BRouterActivity) getContext() ).getStorageDirectories(), guessBaseDir(), message ); + ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), guessBaseDir(), message); waitingForSelection = true; waitingForMigration = true; oldMigrationPath = brd.getAbsolutePath(); return; } else { - startSetup( baseDir, false ); + startSetup(baseDir, false); return; } } } String message = baseDir == null ? "(no basedir configured previously)" : "(previous basedir " + baseDir - + ( bdValid ? " does not contain 'brouter' subfolder)" : " is not valid)" ); + + (bdValid ? " does not contain 'brouter' subfolder)" : " is not valid)"); - ( (BRouterActivity) getContext() ).selectBasedir( ( (BRouterActivity) getContext() ).getStorageDirectories(), guessBaseDir(), message ); + ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), guessBaseDir(), message); waitingForSelection = true; - } - catch (Exception e) - { + } catch (Exception e) { String msg = e instanceof IllegalArgumentException ? e.getMessage() : e.toString(); - AppLogger.log( msg ); - AppLogger.log( AppLogger.formatThrowable( e ) ); + AppLogger.log(msg); + AppLogger.log(AppLogger.formatThrowable(e)); - ( (BRouterActivity) getContext() ).showErrorMessage( msg ); + ((BRouterActivity) getContext()).showErrorMessage(msg); } } - public void startSetup( File baseDir, boolean storeBasedir ) - { + public void startSetup(File baseDir, boolean storeBasedir) { if (baseDir == null) { baseDir = retryBaseDir; retryBaseDir = null; } - if ( storeBasedir ) - { - File td = new File( baseDir, "brouter" ); - try - { + if (storeBasedir) { + File td = new File(baseDir, "brouter"); + try { td.mkdirs(); - } - catch (Exception e) { - Log.d ("BRouterView", "Error creating base directory: " + e.getMessage()); + } catch (Exception e) { + Log.d("BRouterView", "Error creating base directory: " + e.getMessage()); e.printStackTrace(); } - if ( !td.isDirectory() ) { - if (ContextCompat.checkSelfPermission (getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { + if (!td.isDirectory()) { + if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { retryBaseDir = baseDir; - ActivityCompat.requestPermissions ((BRouterActivity) getContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); + ActivityCompat.requestPermissions((BRouterActivity) getContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); } else { - ( (BRouterActivity) getContext() ).selectBasedir( ( (BRouterActivity) getContext() ).getStorageDirectories (), guessBaseDir(), "Cannot access " + baseDir.getAbsolutePath () + "; select another"); + ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), guessBaseDir(), "Cannot access " + baseDir.getAbsolutePath() + "; select another"); } return; } - ConfigHelper.writeBaseDir( getContext(), baseDir ); + ConfigHelper.writeBaseDir(getContext(), baseDir); } - try - { + try { cor = null; String basedir = baseDir.getAbsolutePath(); - AppLogger.log( "using basedir: " + basedir ); + AppLogger.log("using basedir: " + basedir); String version = "v" + getContext().getString(R.string.app_version); // create missing directories - assertDirectoryExists( "project directory", new File (basedir, "brouter"), null, null ); - segmentDir = new File (basedir, "/brouter/segments4"); - if ( assertDirectoryExists( "data directory", segmentDir, "segments4.zip", null ) ) - { + assertDirectoryExists("project directory", new File(basedir, "brouter"), null, null); + segmentDir = new File(basedir, "/brouter/segments4"); + if (assertDirectoryExists("data directory", segmentDir, "segments4.zip", null)) { ConfigMigration.tryMigrateStorageConfig( - new File( basedir + "/brouter/segments3/storageconfig.txt" ), - new File( basedir + "/brouter/segments4/storageconfig.txt" ) ); + new File(basedir + "/brouter/segments3/storageconfig.txt"), + new File(basedir + "/brouter/segments4/storageconfig.txt")); } - profileDir = new File (basedir, "brouter/profiles2"); - assertDirectoryExists( "profile directory", profileDir, "profiles2.zip", version ); - modesDir = new File (basedir, "/brouter/modes"); - assertDirectoryExists( "modes directory", modesDir, "modes.zip", version ); - assertDirectoryExists( "readmes directory", new File (basedir, "brouter/readmes"), "readmes.zip", version ); + profileDir = new File(basedir, "brouter/profiles2"); + assertDirectoryExists("profile directory", profileDir, "profiles2.zip", version); + modesDir = new File(basedir, "/brouter/modes"); + assertDirectoryExists("modes directory", modesDir, "modes.zip", version); + assertDirectoryExists("readmes directory", new File(basedir, "brouter/readmes"), "readmes.zip", version); - File inputDir = new File (basedir, "brouter/import"); - assertDirectoryExists( "input directory", inputDir, null, version ); + File inputDir = new File(basedir, "brouter/import"); + assertDirectoryExists("input directory", inputDir, null, version); // new init is done move old files if (waitingForMigration) { moveFolders(oldMigrationPath, basedir + "/brouter"); waitingForMigration = false; } - + int deviceLevel = Build.VERSION.SDK_INT; int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion; canAccessSdCard = true; @@ -234,7 +219,7 @@ public class BRouterView extends View } cor = CoordinateReader.obtainValidReader(basedir, segmentDir, canAccessSdCard); - + wpList = cor.waypoints; nogoList = cor.nogopoints; nogoVetoList = new ArrayList(); @@ -245,93 +230,77 @@ public class BRouterView extends View needsNogoSelection = nogoList.size() > 0; needsWaypointSelection = wpList.size() == 0; - if ( cor.tracksdir != null ) - { - tracksDir = new File (cor.basedir, cor.tracksdir); - assertDirectoryExists( "track directory", tracksDir, null, null ); + if (cor.tracksdir != null) { + tracksDir = new File(cor.basedir, cor.tracksdir); + assertDirectoryExists("track directory", tracksDir, null, null); // output redirect: look for a pointerfile in tracksdir - File tracksDirPointer = new File( tracksDir, "brouter.redirect" ); - if ( tracksDirPointer.isFile() ) - { - String tracksDirStr = readSingleLineFile( tracksDirPointer ); - if ( tracksDirStr == null ) - throw new IllegalArgumentException( "redirect pointer file is empty: " + tracksDirPointer ); - tracksDir = new File (tracksDirStr); - if ( !( tracksDir.isDirectory() ) ) - throw new IllegalArgumentException( "redirect pointer file " + tracksDirPointer + " does not point to a directory: " + tracksDir ); - } - else - { - File writeTest = new File( tracksDir + "/brouter.writetest" ); - try - { + File tracksDirPointer = new File(tracksDir, "brouter.redirect"); + if (tracksDirPointer.isFile()) { + String tracksDirStr = readSingleLineFile(tracksDirPointer); + if (tracksDirStr == null) + throw new IllegalArgumentException("redirect pointer file is empty: " + tracksDirPointer); + tracksDir = new File(tracksDirStr); + if (!(tracksDir.isDirectory())) + throw new IllegalArgumentException("redirect pointer file " + tracksDirPointer + " does not point to a directory: " + tracksDir); + } else { + File writeTest = new File(tracksDir + "/brouter.writetest"); + try { writeTest.createNewFile(); writeTest.delete(); - } - catch( Exception e ) - { + } catch (Exception e) { tracksDir = null; } } } - if ( tracksDir == null ) - { - tracksDir = new File (basedir, "router"); // fallback + if (tracksDir == null) { + tracksDir = new File(basedir, "router"); // fallback } String[] fileNames = profileDir.list(); ArrayList profiles = new ArrayList(); boolean lookupsFound = false; - for ( String fileName : fileNames ) - { - if ( fileName.endsWith( ".brf" ) ) - { - profiles.add( fileName.substring( 0, fileName.length() - 4 ) ); + for (String fileName : fileNames) { + if (fileName.endsWith(".brf")) { + profiles.add(fileName.substring(0, fileName.length() - 4)); } - if ( fileName.equals( "lookups.dat" ) ) + if (fileName.equals("lookups.dat")) lookupsFound = true; } // add a "last timeout" dummy profile - File lastTimeoutFile = new File( modesDir + "/timeoutdata.txt" ); + File lastTimeoutFile = new File(modesDir + "/timeoutdata.txt"); long lastTimeoutTime = lastTimeoutFile.lastModified(); - if ( lastTimeoutTime > 0 && System.currentTimeMillis() - lastTimeoutTime < 1800000 ) - { - BufferedReader br = new BufferedReader( new FileReader( lastTimeoutFile ) ); + if (lastTimeoutTime > 0 && System.currentTimeMillis() - lastTimeoutTime < 1800000) { + BufferedReader br = new BufferedReader(new FileReader(lastTimeoutFile)); String repeatProfile = br.readLine(); br.close(); - profiles.add( 0, "" ); + profiles.add(0, ""); } - if ( !lookupsFound ) - { - throw new IllegalArgumentException( "The profile-directory " + profileDir + " does not contain the lookups.dat file." - + " see brouter.de/brouter for setup instructions." ); + if (!lookupsFound) { + throw new IllegalArgumentException("The profile-directory " + profileDir + " does not contain the lookups.dat file." + + " see brouter.de/brouter for setup instructions."); } - if ( profiles.size() == 0 ) - { - throw new IllegalArgumentException( "The profile-directory " + profileDir + " contains no routing profiles (*.brf)." - + " see brouter.de/brouter for setup instructions." ); + if (profiles.size() == 0) { + throw new IllegalArgumentException("The profile-directory " + profileDir + " contains no routing profiles (*.brf)." + + " see brouter.de/brouter for setup instructions."); } - if ( !RoutingHelper.hasDirectoryAnyDatafiles( segmentDir ) ) - { - ( (BRouterActivity) getContext() ).startDownloadManager(); + if (!RoutingHelper.hasDirectoryAnyDatafiles(segmentDir)) { + ((BRouterActivity) getContext()).startDownloadManager(); waitingForSelection = true; return; } - ( (BRouterActivity) getContext() ).selectProfile( profiles.toArray( new String[0] ) ); - } - catch (Exception e) - { + ((BRouterActivity) getContext()).selectProfile(profiles.toArray(new String[0])); + } catch (Exception e) { String msg = e instanceof IllegalArgumentException ? e.getMessage() - + ( cor == null ? "" : " (coordinate-source: " + cor.basedir + cor.rootdir + ")" ) : e.toString(); + + (cor == null ? "" : " (coordinate-source: " + cor.basedir + cor.rootdir + ")") : e.toString(); - AppLogger.log( msg ); - AppLogger.log( AppLogger.formatThrowable( e ) ); + AppLogger.log(msg); + AppLogger.log(AppLogger.formatThrowable(e)); - ( (BRouterActivity) getContext() ).showErrorMessage( msg + "\n" + AppLogger.formatThrowable( e ) ); + ((BRouterActivity) getContext()).showErrorMessage(msg + "\n" + AppLogger.formatThrowable(e)); } waitingForSelection = true; } @@ -339,13 +308,13 @@ public class BRouterView extends View private void moveFolders(String oldMigrationPath, String basedir) { File oldDir = new File(oldMigrationPath); File[] oldFiles = oldDir.listFiles(); - for (File f: oldFiles) { + for (File f : oldFiles) { if (f.isDirectory()) { int index = f.getAbsolutePath().lastIndexOf("/"); String tmpdir = basedir + f.getAbsolutePath().substring(index); moveFolders(f.getAbsolutePath(), tmpdir); } else { - if ( ! f.getName().startsWith("v1.6")) { + if (!f.getName().startsWith("v1.6")) { moveFile(oldMigrationPath, f.getName(), basedir); } } @@ -360,9 +329,8 @@ public class BRouterView extends View try { //create output directory if it doesn't exist - File dir = new File (outputPath); - if (!dir.exists()) - { + File dir = new File(outputPath); + if (!dir.exists()) { dir.mkdirs(); } @@ -387,102 +355,79 @@ public class BRouterView extends View new File(inputPath + "/" + inputFile).delete(); - } - - catch (FileNotFoundException fnfe1) { + } catch (FileNotFoundException fnfe1) { Log.e("tag", fnfe1.getMessage()); - } - catch (Exception e) { + } catch (Exception e) { Log.e("tag", e.getMessage()); } } - public boolean hasUpToDateLookups() - { + public boolean hasUpToDateLookups() { BExpressionMetaData meta = new BExpressionMetaData(); - meta.readMetaData( new File( profileDir, "lookups.dat" ) ); + meta.readMetaData(new File(profileDir, "lookups.dat")); return meta.lookupVersion == 10; } - public void continueProcessing() - { + public void continueProcessing() { waitingForSelection = false; invalidate(); } - public void updateViaList( Set selectedVias ) - { - ArrayList filtered = new ArrayList( wpList.size() ); - for ( OsmNodeNamed n : wpList ) - { + public void updateViaList(Set selectedVias) { + ArrayList filtered = new ArrayList(wpList.size()); + for (OsmNodeNamed n : wpList) { String name = n.name; - if ( "from".equals( name ) || "to".equals( name ) || selectedVias.contains( name ) ) - filtered.add( n ); + if ("from".equals(name) || "to".equals(name) || selectedVias.contains(name)) + filtered.add(n); } wpList = filtered; } - public void updateNogoList( boolean[] enabled ) - { - for ( int i = nogoList.size() - 1; i >= 0; i-- ) - { - if ( enabled[i] ) - { - nogoVetoList.add( nogoList.get( i ) ); - nogoList.remove( i ); + public void updateNogoList(boolean[] enabled) { + for (int i = nogoList.size() - 1; i >= 0; i--) { + if (enabled[i]) { + nogoVetoList.add(nogoList.get(i)); + nogoList.remove(i); } } } - public void pickWaypoints() - { + public void pickWaypoints() { String msg = null; - if ( cor.allpoints == null ) - { - try - { + if (cor.allpoints == null) { + try { cor.readAllPoints(); - } - catch (Exception e) - { + } catch (Exception e) { msg = "Error reading waypoints: " + e.toString(); } int size = cor.allpoints.size(); - if ( size < 1 ) + if (size < 1) msg = "coordinate source does not contain any waypoints!"; - if ( size > 1000 ) + if (size > 1000) msg = "coordinate source contains too much waypoints: " + size + "(please use from/to/via names)"; } - if ( msg != null ) - { - ( (BRouterActivity) getContext() ).showErrorMessage( msg ); - } - else - { + if (msg != null) { + ((BRouterActivity) getContext()).showErrorMessage(msg); + } else { String[] wpts = new String[cor.allpoints.size()]; int i = 0; - for ( OsmNodeNamed wp : cor.allpoints ) + for (OsmNodeNamed wp : cor.allpoints) wpts[i++] = wp.name; - ( (BRouterActivity) getContext() ).selectWaypoint( wpts ); + ((BRouterActivity) getContext()).selectWaypoint(wpts); } } - public void updateWaypointList( String waypoint ) - { - for( OsmNodeNamed wp : cor.allpoints ) - { - if ( wp.name.equals( waypoint ) ) - { - if ( wp.ilat != 0 || wp.ilon != 0 ) - { + public void updateWaypointList(String waypoint) { + for (OsmNodeNamed wp : cor.allpoints) { + if (wp.name.equals(waypoint)) { + if (wp.ilat != 0 || wp.ilon != 0) { int nwp = wpList.size(); - if ( nwp == 0 || wpList.get( nwp-1 ) != wp ) - { - wpList.add( wp ); + if (nwp == 0 || wpList.get(nwp - 1) != wp) { + wpList.add(wp); } } return; @@ -490,103 +435,84 @@ public class BRouterView extends View } } - public void startWpDatabaseScan() - { + public void startWpDatabaseScan() { dataBaseScanner = new WpDatabaseScanner(); dataBaseScanner.start(); invalidate(); } - public void saveMaptoolDir( String dir ) - { - ConfigMigration.saveAdditionalMaptoolDir( segmentDir, dir ); - ( (BRouterActivity) getContext() ).showResultMessage( "Success", "please restart to use new config", -1 ); + public void saveMaptoolDir(String dir) { + ConfigMigration.saveAdditionalMaptoolDir(segmentDir, dir); + ((BRouterActivity) getContext()).showResultMessage("Success", "please restart to use new config", -1); } - public void finishWaypointSelection() - { + public void finishWaypointSelection() { needsWaypointSelection = false; } - private List readWpList( BufferedReader br, boolean isNogo ) throws Exception - { - int cnt = Integer.parseInt( br.readLine() ); + private List readWpList(BufferedReader br, boolean isNogo) throws Exception { + int cnt = Integer.parseInt(br.readLine()); List res = new ArrayList(cnt); - for( int i=0; i 0 ? "->" : "" ) + wpList.get( i ).name; + for (int i = 0; i < wpList.size(); i++) + msg += (i > 0 ? "->" : "") + wpList.get(i).name; } - ( (BRouterActivity) getContext() ).showResultMessage( "Select Action", msg, wpList.size() ); + ((BRouterActivity) getContext()).showResultMessage("Select Action", msg, wpList.size()); return; } - try - { + try { waitingForSelection = false; RoutingContext rc = new RoutingContext(); @@ -601,46 +527,41 @@ public class BRouterView extends View int minlat = Integer.MAX_VALUE; OsmNode prev = null; - for ( OsmNode n : wpList ) - { + for (OsmNode n : wpList) { maxlon = Math.max(n.ilon, maxlon); minlon = Math.min(n.ilon, minlon); maxlat = Math.max(n.ilat, maxlat); minlat = Math.min(n.ilat, minlat); - if ( prev != null ) - { - plain_distance += n.calcDistance( prev ); + if (prev != null) { + plain_distance += n.calcDistance(prev); } prev = n; } - toast( "Plain distance = " + plain_distance / 1000. + " km" ); + toast("Plain distance = " + plain_distance / 1000. + " km"); - centerLon = ( maxlon + minlon ) / 2; - centerLat = ( maxlat + minlat ) / 2; + centerLon = (maxlon + minlon) / 2; + centerLat = (maxlat + minlat) / 2; - double[] lonlat2m = CheapRuler.getLonLatToMeterScales( centerLat ); + double[] lonlat2m = CheapRuler.getLonLatToMeterScales(centerLat); double dlon2m = lonlat2m[0]; double dlat2m = lonlat2m[1]; - double difflon = (maxlon - minlon)*dlon2m; - double difflat = (maxlat - minlat)*dlat2m; + double difflon = (maxlon - minlon) * dlon2m; + double difflat = (maxlat - minlat) * dlat2m; - scaleLon = imgw / ( difflon * 1.5 ); - scaleLat = imgh / ( difflat * 1.5 ); + scaleLon = imgw / (difflon * 1.5); + scaleLat = imgh / (difflat * 1.5); scaleMeter2Pixel = Math.min(scaleLon, scaleLat); - scaleLon = scaleMeter2Pixel*dlon2m; - scaleLat = scaleMeter2Pixel*dlat2m; + scaleLon = scaleMeter2Pixel * dlon2m; + scaleLat = scaleMeter2Pixel * dlat2m; startTime = System.currentTimeMillis(); - RoutingContext.prepareNogoPoints( nogoList ); + RoutingContext.prepareNogoPoints(nogoList); rc.nogopoints = nogoList; rc.memoryclass = memoryClass; - if ( memoryClass < 16 ) - { + if (memoryClass < 16) { rc.memoryclass = 16; - } - else if ( memoryClass > 256 ) - { + } else if (memoryClass > 256) { rc.memoryclass = 256; } @@ -648,142 +569,119 @@ public class BRouterView extends View // for profile remote, use ref-track logic same as service interface rc.rawTrackPath = rawTrackPath; - cr = new RoutingEngine( tracksDir + "/brouter", null, segmentDir, wpList, rc ); + cr = new RoutingEngine(tracksDir + "/brouter", null, segmentDir, wpList, rc); cr.start(); invalidate(); - } - catch (Exception e) - { + } catch (Exception e) { String msg = e instanceof IllegalArgumentException ? e.getMessage() : e.toString(); - toast( msg ); + toast(msg); } } - private boolean assertDirectoryExists( String message, File path, String assetZip, String versionTag ) - { + private boolean assertDirectoryExists(String message, File path, String assetZip, String versionTag) { boolean exists = path.exists(); - if ( !exists ) - { + if (!exists) { path.mkdirs(); } - if ( versionTag != null ) - { - File vtag = new File( path, versionTag ); - try - { + if (versionTag != null) { + File vtag = new File(path, versionTag); + try { exists = !vtag.createNewFile(); - } - catch( IOException io ) { } // well.. + } catch (IOException io) { + } // well.. } - if ( !exists ) - { + if (!exists) { // default contents from assets archive - if ( assetZip != null ) - { - try - { + if (assetZip != null) { + try { AssetManager assetManager = getContext().getAssets(); - InputStream is = assetManager.open( assetZip ); - ZipInputStream zis = new ZipInputStream( is ); + InputStream is = assetManager.open(assetZip); + ZipInputStream zis = new ZipInputStream(is); byte[] data = new byte[1024]; - for ( ;; ) - { + for (; ; ) { ZipEntry ze = zis.getNextEntry(); - if ( ze == null ) + if (ze == null) break; String name = ze.getName(); - File outfile = new File( path, name ); + File outfile = new File(path, name); outfile.getParentFile().mkdirs(); - FileOutputStream fos = new FileOutputStream( outfile ); + FileOutputStream fos = new FileOutputStream(outfile); - for ( ;; ) - { - int len = zis.read( data, 0, 1024 ); - if ( len < 0 ) + for (; ; ) { + int len = zis.read(data, 0, 1024); + if (len < 0) break; - fos.write( data, 0, len ); + fos.write(data, 0, len); } fos.close(); } is.close(); return true; - } - catch (IOException io) - { - throw new RuntimeException( "error expanding " + assetZip + ": " + io ); + } catch (IOException io) { + throw new RuntimeException("error expanding " + assetZip + ": " + io); } } } - if ( !path.exists() || !path.isDirectory() ) - throw new IllegalArgumentException( message + ": " + path + " cannot be created" ); + if (!path.exists() || !path.isDirectory()) + throw new IllegalArgumentException(message + ": " + path + " cannot be created"); return false; } - private void paintPosition( int ilon, int ilat, int color, int with ) - { + private void paintPosition(int ilon, int ilat, int color, int with) { int lon = ilon - centerLon; int lat = ilat - centerLat; - int x = imgw / 2 + (int) ( scaleLon * lon ); - int y = imgh / 2 - (int) ( scaleLat * lat ); - for ( int nx = x - with; nx <= x + with; nx++ ) - for ( int ny = y - with; ny <= y + with; ny++ ) - { - if ( nx >= 0 && nx < imgw && ny >= 0 && ny < imgh ) - { + int x = imgw / 2 + (int) (scaleLon * lon); + int y = imgh / 2 - (int) (scaleLat * lat); + for (int nx = x - with; nx <= x + with; nx++) + for (int ny = y - with; ny <= y + with; ny++) { + if (nx >= 0 && nx < imgw && ny >= 0 && ny < imgh) { imgPixels[nx + imgw * ny] = color; } } } - private void paintCircle( Canvas canvas, OsmNodeNamed n, int color, int minradius ) - { + private void paintCircle(Canvas canvas, OsmNodeNamed n, int color, int minradius) { int lon = n.ilon - centerLon; int lat = n.ilat - centerLat; - int x = imgw / 2 + (int) ( scaleLon * lon ); - int y = imgh / 2 - (int) ( scaleLat * lat ); + int x = imgw / 2 + (int) (scaleLon * lon); + int y = imgh / 2 - (int) (scaleLat * lat); - int ir = (int) ( n.radius * scaleMeter2Pixel ); - if ( ir > minradius ) - { + int ir = (int) (n.radius * scaleMeter2Pixel); + if (ir > minradius) { Paint paint = new Paint(); - paint.setColor( Color.RED ); - paint.setStyle( Paint.Style.STROKE ); - canvas.drawCircle( (float) x, (float) y, (float) ir, paint ); + paint.setColor(Color.RED); + paint.setStyle(Paint.Style.STROKE); + canvas.drawCircle((float) x, (float) y, (float) ir, paint); } } - private void paintLine( Canvas canvas, final int ilon0, final int ilat0, final int ilon1, final int ilat1, final Paint paint ) - { + private void paintLine(Canvas canvas, final int ilon0, final int ilat0, final int ilon1, final int ilat1, final Paint paint) { final int lon0 = ilon0 - centerLon; final int lat0 = ilat0 - centerLat; final int lon1 = ilon1 - centerLon; final int lat1 = ilat1 - centerLat; - final int x0 = imgw / 2 + (int) ( scaleLon * lon0 ); - final int y0 = imgh / 2 - (int) ( scaleLat * lat0 ); - final int x1 = imgw / 2 + (int) ( scaleLon * lon1 ); - final int y1 = imgh / 2 - (int) ( scaleLat * lat1 ); - canvas.drawLine( (float) x0, (float) y0, (float) x1, (float) y1, paint ); + final int x0 = imgw / 2 + (int) (scaleLon * lon0); + final int y0 = imgh / 2 - (int) (scaleLat * lat0); + final int x1 = imgw / 2 + (int) (scaleLon * lon1); + final int y1 = imgh / 2 - (int) (scaleLat * lat1); + canvas.drawLine((float) x0, (float) y0, (float) x1, (float) y1, paint); } - private void paintPolygon( Canvas canvas, OsmNogoPolygon p, int minradius ) - { - final int ir = (int) ( p.radius * scaleMeter2Pixel ); - if ( ir > minradius ) - { + private void paintPolygon(Canvas canvas, OsmNogoPolygon p, int minradius) { + final int ir = (int) (p.radius * scaleMeter2Pixel); + if (ir > minradius) { Paint paint = new Paint(); - paint.setColor( Color.RED ); - paint.setStyle( Paint.Style.STROKE ); + paint.setColor(Color.RED); + paint.setStyle(Paint.Style.STROKE); - Point p0 = p.isClosed ? p.points.get(p.points.size()-1) : null; + Point p0 = p.isClosed ? p.points.get(p.points.size() - 1) : null; - for ( final Point p1 : p.points ) - { - if (p0 != null) - { - paintLine( canvas, p0.x, p0.y, p1.x, p1.y, paint ); + for (final Point p1 : p.points) { + if (p0 != null) { + paintLine(canvas, p0.x, p0.y, p1.x, p1.y, paint); } p0 = p1; } @@ -791,13 +689,11 @@ public class BRouterView extends View } @Override - protected void onSizeChanged( int w, int h, int oldw, int oldh ) - { + protected void onSizeChanged(int w, int h, int oldw, int oldh) { } - private void toast( String msg ) - { - Toast.makeText( getContext(), msg, Toast.LENGTH_LONG ).show(); + private void toast(String msg) { + Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); lastDataTime += 4000; // give time for the toast before exiting } @@ -805,59 +701,45 @@ public class BRouterView extends View private long startTime = 0L; @Override - protected void onDraw( Canvas canvas ) - { - try - { - _onDraw( canvas ); - } - catch (Throwable t) - { + protected void onDraw(Canvas canvas) { + try { + _onDraw(canvas); + } catch (Throwable t) { // on out of mem, try to stop the show - if ( cr != null ) + if (cr != null) cr.cleanOnOOM(); cr = null; - try - { - Thread.sleep( 2000 ); + try { + Thread.sleep(2000); + } catch (InterruptedException ie) { } - catch (InterruptedException ie) - { - } - ( (BRouterActivity) getContext() ).showErrorMessage( t.toString() ); + ((BRouterActivity) getContext()).showErrorMessage(t.toString()); waitingForSelection = true; } } - private void showDatabaseScanning( Canvas canvas ) - { - try - { - Thread.sleep( 100 ); - } - catch (InterruptedException ie) - { + private void showDatabaseScanning(Canvas canvas) { + try { + Thread.sleep(100); + } catch (InterruptedException ie) { } Paint paint1 = new Paint(); - paint1.setColor( Color.WHITE ); - paint1.setTextSize( 20 ); + paint1.setColor(Color.WHITE); + paint1.setTextSize(20); Paint paint2 = new Paint(); - paint2.setColor( Color.WHITE ); - paint2.setTextSize( 10 ); + paint2.setColor(Color.WHITE); + paint2.setTextSize(10); String currentDir = dataBaseScanner.getCurrentDir(); String bestGuess = dataBaseScanner.getBestGuess(); - if ( currentDir == null ) // scan finished + if (currentDir == null) // scan finished { - if ( bestGuess.length() == 0 ) - { - ( (BRouterActivity) getContext() ).showErrorMessage( "scan did not find any possible waypoint database" ); - } - else - { - ( (BRouterActivity) getContext() ).showWpDatabaseScanSuccess( bestGuess); + if (bestGuess.length() == 0) { + ((BRouterActivity) getContext()).showErrorMessage("scan did not find any possible waypoint database"); + } else { + ((BRouterActivity) getContext()).showWpDatabaseScanSuccess(bestGuess); } cr = null; dataBaseScanner = null; @@ -865,25 +747,23 @@ public class BRouterView extends View return; } - canvas.drawText( "Scanning:", 10, 30, paint1 ); - canvas.drawText( currentDir, 0, 60, paint2 ); - canvas.drawText( "Best Guess:", 10, 90, paint1 ); - canvas.drawText( bestGuess, 0, 120, paint2 ); - canvas.drawText( "Last Error:", 10, 150, paint1 ); - canvas.drawText( dataBaseScanner.getLastError(), 0, 180, paint2 ); + canvas.drawText("Scanning:", 10, 30, paint1); + canvas.drawText(currentDir, 0, 60, paint2); + canvas.drawText("Best Guess:", 10, 90, paint1); + canvas.drawText(bestGuess, 0, 120, paint2); + canvas.drawText("Last Error:", 10, 150, paint1); + canvas.drawText(dataBaseScanner.getLastError(), 0, 180, paint2); invalidate(); } - private void _onDraw( Canvas canvas ) - { - if ( dataBaseScanner != null ) - { - showDatabaseScanning( canvas ); + private void _onDraw(Canvas canvas) { + if (dataBaseScanner != null) { + showDatabaseScanning(canvas); return; } - if ( waitingForSelection ) + if (waitingForSelection) return; long currentTs = System.currentTimeMillis(); @@ -892,281 +772,218 @@ public class BRouterView extends View while (sleeptime < 200) sleeptime += 500; - try - { - Thread.sleep( sleeptime ); - } - catch (InterruptedException ie) - { + try { + Thread.sleep(sleeptime); + } catch (InterruptedException ie) { } lastTs = System.currentTimeMillis(); - if ( cr == null || cr.isFinished() ) - { - if ( cr != null ) - { - if ( cr.getErrorMessage() != null ) - { - ( (BRouterActivity) getContext() ).showErrorMessage( cr.getErrorMessage() ); + if (cr == null || cr.isFinished()) { + if (cr != null) { + if (cr.getErrorMessage() != null) { + ((BRouterActivity) getContext()).showErrorMessage(cr.getErrorMessage()); cr = null; waitingForSelection = true; return; - } - else - { - String memstat = memoryClass + "mb pathPeak " + ((cr.getPathPeak()+500)/1000) + "k"; + } else { + String memstat = memoryClass + "mb pathPeak " + ((cr.getPathPeak() + 500) / 1000) + "k"; String result = "version = BRouter-" + getContext().getString(R.string.app_version) + "\n" + "mem = " + memstat + "\ndistance = " + cr.getDistance() / 1000. + " km\n" + "filtered ascend = " + cr.getAscend() - + " m\n" + "plain ascend = " + cr.getPlainAscend() + " m\n" + "estimated time = " + cr.getTime(); + + " m\n" + "plain ascend = " + cr.getPlainAscend() + " m\n" + "estimated time = " + cr.getTime(); rawTrack = cr.getFoundRawTrack(); // for profile "remote", always persist referencetrack - if ( cr.getAlternativeIndex() == 0 && rawTrackPath != null ) - { - writeRawTrackToPath( rawTrackPath ); + if (cr.getAlternativeIndex() == 0 && rawTrackPath != null) { + writeRawTrackToPath(rawTrackPath); } String title = "Success"; - if ( cr.getAlternativeIndex() > 0 ) + if (cr.getAlternativeIndex() > 0) title += " / " + cr.getAlternativeIndex() + ". Alternative"; - ( (BRouterActivity) getContext() ).showResultMessage( title, result, rawTrackPath == null ? -1 : -3 ); + ((BRouterActivity) getContext()).showResultMessage(title, result, rawTrackPath == null ? -1 : -3); cr = null; waitingForSelection = true; return; } + } else if (System.currentTimeMillis() > lastDataTime) { + System.exit(0); } - else if ( System.currentTimeMillis() > lastDataTime ) - { - System.exit( 0 ); - } - } - else - { + } else { lastDataTime = System.currentTimeMillis(); imgPixels = new int[imgw * imgh]; int[] openSet = cr.getOpenSet(); - for ( int si = 0; si < openSet.length; si += 2 ) - { - paintPosition( openSet[si], openSet[si + 1], 0xffffff, 1 ); + for (int si = 0; si < openSet.length; si += 2) { + paintPosition(openSet[si], openSet[si + 1], 0xffffff, 1); } // paint nogos on top (red) - for ( int ngi = 0; ngi < nogoList.size(); ngi++ ) - { - OsmNodeNamed n = nogoList.get( ngi ); + for (int ngi = 0; ngi < nogoList.size(); ngi++) { + OsmNodeNamed n = nogoList.get(ngi); int color = 0xff0000; - paintPosition( n.ilon, n.ilat, color, 4 ); + paintPosition(n.ilon, n.ilat, color, 4); } // paint start/end/vias on top (yellow/green/blue) - for ( int wpi = 0; wpi < wpList.size(); wpi++ ) - { - OsmNodeNamed n = wpList.get( wpi ); + for (int wpi = 0; wpi < wpList.size(); wpi++) { + OsmNodeNamed n = wpList.get(wpi); int color = wpi == 0 ? 0xffff00 : wpi < wpList.size() - 1 ? 0xff : 0xff00; - paintPosition( n.ilon, n.ilat, color, 4 ); + paintPosition(n.ilon, n.ilat, color, 4); } - canvas.drawBitmap( imgPixels, 0, imgw, (float) 0., (float) 0., imgw, imgh, false, null ); + canvas.drawBitmap(imgPixels, 0, imgw, (float) 0., (float) 0., imgw, imgh, false, null); // nogo circles if any - for ( int ngi = 0; ngi < nogoList.size(); ngi++ ) - { - OsmNodeNamed n = nogoList.get( ngi ); - if (n instanceof OsmNogoPolygon) - { - paintPolygon( canvas, (OsmNogoPolygon)n, 4 ); - } - else - { + for (int ngi = 0; ngi < nogoList.size(); ngi++) { + OsmNodeNamed n = nogoList.get(ngi); + if (n instanceof OsmNogoPolygon) { + paintPolygon(canvas, (OsmNogoPolygon) n, 4); + } else { int color = 0xff0000; - paintCircle( canvas, n, color, 4 ); + paintCircle(canvas, n, color, 4); } } Paint paint = new Paint(); - paint.setColor( Color.WHITE ); - paint.setTextSize( 20 ); + paint.setColor(Color.WHITE); + paint.setTextSize(20); long mseconds = System.currentTimeMillis() - startTime; long links = cr.getLinksProcessed(); - long perS = ( 1000 * links ) / mseconds; - String msg = "Links: " + cr.getLinksProcessed() + " in " + ( mseconds / 1000 ) + "s (" + perS + " l/s)"; + long perS = (1000 * links) / mseconds; + String msg = "Links: " + cr.getLinksProcessed() + " in " + (mseconds / 1000) + "s (" + perS + " l/s)"; - canvas.drawText( msg, 10, 25, paint ); + canvas.drawText(msg, 10, 25, paint); } // and make sure to redraw asap invalidate(); } - private String guessBaseDir() - { + private String guessBaseDir() { File basedir = Environment.getExternalStorageDirectory(); - try - { - File bd2 = new File( basedir, "external_sd" ); + try { + File bd2 = new File(basedir, "external_sd"); ArrayList basedirGuesses = new ArrayList(); - basedirGuesses.add( basedir.getAbsolutePath() ); + basedirGuesses.add(basedir.getAbsolutePath()); - if ( bd2.exists() ) - { + if (bd2.exists()) { basedir = bd2; - basedirGuesses.add( basedir.getAbsolutePath() ); + basedirGuesses.add(basedir.getAbsolutePath()); } ArrayList rl = new ArrayList(); - for ( String bdg : basedirGuesses ) - { - rl.add( new CoordinateReaderOsmAnd( bdg ) ); - rl.add( new CoordinateReaderLocus( bdg ) ); - rl.add( new CoordinateReaderOrux( bdg ) ); + for (String bdg : basedirGuesses) { + rl.add(new CoordinateReaderOsmAnd(bdg)); + rl.add(new CoordinateReaderLocus(bdg)); + rl.add(new CoordinateReaderOrux(bdg)); } long tmax = 0; CoordinateReader cor = null; - for ( CoordinateReader r : rl ) - { + for (CoordinateReader r : rl) { long t = r.getTimeStamp(); - if ( t > tmax ) - { + if (t > tmax) { tmax = t; cor = r; } } - if ( cor != null ) - { + if (cor != null) { return cor.basedir; } - } - catch (Exception e) - { - System.out.println( "guessBaseDir:" + e ); + } catch (Exception e) { + System.out.println("guessBaseDir:" + e); } return basedir.getAbsolutePath(); } - private void writeRawTrackToMode( String mode ) - { - writeRawTrackToPath( modesDir + "/" + mode + "_rawtrack.dat" ); + private void writeRawTrackToMode(String mode) { + writeRawTrackToPath(modesDir + "/" + mode + "_rawtrack.dat"); } - private void writeRawTrackToPath( String rawTrackPath ) - { - if ( rawTrack != null ) - { - try - { - rawTrack.writeBinary( rawTrackPath ); + private void writeRawTrackToPath(String rawTrackPath) { + if (rawTrack != null) { + try { + rawTrack.writeBinary(rawTrackPath); + } catch (Exception e) { } - catch (Exception e) - { - } - } - else - { - new File( rawTrackPath ).delete(); + } else { + new File(rawTrackPath).delete(); } } - public void startConfigureService() - { + public void startConfigureService() { String[] modes = new String[] - { "foot_short", "foot_fast", "bicycle_short", "bicycle_fast", "motorcar_short", "motorcar_fast" }; + {"foot_short", "foot_fast", "bicycle_short", "bicycle_fast", "motorcar_short", "motorcar_fast"}; boolean[] modesChecked = new boolean[6]; String msg = "Choose service-modes to configure (" + profileName + " [" + nogoVetoList.size() + "])"; - ( (BRouterActivity) getContext() ).selectRoutingModes( modes, modesChecked, msg ); + ((BRouterActivity) getContext()).selectRoutingModes(modes, modesChecked, msg); } - public void configureService( String[] routingModes, boolean[] checkedModes ) - { + public void configureService(String[] routingModes, boolean[] checkedModes) { // read in current config TreeMap map = new TreeMap(); BufferedReader br = null; String modesFile = modesDir + "/serviceconfig.dat"; - try - { - br = new BufferedReader( new FileReader( modesFile ) ); - for ( ;; ) - { + try { + br = new BufferedReader(new FileReader(modesFile)); + for (; ; ) { String line = br.readLine(); - if ( line == null ) + if (line == null) break; - ServiceModeConfig smc = new ServiceModeConfig( line ); - map.put( smc.mode, smc ); + ServiceModeConfig smc = new ServiceModeConfig(line); + map.put(smc.mode, smc); } - } - catch (Exception e) - { - } - finally - { - if ( br != null ) - try - { + } catch (Exception e) { + } finally { + if (br != null) + try { br.close(); - } - catch (Exception ee) - { + } catch (Exception ee) { } } // replace selected modes - for ( int i = 0; i < 6; i++ ) - { - if ( checkedModes[i] ) - { - writeRawTrackToMode( routingModes[i] ); - ServiceModeConfig smc = new ServiceModeConfig( routingModes[i], profileName ); - for ( OsmNodeNamed nogo : nogoVetoList ) - { - smc.nogoVetos.add( nogo.ilon + "," + nogo.ilat ); + for (int i = 0; i < 6; i++) { + if (checkedModes[i]) { + writeRawTrackToMode(routingModes[i]); + ServiceModeConfig smc = new ServiceModeConfig(routingModes[i], profileName); + for (OsmNodeNamed nogo : nogoVetoList) { + smc.nogoVetos.add(nogo.ilon + "," + nogo.ilat); } - map.put( smc.mode, smc ); + map.put(smc.mode, smc); } } // no write new config BufferedWriter bw = null; - StringBuilder msg = new StringBuilder( "Mode mapping is now:\n" ); + StringBuilder msg = new StringBuilder("Mode mapping is now:\n"); msg.append("( ["); - msg.append(nogoVetoList.size()>0?nogoVetoList.size():"..").append( "] counts nogo-vetos)\n" ); - try - { - bw = new BufferedWriter( new FileWriter( modesFile ) ); - for ( ServiceModeConfig smc : map.values() ) - { - bw.write( smc.toLine() ); - bw.write( '\n' ); - msg.append( smc.toString() ).append( '\n' ); + msg.append(nogoVetoList.size() > 0 ? nogoVetoList.size() : "..").append("] counts nogo-vetos)\n"); + try { + bw = new BufferedWriter(new FileWriter(modesFile)); + for (ServiceModeConfig smc : map.values()) { + bw.write(smc.toLine()); + bw.write('\n'); + msg.append(smc.toString()).append('\n'); } - } - catch (Exception e) - { - } - finally - { - if ( bw != null ) - try - { + } catch (Exception e) { + } finally { + if (bw != null) + try { bw.close(); - } - catch (Exception ee) - { + } catch (Exception ee) { } } - ( (BRouterActivity) getContext() ).showModeConfigOverview( msg.toString() ); + ((BRouterActivity) getContext()).showModeConfigOverview(msg.toString()); } - private String readSingleLineFile( File f ) - { - try ( FileInputStream fis = new FileInputStream (f); - InputStreamReader isr = new InputStreamReader (fis); - BufferedReader br = new BufferedReader (isr)) { + private String readSingleLineFile(File f) { + try (FileInputStream fis = new FileInputStream(f); + InputStreamReader isr = new InputStreamReader(fis); + BufferedReader br = new BufferedReader(isr)) { return br.readLine(); - } - catch (Exception e) - { + } catch (Exception e) { return null; } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java index aee03db..2fb29f2 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterWorker.java @@ -1,456 +1,427 @@ -package btools.routingapp; - - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.StringTokenizer; - -import android.os.Bundle; - -import btools.router.OsmNodeNamed; -import btools.router.OsmNogoPolygon; -import btools.router.OsmTrack; -import btools.router.RoutingContext; -import btools.router.RoutingEngine; - -public class BRouterWorker -{ - private static final int OUTPUT_FORMAT_GPX = 0; - private static final int OUTPUT_FORMAT_KML = 1; - private static final int OUTPUT_FORMAT_JSON = 2; - - public String baseDir; - public File segmentDir; - public String profileName; - public String profilePath; - public String rawTrackPath; - public List waypoints; - public List nogoList; - public List nogoPolygonsList; - - public String getTrackFromParams(Bundle params) { - String pathToFileResult = params.getString("pathToFileResult"); - - if (pathToFileResult != null) { - File f = new File(pathToFileResult); - File dir = f.getParentFile(); - if (!dir.exists() || !dir.canWrite()) { - return "file folder does not exists or can not be written!"; - } - } - - long maxRunningTime = 60000; - String sMaxRunningTime = params.getString("maxRunningTime"); - if (sMaxRunningTime != null) { - maxRunningTime = Integer.parseInt(sMaxRunningTime) * 1000; - } - - RoutingContext rc = new RoutingContext(); - rc.rawTrackPath = rawTrackPath; - rc.localFunction = profilePath; - - String tiFormat = params.getString("turnInstructionFormat"); - if (tiFormat != null) { - if ("osmand".equalsIgnoreCase(tiFormat)) { - rc.turnInstructionMode = 3; - } else if ("locus".equalsIgnoreCase(tiFormat)) { - rc.turnInstructionMode = 2; - } - } - if (params.containsKey("timode")) { - rc.turnInstructionMode = params.getInt("timode"); - } - - - if ( params.containsKey( "direction" ) ) - { - rc.startDirection = params.getInt( "direction" ); - } - if ( params.containsKey( "alternativeidx" ) ) { - rc.alternativeIdx = params.getInt( "alternativeidx" ); - } - - readNogos( params ); // add interface provided nogos - if ( nogoList != null ) { - RoutingContext.prepareNogoPoints(nogoList); - if (rc.nogopoints == null) { - rc.nogopoints = nogoList; - } else { - rc.nogopoints.addAll(nogoList); - } - } - if (rc.nogopoints == null) { - rc.nogopoints = nogoPolygonsList; - } else if ( nogoPolygonsList != null ) { - rc.nogopoints.addAll(nogoPolygonsList); - } - List poisList = readPoisList(params); - rc.poipoints = poisList; - - if (params.containsKey("lats")) { - waypoints = readPositions(params); - } - if (params.containsKey("lonlats")) { - waypoints = readLonlats(params); - } - - if (waypoints == null) return "no pts "; - - if (params.containsKey( "extraParams" )) { // add user params - String extraParams = params.getString("extraParams"); - if (rc.keyValues == null) rc.keyValues = new HashMap(); - StringTokenizer tk = new StringTokenizer( extraParams, "?&" ); - while( tk.hasMoreTokens() ) { - String t = tk.nextToken(); - StringTokenizer tk2 = new StringTokenizer( t, "=" ); - if ( tk2.hasMoreTokens() ) { - String key = tk2.nextToken(); - if ( tk2.hasMoreTokens() ) { - String value = tk2.nextToken(); - rc.keyValues.put( key, value ); - } - } - } - } - - - try - { - writeTimeoutData( rc ); - } - catch( Exception e ) {} - - RoutingEngine cr = new RoutingEngine( null, null, segmentDir, waypoints, rc ); - cr.quite = true; - cr.doRun( maxRunningTime ); - - // store new reference track if any - // (can exist for timed-out search) - if ( cr.getFoundRawTrack() != null ) - { - try - { - cr.getFoundRawTrack().writeBinary( rawTrackPath ); - } - catch( Exception e ) {} - } - - if ( cr.getErrorMessage() != null ) - { - return cr.getErrorMessage(); - } - - String format = params.getString("trackFormat"); - int writeFromat = OUTPUT_FORMAT_GPX; - if (format != null) { - if ("kml".equals(format)) writeFromat = OUTPUT_FORMAT_KML; - if ("json".equals(format)) writeFromat = OUTPUT_FORMAT_JSON; - } - - OsmTrack track = cr.getFoundTrack(); - if ( track != null ) - { - if (params.containsKey("exportWaypoints")) { - track.exportWaypoints = (params.getInt("exportWaypoints",0) == 1 ); - } - if ( pathToFileResult == null ) - { - switch ( writeFromat ) { - case OUTPUT_FORMAT_GPX: return track.formatAsGpx(); - case OUTPUT_FORMAT_KML: return track.formatAsKml(); - case OUTPUT_FORMAT_JSON: return track.formatAsGeoJson(); - default: return track.formatAsGpx(); - } - - } - try - { - switch ( writeFromat ) { - case OUTPUT_FORMAT_GPX: track.writeGpx(pathToFileResult); break; - case OUTPUT_FORMAT_KML: track.writeKml(pathToFileResult); break; - case OUTPUT_FORMAT_JSON: track.writeJson(pathToFileResult); break; - default: track.writeGpx(pathToFileResult); break; - } - } - catch( Exception e ) - { - return "error writing file: " + e; - } - } - return null; - } - - private List readPositions( Bundle params ) - { - List wplist = new ArrayList(); - - double[] lats = params.getDoubleArray("lats"); - double[] lons = params.getDoubleArray("lons"); - - if (lats == null || lats.length < 2 || lons == null || lons.length < 2) - { - throw new IllegalArgumentException( "we need two lat/lon points at least!" ); - } - - for( int i=0; i readLonlats(Bundle params ) - { - List wplist = new ArrayList(); - - String lonLats = params.getString( "lonlats" ); - if (lonLats == null) throw new IllegalArgumentException( "lonlats parameter not set" ); - - String[] coords = lonLats.split("\\|"); - if (coords.length < 2) - throw new IllegalArgumentException( "we need two lat/lon points at least!" ); - - for (int i = 0; i < coords.length; i++) - { - String[] lonLat = coords[i].split(","); - if (lonLat.length < 2) - throw new IllegalArgumentException( "we need two lat/lon points at least!" ); - wplist.add( readPosition( lonLat[0], lonLat[1], "via" + i ) ); - } - - wplist.get(0).name = "from"; - wplist.get(wplist.size()-1).name = "to"; - - return wplist; - } - - private static OsmNodeNamed readPosition( String vlon, String vlat, String name ) - { - if ( vlon == null ) throw new IllegalArgumentException( "lon " + name + " not found in input" ); - if ( vlat == null ) throw new IllegalArgumentException( "lat " + name + " not found in input" ); - - return readPosition(Double.parseDouble( vlon ), Double.parseDouble( vlat ), name); - } - - private static OsmNodeNamed readPosition( double lon, double lat, String name ) - { - OsmNodeNamed n = new OsmNodeNamed(); - n.name = name; - n.ilon = (int)( ( lon + 180. ) *1000000. + 0.5); - n.ilat = (int)( ( lat + 90. ) *1000000. + 0.5); - return n; - } - - - private void readNogos( Bundle params ) - { - if (params.containsKey("nogoLats")) { - double[] lats = params.getDoubleArray("nogoLats"); - double[] lons = params.getDoubleArray("nogoLons"); - double[] radi = params.getDoubleArray("nogoRadi"); - - if (lats == null || lons == null || radi == null) return; - - for (int i = 0; i < lats.length && i < lons.length && i < radi.length; i++) { - OsmNodeNamed n = new OsmNodeNamed(); - n.name = "nogo" + (int) radi[i]; - n.ilon = (int) ((lons[i] + 180.) * 1000000. + 0.5); - n.ilat = (int) ((lats[i] + 90.) * 1000000. + 0.5); - n.isNogo = true; - n.nogoWeight = Double.NaN; - AppLogger.log("added interface provided nogo: " + n); - nogoList.add(n); - } - } - if (params.containsKey("nogos")) { - nogoList = readNogoList(params); - } - if (params.containsKey("polylines") || - params.containsKey("polygons")) { - nogoPolygonsList = readNogoPolygons(params); - } - } - - private List readNogoList(Bundle params) - { - // lon,lat,radius|... - String nogos = params.getString( "nogos" ); - if ( nogos == null ) return null; - - String[] lonLatRadList = nogos.split("\\|"); - - List nogoList = new ArrayList(); - for (int i = 0; i < lonLatRadList.length; i++) - { - String[] lonLatRad = lonLatRadList[i].split(","); - String nogoWeight = "NaN"; - if (lonLatRad.length > 3) { - nogoWeight = lonLatRad[3]; - } - nogoList.add(readNogo(lonLatRad[0], lonLatRad[1], lonLatRad[2], nogoWeight)); - } - - return nogoList; - } - - private static OsmNodeNamed readNogo( String lon, String lat, String radius, String nogoWeight ) - { - double weight = "undefined".equals( nogoWeight ) ? Double.NaN : Double.parseDouble( nogoWeight ); - return readNogo(Double.parseDouble( lon ), Double.parseDouble( lat ), Integer.parseInt( radius ), weight ); - } - - private static OsmNodeNamed readNogo( double lon, double lat, int radius, double nogoWeight ) - { - OsmNodeNamed n = new OsmNodeNamed(); - n.name = "nogo" + radius; - n.ilon = (int)( ( lon + 180. ) *1000000. + 0.5); - n.ilat = (int)( ( lat + 90. ) *1000000. + 0.5); - n.isNogo = true; - n.nogoWeight = nogoWeight; - return n; - } - - private List readNogoPolygons(Bundle params) - { - List result = new ArrayList(); - parseNogoPolygons( params.getString("polylines"), result, false ); - parseNogoPolygons( params.getString("polygons"), result, true ); - return result.size() > 0 ? result : null; - } - - private static void parseNogoPolygons(String polygons, List result, boolean closed ) - { - if ( polygons != null ) - { - OsmNogoPolygon polygon = new OsmNogoPolygon(closed); - polygon.name = "nogopoly" ; - String nogoWeight = "NaN"; - String[] polygonList = polygons.split("\\|"); - for (int i = 0; i < polygonList.length; i++) - { - String[] lonLatList = polygonList[i].split(","); - if ( lonLatList.length > 1 ) - { - int j; - for (j = 0; j < 2 * (lonLatList.length / 2) - 1;) - { - String slon = lonLatList[j++]; - String slat = lonLatList[j++]; - int lon = (int)( ( Double.parseDouble(slon) + 180. ) *1000000. + 0.5); - int lat = (int)( ( Double.parseDouble(slat) + 90. ) *1000000. + 0.5); - polygon.addVertex(lon, lat); - } - - - if (j < lonLatList.length) { - nogoWeight = lonLatList[j]; - } - } - } - polygon.nogoWeight = Double.parseDouble( nogoWeight ); - - if ( polygon.points.size() > 0 ) - { - polygon.calcBoundingCircle(); - result.add(polygon); - } - } - } - - private static void parseNogoPolygons_alt(String polygons, List result, boolean closed ) - { - if ( polygons != null ) - { - String[] polygonList = polygons.split("\\|"); - for (int i = 0; i < polygonList.length; i++) - { - String[] lonLatList = polygonList[i].split(","); - if ( lonLatList.length > 1 ) - { - OsmNogoPolygon polygon = new OsmNogoPolygon(closed); - polygon.name = "nogo" + i; - int j; - for (j = 0; j < 2 * (lonLatList.length / 2) - 1;) - { - String slon = lonLatList[j++]; - String slat = lonLatList[j++]; - int lon = (int)( ( Double.parseDouble(slon) + 180. ) *1000000. + 0.5); - int lat = (int)( ( Double.parseDouble(slat) + 90. ) *1000000. + 0.5); - polygon.addVertex(lon, lat); - } - - String nogoWeight = "NaN"; - if (j < lonLatList.length) { - nogoWeight = lonLatList[j]; - } - polygon.nogoWeight = Double.parseDouble( nogoWeight ); - - if ( polygon.points.size() > 0 ) - { - polygon.calcBoundingCircle(); - result.add(polygon); - } - } - } - } - } - - private List readPoisList(Bundle params ) - { - // lon,lat,name|... - String pois = params.getString( "pois" ); - if ( pois == null ) return null; - - String[] lonLatNameList = pois.split("\\|"); - - List poisList = new ArrayList(); - for (int i = 0; i < lonLatNameList.length; i++) - { - String[] lonLatName = lonLatNameList[i].split(","); - - OsmNodeNamed n = new OsmNodeNamed(); - n.ilon = (int)( ( Double.parseDouble(lonLatName[0]) + 180. ) *1000000. + 0.5); - n.ilat = (int)( ( Double.parseDouble(lonLatName[1]) + 90. ) *1000000. + 0.5); - n.name = lonLatName[2]; - poisList.add(n); - } - - return poisList; - } - - private void writeTimeoutData( RoutingContext rc ) throws Exception - { - String timeoutFile = baseDir + "/brouter/modes/timeoutdata.txt"; - - BufferedWriter bw = new BufferedWriter( new FileWriter( timeoutFile ) ); - bw.write( profileName ); - bw.write( "\n" ); - bw.write( rc.rawTrackPath ); - bw.write( "\n" ); - writeWPList( bw, waypoints ); - writeWPList( bw, nogoList ); - bw.close(); - } - - private void writeWPList( BufferedWriter bw, List wps ) throws Exception - { - bw.write( wps.size() + "\n" ); - for( OsmNodeNamed wp : wps ) - { - bw.write( wp.toString() ); - bw.write( "\n" ); - } - } -} - +package btools.routingapp; + + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.StringTokenizer; + +import android.os.Bundle; + +import btools.router.OsmNodeNamed; +import btools.router.OsmNogoPolygon; +import btools.router.OsmTrack; +import btools.router.RoutingContext; +import btools.router.RoutingEngine; + +public class BRouterWorker { + private static final int OUTPUT_FORMAT_GPX = 0; + private static final int OUTPUT_FORMAT_KML = 1; + private static final int OUTPUT_FORMAT_JSON = 2; + + public String baseDir; + public File segmentDir; + public String profileName; + public String profilePath; + public String rawTrackPath; + public List waypoints; + public List nogoList; + public List nogoPolygonsList; + + public String getTrackFromParams(Bundle params) { + String pathToFileResult = params.getString("pathToFileResult"); + + if (pathToFileResult != null) { + File f = new File(pathToFileResult); + File dir = f.getParentFile(); + if (!dir.exists() || !dir.canWrite()) { + return "file folder does not exists or can not be written!"; + } + } + + long maxRunningTime = 60000; + String sMaxRunningTime = params.getString("maxRunningTime"); + if (sMaxRunningTime != null) { + maxRunningTime = Integer.parseInt(sMaxRunningTime) * 1000; + } + + RoutingContext rc = new RoutingContext(); + rc.rawTrackPath = rawTrackPath; + rc.localFunction = profilePath; + + String tiFormat = params.getString("turnInstructionFormat"); + if (tiFormat != null) { + if ("osmand".equalsIgnoreCase(tiFormat)) { + rc.turnInstructionMode = 3; + } else if ("locus".equalsIgnoreCase(tiFormat)) { + rc.turnInstructionMode = 2; + } + } + if (params.containsKey("timode")) { + rc.turnInstructionMode = params.getInt("timode"); + } + + + if (params.containsKey("direction")) { + rc.startDirection = params.getInt("direction"); + } + if (params.containsKey("alternativeidx")) { + rc.alternativeIdx = params.getInt("alternativeidx"); + } + + readNogos(params); // add interface provided nogos + if (nogoList != null) { + RoutingContext.prepareNogoPoints(nogoList); + if (rc.nogopoints == null) { + rc.nogopoints = nogoList; + } else { + rc.nogopoints.addAll(nogoList); + } + } + if (rc.nogopoints == null) { + rc.nogopoints = nogoPolygonsList; + } else if (nogoPolygonsList != null) { + rc.nogopoints.addAll(nogoPolygonsList); + } + List poisList = readPoisList(params); + rc.poipoints = poisList; + + if (params.containsKey("lats")) { + waypoints = readPositions(params); + } + if (params.containsKey("lonlats")) { + waypoints = readLonlats(params); + } + + if (waypoints == null) return "no pts "; + + if (params.containsKey("extraParams")) { // add user params + String extraParams = params.getString("extraParams"); + if (rc.keyValues == null) rc.keyValues = new HashMap(); + StringTokenizer tk = new StringTokenizer(extraParams, "?&"); + while (tk.hasMoreTokens()) { + String t = tk.nextToken(); + StringTokenizer tk2 = new StringTokenizer(t, "="); + if (tk2.hasMoreTokens()) { + String key = tk2.nextToken(); + if (tk2.hasMoreTokens()) { + String value = tk2.nextToken(); + rc.keyValues.put(key, value); + } + } + } + } + + + try { + writeTimeoutData(rc); + } catch (Exception e) { + } + + RoutingEngine cr = new RoutingEngine(null, null, segmentDir, waypoints, rc); + cr.quite = true; + cr.doRun(maxRunningTime); + + // store new reference track if any + // (can exist for timed-out search) + if (cr.getFoundRawTrack() != null) { + try { + cr.getFoundRawTrack().writeBinary(rawTrackPath); + } catch (Exception e) { + } + } + + if (cr.getErrorMessage() != null) { + return cr.getErrorMessage(); + } + + String format = params.getString("trackFormat"); + int writeFromat = OUTPUT_FORMAT_GPX; + if (format != null) { + if ("kml".equals(format)) writeFromat = OUTPUT_FORMAT_KML; + if ("json".equals(format)) writeFromat = OUTPUT_FORMAT_JSON; + } + + OsmTrack track = cr.getFoundTrack(); + if (track != null) { + if (params.containsKey("exportWaypoints")) { + track.exportWaypoints = (params.getInt("exportWaypoints", 0) == 1); + } + if (pathToFileResult == null) { + switch (writeFromat) { + case OUTPUT_FORMAT_GPX: + return track.formatAsGpx(); + case OUTPUT_FORMAT_KML: + return track.formatAsKml(); + case OUTPUT_FORMAT_JSON: + return track.formatAsGeoJson(); + default: + return track.formatAsGpx(); + } + + } + try { + switch (writeFromat) { + case OUTPUT_FORMAT_GPX: + track.writeGpx(pathToFileResult); + break; + case OUTPUT_FORMAT_KML: + track.writeKml(pathToFileResult); + break; + case OUTPUT_FORMAT_JSON: + track.writeJson(pathToFileResult); + break; + default: + track.writeGpx(pathToFileResult); + break; + } + } catch (Exception e) { + return "error writing file: " + e; + } + } + return null; + } + + private List readPositions(Bundle params) { + List wplist = new ArrayList(); + + double[] lats = params.getDoubleArray("lats"); + double[] lons = params.getDoubleArray("lons"); + + if (lats == null || lats.length < 2 || lons == null || lons.length < 2) { + throw new IllegalArgumentException("we need two lat/lon points at least!"); + } + + for (int i = 0; i < lats.length && i < lons.length; i++) { + OsmNodeNamed n = new OsmNodeNamed(); + n.name = "via" + i; + n.ilon = (int) ((lons[i] + 180.) * 1000000. + 0.5); + n.ilat = (int) ((lats[i] + 90.) * 1000000. + 0.5); + wplist.add(n); + } + wplist.get(0).name = "from"; + wplist.get(wplist.size() - 1).name = "to"; + + return wplist; + } + + private List readLonlats(Bundle params) { + List wplist = new ArrayList(); + + String lonLats = params.getString("lonlats"); + if (lonLats == null) throw new IllegalArgumentException("lonlats parameter not set"); + + String[] coords = lonLats.split("\\|"); + if (coords.length < 2) + throw new IllegalArgumentException("we need two lat/lon points at least!"); + + for (int i = 0; i < coords.length; i++) { + String[] lonLat = coords[i].split(","); + if (lonLat.length < 2) + throw new IllegalArgumentException("we need two lat/lon points at least!"); + wplist.add(readPosition(lonLat[0], lonLat[1], "via" + i)); + } + + wplist.get(0).name = "from"; + wplist.get(wplist.size() - 1).name = "to"; + + return wplist; + } + + private static OsmNodeNamed readPosition(String vlon, String vlat, String name) { + if (vlon == null) throw new IllegalArgumentException("lon " + name + " not found in input"); + if (vlat == null) throw new IllegalArgumentException("lat " + name + " not found in input"); + + return readPosition(Double.parseDouble(vlon), Double.parseDouble(vlat), name); + } + + private static OsmNodeNamed readPosition(double lon, double lat, String name) { + OsmNodeNamed n = new OsmNodeNamed(); + n.name = name; + n.ilon = (int) ((lon + 180.) * 1000000. + 0.5); + n.ilat = (int) ((lat + 90.) * 1000000. + 0.5); + return n; + } + + + private void readNogos(Bundle params) { + if (params.containsKey("nogoLats")) { + double[] lats = params.getDoubleArray("nogoLats"); + double[] lons = params.getDoubleArray("nogoLons"); + double[] radi = params.getDoubleArray("nogoRadi"); + + if (lats == null || lons == null || radi == null) return; + + for (int i = 0; i < lats.length && i < lons.length && i < radi.length; i++) { + OsmNodeNamed n = new OsmNodeNamed(); + n.name = "nogo" + (int) radi[i]; + n.ilon = (int) ((lons[i] + 180.) * 1000000. + 0.5); + n.ilat = (int) ((lats[i] + 90.) * 1000000. + 0.5); + n.isNogo = true; + n.nogoWeight = Double.NaN; + AppLogger.log("added interface provided nogo: " + n); + nogoList.add(n); + } + } + if (params.containsKey("nogos")) { + nogoList = readNogoList(params); + } + if (params.containsKey("polylines") || + params.containsKey("polygons")) { + nogoPolygonsList = readNogoPolygons(params); + } + } + + private List readNogoList(Bundle params) { + // lon,lat,radius|... + String nogos = params.getString("nogos"); + if (nogos == null) return null; + + String[] lonLatRadList = nogos.split("\\|"); + + List nogoList = new ArrayList(); + for (int i = 0; i < lonLatRadList.length; i++) { + String[] lonLatRad = lonLatRadList[i].split(","); + String nogoWeight = "NaN"; + if (lonLatRad.length > 3) { + nogoWeight = lonLatRad[3]; + } + nogoList.add(readNogo(lonLatRad[0], lonLatRad[1], lonLatRad[2], nogoWeight)); + } + + return nogoList; + } + + private static OsmNodeNamed readNogo(String lon, String lat, String radius, String nogoWeight) { + double weight = "undefined".equals(nogoWeight) ? Double.NaN : Double.parseDouble(nogoWeight); + return readNogo(Double.parseDouble(lon), Double.parseDouble(lat), Integer.parseInt(radius), weight); + } + + private static OsmNodeNamed readNogo(double lon, double lat, int radius, double nogoWeight) { + OsmNodeNamed n = new OsmNodeNamed(); + n.name = "nogo" + radius; + n.ilon = (int) ((lon + 180.) * 1000000. + 0.5); + n.ilat = (int) ((lat + 90.) * 1000000. + 0.5); + n.isNogo = true; + n.nogoWeight = nogoWeight; + return n; + } + + private List readNogoPolygons(Bundle params) { + List result = new ArrayList(); + parseNogoPolygons(params.getString("polylines"), result, false); + parseNogoPolygons(params.getString("polygons"), result, true); + return result.size() > 0 ? result : null; + } + + private static void parseNogoPolygons(String polygons, List result, boolean closed) { + if (polygons != null) { + OsmNogoPolygon polygon = new OsmNogoPolygon(closed); + polygon.name = "nogopoly"; + String nogoWeight = "NaN"; + String[] polygonList = polygons.split("\\|"); + for (int i = 0; i < polygonList.length; i++) { + String[] lonLatList = polygonList[i].split(","); + if (lonLatList.length > 1) { + int j; + for (j = 0; j < 2 * (lonLatList.length / 2) - 1; ) { + String slon = lonLatList[j++]; + String slat = lonLatList[j++]; + int lon = (int) ((Double.parseDouble(slon) + 180.) * 1000000. + 0.5); + int lat = (int) ((Double.parseDouble(slat) + 90.) * 1000000. + 0.5); + polygon.addVertex(lon, lat); + } + + + if (j < lonLatList.length) { + nogoWeight = lonLatList[j]; + } + } + } + polygon.nogoWeight = Double.parseDouble(nogoWeight); + + if (polygon.points.size() > 0) { + polygon.calcBoundingCircle(); + result.add(polygon); + } + } + } + + private static void parseNogoPolygons_alt(String polygons, List result, boolean closed) { + if (polygons != null) { + String[] polygonList = polygons.split("\\|"); + for (int i = 0; i < polygonList.length; i++) { + String[] lonLatList = polygonList[i].split(","); + if (lonLatList.length > 1) { + OsmNogoPolygon polygon = new OsmNogoPolygon(closed); + polygon.name = "nogo" + i; + int j; + for (j = 0; j < 2 * (lonLatList.length / 2) - 1; ) { + String slon = lonLatList[j++]; + String slat = lonLatList[j++]; + int lon = (int) ((Double.parseDouble(slon) + 180.) * 1000000. + 0.5); + int lat = (int) ((Double.parseDouble(slat) + 90.) * 1000000. + 0.5); + polygon.addVertex(lon, lat); + } + + String nogoWeight = "NaN"; + if (j < lonLatList.length) { + nogoWeight = lonLatList[j]; + } + polygon.nogoWeight = Double.parseDouble(nogoWeight); + + if (polygon.points.size() > 0) { + polygon.calcBoundingCircle(); + result.add(polygon); + } + } + } + } + } + + private List readPoisList(Bundle params) { + // lon,lat,name|... + String pois = params.getString("pois"); + if (pois == null) return null; + + String[] lonLatNameList = pois.split("\\|"); + + List poisList = new ArrayList(); + for (int i = 0; i < lonLatNameList.length; i++) { + String[] lonLatName = lonLatNameList[i].split(","); + + OsmNodeNamed n = new OsmNodeNamed(); + n.ilon = (int) ((Double.parseDouble(lonLatName[0]) + 180.) * 1000000. + 0.5); + n.ilat = (int) ((Double.parseDouble(lonLatName[1]) + 90.) * 1000000. + 0.5); + n.name = lonLatName[2]; + poisList.add(n); + } + + return poisList; + } + + private void writeTimeoutData(RoutingContext rc) throws Exception { + String timeoutFile = baseDir + "/brouter/modes/timeoutdata.txt"; + + BufferedWriter bw = new BufferedWriter(new FileWriter(timeoutFile)); + bw.write(profileName); + bw.write("\n"); + bw.write(rc.rawTrackPath); + bw.write("\n"); + writeWPList(bw, waypoints); + writeWPList(bw, nogoList); + bw.close(); + } + + private void writeWPList(BufferedWriter bw, List wps) throws Exception { + bw.write(wps.size() + "\n"); + for (OsmNodeNamed wp : wps) { + bw.write(wp.toString()); + bw.write("\n"); + } + } +} + diff --git a/brouter-routing-app/src/main/java/btools/routingapp/ConfigHelper.java b/brouter-routing-app/src/main/java/btools/routingapp/ConfigHelper.java index 9575871..6d6ce4c 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/ConfigHelper.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/ConfigHelper.java @@ -10,31 +10,25 @@ import java.io.File; import android.content.Context; -public class ConfigHelper -{ - public static File getBaseDir( Context ctx ) - { +public class ConfigHelper { + public static File getBaseDir(Context ctx) { // get base dir from private file - try (InputStream configInput = ctx.openFileInput( "config15.dat" ); - InputStreamReader isr = new InputStreamReader( configInput ); + try (InputStream configInput = ctx.openFileInput("config15.dat"); + InputStreamReader isr = new InputStreamReader(configInput); BufferedReader br = new BufferedReader(isr)) { - return new File ( br.readLine() ); - } - catch (Exception e) - { + return new File(br.readLine()); + } catch (Exception e) { return null; } } - public static void writeBaseDir( Context ctx, File baseDir ) - { - try (OutputStream configOutput = ctx.openFileOutput( "config15.dat", Context.MODE_PRIVATE ); - OutputStreamWriter osw = new OutputStreamWriter( configOutput ); - BufferedWriter bw = new BufferedWriter( osw)) { - bw.write( baseDir.getAbsolutePath () ); - bw.write( '\n' ); - } - catch (Exception e){ /* ignore */ } + public static void writeBaseDir(Context ctx, File baseDir) { + try (OutputStream configOutput = ctx.openFileOutput("config15.dat", Context.MODE_PRIVATE); + OutputStreamWriter osw = new OutputStreamWriter(configOutput); + BufferedWriter bw = new BufferedWriter(osw)) { + bw.write(baseDir.getAbsolutePath()); + bw.write('\n'); + } catch (Exception e) { /* ignore */ } } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/ConfigMigration.java b/brouter-routing-app/src/main/java/btools/routingapp/ConfigMigration.java index 593b338..40e9db8 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/ConfigMigration.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/ConfigMigration.java @@ -8,136 +8,101 @@ import java.io.FileWriter; import java.util.ArrayList; import java.util.List; -public class ConfigMigration -{ - public static void tryMigrateStorageConfig( File srcFile, File dstFile ) - { - if ( !srcFile.exists() ) return; - +public class ConfigMigration { + public static void tryMigrateStorageConfig(File srcFile, File dstFile) { + if (!srcFile.exists()) return; + String ssd = null; String amd = null; BufferedReader br = null; BufferedWriter bw = null; - try - { - br = new BufferedReader( new FileReader( srcFile ) ); - for ( ;; ) - { + try { + br = new BufferedReader(new FileReader(srcFile)); + for (; ; ) { String line = br.readLine(); - if ( line == null ) break; - if ( line.trim().startsWith( "secondary_segment_dir=" ) ) - { - if ( !"secondary_segment_dir=../segments2".equals( line ) ) - { + if (line == null) break; + if (line.trim().startsWith("secondary_segment_dir=")) { + if (!"secondary_segment_dir=../segments2".equals(line)) { ssd = line; } } - if ( line.trim().startsWith( "additional_maptool_dir=" ) ) - { + if (line.trim().startsWith("additional_maptool_dir=")) { amd = line; } } br.close(); List lines = new ArrayList(); - br = new BufferedReader( new FileReader( dstFile ) ); - for ( ;; ) - { + br = new BufferedReader(new FileReader(dstFile)); + for (; ; ) { String line = br.readLine(); - if ( line == null ) break; - if ( ssd != null && line.trim().startsWith( "secondary_segment_dir=" ) ) - { + if (line == null) break; + if (ssd != null && line.trim().startsWith("secondary_segment_dir=")) { line = ssd; } - if ( amd != null && line.trim().startsWith( "#additional_maptool_dir=" ) ) - { + if (amd != null && line.trim().startsWith("#additional_maptool_dir=")) { line = amd; } - lines.add( line ); + lines.add(line); } br.close(); br = null; - bw = new BufferedWriter( new FileWriter( dstFile ) ); - for( String line: lines ) - { - bw.write( line + "\n" ); - } - } - catch (Exception e) { /* ignore */ } - finally - { - if ( br != null ) - { - try - { + bw = new BufferedWriter(new FileWriter(dstFile)); + for (String line : lines) { + bw.write(line + "\n"); + } + } catch (Exception e) { /* ignore */ } finally { + if (br != null) { + try { br.close(); - } - catch (Exception ee) { /* ignore */ } + } catch (Exception ee) { /* ignore */ } } - if ( bw != null ) - { - try - { + if (bw != null) { + try { bw.close(); - } - catch (Exception ee) { /* ignore */ } + } catch (Exception ee) { /* ignore */ } } } } - public static File saveAdditionalMaptoolDir( File segmentDir, String value ) - { - return saveStorageLocation( segmentDir, "additional_maptool_dir=", value ); + public static File saveAdditionalMaptoolDir(File segmentDir, String value) { + return saveStorageLocation(segmentDir, "additional_maptool_dir=", value); } - private static File saveStorageLocation( File segmentDir, String tag, String value ) - { + private static File saveStorageLocation(File segmentDir, String tag, String value) { File res = null; BufferedReader br = null; BufferedWriter bw = null; - File configFile = new File (segmentDir, "storageconfig.txt"); + File configFile = new File(segmentDir, "storageconfig.txt"); List lines = new ArrayList(); - try - { - br = new BufferedReader( new FileReader( configFile ) ); - for ( ;; ) - { + try { + br = new BufferedReader(new FileReader(configFile)); + for (; ; ) { String line = br.readLine(); - if ( line == null ) break; - if ( !line.trim().startsWith( tag ) ) - { - lines.add( line ); + if (line == null) break; + if (!line.trim().startsWith(tag)) { + lines.add(line); } } - lines.add( tag + value ); + lines.add(tag + value); br.close(); br = null; - bw = new BufferedWriter( new FileWriter( configFile ) ); - for( String line : lines ) - { - bw.write( line + "\r\n" ); + bw = new BufferedWriter(new FileWriter(configFile)); + for (String line : lines) { + bw.write(line + "\r\n"); } - } - catch (Exception e) { /* ignore */ } - finally - { - if ( br != null ) - { - try - { + } catch (Exception e) { /* ignore */ } finally { + if (br != null) { + try { br.close(); - } - catch (Exception ee) { /* ignore */ } + } catch (Exception ee) { /* ignore */ } } - if ( bw != null ) - { - try - { + if (bw != null) { + try { bw.close(); - } - catch (Exception ee) { /* ignore */ } + } catch (Exception ee) { /* ignore */ } } } return res; diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java index a6b7942..8251e1e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java @@ -11,14 +11,14 @@ import java.util.Set; import java.util.TreeMap; import android.os.Environment; + import btools.router.OsmNodeNamed; import btools.router.RoutingHelper; /** * Read coordinates from a gpx-file */ -public abstract class CoordinateReader -{ +public abstract class CoordinateReader { public List waypoints; public List nogopoints; public String basedir; @@ -27,16 +27,15 @@ public abstract class CoordinateReader private boolean nogosOnly; - private Map> allpointsMap; + private Map> allpointsMap; public List allpoints; - - private HashMap pointmap; + + private HashMap pointmap; protected static String[] posnames - = new String[]{ "from", "via1", "via2", "via3", "via4", "via5", "via6", "via7", "via8", "via9", "to" }; + = new String[]{"from", "via1", "via2", "via3", "via4", "via5", "via6", "via7", "via8", "via9", "to"}; - public CoordinateReader( String basedir ) - { + public CoordinateReader(String basedir) { this.basedir = basedir; } @@ -44,30 +43,23 @@ public abstract class CoordinateReader public abstract int getTurnInstructionMode(); - public void readAllPoints() throws Exception - { - allpointsMap = new TreeMap>(); + public void readAllPoints() throws Exception { + allpointsMap = new TreeMap>(); readFromTo(); allpoints = new ArrayList(); Set names = new HashSet(); - for( String category : allpointsMap.keySet() ) - { - Map cat = allpointsMap.get( category ); - if ( cat.size() < 101 ) - { - for ( OsmNodeNamed wp : cat.values() ) - { - if ( names.add( wp.name ) ) - { - allpoints.add( wp ); + for (String category : allpointsMap.keySet()) { + Map cat = allpointsMap.get(category); + if (cat.size() < 101) { + for (OsmNodeNamed wp : cat.values()) { + if (names.add(wp.name)) { + allpoints.add(wp); } } - } - else - { + } else { OsmNodeNamed nocatHint = new OsmNodeNamed(); nocatHint.name = ""; - allpoints.add( nocatHint); + allpoints.add(nocatHint); } } } @@ -75,87 +67,69 @@ public abstract class CoordinateReader /* * read the from, to and via-positions from a gpx-file */ - public void readFromTo() throws Exception - { - pointmap = new HashMap(); + public void readFromTo() throws Exception { + pointmap = new HashMap(); waypoints = new ArrayList(); nogopoints = new ArrayList(); readPointmap(); boolean fromToMissing = false; - for( int i=0; i cat = allpointsMap.get( category ); - if ( cat == null ) - { + protected void checkAddPoint(String category, OsmNodeNamed n) { + if (allpointsMap != null) { + if (category == null) category = ""; + Map cat = allpointsMap.get(category); + if (cat == null) { cat = new TreeMap(); - allpointsMap.put( category, cat ); + allpointsMap.put(category, cat); } - if ( cat.size() < 101 ) - { - cat.put( n.name, n ); + if (cat.size() < 101) { + cat.put(n.name, n); } return; } - + boolean isKnown = false; - for( int i=0; i rl = new ArrayList(); @@ -210,8 +184,7 @@ public abstract class CoordinateReader } } } - if ( cor == null ) - { + if (cor == null) { cor = new CoordinateReaderInternal(basedir); } cor.nogosOnly = nogosOnly; diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java index 7a7927f..ccae01d 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java @@ -21,26 +21,20 @@ import btools.router.OsmNogoPolygon; /** * Read coordinates from a gpx-file */ -public class CoordinateReaderInternal extends CoordinateReader -{ +public class CoordinateReaderInternal extends CoordinateReader { private String internalDir; - public CoordinateReaderInternal(String basedir ) - { - this( basedir, false ); + public CoordinateReaderInternal(String basedir) { + this(basedir, false); } - public CoordinateReaderInternal(String basedir, boolean shortPath ) - { - super( basedir ); - if ( shortPath ) - { + public CoordinateReaderInternal(String basedir, boolean shortPath) { + super(basedir); + if (shortPath) { internalDir = basedir; tracksdir = "/tracks"; rootdir = ""; - } - else - { + } else { internalDir = basedir + "/brouter/import"; tracksdir = "/brouter/import/tracks"; rootdir = "/brouter/import"; @@ -48,16 +42,14 @@ public class CoordinateReaderInternal extends CoordinateReader } @Override - public long getTimeStamp() throws Exception - { - long t1 = new File( internalDir + "/favourites_bak.gpx" ).lastModified(); - long t2 = new File( internalDir + "/favourites.gpx" ).lastModified(); + public long getTimeStamp() throws Exception { + long t1 = new File(internalDir + "/favourites_bak.gpx").lastModified(); + long t2 = new File(internalDir + "/favourites.gpx").lastModified(); return t1 > t2 ? t1 : t2; } @Override - public int getTurnInstructionMode() - { + public int getTurnInstructionMode() { return 4; // comment style } @@ -66,95 +58,78 @@ public class CoordinateReaderInternal extends CoordinateReader * (with hardcoded name for now) */ @Override - public void readPointmap() throws Exception - { - if (! _readPointmap( internalDir + "/favourites_bak.gpx" ) ) { - _readPointmap( internalDir + "/favourites.gpx" ); + public void readPointmap() throws Exception { + if (!_readPointmap(internalDir + "/favourites_bak.gpx")) { + _readPointmap(internalDir + "/favourites.gpx"); } - try - { - _readNogoLines( basedir+tracksdir ); - } - catch( IOException ioe ) - { + try { + _readNogoLines(basedir + tracksdir); + } catch (IOException ioe) { } } - private boolean _readPointmap( String filename ) throws Exception - { + private boolean _readPointmap(String filename) throws Exception { BufferedReader br = null; try { br = new BufferedReader( - new InputStreamReader( - new FileInputStream( filename ) ) ); + new InputStreamReader( + new FileInputStream(filename))); } catch (FileNotFoundException e) { // ignore until it's reading error return false; } OsmNodeNamed n = null; - for(;;) - { - String line = br.readLine(); - if ( line == null ) break; + for (; ; ) { + String line = br.readLine(); + if (line == null) break; - int idx0 = line.indexOf( " lat=\"" ); - int idx10 = line.indexOf( "" ); - if ( idx0 >= 0 ) - { - n = new OsmNodeNamed(); - idx0 += 6; - int idx1 = line.indexOf( '"', idx0 ); - n.ilat = (int)( (Double.parseDouble( line.substring( idx0, idx1 ) ) + 90. )*1000000. + 0.5); - int idx2 = line.indexOf( " lon=\"" ); - if ( idx2 < 0 ) continue; - idx2 += 6; - int idx3 = line.indexOf( '"', idx2 ); - n.ilon = (int)( ( Double.parseDouble( line.substring( idx2, idx3 ) ) + 180. )*1000000. + 0.5); - if ( idx3 < 0 ) continue; - } - if ( n != null && idx10 >= 0 ) - { - idx10 += 6; - int idx11 = line.indexOf( "", idx10 ); - if ( idx11 >= 0 ) - { - n.name = line.substring( idx10, idx11 ).trim(); - checkAddPoint( "(one-for-all)", n ); - } + int idx0 = line.indexOf(" lat=\""); + int idx10 = line.indexOf(""); + if (idx0 >= 0) { + n = new OsmNodeNamed(); + idx0 += 6; + int idx1 = line.indexOf('"', idx0); + n.ilat = (int) ((Double.parseDouble(line.substring(idx0, idx1)) + 90.) * 1000000. + 0.5); + int idx2 = line.indexOf(" lon=\""); + if (idx2 < 0) continue; + idx2 += 6; + int idx3 = line.indexOf('"', idx2); + n.ilon = (int) ((Double.parseDouble(line.substring(idx2, idx3)) + 180.) * 1000000. + 0.5); + if (idx3 < 0) continue; + } + if (n != null && idx10 >= 0) { + idx10 += 6; + int idx11 = line.indexOf("", idx10); + if (idx11 >= 0) { + n.name = line.substring(idx10, idx11).trim(); + checkAddPoint("(one-for-all)", n); } } - br.close(); - return true; + } + br.close(); + return true; } - - private void _readNogoLines( String dirname ) throws IOException - { - - File dir = new File( dirname ); - - if (dir.exists() && dir.isDirectory()) - { - for (final File file : dir.listFiles()) - { + + private void _readNogoLines(String dirname) throws IOException { + + File dir = new File(dirname); + + if (dir.exists() && dir.isDirectory()) { + for (final File file : dir.listFiles()) { final String name = file.getName(); - if (name.startsWith("nogo") && name.endsWith(".gpx")) - { - try - { + if (name.startsWith("nogo") && name.endsWith(".gpx")) { + try { _readNogoLine(file); - } - catch (Exception e) - { + } catch (Exception e) { } } } } } - - private void _readNogoLine( File file ) throws Exception - { + + private void _readNogoLine(File file) throws Exception { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(false); XmlPullParser xpp = factory.newPullParser(); @@ -165,45 +140,45 @@ public class CoordinateReaderInternal extends CoordinateReader int eventType = xpp.getEventType(); int numSeg = 0; while (eventType != XmlPullParser.END_DOCUMENT) { - switch(eventType) { - case XmlPullParser.START_TAG: { - if (xpp.getName().equals("trkpt") || xpp.getName().equals("rtept")) { - final String lon = xpp.getAttributeValue(null,"lon"); - final String lat = xpp.getAttributeValue(null,"lat"); - if (lon != null && lat != null) { - tmpPts.add(new Point( - (int)( ( Double.parseDouble(lon) + 180. ) *1000000. + 0.5), - (int)( ( Double.parseDouble(lat) + 90. ) *1000000. + 0.5)) ); + switch (eventType) { + case XmlPullParser.START_TAG: { + if (xpp.getName().equals("trkpt") || xpp.getName().equals("rtept")) { + final String lon = xpp.getAttributeValue(null, "lon"); + final String lat = xpp.getAttributeValue(null, "lat"); + if (lon != null && lat != null) { + tmpPts.add(new Point( + (int) ((Double.parseDouble(lon) + 180.) * 1000000. + 0.5), + (int) ((Double.parseDouble(lat) + 90.) * 1000000. + 0.5))); + } } + break; } - break; - } - case XmlPullParser.END_TAG: { - if (xpp.getName().equals("trkseg") || xpp.getName().equals("rte")) { // rte has no segment - OsmNogoPolygon nogo = null; - if (tmpPts.size() >= 0) { - if (tmpPts.get(0).x == tmpPts.get(tmpPts.size()-1).x && - tmpPts.get(0).y == tmpPts.get(tmpPts.size()-1).y) { - nogo = new OsmNogoPolygon(true); - } else { - nogo = new OsmNogoPolygon(false); + case XmlPullParser.END_TAG: { + if (xpp.getName().equals("trkseg") || xpp.getName().equals("rte")) { // rte has no segment + OsmNogoPolygon nogo = null; + if (tmpPts.size() >= 0) { + if (tmpPts.get(0).x == tmpPts.get(tmpPts.size() - 1).x && + tmpPts.get(0).y == tmpPts.get(tmpPts.size() - 1).y) { + nogo = new OsmNogoPolygon(true); + } else { + nogo = new OsmNogoPolygon(false); + } + for (Point p : tmpPts) { + nogo.addVertex(p.x, p.y); + } + nogo.calcBoundingCircle(); + final String name = file.getName(); + nogo.name = name.substring(0, name.length() - 4); + if (numSeg > 0) { + nogo.name += Integer.toString(numSeg + 1); + } + numSeg++; + checkAddPoint("(one-for-all)", nogo); } - for (Point p : tmpPts) { - nogo.addVertex(p.x, p.y); - } - nogo.calcBoundingCircle(); - final String name = file.getName(); - nogo.name = name.substring(0, name.length() - 4); - if (numSeg > 0) { - nogo.name += Integer.toString(numSeg + 1); - } - numSeg++; - checkAddPoint("(one-for-all)", nogo); + tmpPts.clear(); } - tmpPts.clear(); + break; } - break; - } } eventType = xpp.next(); } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java index 9238f2b..105de7f 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java @@ -4,24 +4,22 @@ import java.io.File; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; + import btools.router.OsmNodeNamed; /** * Read coordinates from a gpx-file */ -public class CoordinateReaderLocus extends CoordinateReader -{ - public CoordinateReaderLocus( String basedir ) - { - super( basedir ); +public class CoordinateReaderLocus extends CoordinateReader { + public CoordinateReaderLocus(String basedir) { + super(basedir); tracksdir = "/Locus/mapItems"; rootdir = "/Locus"; } @Override - public long getTimeStamp() throws Exception - { - File f = new File( basedir + "/Locus/data/database/waypoints.db" ); + public long getTimeStamp() throws Exception { + File f = new File(basedir + "/Locus/data/database/waypoints.db"); long t1 = f.lastModified(); // Android 10 delivers file size but can't read it boolean canRead = f.canRead(); @@ -29,8 +27,7 @@ public class CoordinateReaderLocus extends CoordinateReader } @Override - public int getTurnInstructionMode() - { + public int getTurnInstructionMode() { return 2; // locus style } @@ -39,34 +36,31 @@ public class CoordinateReaderLocus extends CoordinateReader * (with hardcoded name for now) */ @Override - public void readPointmap() throws Exception - { - _readPointmap( basedir + "/Locus/data/database/waypoints.db" ); + public void readPointmap() throws Exception { + _readPointmap(basedir + "/Locus/data/database/waypoints.db"); } - private void _readPointmap( String filename ) throws Exception - { + private void _readPointmap(String filename) throws Exception { SQLiteDatabase myDataBase = null; try { - myDataBase = SQLiteDatabase.openDatabase( filename, null, SQLiteDatabase.OPEN_READONLY); + myDataBase = SQLiteDatabase.openDatabase(filename, null, SQLiteDatabase.OPEN_READONLY); } catch (Exception e) { // not open, do not produce an error return; } - + Cursor c = myDataBase.rawQuery("SELECT c.name, w.name, w.longitude, w.latitude FROM waypoints w, categories c where w.parent_id = c._id", null); if (c.getCount() == 0) { c.close(); - c = myDataBase.rawQuery("SELECT c.name, w.name, w.longitude, w.latitude FROM waypoints w, groups c where w.parent_id = c._id;", null ); + c = myDataBase.rawQuery("SELECT c.name, w.name, w.longitude, w.latitude FROM waypoints w, groups c where w.parent_id = c._id;", null); } - while (c.moveToNext()) - { + while (c.moveToNext()) { OsmNodeNamed n = new OsmNodeNamed(); String category = c.getString(0); n.name = c.getString(1); - n.ilon = (int)( ( c.getDouble(2) + 180. )*1000000. + 0.5); - n.ilat = (int)( ( c.getDouble(3) + 90. )*1000000. + 0.5); - checkAddPoint( category, n ); + n.ilon = (int) ((c.getDouble(2) + 180.) * 1000000. + 0.5); + n.ilat = (int) ((c.getDouble(3) + 90.) * 1000000. + 0.5); + checkAddPoint(category, n); } c.close(); myDataBase.close(); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java index 3d4d44b..c4628f4 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java @@ -4,24 +4,22 @@ import java.io.File; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; + import btools.router.OsmNodeNamed; /** * Read coordinates from a gpx-file */ -public class CoordinateReaderOrux extends CoordinateReader -{ - public CoordinateReaderOrux( String basedir ) - { - super( basedir ); +public class CoordinateReaderOrux extends CoordinateReader { + public CoordinateReaderOrux(String basedir) { + super(basedir); tracksdir = "/oruxmaps/tracklogs"; rootdir = "/oruxmaps"; } @Override - public long getTimeStamp() throws Exception - { - File f = new File( basedir + "/oruxmaps/tracklogs/oruxmapstracks.db" ); + public long getTimeStamp() throws Exception { + File f = new File(basedir + "/oruxmaps/tracklogs/oruxmapstracks.db"); long t1 = f.lastModified(); // Android 10 delivers file size but can't read it boolean canRead = f.canRead(); @@ -29,8 +27,7 @@ public class CoordinateReaderOrux extends CoordinateReader } @Override - public int getTurnInstructionMode() - { + public int getTurnInstructionMode() { return 0; // none } @@ -39,29 +36,26 @@ public class CoordinateReaderOrux extends CoordinateReader * (with hardcoded name for now) */ @Override - public void readPointmap() throws Exception - { - _readPointmap( basedir + "/oruxmaps/tracklogs/oruxmapstracks.db" ); + public void readPointmap() throws Exception { + _readPointmap(basedir + "/oruxmaps/tracklogs/oruxmapstracks.db"); } - private void _readPointmap( String filename ) throws Exception - { + private void _readPointmap(String filename) throws Exception { SQLiteDatabase myDataBase = null; try { - myDataBase = SQLiteDatabase.openDatabase( filename, null, SQLiteDatabase.OPEN_READONLY); + myDataBase = SQLiteDatabase.openDatabase(filename, null, SQLiteDatabase.OPEN_READONLY); } catch (Exception e) { // not open, do not produce an error return; } Cursor c = myDataBase.rawQuery("SELECT poiname, poilon, poilat, poifolder FROM pois", null); - while (c.moveToNext()) - { + while (c.moveToNext()) { OsmNodeNamed n = new OsmNodeNamed(); n.name = c.getString(0); - n.ilon = (int)( ( c.getDouble(1) + 180. )*1000000. + 0.5); - n.ilat = (int)( ( c.getDouble(2) + 90. )*1000000. + 0.5); + n.ilon = (int) ((c.getDouble(1) + 180.) * 1000000. + 0.5); + n.ilat = (int) ((c.getDouble(2) + 90.) * 1000000. + 0.5); String category = c.getString(3); - checkAddPoint( category, n ); + checkAddPoint(category, n); } c.close(); myDataBase.close(); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java index 7e9ef94..bf6423b 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java @@ -16,26 +16,20 @@ import btools.router.OsmNogoPolygon; /** * Read coordinates from a gpx-file */ -public class CoordinateReaderOsmAnd extends CoordinateReader -{ +public class CoordinateReaderOsmAnd extends CoordinateReader { private String osmandDir; - public CoordinateReaderOsmAnd( String basedir ) - { - this( basedir, false ); + public CoordinateReaderOsmAnd(String basedir) { + this(basedir, false); } - public CoordinateReaderOsmAnd( String basedir, boolean shortPath ) - { - super( basedir ); - if ( shortPath ) - { + public CoordinateReaderOsmAnd(String basedir, boolean shortPath) { + super(basedir); + if (shortPath) { osmandDir = basedir; tracksdir = "/tracks"; rootdir = ""; - } - else - { + } else { osmandDir = basedir + "/osmand"; tracksdir = "/osmand/tracks"; rootdir = "/osmand"; @@ -43,18 +37,16 @@ public class CoordinateReaderOsmAnd extends CoordinateReader } @Override - public long getTimeStamp() throws Exception - { - File f1 = new File( osmandDir + "/favourites_bak.gpx" ); - File f2 = new File( osmandDir + "/favourites.gpx" ); - long t1 = f1.canRead()?f1.lastModified():0L; - long t2 = f2.canRead()?f2.lastModified():0L; + public long getTimeStamp() throws Exception { + File f1 = new File(osmandDir + "/favourites_bak.gpx"); + File f2 = new File(osmandDir + "/favourites.gpx"); + long t1 = f1.canRead() ? f1.lastModified() : 0L; + long t2 = f2.canRead() ? f2.lastModified() : 0L; return t1 > t2 ? t1 : t2; } @Override - public int getTurnInstructionMode() - { + public int getTurnInstructionMode() { return 3; // osmand style } @@ -63,92 +55,72 @@ public class CoordinateReaderOsmAnd extends CoordinateReader * (with hardcoded name for now) */ @Override - public void readPointmap() throws Exception - { - try - { - _readPointmap( osmandDir + "/favourites_bak.gpx" ); + public void readPointmap() throws Exception { + try { + _readPointmap(osmandDir + "/favourites_bak.gpx"); + } catch (Exception e) { + _readPointmap(osmandDir + "/favourites.gpx"); } - catch( Exception e ) - { - _readPointmap( osmandDir + "/favourites.gpx" ); - } - try - { - _readNogoLines( basedir+tracksdir ); - } - catch( IOException ioe ) - { + try { + _readNogoLines(basedir + tracksdir); + } catch (IOException ioe) { } } - private void _readPointmap( String filename ) throws Exception - { - BufferedReader br = new BufferedReader( - new InputStreamReader( - new FileInputStream( filename ) ) ); - OsmNodeNamed n = null; + private void _readPointmap(String filename) throws Exception { + BufferedReader br = new BufferedReader( + new InputStreamReader( + new FileInputStream(filename))); + OsmNodeNamed n = null; - for(;;) - { - String line = br.readLine(); - if ( line == null ) break; + for (; ; ) { + String line = br.readLine(); + if (line == null) break; - int idx0 = line.indexOf( "" ); - if ( idx0 >= 0 ) - { - n = new OsmNodeNamed(); - idx0 += 10; - int idx1 = line.indexOf( '"', idx0 ); - n.ilat = (int)( (Double.parseDouble( line.substring( idx0, idx1 ) ) + 90. )*1000000. + 0.5); - int idx2 = line.indexOf( " lon=\"" ); - if ( idx2 < 0 ) continue; - idx2 += 6; - int idx3 = line.indexOf( '"', idx2 ); - n.ilon = (int)( ( Double.parseDouble( line.substring( idx2, idx3 ) ) + 180. )*1000000. + 0.5); - continue; - } - if ( n != null && idx10 >= 0 ) - { - idx10 += 6; - int idx11 = line.indexOf( "", idx10 ); - if ( idx11 >= 0 ) - { - n.name = line.substring( idx10, idx11 ).trim(); - checkAddPoint( "(one-for-all)", n ); - } + int idx0 = line.indexOf(""); + if (idx0 >= 0) { + n = new OsmNodeNamed(); + idx0 += 10; + int idx1 = line.indexOf('"', idx0); + n.ilat = (int) ((Double.parseDouble(line.substring(idx0, idx1)) + 90.) * 1000000. + 0.5); + int idx2 = line.indexOf(" lon=\""); + if (idx2 < 0) continue; + idx2 += 6; + int idx3 = line.indexOf('"', idx2); + n.ilon = (int) ((Double.parseDouble(line.substring(idx2, idx3)) + 180.) * 1000000. + 0.5); + continue; + } + if (n != null && idx10 >= 0) { + idx10 += 6; + int idx11 = line.indexOf("", idx10); + if (idx11 >= 0) { + n.name = line.substring(idx10, idx11).trim(); + checkAddPoint("(one-for-all)", n); } } - br.close(); + } + br.close(); } - - private void _readNogoLines( String dirname ) throws IOException - { - - File dir = new File( dirname ); - - if (dir.exists() && dir.isDirectory()) - { - for (final File file : dir.listFiles()) - { + + private void _readNogoLines(String dirname) throws IOException { + + File dir = new File(dirname); + + if (dir.exists() && dir.isDirectory()) { + for (final File file : dir.listFiles()) { final String name = file.getName(); - if (name.startsWith("nogo") && name.endsWith(".gpx")) - { - try - { + if (name.startsWith("nogo") && name.endsWith(".gpx")) { + try { _readNogoLine(file); - } - catch (Exception e) - { + } catch (Exception e) { } } } } } - - private void _readNogoLine( File file ) throws Exception - { + + private void _readNogoLine(File file) throws Exception { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(false); XmlPullParser xpp = factory.newPullParser(); @@ -158,33 +130,32 @@ public class CoordinateReaderOsmAnd extends CoordinateReader int eventType = xpp.getEventType(); int numSeg = 0; while (eventType != XmlPullParser.END_DOCUMENT) { - switch(eventType) { - case XmlPullParser.START_TAG: { - if (xpp.getName().equals("trkpt")) { - final String lon = xpp.getAttributeValue(null,"lon"); - final String lat = xpp.getAttributeValue(null,"lat"); - if (lon != null && lat != null) { - nogo.addVertex( - (int)( ( Double.parseDouble(lon) + 180. ) *1000000. + 0.5), - (int)( ( Double.parseDouble(lat) + 90. ) *1000000. + 0.5)); + switch (eventType) { + case XmlPullParser.START_TAG: { + if (xpp.getName().equals("trkpt")) { + final String lon = xpp.getAttributeValue(null, "lon"); + final String lat = xpp.getAttributeValue(null, "lat"); + if (lon != null && lat != null) { + nogo.addVertex( + (int) ((Double.parseDouble(lon) + 180.) * 1000000. + 0.5), + (int) ((Double.parseDouble(lat) + 90.) * 1000000. + 0.5)); + } } + break; } - break; - } - case XmlPullParser.END_TAG: { - if (xpp.getName().equals("trkseg")) { - nogo.calcBoundingCircle(); - final String name = file.getName(); - nogo.name = name.substring(0, name.length()-4); - if (numSeg > 0) - { - nogo.name += Integer.toString(numSeg+1); + case XmlPullParser.END_TAG: { + if (xpp.getName().equals("trkseg")) { + nogo.calcBoundingCircle(); + final String name = file.getName(); + nogo.name = name.substring(0, name.length() - 4); + if (numSeg > 0) { + nogo.name += Integer.toString(numSeg + 1); + } + numSeg++; + checkAddPoint("(one-for-all)", nogo); } - numSeg++; - checkAddPoint( "(one-for-all)", nogo ); + break; } - break; - } } eventType = xpp.next(); } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/DownloadService.java b/brouter-routing-app/src/main/java/btools/routingapp/DownloadService.java index 94d9a82..c1a0c0a 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/DownloadService.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/DownloadService.java @@ -28,482 +28,460 @@ import btools.mapaccess.Rd5DiffManager; import btools.mapaccess.Rd5DiffTool; import btools.util.ProgressListener; -public class DownloadService extends Service implements ProgressListener { +public class DownloadService extends Service implements ProgressListener { - private static final boolean DEBUG = false; + private static final boolean DEBUG = false; - private ServerConfig mServerConfig; + private ServerConfig mServerConfig; - private NotificationHelper mNotificationHelper; - private List mUrlList; - private String baseDir; + private NotificationHelper mNotificationHelper; + private List mUrlList; + private String baseDir; - private volatile String newDownloadAction = ""; - private volatile String currentDownloadOperation = ""; - private long availableSize; + private volatile String newDownloadAction = ""; + private volatile String currentDownloadOperation = ""; + private long availableSize; - private Looper mServiceLooper; - private ServiceHandler mServiceHandler; - private NotificationManager mNM; - String downloadUrl; - public static boolean serviceState = false; - private boolean bIsDownloading; + private Looper mServiceLooper; + private ServiceHandler mServiceHandler; + private NotificationManager mNM; + String downloadUrl; + public static boolean serviceState = false; + private boolean bIsDownloading; - // Handler that receives messages from the thread - private final class ServiceHandler extends Handler { - public ServiceHandler(Looper looper) { - super(looper); - } + // Handler that receives messages from the thread + private final class ServiceHandler extends Handler { + public ServiceHandler(Looper looper) { + super(looper); + } - @Override - public void handleMessage(Message msg) { - bIsDownloading = true; - downloadFiles(); + @Override + public void handleMessage(Message msg) { + bIsDownloading = true; + downloadFiles(); - stopForeground(true); - stopSelf(msg.arg1); - mNotificationHelper.stopNotification(); - } + stopForeground(true); + stopSelf(msg.arg1); + mNotificationHelper.stopNotification(); + } + } + + + @Override + public void onCreate() { + if (DEBUG) Log.d("SERVICE", "onCreate"); + serviceState = true; + mServerConfig = new ServerConfig(getApplicationContext()); + + HandlerThread thread = new HandlerThread("ServiceStartArguments", 1); + thread.start(); + + // Get the HandlerThread's Looper and use it for our Handler + mServiceLooper = thread.getLooper(); + mServiceHandler = new ServiceHandler(mServiceLooper); + + availableSize = -1; + try { + availableSize = BInstallerActivity.getAvailableSpace(baseDir); + //StatFs stat = new StatFs(baseDir); + //availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong(); + } catch (Exception e) { /* ignore */ } + + } + + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (DEBUG) Log.d("SERVICE", "onStartCommand"); + + mNotificationHelper = new NotificationHelper(this); + Bundle extra = intent.getExtras(); + if (extra != null) { + String dir = extra.getString("dir"); + List urlparts = extra.getStringArrayList("urlparts"); + mUrlList = urlparts; + baseDir = dir; + } + + mNotificationHelper.startNotification(this); + + Message msg = mServiceHandler.obtainMessage(); + msg.arg1 = startId; + mServiceHandler.sendMessage(msg); + + // If we get killed, after returning from here, restart + return START_STICKY; + } + + + @Override + public void onDestroy() { + if (DEBUG) Log.d("SERVICE", "onDestroy"); + serviceState = false; + super.onDestroy(); + } + + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + + public void downloadFiles() { + + // first check lookup table and profiles + String result = checkScripts(); + if (result != null) { + if (DEBUG) Log.d("BR", "error: " + result); + bIsDownloading = false; + updateProgress("finished "); + + Toast.makeText(this, result, Toast.LENGTH_LONG).show(); + return; } - @Override - public void onCreate() { - if (DEBUG) Log.d("SERVICE", "onCreate"); - serviceState = true; - mServerConfig = new ServerConfig(getApplicationContext()); - - HandlerThread thread = new HandlerThread("ServiceStartArguments", 1); - thread.start(); - - // Get the HandlerThread's Looper and use it for our Handler - mServiceLooper = thread.getLooper(); - mServiceHandler = new ServiceHandler(mServiceLooper); - - availableSize = -1; - try - { - availableSize = BInstallerActivity.getAvailableSpace(baseDir); - //StatFs stat = new StatFs(baseDir); - //availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong(); - } - catch (Exception e) { /* ignore */ } + int count = 1; + int size = mUrlList.size(); + for (String part : mUrlList) { + String url = mServerConfig.getSegmentUrl() + part + ".rd5"; + if (DEBUG) Log.d("BR", "downlaod " + url); + result = download(count, size, url); + if (result != null) { + if (DEBUG) Log.d("BR", "" + result); + Toast.makeText(this, result, Toast.LENGTH_LONG).show(); + break; + } else { + updateProgress("Download " + part + " " + count + "/" + size + " finshed"); + } + count++; } + bIsDownloading = false; + updateProgress("finished "); + } - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - if (DEBUG) Log.d("SERVICE", "onStartCommand"); - mNotificationHelper = new NotificationHelper(this); - Bundle extra = intent.getExtras(); - if (extra != null) { - String dir = extra.getString("dir"); - List urlparts = extra.getStringArrayList("urlparts"); - mUrlList = urlparts; - baseDir = dir; - } - - mNotificationHelper.startNotification(this); - - Message msg = mServiceHandler.obtainMessage(); - msg.arg1 = startId; - mServiceHandler.sendMessage(msg); - - // If we get killed, after returning from here, restart - return START_STICKY; + public void updateProgress(String progress) { + if (!newDownloadAction.equals(progress)) { + if (DEBUG) Log.d("BR", "up " + progress); + Intent intent = new Intent(BInstallerActivity.DOWNLOAD_ACTION); + intent.putExtra("txt", progress); + intent.putExtra("ready", bIsDownloading); + sendBroadcast(intent); + ; + newDownloadAction = progress; + mNotificationHelper.progressUpdate(newDownloadAction); } + } - @Override - public void onDestroy() { - if (DEBUG) Log.d("SERVICE", "onDestroy"); - serviceState = false; - super.onDestroy(); - } + private String download(int counter, int size, String surl) { + InputStream input = null; + OutputStream output = null; + HttpURLConnection connection = null; + File fname = null; + File tmp_file = null; + try { + try { + TrafficStats.setThreadStatsTag(1); + int slidx = surl.lastIndexOf("segments4/"); + String name = surl.substring(slidx + 10); + String surlBase = surl.substring(0, slidx + 10); + fname = new File(baseDir, "segments4/" + name); - @Override - public IBinder onBind(Intent intent) { + boolean delta = true; + + // if (!targetFile.getParentFile().exists()) targetFile.getParentFile().mkdirs(); + if (fname.exists()) { + updateProgress("Calculating local checksum.."); + + // first check for a delta file + String md5 = Rd5DiffManager.getMD5(fname); + String surlDelta = surlBase + "diff/" + name.replace(".rd5", "/" + md5 + ".df5"); + + URL urlDelta = new URL(surlDelta); + + connection = (HttpURLConnection) urlDelta.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + // 404 kind of expected here, means there's no delta file + if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { + connection = null; + } else { + updateProgress("Connecting.." + surlDelta); + } + } + + if (connection == null) { + updateProgress("Connecting.." + name); + + delta = false; + URL url = new URL(surl); + connection = (HttpURLConnection) url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + } + + updateProgress("Connecting.." + counter + "/" + size); + + // expect HTTP 200 OK, so we don't mistakenly save error report + // instead of the file + if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { + return "Server returned HTTP " + connection.getResponseCode() + + " " + connection.getResponseMessage(); + } + + // this will be useful to display download percentage + // might be -1: server did not report the length + int fileLength = connection.getContentLength(); + long currentDownloadSize = fileLength; + if (availableSize >= 0 && fileLength > availableSize) return "not enough space on sd-card"; + + currentDownloadOperation = delta ? "Updating" : "Loading"; + updateProgress(currentDownloadOperation); + + // download the file + input = connection.getInputStream(); + + tmp_file = new File(fname.getAbsolutePath() + (delta ? "_diff" : "_tmp")); + output = new FileOutputStream(tmp_file); + + byte[] data = new byte[4096]; + long total = 0; + long t0 = System.currentTimeMillis(); + int count; + while ((count = input.read(data)) != -1) { + if (isCanceled()) { + return "Download canceled!"; + } + total += count; + // publishing the progress.... + if (fileLength > 0) // only if total length is known + { + int pct = (int) (total * 100 / fileLength); + updateProgress("Progress " + counter + "/" + size + " .. " + pct + "%"); + } else { + updateProgress("Progress (unnown size)"); + } + + output.write(data, 0, count); + + // enforce < 2 Mbit/s + long dt = t0 + total / 524 - System.currentTimeMillis(); + if (dt > 0) { + try { + Thread.sleep(dt); + } catch (InterruptedException ie) { + } + } + } + output.close(); + output = null; + + if (delta) { + updateProgress("Applying delta.."); + File diffFile = tmp_file; + tmp_file = new File(fname + "_tmp"); + Rd5DiffTool.recoverFromDelta(fname, diffFile, tmp_file, this); + diffFile.delete(); + } + if (isCanceled()) { + return "Canceled!"; + } + if (tmp_file != null) { + updateProgress("Verifying integrity.."); + String check_result = PhysicalFile.checkFileIntegrity(tmp_file); + if (check_result != null) { + if (check_result.startsWith("version old lookups.dat")) { + + } + return check_result; + } + if (fname.exists()) fname.delete(); + if (!tmp_file.renameTo(fname)) { + return "Could not rename to " + fname.getAbsolutePath(); + } + + } return null; + } catch (Exception e) { + //e.printStackTrace(); ; + return e.toString(); + } finally { + try { + if (output != null) + output.close(); + if (input != null) + input.close(); + } catch (IOException ignored) { + } + + if (connection != null) + connection.disconnect(); + } + } finally { + if (tmp_file != null) tmp_file.delete(); // just to be sure + } + } + + private String checkScripts() { + + String[] sa = mServerConfig.getLookups(); + for (String f : sa) { + if (f.length() > 0) { + File file = new File(baseDir + "profiles2", f); + checkOrDownloadLookup(f, file); + } } + sa = mServerConfig.getProfiles(); + for (String f : sa) { + if (f.length() > 0) { + File file = new File(baseDir + "profiles2", f); + if (file.exists()) { + String result = checkOrDownloadScript(f, file); + if (result != null) { + return result; + } + } + } + } + return null; + } - public void downloadFiles() { + private String checkOrDownloadLookup(String fileName, File f) { + String url = mServerConfig.getLookupUrl() + fileName; + return downloadScript(url, f); + } - // first check lookup table and profiles - String result = checkScripts(); - if ( result != null) { - if (DEBUG) Log.d("BR", "error: " + result); - bIsDownloading = false; - updateProgress( "finished " ); + private String checkOrDownloadScript(String fileName, File f) { + String url = mServerConfig.getProfilesUrl() + fileName; + return downloadScript(url, f); + } - Toast.makeText(this, result, Toast.LENGTH_LONG).show(); - return; + private String downloadScript(String surl, File f) { + long size = 0L; + if (f.exists()) { + size = f.length(); + } + + InputStream input = null; + OutputStream output = null; + HttpURLConnection connection = null; + File tmp_file = null; + File targetFile = f; + + try { + try { + TrafficStats.setThreadStatsTag(1); + + URL url = new URL(surl); + connection = (HttpURLConnection) url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + // expect HTTP 200 OK, so we don't mistakenly save error report + // instead of the file + if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { + return null; + } + if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { + return "Server returned HTTP " + connection.getResponseCode() + + " " + connection.getResponseMessage() + " " + f.getName(); } - int count = 1; - int size = mUrlList.size(); - for (String part: mUrlList) { - String url = mServerConfig.getSegmentUrl() + part + ".rd5"; - if (DEBUG) Log.d("BR", "downlaod " + url); + // this will be useful to display download percentage + // might be -1: server did not report the length + long fileLength = (long) connection.getContentLength(); + if (DEBUG) Log.d("BR", "file size " + size + " == " + fileLength + " " + f.getName()); + if (fileLength != size) { + long currentDownloadSize = fileLength; + if (availableSize >= 0 && fileLength > availableSize) + return "not enough space on sd-card"; - result = download(count, size, url); - if (result != null) { - if (DEBUG) Log.d("BR", "" + result); - Toast.makeText(this, result, Toast.LENGTH_LONG).show(); - break; + currentDownloadOperation = "Updating"; + + // download the file + input = connection.getInputStream(); + + tmp_file = new File(f.getAbsolutePath() + "_tmp"); + output = new FileOutputStream(tmp_file); + + byte data[] = new byte[4096]; + long total = 0; + long t0 = System.currentTimeMillis(); + int count; + while ((count = input.read(data)) != -1) { + if (isCanceled()) { + return "Download canceled!"; + } + total += count; + // publishing the progress.... + if (fileLength > 0) // only if total length is known + { + int pct = (int) (total * 100 / fileLength); + updateProgress("Progress " + pct + "%"); } else { - updateProgress( "Download " + part + " " + count + "/"+ size + " finshed"); + updateProgress("Progress (unnown size)"); } - count++; - } - bIsDownloading = false; - updateProgress( "finished " ); - } + output.write(data, 0, count); - - public void updateProgress( String progress ) - { - if ( !newDownloadAction.equals( progress ) ) - { - if (DEBUG) Log.d("BR", "up " + progress); - Intent intent = new Intent(BInstallerActivity.DOWNLOAD_ACTION); - intent.putExtra("txt", progress); - intent.putExtra("ready", bIsDownloading); - sendBroadcast(intent);; - newDownloadAction = progress; - mNotificationHelper.progressUpdate(newDownloadAction); - } - - } - - private String download(int counter, int size, String surl) - { - InputStream input = null; - OutputStream output = null; - HttpURLConnection connection = null; - File fname = null; - File tmp_file = null; - try - { - try - { - TrafficStats.setThreadStatsTag(1); - - int slidx = surl.lastIndexOf( "segments4/" ); - String name = surl.substring( slidx+10 ); - String surlBase = surl.substring( 0, slidx+10 ); - fname = new File (baseDir, "segments4/" + name); - - boolean delta = true; - - // if (!targetFile.getParentFile().exists()) targetFile.getParentFile().mkdirs(); - if ( fname.exists() ) - { - updateProgress( "Calculating local checksum.." ); - - // first check for a delta file - String md5 = Rd5DiffManager.getMD5( fname ); - String surlDelta = surlBase + "diff/" + name.replace( ".rd5", "/" + md5 + ".df5" ); - - URL urlDelta = new URL(surlDelta); - - connection = (HttpURLConnection) urlDelta.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - // 404 kind of expected here, means there's no delta file - if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND ) - { - connection = null; - } else { - updateProgress( "Connecting.." + surlDelta ); - } - } - - if ( connection == null ) - { - updateProgress( "Connecting.." + name ); - - delta = false; - URL url = new URL(surl); - connection = (HttpURLConnection) url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - } - - updateProgress( "Connecting.." + counter + "/"+size ); - - // expect HTTP 200 OK, so we don't mistakenly save error report - // instead of the file - if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { - return "Server returned HTTP " + connection.getResponseCode() - + " " + connection.getResponseMessage(); - } - - // this will be useful to display download percentage - // might be -1: server did not report the length - int fileLength = connection.getContentLength(); - long currentDownloadSize = fileLength; - if ( availableSize >= 0 && fileLength > availableSize ) return "not enough space on sd-card"; - - currentDownloadOperation = delta ? "Updating" : "Loading"; - updateProgress( currentDownloadOperation); - - // download the file - input = connection.getInputStream(); - - tmp_file = new File( fname.getAbsolutePath() + ( delta ? "_diff" : "_tmp" ) ); - output = new FileOutputStream( tmp_file ); - - byte[] data = new byte[4096]; - long total = 0; - long t0 = System.currentTimeMillis(); - int count; - while ((count = input.read(data)) != -1) { - if (isCanceled()) { - return "Download canceled!"; - } - total += count; - // publishing the progress.... - if (fileLength > 0) // only if total length is known - { - int pct = (int) (total * 100 / fileLength); - updateProgress( "Progress " + counter + "/"+size + " .. " + pct + "%" ); - } - else - { - updateProgress( "Progress (unnown size)" ); - } - - output.write(data, 0, count); - - // enforce < 2 Mbit/s - long dt = t0 + total/524 - System.currentTimeMillis(); - if ( dt > 0 ) - { - try { Thread.sleep( dt ); } catch( InterruptedException ie ) {} - } - } - output.close(); - output = null; - - if ( delta ) - { - updateProgress( "Applying delta.." ); - File diffFile = tmp_file; - tmp_file = new File( fname + "_tmp" ); - Rd5DiffTool.recoverFromDelta( fname, diffFile, tmp_file, this ); - diffFile.delete(); - } - if (isCanceled()) - { - return "Canceled!"; - } - if ( tmp_file != null ) - { - updateProgress( "Verifying integrity.." ); - String check_result = PhysicalFile.checkFileIntegrity( tmp_file ); - if ( check_result != null ) { - if (check_result.startsWith("version old lookups.dat") ) { - - } - return check_result; - } - if (fname.exists()) fname.delete(); - if ( !tmp_file.renameTo( fname ) ) - { - return "Could not rename to " + fname.getAbsolutePath(); - } - - } - return null; - } catch (Exception e) { - //e.printStackTrace(); ; - return e.toString(); - } finally { - try { - if (output != null) - output.close(); - if (input != null) - input.close(); - } catch (IOException ignored) { - } - - if (connection != null) - connection.disconnect(); - } - } - finally - { - if ( tmp_file != null ) tmp_file.delete(); // just to be sure - } - } - - private String checkScripts() { - - String[] sa = mServerConfig.getLookups(); - for (String f: sa) { - if (f.length()>0) { - File file = new File(baseDir + "profiles2", f); - checkOrDownloadLookup(f, file); + // enforce < 2 Mbit/s + long dt = t0 + total / 524 - System.currentTimeMillis(); + if (dt > 0) { + try { + Thread.sleep(dt); + } catch (InterruptedException ie) { + } } + } + output.close(); + output = null; } - sa = mServerConfig.getProfiles(); - for (String f : sa) { - if (f.length()>0) { - File file = new File(baseDir + "profiles2", f); - if (file.exists()) { - String result = checkOrDownloadScript(f, file); - if (result != null) { - return result; - } - } - } + if (isCanceled()) { + return "Canceled!"; + } + if (tmp_file != null) { + f.delete(); + + if (!tmp_file.renameTo(f)) { + return "Could not rename to " + f.getName(); + } + if (DEBUG) Log.d("BR", "update " + f.getName()); } return null; - } - - private String checkOrDownloadLookup(String fileName, File f) { - String url = mServerConfig.getLookupUrl() + fileName; - return downloadScript(url, f); - } - - private String checkOrDownloadScript(String fileName, File f) { - String url = mServerConfig.getProfilesUrl() + fileName; - return downloadScript(url, f); - } - - private String downloadScript(String surl, File f) { - long size = 0L; - if (f.exists()) { - size = f.length(); + } catch (Exception e) { + return e.toString(); + } finally { + try { + if (output != null) + output.close(); + if (input != null) + input.close(); + } catch (IOException ignored) { } - InputStream input = null; - OutputStream output = null; - HttpURLConnection connection = null; - File tmp_file = null; - File targetFile = f; - - try - { - try - { - TrafficStats.setThreadStatsTag(1); - - URL url = new URL(surl); - connection = (HttpURLConnection) url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - // expect HTTP 200 OK, so we don't mistakenly save error report - // instead of the file - if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { - return null; - } - if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { - return "Server returned HTTP " + connection.getResponseCode() - + " " + connection.getResponseMessage() + " " + f.getName(); - } - - - // this will be useful to display download percentage - // might be -1: server did not report the length - long fileLength = (long)connection.getContentLength(); - if (DEBUG) Log.d("BR", "file size " + size + " == " + fileLength + " " + f.getName()); - if (fileLength != size) { - long currentDownloadSize = fileLength; - if (availableSize >= 0 && fileLength > availableSize) - return "not enough space on sd-card"; - - currentDownloadOperation = "Updating"; - - // download the file - input = connection.getInputStream(); - - tmp_file = new File(f.getAbsolutePath() + "_tmp"); - output = new FileOutputStream(tmp_file); - - byte data[] = new byte[4096]; - long total = 0; - long t0 = System.currentTimeMillis(); - int count; - while ((count = input.read(data)) != -1) { - if (isCanceled()) { - return "Download canceled!"; - } - total += count; - // publishing the progress.... - if (fileLength > 0) // only if total length is known - { - int pct = (int) (total * 100 / fileLength); - updateProgress("Progress " + pct + "%"); - } else { - updateProgress("Progress (unnown size)"); - } - - output.write(data, 0, count); - - // enforce < 2 Mbit/s - long dt = t0 + total / 524 - System.currentTimeMillis(); - if (dt > 0) { - try { - Thread.sleep(dt); - } catch (InterruptedException ie) { - } - } - } - output.close(); - output = null; - } - - if (isCanceled()) - { - return "Canceled!"; - } - if ( tmp_file != null ) - { - f.delete(); - - if ( !tmp_file.renameTo( f ) ) - { - return "Could not rename to " + f.getName(); - } - if (DEBUG) Log.d("BR", "update " + f.getName()); - } - return null; - } catch (Exception e) { - return e.toString() ; - } finally { - try { - if (output != null) - output.close(); - if (input != null) - input.close(); - } catch (IOException ignored) { - } - - if (connection != null) - connection.disconnect(); - - } - } - finally - { - if ( tmp_file != null ) tmp_file.delete(); // just to be sure - } + if (connection != null) + connection.disconnect(); + } + } finally { + if (tmp_file != null) tmp_file.delete(); // just to be sure } + } - public boolean isCanceled() { - return BInstallerView.downloadCanceled; - } + + public boolean isCanceled() { + return BInstallerView.downloadCanceled; + } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/NotificationHelper.java b/brouter-routing-app/src/main/java/btools/routingapp/NotificationHelper.java index ddbf4a1..a97f296 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/NotificationHelper.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/NotificationHelper.java @@ -18,118 +18,117 @@ import static android.content.Context.NOTIFICATION_SERVICE; public class NotificationHelper { - private static final boolean DEBUG = false; + private static final boolean DEBUG = false; - public static String BRouterNotificationChannel1 = "brouter_channel_01"; + public static String BRouterNotificationChannel1 = "brouter_channel_01"; - private Context mContext; - private int NOTIFICATION_ID = 111; - private Notification mNotification; - private NotificationManager mNotificationManager; - private PendingIntent mContentIntent; - private CharSequence mContentTitle; + private Context mContext; + private int NOTIFICATION_ID = 111; + private Notification mNotification; + private NotificationManager mNotificationManager; + private PendingIntent mContentIntent; + private CharSequence mContentTitle; - public NotificationHelper(Context context) - { - if (DEBUG) Log.d("NH", "init " ); - mContext = context; - createNotificationChannels(); + public NotificationHelper(Context context) { + if (DEBUG) Log.d("NH", "init "); + mContext = context; + createNotificationChannels(); + } + + public void startNotification(Service service) { + if (DEBUG) Log.d("NH", "startNotification "); + + mNotification = createNotification("BRouter Download", "Download some files"); + + if (service != null) service.startForeground(NOTIFICATION_ID, mNotification); + + mNotificationManager.notify(NOTIFICATION_ID, mNotification); + + } + + public void progressUpdate(String text) { + mNotification = createNotification("BRouter Download", text); + mNotification.flags = Notification.FLAG_NO_CLEAR | + Notification.FLAG_ONGOING_EVENT; + + mNotificationManager.notify(NOTIFICATION_ID, mNotification); + } + + + public Notification createNotification(String title, String desc) { + + Intent resultIntent = new Intent(mContext, BInstallerActivity.class); + + Intent notificationIntent = new Intent(); + mContentIntent = PendingIntent.getActivity(mContext, 0, resultIntent, PendingIntent.FLAG_IMMUTABLE); + + mNotificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + + + final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, BRouterNotificationChannel1); + builder.setSmallIcon(android.R.drawable.stat_sys_download) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setContentTitle(title) + .setContentText(desc) + .setTicker(desc) + .setOngoing(true) + .setAutoCancel(true) + .setOnlyAlertOnce(true) + .setCategory(NotificationCompat.CATEGORY_SERVICE) + .setContentIntent(mContentIntent); + + return builder.build(); + + } else { + final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); + builder.setSmallIcon(android.R.drawable.stat_sys_download) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setContentTitle(title) + .setContentText(desc) + .setOnlyAlertOnce(true) + .setCategory(NotificationCompat.CATEGORY_SERVICE) + .setContentIntent(mContentIntent); + + return builder.build(); } - public void startNotification(Service service) { - if (DEBUG) Log.d("NH", "startNotification " ); + } - mNotification = createNotification("BRouter Download", "Download some files"); + /** + * create notification channels + */ + public void createNotificationChannels() { + if (DEBUG) Log.d("NH", "createNotificationChannels "); - if (service != null) service.startForeground(NOTIFICATION_ID, mNotification); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - mNotificationManager.notify(NOTIFICATION_ID, mNotification); + NotificationManager sNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + // Sound channel + CharSequence name = "BRouter Download"; + // The user-visible description of the channel. + String description = "BRouter Download Channel"; //getString(R.string.channel_description); + + NotificationChannel channel = new NotificationChannel(BRouterNotificationChannel1, name, NotificationManager.IMPORTANCE_LOW); + channel.setDescription(description); + AudioAttributes att = new AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_UNKNOWN) + .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) + .build(); + channel.setSound(null, null); + channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); + + sNotificationManager.createNotificationChannel(channel); } + } - public void progressUpdate(String text) { - mNotification = createNotification("BRouter Download", text); - mNotification.flags = Notification.FLAG_NO_CLEAR | - Notification.FLAG_ONGOING_EVENT; - - mNotificationManager.notify(NOTIFICATION_ID, mNotification); + public void stopNotification() { + if (DEBUG) Log.d("NH", "stopNotification "); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + mNotificationManager.deleteNotificationChannel(BRouterNotificationChannel1); } - - - public Notification createNotification(String title, String desc) { - - Intent resultIntent = new Intent(mContext, BInstallerActivity.class); - - Intent notificationIntent = new Intent(); - mContentIntent = PendingIntent.getActivity(mContext, 0, resultIntent, PendingIntent.FLAG_IMMUTABLE); - - mNotificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - - - final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, BRouterNotificationChannel1); - builder.setSmallIcon(android.R.drawable.stat_sys_download) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setContentTitle(title) - .setContentText(desc) - .setTicker(desc) - .setOngoing(true) - .setAutoCancel(true) - .setOnlyAlertOnce(true) - .setCategory(NotificationCompat.CATEGORY_SERVICE) - .setContentIntent(mContentIntent); - - return builder.build(); - - } else { - final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); - builder.setSmallIcon(android.R.drawable.stat_sys_download) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setContentTitle(title) - .setContentText(desc) - .setOnlyAlertOnce(true) - .setCategory(NotificationCompat.CATEGORY_SERVICE) - .setContentIntent(mContentIntent); - - return builder.build(); - } - - } - - /** - * create notification channels - */ - public void createNotificationChannels() { - if (DEBUG) Log.d("NH", "createNotificationChannels " ); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - - NotificationManager sNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - // Sound channel - CharSequence name = "BRouter Download"; - // The user-visible description of the channel. - String description = "BRouter Download Channel"; //getString(R.string.channel_description); - - NotificationChannel channel = new NotificationChannel(BRouterNotificationChannel1, name, NotificationManager.IMPORTANCE_LOW); - channel.setDescription(description); - AudioAttributes att = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_UNKNOWN) - .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) - .build(); - channel.setSound(null, null); - channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - - sNotificationManager.createNotificationChannel(channel); - - } - } - - public void stopNotification() { - if (DEBUG) Log.d("NH", "stopNotification " ); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - mNotificationManager.deleteNotificationChannel(BRouterNotificationChannel1); - } - mNotificationManager.cancel(NOTIFICATION_ID); - } -} \ No newline at end of file + mNotificationManager.cancel(NOTIFICATION_ID); + } +} diff --git a/brouter-routing-app/src/main/java/btools/routingapp/ServiceModeConfig.java b/brouter-routing-app/src/main/java/btools/routingapp/ServiceModeConfig.java index 99c36ad..e66cda9 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/ServiceModeConfig.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/ServiceModeConfig.java @@ -7,44 +7,38 @@ import java.util.TreeSet; /** * Decsription of a service config */ -public class ServiceModeConfig -{ +public class ServiceModeConfig { public String mode; public String profile; public TreeSet nogoVetos; - public ServiceModeConfig( String line ) - { - StringTokenizer tk = new StringTokenizer( line ); + public ServiceModeConfig(String line) { + StringTokenizer tk = new StringTokenizer(line); mode = tk.nextToken(); profile = tk.nextToken(); nogoVetos = new TreeSet(); - while( tk.hasMoreTokens() ) - { - nogoVetos.add( tk.nextToken() ); + while (tk.hasMoreTokens()) { + nogoVetos.add(tk.nextToken()); } } - public ServiceModeConfig( String mode, String profile ) - { + public ServiceModeConfig(String mode, String profile) { this.mode = mode; this.profile = profile; nogoVetos = new TreeSet(); } - - public String toLine() - { - StringBuilder sb = new StringBuilder( 100 ); - sb.append( mode ).append( ' ' ).append( profile ); - for( String veto: nogoVetos ) sb.append( ' ' ).append( veto ); + + public String toLine() { + StringBuilder sb = new StringBuilder(100); + sb.append(mode).append(' ').append(profile); + for (String veto : nogoVetos) sb.append(' ').append(veto); return sb.toString(); } - public String toString() - { - StringBuilder sb = new StringBuilder( 100 ); - sb.append( mode ).append( "->" ).append( profile ); - sb.append ( " [" + nogoVetos.size() + "]" ); + public String toString() { + StringBuilder sb = new StringBuilder(100); + sb.append(mode).append("->").append(profile); + sb.append(" [" + nogoVetos.size() + "]"); return sb.toString(); } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java b/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java index cde4689..0c91da0 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java @@ -2,8 +2,7 @@ package btools.routingapp; import java.io.File; -public class WpDatabaseScanner extends Thread -{ +public class WpDatabaseScanner extends Thread { private String currentDir = ""; private String bestGuess = ""; private String lastError = ""; @@ -12,116 +11,87 @@ public class WpDatabaseScanner extends Thread private long maxtimestamp = 0; - public String getCurrentDir() - { - synchronized (currentDirSync) - { + public String getCurrentDir() { + synchronized (currentDirSync) { return currentDir; } } - private void setCurrentDir( String dir ) - { - synchronized (currentDirSync) - { + private void setCurrentDir(String dir) { + synchronized (currentDirSync) { currentDir = dir; } } - public String getBestGuess() - { - synchronized (currentDirSync) - { + public String getBestGuess() { + synchronized (currentDirSync) { return bestGuess; } } - public String getLastError() - { - synchronized (currentDirSync) - { + public String getLastError() { + synchronized (currentDirSync) { return lastError; } } - private void setLastError( String msg ) - { - synchronized (currentDirSync) - { + private void setLastError(String msg) { + synchronized (currentDirSync) { lastError = msg; } } - - private static String[] vetos = new String[] { "dev", "sys", "system", "proc", "etc", "init", "d", "cache", "acct", "data" }; - private void scan( File dir, int level ) - { - if ( level > 8 ) - { + private static String[] vetos = new String[]{"dev", "sys", "system", "proc", "etc", "init", "d", "cache", "acct", "data"}; + + private void scan(File dir, int level) { + if (level > 8) { return; } - - try - { - if ( dir.isDirectory() ) - { - if ( level == 1 ) - { + + try { + if (dir.isDirectory()) { + if (level == 1) { String name = dir.getName(); - for( String veto: vetos ) - { - if ( veto.equals( name ) ) - { + for (String veto : vetos) { + if (veto.equals(name)) { return; } } } - - testPath( dir.getPath() ); + + testPath(dir.getPath()); File[] childs = dir.listFiles(); - if ( childs == null ) - { + if (childs == null) { return; } - for ( File child : childs ) - { - scan( child, level+1 ); + for (File child : childs) { + scan(child, level + 1); } } - } - catch (Exception e) - { - setLastError( e.toString() ); + } catch (Exception e) { + setLastError(e.toString()); } } - private void testPath( String path ) throws Exception - { - setCurrentDir( path ); + private void testPath(String path) throws Exception { + setCurrentDir(path); - testReader( new CoordinateReaderOsmAnd( path ) ); - testReader( new CoordinateReaderOsmAnd( path, true ) ); - testReader( new CoordinateReaderLocus( path ) ); - testReader( new CoordinateReaderOrux( path ) ); + testReader(new CoordinateReaderOsmAnd(path)); + testReader(new CoordinateReaderOsmAnd(path, true)); + testReader(new CoordinateReaderLocus(path)); + testReader(new CoordinateReaderOrux(path)); } - private void testReader( CoordinateReader cor ) throws Exception - { + private void testReader(CoordinateReader cor) throws Exception { long ts = cor.getTimeStamp(); - if ( ts > maxtimestamp ) - { + if (ts > maxtimestamp) { maxtimestamp = ts; - synchronized (currentDirSync) - { + synchronized (currentDirSync) { bestGuess = cor.basedir; } - } - else if ( ts > 0 && ts == maxtimestamp ) - { - synchronized (currentDirSync) - { - if ( cor.basedir.length() < bestGuess.length() ) - { + } else if (ts > 0 && ts == maxtimestamp) { + synchronized (currentDirSync) { + if (cor.basedir.length() < bestGuess.length()) { bestGuess = cor.basedir; } } @@ -129,10 +99,9 @@ public class WpDatabaseScanner extends Thread } @Override - public void run() - { - scan( new File( "/" ), 0 ); - setCurrentDir( null ); - + public void run() { + scan(new File("/"), 0); + setCurrentDir(null); + } }