add new db table for tracking dict-releated stuff. Use it to save

state in wordlist browser so can return to same place as left for each
dict.  Eventually the new table should replace DictLangCache.
This commit is contained in:
Eric House 2012-09-12 07:44:58 -07:00
parent a6bed2706d
commit ce6356fb92
3 changed files with 194 additions and 31 deletions

View file

@ -28,8 +28,9 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME_SUM = "summaries";
public static final String TABLE_NAME_OBITS = "obits";
public static final String TABLE_NAME_DICTS = "dicts";
private static final String DB_NAME = "xwdb";
private static final int DB_VERSION = 12;
private static final int DB_VERSION = 13;
public static final String GAME_NAME = "GAME_NAME";
public static final String NUM_MOVES = "NUM_MOVES";
@ -60,6 +61,19 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String RELAYID = "RELAYID";
public static final String SEED = "SEED";
public static final String SMSPHONE = "SMSPHONE";
public static final String DICTNAME = "DICTNAME";
public static final String MD5SUM = "MD5SUM";
public static final String WORDCOUNT = "WORDCOUNT";
public static final String WORDCOUNTS = "WORDCOUNTS";
public static final String LANGCODE = "LANGCODE";
public static final String LOC = "LOC";
public static final String ITERMIN = "ITERMIN";
public static final String ITERMAX = "ITERMAX";
public static final String ITERPOS = "ITERPOS";
public static final String ITERTOP = "ITERTOP";
public static final String ITERPREFIX = "ITERPREFIX";
// not used yet
public static final String CREATE_TIME = "CREATE_TIME";
// not used yet
@ -122,11 +136,29 @@ public class DBHelper extends SQLiteOpenHelper {
+ ");" );
}
private void onCreateDictsDB( SQLiteDatabase db )
{
db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTS + " ("
+ DICTNAME + " TEXT,"
+ MD5SUM + " TEXT(32),"
+ WORDCOUNT + " INTEGER,"
+ WORDCOUNTS + " TEXT,"
+ LANGCODE + " INTEGER,"
+ LOC + " INTEGER(2),"
+ ITERMIN + " INTEGER(4),"
+ ITERMAX + " INTEGER(4),"
+ ITERPOS + " INTEGER,"
+ ITERTOP + " INTEGER,"
+ ITERPREFIX + " TEXT"
+ ");" );
}
@Override
public void onCreate( SQLiteDatabase db )
{
onCreateSum( db );
onCreateObits( db );
onCreateDictsDB( db );
}
@Override
@ -154,6 +186,7 @@ public class DBHelper extends SQLiteOpenHelper {
case 11:
addColumn( db, REMOTEDEVS, "TEXT" );
case 12:
onCreateDictsDB( db );
// nothing yet
break;
default:

View file

@ -54,6 +54,7 @@ public class DBUtils {
private static final String ROW_ID = "rowid";
private static final String ROW_ID_FMT = "rowid=%d";
private static final String NAME_FMT = "%s='%s'";
private static long s_cachedRowID = -1;
private static byte[] s_cachedBytes = null;
@ -84,6 +85,15 @@ public class DBUtils {
boolean sourceLocal;
}
public static class DictBrowseState {
public int m_minShown;
public int m_maxShown;
public int m_pos;
public int m_top;
public int[] m_counts;
public int m_count;
}
public static GameSummary getSummary( Context context, long rowid,
long maxMillis )
{
@ -938,6 +948,83 @@ public class DBUtils {
return success;
}
/////////////////////////////////////////////////////////////////
// DictsDB stuff
/////////////////////////////////////////////////////////////////
public static DictBrowseState dictsGetOffset( Context context,
String name )
{
DictBrowseState result = null;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.ITERPOS, DBHelper.ITERTOP,
DBHelper.ITERMIN, DBHelper.ITERMAX,
DBHelper.WORDCOUNTS, DBHelper.WORDCOUNT };
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTS, columns,
selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
result = new DictBrowseState();
result.m_pos = cursor.getInt( cursor
.getColumnIndex(DBHelper.ITERPOS));
result.m_top = cursor.getInt( cursor
.getColumnIndex(DBHelper.ITERTOP));
result.m_minShown =
cursor.getInt( cursor
.getColumnIndex(DBHelper.ITERMIN));
result.m_maxShown =
cursor.getInt( cursor
.getColumnIndex(DBHelper.ITERMAX));
result.m_count =
cursor.getInt( cursor.getColumnIndex(DBHelper.WORDCOUNT));
String counts =
cursor.getString( cursor.getColumnIndex(DBHelper.WORDCOUNTS));
if ( null != counts ) {
String[] nums = TextUtils.split( counts, ":" );
int[] ints = new int[nums.length];
for ( int ii = 0; ii < nums.length; ++ii ) {
ints[ii] = Integer.parseInt( nums[ii] );
}
result.m_counts = ints;
}
}
cursor.close();
db.close();
}
return result;
}
public static void dictsSetOffset( Context context, String name,
DictBrowseState state )
{
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
ContentValues values = new ContentValues();
values.put( DBHelper.ITERPOS, state.m_pos );
values.put( DBHelper.ITERTOP, state.m_top );
values.put( DBHelper.ITERMIN, state.m_minShown );
values.put( DBHelper.ITERMAX, state.m_maxShown );
values.put( DBHelper.WORDCOUNT, state.m_count );
if ( null != state.m_counts ) {
String[] nums = new String[state.m_counts.length];
for ( int ii = 0; ii < nums.length; ++ii ) {
nums[ii] = String.format( "%d", state.m_counts[ii] );
}
values.put( DBHelper.WORDCOUNTS, TextUtils.join( ":", nums ) );
}
int result = db.update( DBHelper.TABLE_NAME_DICTS,
values, selection, null );
if ( 0 == result ) {
values.put( DBHelper.DICTNAME, name );
db.insert( DBHelper.TABLE_NAME_DICTS, null, values );
}
db.close();
}
}
private static void copyGameDB( Context context, boolean toSDCard )
{
String name = DBHelper.getDBName();

View file

@ -32,6 +32,7 @@ import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.Spinner;
import android.widget.TextView;
@ -48,9 +49,6 @@ public class DictBrowseActivity extends XWListActivity
implements View.OnClickListener, OnItemSelectedListener {
public static final String DICT_NAME = "DICT_NAME";
public static final String DICT_MIN = "DICT_MIN";
public static final String DICT_MAX = "DICT_MAX";
public static final String DICT_COUNTS = "DICT_COUNTS";
private static final int MIN_LEN = 2;
private static final int FINISH_ACTION = 1;
@ -60,11 +58,9 @@ public class DictBrowseActivity extends XWListActivity
private String m_name;
private Spinner m_minSpinner;
private Spinner m_maxSpinner;
private int m_minShown;
private int m_maxShown;
private DBUtils.DictBrowseState m_browseState;
private int m_minAvail;
private int m_maxAvail;
private int[] m_counts;
// - Steps to reproduce the problem:
@ -84,13 +80,15 @@ public class DictBrowseActivity extends XWListActivity
{
super();
XwJNI.dict_iter_setMinMax( m_dictClosure, m_minShown, m_maxShown );
XwJNI.dict_iter_setMinMax( m_dictClosure, m_browseState.m_minShown,
m_browseState.m_maxShown );
m_nWords = XwJNI.dict_iter_wordCount( m_dictClosure );
int format = m_minShown == m_maxShown ?
int format = m_browseState.m_minShown == m_browseState.m_maxShown ?
R.string.dict_browse_title1f : R.string.dict_browse_titlef;
setTitle( Utils.format( DictBrowseActivity.this, format,
m_name, m_nWords, m_minShown, m_maxShown ));
m_name, m_nWords, m_browseState.m_minShown,
m_browseState.m_maxShown ));
String desc = XwJNI.dict_iter_getDesc( m_dictClosure );
if ( null != desc ) {
@ -170,11 +168,21 @@ public class DictBrowseActivity extends XWListActivity
pairs.m_paths[0],
JNIUtilsImpl.get() );
m_counts = intent.getIntArrayExtra( DICT_COUNTS );
if ( null == m_counts ) {
m_counts = XwJNI.dict_iter_getCounts( m_dictClosure );
m_browseState = DBUtils.dictsGetOffset( this, name );
boolean newState = null == m_browseState;
if ( newState ) {
m_browseState = new DBUtils.DictBrowseState();
m_browseState.m_pos = 0;
m_browseState.m_top = 0;
m_browseState.m_count =
XwJNI.dict_iter_wordCount( m_dictClosure );
}
if ( null == m_counts ) {
if ( null == m_browseState.m_counts ) {
m_browseState.m_counts =
XwJNI.dict_iter_getCounts( m_dictClosure );
}
if ( null == m_browseState.m_counts ) {
// empty dict? Just close down for now. Later if
// this is extended to include tile info -- it should
// be -- then use an empty list elem and disable
@ -183,7 +191,11 @@ public class DictBrowseActivity extends XWListActivity
name );
showOKOnlyDialogThen( msg, FINISH_ACTION );
} else {
figureMinMax();
figureMinMax( m_browseState.m_counts );
if ( newState ) {
m_browseState.m_minShown = m_minAvail;
m_browseState.m_maxShown = m_maxAvail;
}
setContentView( R.layout.dict_browser );
@ -195,14 +207,38 @@ public class DictBrowseActivity extends XWListActivity
}
} );
m_minShown = intent.getIntExtra( DICT_MIN, m_minAvail );
m_maxShown = intent.getIntExtra( DICT_MAX, m_maxAvail );
setUpSpinners();
setListAdapter( new DictListAdapter() );
getListView().setFastScrollEnabled( true );
getListView().setSelectionFromTop( m_browseState.m_pos,
m_browseState.m_top );
}
}
} // onCreate
@Override
protected void onPause()
{
if ( null != m_browseState ) { // already saved?
ListView list = getListView();
m_browseState.m_pos = list.getFirstVisiblePosition();
View view = list.getChildAt( 0 );
m_browseState.m_top = (view == null) ? 0 : view.getTop();
DBUtils.dictsSetOffset( this, m_name, m_browseState );
m_browseState = null;
}
super.onPause();
}
@Override
protected void onResume()
{
super.onResume();
if ( null == m_browseState ) {
m_browseState = DBUtils.dictsGetOffset( this, m_name );
}
}
@Override
@ -247,9 +283,9 @@ public class DictBrowseActivity extends XWListActivity
TextView text = (TextView)view;
int newval = Integer.parseInt( text.getText().toString() );
if ( parent == m_minSpinner ) {
setMinMax( newval, m_maxShown );
setMinMax( newval, m_browseState.m_maxShown );
} else if ( parent == m_maxSpinner ) {
setMinMax( m_minShown, newval );
setMinMax( m_browseState.m_minShown, newval );
}
}
@ -289,26 +325,31 @@ public class DictBrowseActivity extends XWListActivity
// adapter/making it recognized a changed dataset. So, as a
// workaround, relaunch the activity with different
// parameters.
if ( m_minShown != min || m_maxShown != max ) {
Intent intent = getIntent();
intent.putExtra( DICT_MIN, min );
intent.putExtra( DICT_MAX, max );
intent.putExtra( DICT_COUNTS, m_counts );
startActivity( intent );
if ( m_browseState.m_minShown != min ||
m_browseState.m_maxShown != max ) {
m_browseState.m_pos = 0;
m_browseState.m_top = 0;
m_browseState.m_minShown = min;
m_browseState.m_maxShown = max;
DBUtils.dictsSetOffset( this, m_name, m_browseState );
m_browseState = null;
startActivity( getIntent() );
finish();
}
}
private void figureMinMax()
private void figureMinMax( int[] counts )
{
Assert.assertTrue( m_counts.length == XwJNI.MAX_COLS_DICT + 1 );
Assert.assertTrue( counts.length == XwJNI.MAX_COLS_DICT + 1 );
m_minAvail = 0;
while ( 0 == m_counts[m_minAvail] ) {
while ( 0 == counts[m_minAvail] ) {
++m_minAvail;
}
m_maxAvail = XwJNI.MAX_COLS_DICT;
while ( 0 == m_counts[m_maxAvail] ) { //
while ( 0 == counts[m_maxAvail] ) { //
--m_maxAvail;
}
}
@ -342,11 +383,13 @@ public class DictBrowseActivity extends XWListActivity
// current max the largest min allowed, and the current
// min the smallest max allowed.
m_minSpinner = (Spinner)findViewById( R.id.wordlen_min );
makeAdapter( m_minSpinner, m_minAvail, m_maxShown, m_minShown );
makeAdapter( m_minSpinner, m_minAvail, m_browseState.m_maxShown,
m_browseState.m_minShown );
m_minSpinner.setOnItemSelectedListener( this );
m_maxSpinner = (Spinner)findViewById( R.id.wordlen_max );
makeAdapter( m_maxSpinner, m_minShown, m_maxAvail, m_maxShown );
makeAdapter( m_maxSpinner, m_browseState.m_minShown,
m_maxAvail, m_browseState.m_maxShown );
m_maxSpinner.setOnItemSelectedListener( this );
}