use of android.content.ContentValues in project k-9 by k9mail.
the class MigrationTo55 method createFtsSearchTable.
static void createFtsSearchTable(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
db.execSQL("CREATE VIRTUAL TABLE messages_fulltext USING fts4 (fulltext)");
LocalStore localStore = migrationsHelper.getLocalStore();
MessageFulltextCreator fulltextCreator = localStore.getMessageFulltextCreator();
try {
List<LocalFolder> folders = localStore.getPersonalNamespaces(true);
ContentValues cv = new ContentValues();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.BODY);
for (LocalFolder folder : folders) {
List<String> messageUids = folder.getAllMessageUids();
for (String messageUid : messageUids) {
LocalMessage localMessage = folder.getMessage(messageUid);
folder.fetch(Collections.singletonList(localMessage), fp, null);
String fulltext = fulltextCreator.createFulltext(localMessage);
if (!TextUtils.isEmpty(fulltext)) {
Timber.d("fulltext for msg id %d is %d chars long", localMessage.getId(), fulltext.length());
cv.clear();
cv.put("docid", localMessage.getId());
cv.put("fulltext", fulltext);
db.insert("messages_fulltext", null, cv);
} else {
Timber.d("no fulltext for msg id %d :(", localMessage.getId());
}
}
}
} catch (MessagingException e) {
Timber.e(e, "error indexing fulltext - skipping rest, fts index is incomplete!");
}
}
use of android.content.ContentValues in project k-9 by k9mail.
the class MigrationTo43 method fixOutboxFolders.
public static void fixOutboxFolders(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
try {
LocalStore localStore = migrationsHelper.getLocalStore();
Account account = migrationsHelper.getAccount();
Context context = migrationsHelper.getContext();
// If folder "OUTBOX" (old, v3.800 - v3.802) exists, rename it to
// "K9MAIL_INTERNAL_OUTBOX" (new)
LocalFolder oldOutbox = new LocalFolder(localStore, "OUTBOX");
if (oldOutbox.exists()) {
ContentValues cv = new ContentValues();
cv.put("name", Account.OUTBOX);
db.update("folders", cv, "name = ?", new String[] { "OUTBOX" });
Timber.i("Renamed folder OUTBOX to %s", OUTBOX);
}
// Check if old (pre v3.800) localized outbox folder exists
String localizedOutbox = context.getString(R.string.special_mailbox_name_outbox);
LocalFolder obsoleteOutbox = new LocalFolder(localStore, localizedOutbox);
if (obsoleteOutbox.exists()) {
// Get all messages from the localized outbox ...
List<? extends Message> messages = obsoleteOutbox.getMessages(null, false);
if (messages.size() > 0) {
// ... and move them to the drafts folder (we don't want to
// surprise the user by sending potentially very old messages)
LocalFolder drafts = new LocalFolder(localStore, account.getDraftsFolderName());
obsoleteOutbox.moveMessages(messages, drafts);
}
// Now get rid of the localized outbox
obsoleteOutbox.delete();
obsoleteOutbox.delete(true);
}
} catch (Exception e) {
Timber.e(e, "Error trying to fix the outbox folders");
}
}
use of android.content.ContentValues in project k-9 by k9mail.
the class MigrationTo46 method addMessagesFlagColumns.
public static void addMessagesFlagColumns(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
db.execSQL("ALTER TABLE messages ADD read INTEGER default 0");
db.execSQL("ALTER TABLE messages ADD flagged INTEGER default 0");
db.execSQL("ALTER TABLE messages ADD answered INTEGER default 0");
db.execSQL("ALTER TABLE messages ADD forwarded INTEGER default 0");
String[] projection = { "id", "flags" };
ContentValues cv = new ContentValues();
List<Flag> extraFlags = new ArrayList<>();
Cursor cursor = db.query("messages", projection, null, null, null, null, null);
try {
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String flagList = cursor.getString(1);
boolean read = false;
boolean flagged = false;
boolean answered = false;
boolean forwarded = false;
if (flagList != null && flagList.length() > 0) {
String[] flags = flagList.split(",");
for (String flagStr : flags) {
try {
Flag flag = Flag.valueOf(flagStr);
switch(flag) {
case ANSWERED:
{
answered = true;
break;
}
case DELETED:
{
// Don't store this in column 'flags'
break;
}
case FLAGGED:
{
flagged = true;
break;
}
case FORWARDED:
{
forwarded = true;
break;
}
case SEEN:
{
read = true;
break;
}
case DRAFT:
case RECENT:
case X_DESTROYED:
case X_DOWNLOADED_FULL:
case X_DOWNLOADED_PARTIAL:
case X_REMOTE_COPY_STARTED:
case X_SEND_FAILED:
case X_SEND_IN_PROGRESS:
{
extraFlags.add(flag);
break;
}
}
} catch (Exception e) {
// Ignore bad flags
}
}
}
cv.put("flags", migrationsHelper.serializeFlags(extraFlags));
cv.put("read", read);
cv.put("flagged", flagged);
cv.put("answered", answered);
cv.put("forwarded", forwarded);
db.update("messages", cv, "id = ?", new String[] { Long.toString(id) });
cv.clear();
extraFlags.clear();
}
} finally {
cursor.close();
}
db.execSQL("CREATE INDEX IF NOT EXISTS msg_read ON messages (read)");
db.execSQL("CREATE INDEX IF NOT EXISTS msg_flagged ON messages (flagged)");
}
use of android.content.ContentValues in project k-9 by k9mail.
the class MigrationTo47 method createThreadsTable.
public static void createThreadsTable(SQLiteDatabase db) {
// Create new 'threads' table
db.execSQL("DROP TABLE IF EXISTS threads");
db.execSQL("CREATE TABLE threads (" + "id INTEGER PRIMARY KEY, " + "message_id INTEGER, " + "root INTEGER, " + "parent INTEGER" + ")");
// Create indices for new table
db.execSQL("DROP INDEX IF EXISTS threads_message_id");
db.execSQL("CREATE INDEX IF NOT EXISTS threads_message_id ON threads (message_id)");
db.execSQL("DROP INDEX IF EXISTS threads_root");
db.execSQL("CREATE INDEX IF NOT EXISTS threads_root ON threads (root)");
db.execSQL("DROP INDEX IF EXISTS threads_parent");
db.execSQL("CREATE INDEX IF NOT EXISTS threads_parent ON threads (parent)");
// Create entries for all messages in 'threads' table
db.execSQL("INSERT INTO threads (message_id) SELECT id FROM messages");
// Copy thread structure from 'messages' table to 'threads'
Cursor cursor = db.query("messages", new String[] { "id", "thread_root", "thread_parent" }, null, null, null, null, null);
try {
ContentValues cv = new ContentValues();
while (cursor.moveToNext()) {
cv.clear();
long messageId = cursor.getLong(0);
if (!cursor.isNull(1)) {
long threadRootMessageId = cursor.getLong(1);
db.execSQL("UPDATE threads SET root = (SELECT t.id FROM " + "threads t WHERE t.message_id = ?) " + "WHERE message_id = ?", new String[] { Long.toString(threadRootMessageId), Long.toString(messageId) });
}
if (!cursor.isNull(2)) {
long threadParentMessageId = cursor.getLong(2);
db.execSQL("UPDATE threads SET parent = (SELECT t.id FROM " + "threads t WHERE t.message_id = ?) " + "WHERE message_id = ?", new String[] { Long.toString(threadParentMessageId), Long.toString(messageId) });
}
}
} finally {
cursor.close();
}
// Remove indices for old thread-related columns in 'messages' table
db.execSQL("DROP INDEX IF EXISTS msg_thread_root");
db.execSQL("DROP INDEX IF EXISTS msg_thread_parent");
// Clear out old thread-related columns in 'messages'
ContentValues cv = new ContentValues();
cv.putNull("thread_root");
cv.putNull("thread_parent");
db.update("messages", cv, null, null);
}
use of android.content.ContentValues in project k-9 by k9mail.
the class MigrationTo51 method insertBodyAsMultipartAlternative.
private static MimeStructureState insertBodyAsMultipartAlternative(SQLiteDatabase db, MimeStructureState structureState, MimeHeader mimeHeader, String textContent, String htmlContent) throws IOException {
if (mimeHeader == null) {
mimeHeader = new MimeHeader();
}
String boundary = MimeUtility.getHeaderParameter(mimeHeader.getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE), "boundary");
if (TextUtils.isEmpty(boundary)) {
boundary = MimeUtil.createUniqueBoundary();
}
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("multipart/alternative; boundary=\"%s\";", boundary));
int dataLocation = textContent != null || htmlContent != null ? DATA_LOCATION__IN_DATABASE : DATA_LOCATION__MISSING;
ContentValues cv = new ContentValues();
cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
cv.put("data_location", dataLocation);
cv.put("mime_type", "multipart/alternative");
cv.put("header", mimeHeader.toString());
cv.put("boundary", boundary);
structureState.applyValues(cv);
long multipartAlternativePartId = db.insertOrThrow("message_parts", null, cv);
structureState = structureState.nextMultipartChild(multipartAlternativePartId);
if (textContent != null) {
structureState = insertTextualPartIntoDatabase(db, structureState, null, textContent, false);
}
if (htmlContent != null) {
structureState = insertTextualPartIntoDatabase(db, structureState, null, htmlContent, true);
}
return structureState;
}
Aggregations