merge in the db part of 82c39489f0

(android_groups branch, local only right now), but not the UI part
since it won't make the next release.
This commit is contained in:
Eric House 2012-11-26 08:05:05 -08:00
parent 9130c8da50
commit e590db5f3f
5 changed files with 103 additions and 31 deletions

View file

@ -71,6 +71,7 @@
<string name="key_gcmvers_regid">key_gcmvers_regid</string>
<string name="key_relay_regid">key_relay_regid</string>
<string name="key_checked_sms">key_checked_sms</string>
<string name="key_default_group">key_default_group</string>
<string name="key_notagain_sync">key_notagain_sync</string>
<string name="key_notagain_chat">key_notagain_chat</string>

View file

@ -2137,4 +2137,7 @@
<string name="default_loc_summary">(Not in external/sdcard memory)</string>
<string name="download_path_title">Downloads Directory</string>
<string name="group_cur_games">My games</string>
<string name="group_new_games">New games</string>
</resources>

View file

@ -20,9 +20,10 @@
package org.eehouse.android.xw4;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
@ -30,8 +31,9 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME_OBITS = "obits";
public static final String TABLE_NAME_DICTBROWSE = "dictbrowse";
public static final String TABLE_NAME_DICTINFO = "dictinfo";
public static final String TABLE_NAME_GROUPS = "groups";
private static final String DB_NAME = "xwdb";
private static final int DB_VERSION = 14;
private static final int DB_VERSION = 15;
public static final String GAME_NAME = "GAME_NAME";
public static final String NUM_MOVES = "NUM_MOVES";
@ -60,7 +62,7 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String SEED = "SEED";
public static final String SMSPHONE = "SMSPHONE";
public static final String LASTMOVE = "LASTMOVE";
public static final String GROUPID = "GROUPID";
public static final String DICTNAME = "DICTNAME";
public static final String MD5SUM = "MD5SUM";
@ -76,6 +78,11 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String CREATE_TIME = "CREATE_TIME";
public static final String LASTPLAY_TIME = "LASTPLAY_TIME";
public static final String GROUPNAME = "GROUPNAME";
public static final String EXPANDED = "EXPANDED";
private Context m_context;
private static final String[] s_summaryColsAndTypes = {
GAME_NAME, "TEXT"
,NUM_MOVES, "INTEGER"
@ -99,6 +106,7 @@ public class DBHelper extends SQLiteOpenHelper {
,GAMEID, "INTEGER"
,REMOTEDEVS, "TEXT"
,LASTMOVE, "INTEGER DEFAULT 0"
,GROUPID, "INTEGER"
// HASMSGS: sqlite doesn't have bool; use 0 and 1
,HASMSGS, "INTEGER DEFAULT 0"
,CONTRACTED, "INTEGER DEFAULT 0"
@ -131,9 +139,15 @@ public class DBHelper extends SQLiteOpenHelper {
,ITERPREFIX, "TEXT"
};
private static final String[] s_groupsSchema = {
GROUPNAME, "TEXT"
,EXPANDED, "INTEGER(1)"
};
public DBHelper( Context context )
{
super( context, DB_NAME, null, DB_VERSION );
m_context = context;
}
public static String getDBName()
@ -148,6 +162,7 @@ public class DBHelper extends SQLiteOpenHelper {
createTable( db, TABLE_NAME_OBITS, s_obitsColsAndTypes );
createTable( db, TABLE_NAME_DICTINFO, s_dictInfoColsAndTypes );
createTable( db, TABLE_NAME_DICTBROWSE, s_dictBrowseColsAndTypes );
createGroupsTable( db );
}
@Override
@ -177,9 +192,11 @@ public class DBHelper extends SQLiteOpenHelper {
case 12:
createTable( db, TABLE_NAME_DICTINFO, s_dictInfoColsAndTypes );
createTable( db, TABLE_NAME_DICTBROWSE, s_dictBrowseColsAndTypes );
case 13:
addSumColumn( db, LASTMOVE );
case 14:
addSumColumn( db, GROUPID );
createGroupsTable( db );
// nothing yet
break;
default:
@ -221,4 +238,26 @@ public class DBHelper extends SQLiteOpenHelper {
db.execSQL( query.toString() );
}
private void createGroupsTable( SQLiteDatabase db )
{
createTable( db, TABLE_NAME_GROUPS, s_groupsSchema );
// Create an empty group name
ContentValues values = new ContentValues();
values.put( GROUPNAME, m_context.getString(R.string.group_cur_games) );
values.put( EXPANDED, 1 );
long curGroup = db.insert( TABLE_NAME_GROUPS, null, values );
values = new ContentValues();
values.put( GROUPNAME, m_context.getString(R.string.group_new_games) );
values.put( EXPANDED, 0 );
long newGroup = db.insert( TABLE_NAME_GROUPS, null, values );
// place all existing games in the initial unnamed group
values = new ContentValues();
values.put( GROUPID, curGroup );
db.update( DBHelper.TABLE_NAME_SUM, values, null, null );
XWPrefs.setDefaultNewGameGroup( m_context, newGroup );
}
}

View file

@ -364,18 +364,9 @@ public class DBUtils {
private static void setInt( long rowid, String column, int value )
{
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( ROW_ID_FMT, rowid );
ContentValues values = new ContentValues();
values.put( column, value );
int result = db.update( DBHelper.TABLE_NAME_SUM,
values, selection, null );
Assert.assertTrue( result == 1 );
db.close();
}
ContentValues values = new ContentValues();
values.put( column, value );
updateRow( null, DBHelper.TABLE_NAME_SUM, rowid, values );
}
public static void setMsgFlags( long rowid, int flags )
@ -693,6 +684,8 @@ public class DBUtils {
long timestamp = new Date().getTime();
values.put( DBHelper.CREATE_TIME, timestamp );
values.put( DBHelper.LASTPLAY_TIME, timestamp );
values.put( DBHelper.GROUPID,
XWPrefs.getDefaultNewGameGroup( context ) );
long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values );
@ -840,21 +833,9 @@ public class DBUtils {
public static void setName( Context context, long rowid, String name )
{
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( ROW_ID_FMT, rowid );
ContentValues values = new ContentValues();
values.put( DBHelper.GAME_NAME, name );
int result = db.update( DBHelper.TABLE_NAME_SUM,
values, selection, null );
db.close();
if ( 0 == result ) {
DbgUtils.logf( "setName(%d,%s) failed", rowid, name );
}
}
ContentValues values = new ContentValues();
values.put( DBHelper.GAME_NAME, name );
updateRow( context, DBHelper.TABLE_NAME_SUM, rowid, values );
}
public static HistoryPair[] getChatHistory( Context context, long rowid )
@ -874,6 +855,23 @@ public class DBUtils {
return result;
}
private static void updateRow( Context context, String table,
long rowid, ContentValues values )
{
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( ROW_ID_FMT, rowid );
int result = db.update( table, values, selection, null );
db.close();
if ( 0 == result ) {
DbgUtils.logf( "updateRow failed" );
}
}
}
private static String getChatHistoryStr( Context context, long rowid )
{
String result = null;
@ -1220,6 +1218,7 @@ public class DBUtils {
private static void initDB( Context context )
{
if ( null == s_dbHelper ) {
Assert.assertNotNull( context );
s_dbHelper = new DBHelper( context );
// force any upgrade
s_dbHelper.getWritableDatabase().close();

View file

@ -151,6 +151,25 @@ public class XWPrefs {
editor.commit();
}
public static long getPrefsLong( Context context, int keyID,
long defaultValue )
{
String key = context.getString( keyID );
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
return sp.getLong( key, defaultValue );
}
public static void setPrefsLong( Context context, int keyID, long newVal )
{
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences( context );
SharedPreferences.Editor editor = sp.edit();
String key = context.getString( keyID );
editor.putLong( key, newVal );
editor.commit();
}
public static void setClosedLangs( Context context, String[] langs )
{
setPrefsString( context, R.string.key_closed_langs,
@ -275,6 +294,17 @@ public class XWPrefs {
return getPrefsBoolean( context, R.string.key_default_loc, true );
}
public static long getDefaultNewGameGroup( Context context )
{
return getPrefsLong( context, R.string.key_default_group,
DBUtils.ROWID_NOTFOUND );
}
public static void setDefaultNewGameGroup( Context context, long val )
{
setPrefsLong( context, R.string.key_default_group, val );
}
protected static String getPrefsString( Context context, int keyID )
{
String key = context.getString( keyID );