Fixed some bugs and cleaned up codes
* Fixed crash on backspacing from symbol (1 key) * Hopefully fixed weird crash trying to dismiss view * Fixed long press not working for SoftKeys
This commit is contained in:
parent
38df7d4563
commit
e226f45c26
13 changed files with 1169 additions and 1092 deletions
|
|
@ -20,7 +20,7 @@ public class T9DB {
|
|||
protected static final int DATABASE_VERSION = 1;
|
||||
protected static final String WORD_TABLE_NAME = "word";
|
||||
protected static final String FREQ_TRIGGER_NAME = "freqtrigger";
|
||||
//50k, 10k
|
||||
// 50k, 10k
|
||||
private static final int FREQ_MAX = 50000;
|
||||
private static final int FREQ_DIV = 10000;
|
||||
|
||||
|
|
@ -32,34 +32,34 @@ public class T9DB {
|
|||
|
||||
private static final int MAX_RESULTS = 8;
|
||||
|
||||
private static final int CAPS_OFF = 0;
|
||||
private static final int CAPS_SINGLE = 1;
|
||||
private static final int CAPS_ALL = 2;
|
||||
|
||||
private static final int CAPS_OFF = 0;
|
||||
private static final int CAPS_SINGLE = 1;
|
||||
private static final int CAPS_ALL = 2;
|
||||
|
||||
private DatabaseHelper mOpenHelper;
|
||||
private SQLiteDatabase db;
|
||||
|
||||
private Context parent;
|
||||
|
||||
|
||||
public T9DB(Context caller) {
|
||||
//create db
|
||||
// create db
|
||||
parent = caller;
|
||||
mOpenHelper = new DatabaseHelper(caller);
|
||||
}
|
||||
|
||||
|
||||
public static SQLiteDatabase getSQLDB(Context caller) {
|
||||
return new DatabaseHelper(caller).getWritableDatabase();
|
||||
}
|
||||
|
||||
|
||||
public void init() {
|
||||
db = mOpenHelper.getWritableDatabase();
|
||||
//mOpenHelper.onUpgrade(db, 0, DATABASE_VERSION);
|
||||
// mOpenHelper.onUpgrade(db, 0, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
db.close();
|
||||
}
|
||||
|
||||
|
||||
public void nuke() {
|
||||
init();
|
||||
mOpenHelper.onUpgrade(db, 0, DATABASE_VERSION);
|
||||
|
|
@ -70,43 +70,89 @@ public class T9DB {
|
|||
public void addWord(String iword) throws DBException {
|
||||
Resources r = parent.getResources();
|
||||
if (iword.equals("")) {
|
||||
throw new DBException(r.getString(R.string.add_word_blank));
|
||||
throw new DBException(r.getString(R.string.add_word_blank));
|
||||
}
|
||||
//get int sequence
|
||||
String seq = CharMap.getStringSequence(iword);
|
||||
//add int sequence into num table
|
||||
// get int sequence
|
||||
String seq;
|
||||
try {
|
||||
seq = CharMap.getStringSequence(iword);
|
||||
} catch (NullPointerException e) {
|
||||
throw new DBException(r.getString(R.string.add_word_badchar));
|
||||
}
|
||||
// add int sequence into num table
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(COLUMN_SEQ, seq);
|
||||
//add word into word
|
||||
// add word into word
|
||||
values.put(COLUMN_WORD, iword);
|
||||
values.put(COLUMN_FREQUENCY, 1);
|
||||
try {
|
||||
db.insertOrThrow(WORD_TABLE_NAME, null, values);
|
||||
} catch (SQLiteConstraintException e) {
|
||||
String msg = r.getString(R.string.add_word_exist1) + iword + r.getString(R.string.add_word_exist2);
|
||||
String msg = r.getString(R.string.add_word_exist1) + iword
|
||||
+ r.getString(R.string.add_word_exist2);
|
||||
Log.w("T9DB.addWord", msg);
|
||||
throw new DBException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementWord(int id) {
|
||||
db.execSQL("UPDATE " + WORD_TABLE_NAME + " SET " + COLUMN_FREQUENCY + " = " +
|
||||
COLUMN_FREQUENCY + "+ 1 WHERE " + COLUMN_ID + " = \"" + id + "\"");
|
||||
// if id's freq is greater than X we should normalise those with the same seq
|
||||
db.execSQL(
|
||||
"UPDATE " + WORD_TABLE_NAME +
|
||||
" SET " + COLUMN_FREQUENCY + " = " + COLUMN_FREQUENCY + "+ 1" +
|
||||
" WHERE " + COLUMN_ID + " = \"" + id + "\"");
|
||||
// if id's freq is greater than X we should normalise those with the
|
||||
// same seq
|
||||
}
|
||||
|
||||
protected String getWord(String is) {
|
||||
String result = null;
|
||||
String q =
|
||||
"SELECT " + COLUMN_WORD + " FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + "=?" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC";
|
||||
Cursor cur = db.rawQuery(q, new String[] { is });
|
||||
int hits = 0;
|
||||
if (cur.moveToFirst()) {
|
||||
result = cur.getString(0);
|
||||
}
|
||||
cur.close();
|
||||
if (result != null) {
|
||||
return result;
|
||||
} else {
|
||||
int islen = is.length();
|
||||
char c = is.charAt(islen - 1);
|
||||
c++;
|
||||
q = "SELECT " + COLUMN_WORD + " FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + " >= '" + is + "1" + "' AND " + COLUMN_SEQ + " < '"
|
||||
+ is.substring(0, islen - 1) + c + "'" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC, " + COLUMN_SEQ + " ASC" +
|
||||
" LIMIT " + (MAX_RESULTS - hits);
|
||||
cur = db.rawQuery(q, null);
|
||||
|
||||
if (cur.moveToFirst()) {
|
||||
result = cur.getString(1);
|
||||
}
|
||||
if (result == null) {
|
||||
result = "";
|
||||
}
|
||||
cur.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void updateWords(String is, ArrayList<String> stringList, ArrayList<Integer> intList, int capsMode){
|
||||
public void updateWords(String is, ArrayList<String> stringList, ArrayList<Integer> intList,
|
||||
int capsMode) {
|
||||
stringList.clear();
|
||||
intList.clear();
|
||||
//String[] sa = packInts(stringToInts(is), true);
|
||||
// String[] sa = packInts(stringToInts(is), true);
|
||||
int islen = is.length();
|
||||
String q = "SELECT " + COLUMN_ID + ", " + COLUMN_WORD +
|
||||
" FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + "=?" +
|
||||
String q =
|
||||
"SELECT " + COLUMN_ID + ", " + COLUMN_WORD + " FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + "=?" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC";
|
||||
Cursor cur = db.rawQuery(q, new String[] {is});
|
||||
Cursor cur = db.rawQuery(q, new String[] { is });
|
||||
int hits = 0;
|
||||
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
intList.add(cur.getInt(0));
|
||||
stringList.add(cur.getString(1));
|
||||
if (hits >= 15) {
|
||||
|
|
@ -116,22 +162,24 @@ public class T9DB {
|
|||
}
|
||||
cur.close();
|
||||
if (hits < 4) {
|
||||
char c = is.charAt(islen-1);
|
||||
char c = is.charAt(islen - 1);
|
||||
c++;
|
||||
// q = "SELECT " + COLUMN_WORD +", " + COLUMN_FREQUENCY +
|
||||
// " FROM " + WORD_TABLE_NAME +
|
||||
// " WHERE " + COLUMN_SEQ + " LIKE ?" +
|
||||
// " ORDER BY " + COLUMN_SEQ + " ASC, " + COLUMN_FREQUENCY + " DESC;";
|
||||
// c = db.rawQuery(q, new String[] {is + "_%"});
|
||||
//above is hella slow below is gotta query fast
|
||||
// q = "SELECT " + COLUMN_WORD +", " + COLUMN_FREQUENCY +
|
||||
// " FROM " + WORD_TABLE_NAME +
|
||||
// " WHERE " + COLUMN_SEQ + " LIKE ?" +
|
||||
// " ORDER BY " + COLUMN_SEQ + " ASC, " + COLUMN_FREQUENCY +
|
||||
// " DESC;";
|
||||
// c = db.rawQuery(q, new String[] {is + "_%"});
|
||||
// above is hella slow below is gotta query fast
|
||||
q = "SELECT " + COLUMN_ID + ", " + COLUMN_WORD +
|
||||
" FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + " >= '" + is + "1" + "' AND " + COLUMN_SEQ + " < '" + is.substring(0, islen-1) + c + "'" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC, " + COLUMN_SEQ + " ASC" +
|
||||
" WHERE " + COLUMN_SEQ + " >= '" + is + "1" + "' AND " + COLUMN_SEQ + " < '"
|
||||
+ is.substring(0, islen - 1) + c + "'" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC, " + COLUMN_SEQ + " ASC" +
|
||||
" LIMIT " + (MAX_RESULTS - hits);
|
||||
cur = db.rawQuery(q, null);
|
||||
|
||||
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
|
||||
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
intList.add(cur.getInt(0));
|
||||
stringList.add(cur.getString(1));
|
||||
if (hits >= 10) {
|
||||
|
|
@ -141,11 +189,11 @@ public class T9DB {
|
|||
}
|
||||
cur.close();
|
||||
}
|
||||
//Log.d("T9DB.updateWords", "pre: " + stringList);
|
||||
// Log.d("T9DB.updateWords", "pre: " + stringList);
|
||||
if (capsMode == CAPS_OFF) {
|
||||
return;
|
||||
}
|
||||
//Log.d("T9DB.updateWords", "filtering...");
|
||||
// Log.d("T9DB.updateWords", "filtering...");
|
||||
// filter list
|
||||
Iterator<String> iter = stringList.iterator();
|
||||
String word;
|
||||
|
|
@ -160,25 +208,25 @@ public class T9DB {
|
|||
if (wordtemp.equals(word)) {
|
||||
index++;
|
||||
continue;
|
||||
} else if (stringList.contains(wordtemp)){
|
||||
//remove this entry
|
||||
} else if (stringList.contains(wordtemp)) {
|
||||
// remove this entry
|
||||
iter.remove();
|
||||
removed = true;
|
||||
} else {
|
||||
stringList.set(index, wordtemp);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case CAPS_SINGLE:
|
||||
if (word.length() > 1) {
|
||||
wordtemp = word.substring(0, 1).toUpperCase(Locale.US) + word.substring(1);
|
||||
} else {
|
||||
wordtemp = word.toUpperCase(Locale.US);
|
||||
wordtemp = word.toUpperCase(Locale.US);
|
||||
}
|
||||
if (wordtemp.equals(word)) {
|
||||
index++;
|
||||
continue;
|
||||
} else if (stringList.contains(wordtemp)){
|
||||
//remove this entry
|
||||
} else if (stringList.contains(wordtemp)) {
|
||||
// remove this entry
|
||||
iter.remove();
|
||||
removed = true;
|
||||
} else {
|
||||
|
|
@ -195,22 +243,21 @@ public class T9DB {
|
|||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
protected void updateWordsW(String is, ArrayList<String> stringList, ArrayList<Integer> intList,
|
||||
ArrayList<Integer> freq){
|
||||
|
||||
protected void updateWordsW(String is, ArrayList<String> stringList,
|
||||
ArrayList<Integer> intList, ArrayList<Integer> freq) {
|
||||
stringList.clear();
|
||||
intList.clear();
|
||||
freq.clear();
|
||||
//String[] sa = packInts(stringToInts(is), true);
|
||||
// String[] sa = packInts(stringToInts(is), true);
|
||||
int islen = is.length();
|
||||
String q = "SELECT " + COLUMN_ID + ", " + COLUMN_WORD + ", " + COLUMN_FREQUENCY +
|
||||
" FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + "=?" +
|
||||
" WHERE " + COLUMN_SEQ + "=?" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC";
|
||||
Cursor cur = db.rawQuery(q, new String[] {is});
|
||||
Cursor cur = db.rawQuery(q, new String[] { is });
|
||||
int hits = 0;
|
||||
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
intList.add(cur.getInt(0));
|
||||
stringList.add(cur.getString(1));
|
||||
freq.add(cur.getInt(2));
|
||||
|
|
@ -221,22 +268,24 @@ public class T9DB {
|
|||
}
|
||||
cur.close();
|
||||
if (hits < 4) {
|
||||
char c = is.charAt(islen-1);
|
||||
char c = is.charAt(islen - 1);
|
||||
c++;
|
||||
// q = "SELECT " + COLUMN_WORD +", " + COLUMN_FREQUENCY +
|
||||
// " FROM " + WORD_TABLE_NAME +
|
||||
// " WHERE " + COLUMN_SEQ + " LIKE ?" +
|
||||
// " ORDER BY " + COLUMN_SEQ + " ASC, " + COLUMN_FREQUENCY + " DESC;";
|
||||
// c = db.rawQuery(q, new String[] {is + "_%"});
|
||||
//above is hella slow
|
||||
q = "SELECT " + COLUMN_ID + ", " + COLUMN_WORD + ", " + COLUMN_FREQUENCY +
|
||||
// q = "SELECT " + COLUMN_WORD +", " + COLUMN_FREQUENCY +
|
||||
// " FROM " + WORD_TABLE_NAME +
|
||||
// " WHERE " + COLUMN_SEQ + " LIKE ?" +
|
||||
// " ORDER BY " + COLUMN_SEQ + " ASC, " + COLUMN_FREQUENCY +
|
||||
// " DESC;";
|
||||
// c = db.rawQuery(q, new String[] {is + "_%"});
|
||||
// above is hella slow
|
||||
q = "SELECT " + COLUMN_ID + ", " + COLUMN_WORD + ", " + COLUMN_FREQUENCY +
|
||||
" FROM " + WORD_TABLE_NAME +
|
||||
" WHERE " + COLUMN_SEQ + " >= '" + is + "' AND " + COLUMN_SEQ + " < '" + is.substring(0, islen-1) + c + "'" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC, " + COLUMN_SEQ + " ASC" +
|
||||
" WHERE " + COLUMN_SEQ + " >= '" + is + "' AND " + COLUMN_SEQ
|
||||
+ " < '" + is.substring(0, islen - 1) + c + "'" +
|
||||
" ORDER BY " + COLUMN_FREQUENCY + " DESC, " + COLUMN_SEQ + " ASC" +
|
||||
" LIMIT " + (MAX_RESULTS - hits);
|
||||
cur = db.rawQuery(q, null);
|
||||
|
||||
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
|
||||
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
||||
intList.add(cur.getInt(0));
|
||||
stringList.add(cur.getString(1));
|
||||
freq.add(cur.getInt(2));
|
||||
|
|
@ -249,35 +298,6 @@ public class T9DB {
|
|||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
protected static int[] stringToInts(String s){
|
||||
int[] il = new int[s.length()];
|
||||
for (int x = 0; x < s.length(); x++){
|
||||
il[x] = s.charAt(x) - 48; //lol hope this is ASCII all the time
|
||||
}
|
||||
return il;
|
||||
}
|
||||
|
||||
protected static String[] packInts(int[] intseq, boolean stringArray){
|
||||
int[] ia = packInts(intseq);
|
||||
String[] sa = {Integer.toString(ia[0]), Integer.toString(ia[1])};
|
||||
return sa;
|
||||
}
|
||||
protected static int[] packInts(int[] intseq){
|
||||
int[] ia = {0, 0};
|
||||
|
||||
for (int x = 0; x < intseq.length; x++){
|
||||
if (x < 8){
|
||||
//packing 8 ints (<10) into 32bit int ... I hope...
|
||||
ia[0] = ia[0] | (intseq[x] << x*4);
|
||||
} else {
|
||||
ia[1] = intseq[x] << (x % 8) * 4;
|
||||
}
|
||||
}
|
||||
|
||||
return ia;
|
||||
}
|
||||
|
||||
private static class DatabaseHelper extends SQLiteOpenHelper {
|
||||
|
||||
|
|
@ -287,27 +307,27 @@ public class T9DB {
|
|||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL("CREATE TABLE " + WORD_TABLE_NAME + " ("
|
||||
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
||||
+ COLUMN_SEQ + " TEXT, "
|
||||
+ COLUMN_WORD + " TEXT UNIQUE, "
|
||||
+ COLUMN_FREQUENCY + " INTEGER"
|
||||
+ ")");
|
||||
db.execSQL("CREATE TABLE " + WORD_TABLE_NAME + " (" +
|
||||
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||
COLUMN_SEQ + " TEXT, " +
|
||||
COLUMN_WORD + " TEXT UNIQUE, " +
|
||||
COLUMN_FREQUENCY + " INTEGER" + ")");
|
||||
db.execSQL("CREATE INDEX idx ON " + WORD_TABLE_NAME + "("
|
||||
//+ COLUMN_NUMHIGH + ", " + COLUMN_NUMLOW + ")");
|
||||
+ COLUMN_SEQ + " ASC, " + COLUMN_FREQUENCY + " DESC )");
|
||||
db.execSQL("CREATE TRIGGER " + FREQ_TRIGGER_NAME + " AFTER UPDATE ON " + WORD_TABLE_NAME
|
||||
+ " WHEN NEW." + COLUMN_FREQUENCY + " > " + FREQ_MAX
|
||||
+ " BEGIN"
|
||||
+ " UPDATE " + WORD_TABLE_NAME + " SET " + COLUMN_FREQUENCY
|
||||
+ " = " + COLUMN_FREQUENCY + " / " + FREQ_DIV + " WHERE " + COLUMN_SEQ + " = NEW." + COLUMN_SEQ + ";"
|
||||
+ " END;");
|
||||
+ COLUMN_SEQ + " ASC, " + COLUMN_FREQUENCY + " DESC )");
|
||||
db.execSQL("CREATE TRIGGER " + FREQ_TRIGGER_NAME +
|
||||
" AFTER UPDATE ON " + WORD_TABLE_NAME +
|
||||
" WHEN NEW." + COLUMN_FREQUENCY + " > " + FREQ_MAX +
|
||||
" BEGIN" +
|
||||
" UPDATE " + WORD_TABLE_NAME + " SET " + COLUMN_FREQUENCY + " = "
|
||||
+ COLUMN_FREQUENCY + " / " + FREQ_DIV +
|
||||
" WHERE " + COLUMN_SEQ + " = NEW." + COLUMN_SEQ + ";" +
|
||||
" END;");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
Log.w("T9DB", "Upgrading database from version " + oldVersion + " to "
|
||||
+ newVersion + ", which will destroy all old data");
|
||||
Log.w("T9DB", "Upgrading database from version " + oldVersion + " to " + newVersion
|
||||
+ ", which will destroy all old data");
|
||||
db.execSQL("DROP TABLE IF EXISTS " + WORD_TABLE_NAME);
|
||||
db.execSQL("DROP INDEX IF EXISTS idx");
|
||||
onCreate(db);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue