From af2c3612254876c88f910fa41d90f27d0cf747e0 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 30 Jul 2012 07:23:52 -0700 Subject: [PATCH] add debug menu option to enable debug features, and first such feature, to copy xwdb file to /sdcard. Option to do opposite is stubbed out. --- .../android/XWords4-sms/res/xml/xwprefs.xml | 29 +++++++----- .../XWords4/res/menu/games_list_menu.xml | 8 ++++ .../XWords4/res/values/common_rsrc.xml | 3 ++ xwords4/android/XWords4/res/xml/xwprefs.xml | 22 ++++++---- .../src/org/eehouse/android/xw4/DBHelper.java | 5 +++ .../src/org/eehouse/android/xw4/DBUtils.java | 44 +++++++++++++++++-- .../org/eehouse/android/xw4/GamesList.java | 23 ++++++++++ .../src/org/eehouse/android/xw4/XWPrefs.java | 7 ++- 8 files changed, 115 insertions(+), 26 deletions(-) diff --git a/xwords4/android/XWords4-sms/res/xml/xwprefs.xml b/xwords4/android/XWords4-sms/res/xml/xwprefs.xml index 436221526..001bac534 100644 --- a/xwords4/android/XWords4-sms/res/xml/xwprefs.xml +++ b/xwords4/android/XWords4-sms/res/xml/xwprefs.xml @@ -281,6 +281,23 @@ + + + + - - - diff --git a/xwords4/android/XWords4/res/menu/games_list_menu.xml b/xwords4/android/XWords4/res/menu/games_list_menu.xml index f7899f06d..038bf2f3a 100644 --- a/xwords4/android/XWords4/res/menu/games_list_menu.xml +++ b/xwords4/android/XWords4/res/menu/games_list_menu.xml @@ -32,5 +32,13 @@ android:icon="@drawable/stat_notify_sync" /> + + + + diff --git a/xwords4/android/XWords4/res/values/common_rsrc.xml b/xwords4/android/XWords4/res/values/common_rsrc.xml index b32f4d190..d6b3665b3 100644 --- a/xwords4/android/XWords4/res/values/common_rsrc.xml +++ b/xwords4/android/XWords4/res/values/common_rsrc.xml @@ -88,6 +88,7 @@ key_na_lookup key_na_browse key_na_values + key_enable_debug xwords@eehouse.org @@ -110,6 +111,8 @@ Relay game port Relay device port %1$s/%2$s + Write DB to SD card + Load DB from SD card diff --git a/xwords4/android/XWords4/res/xml/xwprefs.xml b/xwords4/android/XWords4/res/xml/xwprefs.xml index 723043c4a..c68fb8240 100644 --- a/xwords4/android/XWords4/res/xml/xwprefs.xml +++ b/xwords4/android/XWords4/res/xml/xwprefs.xml @@ -275,6 +275,19 @@ + + + - - - diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java index f1322c055..a369f9a45 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java @@ -71,6 +71,11 @@ public class DBHelper extends SQLiteOpenHelper { super( context, DB_NAME, null, DB_VERSION ); } + public static String getDBName() + { + return DB_NAME; + } + private void onCreateSum( SQLiteDatabase db ) { db.execSQL( "CREATE TABLE " + TABLE_NAME_SUM + " (" 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 87eb388ab..04d51eff4 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -20,20 +20,26 @@ package org.eehouse.android.xw4; +import android.content.ContentValues; import android.content.Context; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.database.Cursor; import android.net.Uri; -import java.util.StringTokenizer; -import android.content.ContentValues; +import android.os.Environment; +import android.text.TextUtils; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.FileOutputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import android.text.TextUtils; +import java.util.StringTokenizer; import junit.framework.Assert; @@ -897,6 +903,36 @@ public class DBUtils { } } + public static void saveDB( Context context ) + { + 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); + byte[] buffer = new byte[1024]; + for ( ; ; ) { + int nRead = src.read(buffer); + if ( 0 > nRead ) { + break; + } + dest.write( buffer, 0, nRead ); + } + + 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() ); + } + } + } + // Chat is independent of the GameLock mechanism because it's not // touching the SNAPSHOT column. private static void saveChatHistory( Context context, long rowid, 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 df5ac653d..a42317302 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -71,6 +71,9 @@ public class GamesList extends XWListActivity private static final int DELETE_ALL_ACTION = 4; private static final int SYNC_MENU_ACTION = 5; private static final int NEW_FROM_ACTION = 6; + private static final int[] DEBUGITEMS = { R.id.gamel_menu_loaddb + , R.id.gamel_menu_storedb + }; private static boolean s_firstShown = false; @@ -525,13 +528,26 @@ public class GamesList extends XWListActivity return handleMenuItem( item.getItemId(), info.position ); } // onContextItemSelected + @Override public boolean onCreateOptionsMenu( Menu menu ) { MenuInflater inflater = getMenuInflater(); inflater.inflate( R.menu.games_list_menu, menu ); + return true; } + @Override + public boolean onPrepareOptionsMenu( Menu menu ) + { + boolean visible = XWPrefs.getDebugEnabled( this ) ; + for ( int id : DEBUGITEMS ) { + MenuItem item = menu.findItem( id ); + item.setVisible( visible ); + } + return super.onPrepareOptionsMenu( menu ); + } + public boolean onOptionsItemSelected( MenuItem item ) { boolean handled = true; @@ -574,6 +590,13 @@ public class GamesList extends XWListActivity Utils.emailAuthor( this ); break; + case R.id.gamel_menu_loaddb: + Utils.notImpl(this); + break; + case R.id.gamel_menu_storedb: + DBUtils.saveDB( this ); + break; + // case R.id.gamel_menu_view_hidden: // Utils.notImpl( this ); // break; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java index 10a166063..e5af01811 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java @@ -1,4 +1,4 @@ -/* -*- compile-command: "cd ../../../../../../; ant debug install"; -*- */ +/* -*- compile-command: "cd ../../../../../; ant debug install"; -*- */ /* * Copyright 2009 - 2012 by Eric House (xwords@eehouse.org). All * rights reserved. @@ -31,6 +31,11 @@ public class XWPrefs { return getPrefsBoolean( context, R.string.key_enable_sms, false ); } + public static boolean getDebugEnabled( Context context ) + { + return getPrefsBoolean( context, R.string.key_enable_debug, false ); + } + public static boolean getPrefsBoolean( Context context, int keyID, boolean defaultValue ) {