From 6b59093d844575430f6f616dd8fb3af490f2e74f Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 5 Apr 2014 14:04:02 -0700 Subject: [PATCH] use new loc db to save translation data (per locale) across boots. --- .../src/org/eehouse/android/xw4/DBUtils.java | 60 +++++++++++++++++++ .../eehouse/android/xw4/loc/LocActivity.java | 6 ++ .../eehouse/android/xw4/loc/LocDelegate.java | 6 ++ .../org/eehouse/android/xw4/loc/LocUtils.java | 8 ++- 4 files changed, 79 insertions(+), 1 deletion(-) 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 e8815b452..ddd279397 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -1745,6 +1745,66 @@ public class DBUtils { studyListClear( context, lang, null ); } + public static void saveXlations( Context context, String locale, + HashMap data ) + { + if ( null != data && 0 < data.size() ) { + Iterator iter = data.keySet().iterator(); + + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getWritableDatabase(); + + while ( iter.hasNext() ) { + String key = iter.next(); + String value = data.get( key ); + + String selection = String.format( "%s = '%s'", + DBHelper.KEY, + key ); + ContentValues values = new ContentValues(); + values.put( DBHelper.XLATION, value); + values.put( DBHelper.LOCALE, locale); + long result = db.update( DBHelper.TABLE_NAME_LOC, + values, selection, null ); + if ( 0 == result ) { + values.put( DBHelper.KEY, key ); + db.insert( DBHelper.TABLE_NAME_LOC, null, values ); + } + } + db.close(); + } + } + } + + public static HashMap getXlations( Context context, + String locale ) + { + HashMap result = new HashMap(); + + String selection = String.format( "%s = '%s'", DBHelper.LOCALE, + locale ); + String[] columns = { DBHelper.KEY, DBHelper.XLATION }; + + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getReadableDatabase(); + Cursor cursor = db.query( DBHelper.TABLE_NAME_LOC, columns, + selection, null, null, null, null ); + int keyIndex = cursor.getColumnIndex( DBHelper.KEY ); + int valueIndex = cursor.getColumnIndex( DBHelper.XLATION ); + while ( cursor.moveToNext() ) { + String key = cursor.getString( keyIndex ); + String value = cursor.getString( valueIndex ); + result.put( key, value ); + } + cursor.close(); + db.close(); + } + + return result; + } + private static void copyGameDB( Context context, boolean toSDCard ) { String name = DBHelper.getDBName(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocActivity.java index 5dc789d69..2132a6efc 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocActivity.java @@ -33,4 +33,10 @@ public class LocActivity extends ListActivity { m_dlgt = new LocDelegate( this, savedInstanceState ); } // onCreate + @Override + public void onBackPressed() { + if ( !m_dlgt.onBackPressed() ) { + super.onBackPressed(); + } + } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java index ab6d1e185..62cec8a36 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocDelegate.java @@ -39,6 +39,12 @@ public class LocDelegate extends DelegateBase { init( savedInstanceState ); } + protected boolean onBackPressed() + { + LocUtils.saveData( m_activity ); + return false; + } + private void init( Bundle savedInstanceState ) { m_activity.setContentView( R.layout.loc_main ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java index 80661a0ad..293df411a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java @@ -33,6 +33,7 @@ import junit.framework.Assert; import org.eehouse.android.xw4.R; import org.eehouse.android.xw4.DbgUtils; +import org.eehouse.android.xw4.DBUtils; public class LocUtils { // Keep this in sync with gen_loc_ids.py and what's used in the menu.xml @@ -115,6 +116,11 @@ public class LocUtils { return result; } + public static void saveData( Context context ) + { + DBUtils.saveXlations( context, "te_ST", s_xlations ); + } + private static void xlateMenu( final Activity activity, Menu menu, int depth ) { @@ -161,7 +167,7 @@ public class LocUtils { private static void loadXlations( Context context ) { if ( null == s_xlations ) { - s_xlations = new HashMap(); + s_xlations = DBUtils.getXlations( context, "te_ST" ); } }