From 4414943f904cdb9e3a80edd0e5f0db7c90d07976 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 30 Jul 2012 18:36:41 -0700 Subject: [PATCH] implement loading saved DB from /sdcard --- .../src/org/eehouse/android/xw4/DBUtils.java | 34 +++++++++++++------ .../org/eehouse/android/xw4/GamesList.java | 3 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index 04d51eff4..a6a17eb46 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -903,16 +903,28 @@ public class DBUtils { } } + public static void loadDB( Context context ) + { + copyGameDB( context, false ); + } + public static void saveDB( Context context ) + { + copyGameDB( context, true ); + } + + private static void copyGameDB( Context context, boolean toSDCard ) { String name = DBHelper.getDBName(); - File srcDB = context.getDatabasePath( name ); - if ( srcDB.exists() ) { - try { - File destDB = new File( Environment.getExternalStorageDirectory(), - name ); - InputStream src = new FileInputStream(srcDB); - OutputStream dest = new FileOutputStream(destDB); + File gamesDB = context.getDatabasePath( name ); + File sdcardDB = new File( Environment.getExternalStorageDirectory(), + name ); + try { + File srcDB = toSDCard? gamesDB : sdcardDB; + if ( srcDB.exists() ) { + FileInputStream src = new FileInputStream( srcDB ); + FileOutputStream dest = + new FileOutputStream( toSDCard? sdcardDB : gamesDB ); byte[] buffer = new byte[1024]; for ( ; ; ) { int nRead = src.read(buffer); @@ -925,11 +937,11 @@ public class DBUtils { dest.flush(); dest.close(); src.close(); - } catch( java.io.FileNotFoundException fnfe ) { - DbgUtils.logf( "in saveDB: %s", fnfe.toString() ); - } catch( java.io.IOException ioe ) { - DbgUtils.logf( "in saveDB: %s", ioe.toString() ); } + } catch( java.io.FileNotFoundException fnfe ) { + DbgUtils.logf( "in saveDB: %s", fnfe.toString() ); + } catch( java.io.IOException ioe ) { + DbgUtils.logf( "in saveDB: %s", ioe.toString() ); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index a42317302..bc93b4a85 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -591,7 +591,8 @@ public class GamesList extends XWListActivity break; case R.id.gamel_menu_loaddb: - Utils.notImpl(this); + DBUtils.loadDB( this ); + onContentChanged(); break; case R.id.gamel_menu_storedb: DBUtils.saveDB( this );