Simplified UUID generation, no more proof text

pull/14/head
Sam Bosley 12 years ago
parent 3f2e3921a4
commit c1f1681bc0

@ -35,12 +35,6 @@ abstract public class RemoteModel extends AbstractModel {
/** user json property */ /** user json property */
public static final StringProperty USER_JSON_PROPERTY = new StringProperty(null, USER_JSON_PROPERTY_NAME); public static final StringProperty USER_JSON_PROPERTY = new StringProperty(null, USER_JSON_PROPERTY_NAME);
/** proof text property name */
protected static final String PROOF_TEXT_PROPERTY_NAME = "proofText"; //$NON-NLS-1$
/** proof text property */
public static final StringProperty PROOF_TEXT_PROPERTY = new StringProperty(null, PROOF_TEXT_PROPERTY_NAME);
/** pushed at date property name */ /** pushed at date property name */
public static final String PUSHED_AT_PROPERTY_NAME = "pushedAt"; //$NON-NLS-1$ public static final String PUSHED_AT_PROPERTY_NAME = "pushedAt"; //$NON-NLS-1$

@ -117,10 +117,6 @@ public final class TagData extends RemoteModel {
public static final StringProperty TAG_DESCRIPTION = new StringProperty( public static final StringProperty TAG_DESCRIPTION = new StringProperty(
TABLE, "tagDescription"); TABLE, "tagDescription");
/** UUID proof text */
public static final StringProperty PROOF_TEXT = new StringProperty(
TABLE, PROOF_TEXT_PROPERTY_NAME);
/** Pushed at date */ /** Pushed at date */
public static final LongProperty PUSHED_AT = new LongProperty( public static final LongProperty PUSHED_AT = new LongProperty(
TABLE, PUSHED_AT_PROPERTY_NAME); TABLE, PUSHED_AT_PROPERTY_NAME);

@ -167,10 +167,6 @@ public final class Task extends RemoteModel {
public static final LongProperty LAST_SYNC = new LongProperty( public static final LongProperty LAST_SYNC = new LongProperty(
TABLE, "lastSync"); TABLE, "lastSync");
/** UUID proof text */
public static final StringProperty PROOF_TEXT = new StringProperty(
TABLE, PROOF_TEXT_PROPERTY_NAME);
/** Pushed at date */ /** Pushed at date */
public static final LongProperty PUSHED_AT = new LongProperty( public static final LongProperty PUSHED_AT = new LongProperty(
TABLE, PUSHED_AT_PROPERTY_NAME); TABLE, PUSHED_AT_PROPERTY_NAME);

@ -106,10 +106,6 @@ public class Update extends RemoteModel {
public static final LongProperty CREATION_DATE = new LongProperty( public static final LongProperty CREATION_DATE = new LongProperty(
TABLE, "created"); TABLE, "created");
/** UUID proof text */
public static final StringProperty PROOF_TEXT = new StringProperty(
TABLE, PROOF_TEXT_PROPERTY_NAME);
/** Pushed at date */ /** Pushed at date */
public static final LongProperty PUSHED_AT = new LongProperty( public static final LongProperty PUSHED_AT = new LongProperty(
TABLE, PUSHED_AT_PROPERTY_NAME); TABLE, PUSHED_AT_PROPERTY_NAME);

@ -88,7 +88,6 @@ public class NameMaps {
putPropertyToServerName(Task.USER, "user", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true); putPropertyToServerName(Task.USER, "user", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true);
putPropertyToServerName(Task.CREATOR_ID, "creator_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true); putPropertyToServerName(Task.CREATOR_ID, "creator_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true);
putPropertyToServerName(Task.UUID, "uuid", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true); putPropertyToServerName(Task.UUID, "uuid", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true);
putPropertyToServerName(Task.PROOF_TEXT, "proof_text", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, false);
putPropertyToServerName(Task.PUSHED_AT, "pushed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true); putPropertyToServerName(Task.PUSHED_AT, "pushed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, true);
TASK_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TASK_PROPERTIES_LOCAL_TO_SERVER); TASK_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TASK_PROPERTIES_LOCAL_TO_SERVER);
@ -116,7 +115,6 @@ public class NameMaps {
putPropertyToServerName(TagData.CREATION_DATE, "created_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false); putPropertyToServerName(TagData.CREATION_DATE, "created_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false);
putPropertyToServerName(TagData.DELETION_DATE, "deleted_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false); putPropertyToServerName(TagData.DELETION_DATE, "deleted_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false);
putPropertyToServerName(TagData.UUID, "uuid", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, true); putPropertyToServerName(TagData.UUID, "uuid", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, true);
putPropertyToServerName(TagData.PROOF_TEXT, "proof_text", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false);
putPropertyToServerName(TagData.TAG_ORDERING, "tag_ordering", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false); //TODO: NOT CORRECT putPropertyToServerName(TagData.TAG_ORDERING, "tag_ordering", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, false); //TODO: NOT CORRECT
putPropertyToServerName(TagData.PUSHED_AT, "pushed_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, true); putPropertyToServerName(TagData.PUSHED_AT, "pushed_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, true);

@ -9,7 +9,6 @@ import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Pair;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TagDataDao; import com.todoroo.astrid.dao.TagDataDao;
@ -150,9 +149,7 @@ public class AstridNewSyncMigrator {
instance.readPropertiesFromCursor(cursor); instance.readPropertiesFromCursor(cursor);
if (!instance.containsNonNullValue(RemoteModel.REMOTE_ID_PROPERTY) || instance.getValue(RemoteModel.REMOTE_ID_PROPERTY) == 0) { if (!instance.containsNonNullValue(RemoteModel.REMOTE_ID_PROPERTY) || instance.getValue(RemoteModel.REMOTE_ID_PROPERTY) == 0) {
// No remote id exists, just create a UUID // No remote id exists, just create a UUID
Pair<String, String> uuidPair = UUIDHelper.newUUID(); instance.setValue(RemoteModel.UUID_PROPERTY, UUIDHelper.newUUID());
instance.setValue(RemoteModel.UUID_PROPERTY, uuidPair.getLeft());
instance.setValue(RemoteModel.PROOF_TEXT_PROPERTY, uuidPair.getRight());
} else { } else {
// Migrate remote id to uuid field // Migrate remote id to uuid field
instance.setValue(RemoteModel.UUID_PROPERTY, Long.toString(instance.getValue(RemoteModel.REMOTE_ID_PROPERTY))); instance.setValue(RemoteModel.UUID_PROPERTY, Long.toString(instance.getValue(RemoteModel.REMOTE_ID_PROPERTY)));

@ -360,13 +360,10 @@ public class Database extends AbstractDatabase {
database.execSQL(createTableSql(visitor, TaskOutstanding.TABLE.name, TaskOutstanding.PROPERTIES)); database.execSQL(createTableSql(visitor, TaskOutstanding.TABLE.name, TaskOutstanding.PROPERTIES));
database.execSQL(createTableSql(visitor, TagOutstanding.TABLE.name, TagOutstanding.PROPERTIES)); database.execSQL(createTableSql(visitor, TagOutstanding.TABLE.name, TagOutstanding.PROPERTIES));
database.execSQL(addColumnSql(Task.TABLE, Task.PROOF_TEXT, visitor, null));
database.execSQL(addColumnSql(Task.TABLE, Task.PUSHED_AT, visitor, null)); database.execSQL(addColumnSql(Task.TABLE, Task.PUSHED_AT, visitor, null));
database.execSQL(addColumnSql(Task.TABLE, Task.UUID, visitor, null)); database.execSQL(addColumnSql(Task.TABLE, Task.UUID, visitor, null));
database.execSQL(addColumnSql(TagData.TABLE, TagData.PROOF_TEXT, visitor, null));
database.execSQL(addColumnSql(TagData.TABLE, TagData.PUSHED_AT, visitor, null)); database.execSQL(addColumnSql(TagData.TABLE, TagData.PUSHED_AT, visitor, null));
database.execSQL(addColumnSql(TagData.TABLE, TagData.UUID, visitor, null)); database.execSQL(addColumnSql(TagData.TABLE, TagData.UUID, visitor, null));
database.execSQL(addColumnSql(Update.TABLE, Update.PROOF_TEXT, visitor, null));
database.execSQL(addColumnSql(Update.TABLE, Update.PUSHED_AT, visitor, null)); database.execSQL(addColumnSql(Update.TABLE, Update.PUSHED_AT, visitor, null));
database.execSQL(addColumnSql(Update.TABLE, Update.UUID, visitor, null)); database.execSQL(addColumnSql(Update.TABLE, Update.UUID, visitor, null));
database.execSQL(addColumnSql(Update.TABLE, Update.TASK_UUID, visitor, null)); database.execSQL(addColumnSql(Update.TABLE, Update.TASK_UUID, visitor, null));

@ -1,7 +1,6 @@
package com.todoroo.astrid.dao; package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.utility.Pair;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.helper.UUIDHelper; import com.todoroo.astrid.helper.UUIDHelper;
@ -23,9 +22,7 @@ public class RemoteModelDao<RTYPE extends RemoteModel> extends DatabaseDao<RTYPE
@Override @Override
public boolean createNew(RTYPE item) { public boolean createNew(RTYPE item) {
if (!item.containsValue(RemoteModel.UUID_PROPERTY)) { if (!item.containsValue(RemoteModel.UUID_PROPERTY)) {
Pair<String, String> uuidPair = UUIDHelper.newUUID(); item.setValue(RemoteModel.UUID_PROPERTY, UUIDHelper.newUUID());
item.setValue(RemoteModel.UUID_PROPERTY, uuidPair.getLeft());
item.setValue(RemoteModel.PROOF_TEXT_PROPERTY, uuidPair.getRight());
} }
return super.createNew(item); return super.createNew(item);
}; };

@ -341,7 +341,6 @@ public class TaskDao extends RemoteModelDao<Task> {
if (existing.getValue(Task.CREATION_DATE).equals(newConflict.getValue(Task.CREATION_DATE))) if (existing.getValue(Task.CREATION_DATE).equals(newConflict.getValue(Task.CREATION_DATE)))
newConflict.setValue(Task.CREATION_DATE, newConflict.getValue(Task.CREATION_DATE) + 1000L); newConflict.setValue(Task.CREATION_DATE, newConflict.getValue(Task.CREATION_DATE) + 1000L);
newConflict.clearValue(Task.UUID); newConflict.clearValue(Task.UUID);
newConflict.clearValue(Task.PROOF_TEXT);
saveExisting(newConflict); saveExisting(newConflict);
} else { } else {
delete(newConflict.getId()); delete(newConflict.getId());

@ -1,86 +1,20 @@
package com.todoroo.astrid.helper; package com.todoroo.astrid.helper;
import java.security.SecureRandom;
import java.util.UUID; import java.util.UUID;
import com.todoroo.andlib.utility.Pair;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
public class UUIDHelper { public class UUIDHelper {
private static final String PREF_DEVICE_ID = "pref_device_id"; //$NON-NLS-1$
private static final String PREF_LAST_KNOWN_USER = "pref_last_known_user"; //$NON-NLS-1$
public static String getDeviceId() {
long lastUser = Preferences.getLong(PREF_LAST_KNOWN_USER, 0);
long currentUser = ActFmPreferenceService.userId();
if (currentUser > 0 && lastUser > 0 && currentUser != lastUser) { // User has changed
Preferences.clear(PREF_DEVICE_ID);
Preferences.setLong(PREF_LAST_KNOWN_USER, currentUser);
} else if (currentUser > 0 && lastUser <= 0) {
Preferences.setLong(PREF_LAST_KNOWN_USER, currentUser);
}
String saved = Preferences.getStringValue(PREF_DEVICE_ID);
if (saved != null)
return saved;
saved = UUID.randomUUID().toString();
Preferences.setString(PREF_DEVICE_ID, saved);
return saved;
}
private static final SecureRandom RANDOM = new SecureRandom();
private static final long MIN_UUID = 100000000; private static final long MIN_UUID = 100000000;
/** /**
* @return a pair consisting of the newly * @return a pair consisting of the newly
* generated uuid and the corresponding proof text * generated uuid and the corresponding proof text
*/ */
public static Pair<String, String> newUUID() { public static String newUUID() {
long uuid = 0; long uuid = 0;
String proofText = ""; //$NON-NLS-1$
do { do {
String hexDigits = "0123456789abcdef"; //$NON-NLS-1$ uuid = UUID.randomUUID().getLeastSignificantBits() & 0x7fffffffffffffffL;;
char[] chars = new char[80];
for (int i = 0; i < chars.length; i++) {
chars[i] = hexDigits.charAt(RANDOM.nextInt(hexDigits.length()));
}
String s = new String(chars);
String d = getDeviceId();
proofText = d + "," + s; //$NON-NLS-1$
uuid = bcryptToLong(proofText);
} while (uuid < MIN_UUID); } while (uuid < MIN_UUID);
return Pair.create(Long.toString(uuid), proofText); return Long.toString(uuid);
} }
private static final String SALT = "$2a$10$2RHhxhKaPb4VXlQUJyBU/O"; //$NON-NLS-1$
private static final String BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./"; //$NON-NLS-1$
private static long bcryptToLong(String proofText) {
String a = BCrypt.hashpw(proofText, SALT);
int start = 29;
int endPlusOne = 60;
String b = a.substring(start, endPlusOne);
long result = 1;
for (int i = 0; i < b.length(); i++) {
result = 31 * result + iOf(b.charAt(i));
}
return result;
}
@SuppressWarnings("nls")
private static int iOf(char c) {
int index = BASE_64_CHARS.indexOf(c);
if (index < 0)
throw new RuntimeException("Base64Chars is wrong! No character " + c + " found. Probably needs to be replaces with something else");
return index;
}
} }

@ -55,7 +55,6 @@ import com.todoroo.astrid.gcal.CalendarStartupReceiver;
import com.todoroo.astrid.gtasks.GtasksMetadata; import com.todoroo.astrid.gtasks.GtasksMetadata;
import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.sync.GtasksSyncService; import com.todoroo.astrid.gtasks.sync.GtasksSyncService;
import com.todoroo.astrid.helper.UUIDHelper;
import com.todoroo.astrid.opencrx.OpencrxCoreUtils; import com.todoroo.astrid.opencrx.OpencrxCoreUtils;
import com.todoroo.astrid.producteev.ProducteevUtilities; import com.todoroo.astrid.producteev.ProducteevUtilities;
import com.todoroo.astrid.reminders.ReengagementService; import com.todoroo.astrid.reminders.ReengagementService;
@ -158,9 +157,6 @@ public class StartupService {
Toast.makeText(context, R.string.TLA_notification_volume_low, Toast.LENGTH_LONG).show(); Toast.makeText(context, R.string.TLA_notification_volume_low, Toast.LENGTH_LONG).show();
} }
// Initializes the device id
UUIDHelper.getDeviceId();
// read current version // read current version
int latestSetVersion = 0; int latestSetVersion = 0;
try { try {

@ -172,7 +172,6 @@ public class TaskService {
return new Task(); return new Task();
newTask.clearValue(Task.ID); newTask.clearValue(Task.ID);
newTask.clearValue(Task.UUID); newTask.clearValue(Task.UUID);
newTask.clearValue(Task.PROOF_TEXT);
TodorooCursor<Metadata> cursor = metadataDao.query( TodorooCursor<Metadata> cursor = metadataDao.query(
Query.select(Metadata.PROPERTIES).where(MetadataCriteria.byTask(task.getId()))); Query.select(Metadata.PROPERTIES).where(MetadataCriteria.byTask(task.getId())));
try { try {
@ -216,7 +215,6 @@ public class TaskService {
return new Task(); return new Task();
newTask.clearValue(Task.ID); newTask.clearValue(Task.ID);
newTask.clearValue(Task.UUID); newTask.clearValue(Task.UUID);
newTask.clearValue(Task.PROOF_TEXT);
newTask.clearValue(Task.USER); newTask.clearValue(Task.USER);
newTask.clearValue(Task.USER_ID); newTask.clearValue(Task.USER_ID);

@ -17,39 +17,11 @@ public class SyncModelTest extends NewSyncTestCase {
public void testCreateTaskMakesUuid() { public void testCreateTaskMakesUuid() {
Task task = createTask(); Task task = createTask();
assertFalse(RemoteModel.NO_UUID.equals(task.getValue(Task.UUID))); assertFalse(RemoteModel.NO_UUID.equals(task.getValue(Task.UUID)));
assertFalse(TextUtils.isEmpty(task.getValue(Task.PROOF_TEXT)));
} }
public void testCreateTagMakesUuid() { public void testCreateTagMakesUuid() {
TagData tag = createTagData(); TagData tag = createTagData();
assertFalse(RemoteModel.NO_UUID.equals(tag.getValue(TagData.UUID))); assertFalse(RemoteModel.NO_UUID.equals(tag.getValue(TagData.UUID)));
assertFalse(TextUtils.isEmpty(tag.getValue(TagData.PROOF_TEXT)));
}
public void testCreateTaskMakesOutstandingProofText() {
Task task = createTask();
TodorooCursor<TaskOutstanding> cursor = taskOutstandingDao.query(
Query.select(TaskOutstanding.PROPERTIES)
.where(Criterion.and(TaskOutstanding.TASK_ID.eq(task.getId()),
TaskOutstanding.COLUMN_STRING.eq(RemoteModel.PROOF_TEXT_PROPERTY.name))));
try {
assertTrue(cursor.getCount() > 0);
} finally {
cursor.close();
}
}
public void testCreateTagMakesOutstandingProofText() {
TagData tag = createTagData();
TodorooCursor<TagOutstanding> cursor = tagOutstandingDao.query(
Query.select(TagOutstanding.PROPERTIES)
.where(Criterion.and(TagOutstanding.TAG_DATA_ID.eq(tag.getId()),
TagOutstanding.COLUMN_STRING.eq(RemoteModel.PROOF_TEXT_PROPERTY.name))));
try {
assertTrue(cursor.getCount() > 0);
} finally {
cursor.close();
}
} }
public void testChangeTaskMakesOutstandingEntries() { public void testChangeTaskMakesOutstandingEntries() {

Loading…
Cancel
Save