Search in sources :

Example 61 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project storymaker by StoryMaker.

the class ProjectsProvider method query.

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    mCacheWordHandler.connectToService();
    setTimer(60000);
    SQLiteDatabase db = getDB();
    if (db != null) {
        int uriType = sURIMatcher.match(uri);
        switch(uriType) {
            case PROJECT_ID:
                return (new ProjectTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case PROJECTS:
                return (new ProjectTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case SCENE_ID:
                return (new SceneTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case SCENES:
                return (new SceneTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case LESSON_ID:
                return (new LessonTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case LESSONS:
                return (new LessonTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case MEDIA_ID:
                return (new MediaTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case MEDIA:
                return (new MediaTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case AUTH_ID:
                return (new AuthTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case AUTH:
                return (new AuthTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case TAG_ID:
                return (new TagTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case TAGS:
                return (new TagTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case DISTINCT_TAG_ID:
                return (new TagTable(db)).queryOneDistinct(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case DISTINCT_TAGS:
                return (new TagTable(db)).queryAllDistinct(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case JOB_ID:
                return (new JobTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case JOBS:
                return (new JobTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case PUBLISH_JOB_ID:
                return (new PublishJobTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case PUBLISH_JOBS:
                return (new PublishJobTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case AUDIO_CLIP_ID:
                return (new AudioClipTable(db)).queryOne(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            case AUDIO_CLIPS:
                return (new AudioClipTable(db)).queryAll(getContext(), uri, projection, selection, selectionArgs, sortOrder);
            default:
                throw new IllegalArgumentException("Unknown URI");
        }
    }
    return null;
}
Also used : SceneTable(org.storymaker.app.model.SceneTable) PublishJobTable(org.storymaker.app.model.PublishJobTable) JobTable(org.storymaker.app.model.JobTable) PublishJobTable(org.storymaker.app.model.PublishJobTable) MediaTable(org.storymaker.app.model.MediaTable) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) AudioClipTable(org.storymaker.app.model.AudioClipTable) AuthTable(org.storymaker.app.model.AuthTable) TagTable(org.storymaker.app.model.TagTable) ProjectTable(org.storymaker.app.model.ProjectTable) LessonTable(org.storymaker.app.model.LessonTable) Nullable(android.support.annotation.Nullable)

Example 62 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Zom-Android by zom.

the class ImpsProvider method update.

@Override
public final int update(final Uri url, final ContentValues values, final String selection, final String[] selectionArgs) {
    DatabaseHelper dbHelper = getDBHelper();
    int result = 0;
    if (dbHelper != null) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db.isOpen()) {
            try {
                db.beginTransaction();
                result = updateInternal(url, values, selection, selectionArgs);
                db.setTransactionSuccessful();
                db.endTransaction();
            } catch (Exception e) {
                if (db.isOpen() && db.inTransaction())
                    db.endTransaction();
            }
            if (result > 0) {
                getContext().getContentResolver().notifyChange(url, null, /* observer */
                false);
            }
        }
    }
    return result;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(net.sqlcipher.database.SQLiteConstraintException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 63 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Zom-Android by zom.

the class ImpsProvider method insertInternal.

private Uri insertInternal(Uri url, ContentValues initialValues) {
    Uri resultUri = null;
    long rowID = 0;
    long account = 0;
    String contact = null;
    long threadId = 0;
    boolean notifyContactListContentUri = false;
    boolean notifyContactContentUri = false;
    boolean notifyMessagesContentUri = false;
    boolean notifyMessagesByContactContentUri = false;
    boolean notifyMessagesByThreadIdContentUri = false;
    boolean notifyProviderAccountContentUri = false;
    final SQLiteDatabase db = getDBHelper().getWritableDatabase();
    int match = mUrlMatcher.match(url);
    log("insert to " + url + ", match " + match);
    switch(match) {
        case MATCH_PROVIDERS:
            // Insert into the providers table
            rowID = db.insert(TABLE_PROVIDERS, "name", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Provider.CONTENT_URI + "/" + rowID);
            }
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_ACCOUNTS:
            // Insert into the accounts table
            rowID = db.insert(TABLE_ACCOUNTS, "name", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Account.CONTENT_URI + "/" + rowID);
            }
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_CONTACTS_BY_PROVIDER:
            appendValuesFromUrl(initialValues, url, Imps.Contacts.PROVIDER, Imps.Contacts.ACCOUNT);
        // fall through
        case MATCH_CONTACTS:
        case MATCH_CONTACTS_BAREBONE:
            // Insert into the contacts table
            rowID = db.insert(TABLE_CONTACTS, USERNAME, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Contacts.CONTENT_URI + "/" + rowID);
            }
            notifyContactContentUri = true;
            break;
        case MATCH_CONTACTS_BULK:
            if (insertBulkContacts(initialValues)) {
                // notify change using the "content://im/contacts" url,
                // so the change will be observed by listeners interested
                // in contacts changes.
                resultUri = Imps.Contacts.CONTENT_URI;
            }
            notifyContactContentUri = true;
            break;
        case MATCH_CONTACTLISTS_BY_PROVIDER:
            appendValuesFromUrl(initialValues, url, Imps.ContactList.PROVIDER, Imps.ContactList.ACCOUNT);
        // fall through
        case MATCH_CONTACTLISTS:
            // Insert into the contactList table
            rowID = db.insert(TABLE_CONTACT_LIST, "name", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.ContactList.CONTENT_URI + "/" + rowID);
            }
            notifyContactListContentUri = true;
            break;
        case MATCH_BLOCKEDLIST_BY_PROVIDER:
            appendValuesFromUrl(initialValues, url, Imps.BlockedList.PROVIDER, Imps.BlockedList.ACCOUNT);
        // fall through
        case MATCH_BLOCKEDLIST:
            // Insert into the blockedList table
            rowID = db.insert(TABLE_BLOCKED_LIST, "username", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.BlockedList.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_CONTACTS_ETAGS:
            rowID = db.replace(TABLE_CONTACTS_ETAG, Imps.ContactsEtag.ETAG, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.ContactsEtag.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_MESSAGES_BY_CONTACT:
            String accountStr = decodeURLSegment(url.getPathSegments().get(1));
            try {
                account = Long.parseLong(accountStr);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            contact = decodeURLSegment(url.getPathSegments().get(2));
            initialValues.put(Imps.Messages.THREAD_ID, getContactId(db, accountStr, contact));
            notifyMessagesContentUri = true;
            // Insert into the messages table.
            rowID = db.insert(TABLE_MESSAGES, "thread_id", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Messages.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_MESSAGES_BY_THREAD_ID:
            appendValuesFromUrl(initialValues, url, Imps.Messages.THREAD_ID);
        case MATCH_MESSAGES:
            // Insert into the messages table.
            notifyMessagesContentUri = true;
            rowID = db.insert(TABLE_MESSAGES, "thread_id", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Messages.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_OTR_MESSAGES_BY_CONTACT:
            String accountStr2 = decodeURLSegment(url.getPathSegments().get(1));
            try {
                account = Long.parseLong(accountStr2);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            contact = decodeURLSegment(url.getPathSegments().get(2));
            initialValues.put(Imps.Messages.THREAD_ID, getContactId(db, accountStr2, contact));
            notifyMessagesByContactContentUri = true;
            // Insert into the in-memory messages table.
            rowID = db.insert(TABLE_IN_MEMORY_MESSAGES, "thread_id", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Messages.OTR_MESSAGES_CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_OTR_MESSAGES_BY_THREAD_ID:
            try {
                threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1)));
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            initialValues.put(Imps.Messages.THREAD_ID, threadId);
            notifyMessagesByThreadIdContentUri = true;
        case MATCH_OTR_MESSAGES:
            // Insert into the messages table.
            rowID = db.insert(TABLE_IN_MEMORY_MESSAGES, "thread_id", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Messages.OTR_MESSAGES_CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_INVITATIONS:
            rowID = db.insert(TABLE_INVITATIONS, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Invitation.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_GROUP_MEMBERS:
            rowID = db.insert(TABLE_GROUP_MEMBERS, "nickname", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.GroupMembers.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_GROUP_MEMBERS_BY_GROUP:
            appendValuesFromUrl(initialValues, url, Imps.GroupMembers.GROUP);
            rowID = db.insert(TABLE_GROUP_MEMBERS, "nickname", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.GroupMembers.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_AVATAR_BY_PROVIDER:
            appendValuesFromUrl(initialValues, url, Imps.Avatars.PROVIDER, Imps.Avatars.ACCOUNT);
        // fall through
        case MATCH_AVATARS:
            // Insert into the avatars table
            rowID = db.replace(TABLE_AVATARS, "contact", initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Avatars.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_CHATS_ID:
            appendValuesFromUrl(initialValues, url, Imps.Chats.CONTACT_ID);
        // fall through
        case MATCH_CHATS:
            // Insert into the chats table
            initialValues.put(Imps.Chats.SHORTCUT, -1);
            rowID = db.replace(TABLE_CHATS, Imps.Chats.CONTACT_ID, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Chats.CONTENT_URI + "/" + rowID);
                addToQuickSwitch(rowID);
            }
            notifyContactContentUri = true;
            // For updating account stats in account list
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_PRESENCE:
            rowID = db.replace(TABLE_PRESENCE, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.Presence.CONTENT_URI + "/" + rowID);
            }
            notifyContactContentUri = true;
            break;
        case MATCH_PRESENCE_SEED_BY_ACCOUNT:
            try {
                seedInitialPresenceByAccount(Long.parseLong(url.getLastPathSegment()));
                resultUri = Imps.Presence.CONTENT_URI;
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            break;
        case MATCH_SESSIONS_BY_PROVIDER:
            appendValuesFromUrl(initialValues, url, Imps.SessionCookies.PROVIDER, Imps.SessionCookies.ACCOUNT);
        // fall through
        case MATCH_SESSIONS:
            rowID = db.insert(TABLE_SESSION_COOKIES, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.SessionCookies.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_PROVIDER_SETTINGS:
            rowID = db.replace(TABLE_PROVIDER_SETTINGS, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.ProviderSettings.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_ACCOUNTS_STATUS:
            rowID = db.replace(TABLE_ACCOUNT_STATUS, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.AccountStatus.CONTENT_URI + "/" + rowID);
            }
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_BRANDING_RESOURCE_MAP_CACHE:
            rowID = db.insert(TABLE_BRANDING_RESOURCE_MAP_CACHE, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.BrandingResourceMapCache.CONTENT_URI + "/" + rowID);
            }
            break;
        // mcs/rmq stuff
        case MATCH_OUTGOING_RMQ_MESSAGES:
            rowID = db.insert(TABLE_OUTGOING_RMQ_MESSAGES, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.OutgoingRmq.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_LAST_RMQ_ID:
            rowID = db.replace(TABLE_LAST_RMQ_ID, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.LastRmqId.CONTENT_URI + "/" + rowID);
            }
            break;
        case MATCH_S2D_RMQ_IDS:
            rowID = db.insert(TABLE_S2D_RMQ_IDS, null, initialValues);
            if (rowID > 0) {
                resultUri = Uri.parse(Imps.ServerToDeviceRmqIds.CONTENT_URI + "/" + rowID);
            }
            break;
        default:
            throw new UnsupportedOperationException("Cannot insert into URL: " + url);
    }
    if (resultUri != null) {
        ContentResolver resolver = getContext().getContentResolver();
        // we should also notify that contacts changes when presence or chats changed.
        if (notifyContactContentUri) {
            resolver.notifyChange(Imps.Contacts.CONTENT_URI, null);
        }
        if (notifyContactListContentUri) {
            resolver.notifyChange(Imps.ContactList.CONTENT_URI, null);
        }
        if (notifyMessagesContentUri) {
            resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
        }
        if (notifyMessagesByContactContentUri) {
            resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
            resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null);
        }
        if (notifyMessagesByThreadIdContentUri) {
            resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
            resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null);
        }
        if (notifyProviderAccountContentUri) {
            log("notify insert for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT);
            resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null);
        }
    }
    return resultUri;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 64 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Zom-Android by zom.

the class ImpsProvider method insertBulkContacts.

// package scope for testing.
boolean insertBulkContacts(ContentValues values) {
    // log("insertBulkContacts: begin");
    ArrayList<String> usernames = getStringArrayList(values, Imps.Contacts.USERNAME);
    ArrayList<String> nicknames = getStringArrayList(values, Imps.Contacts.NICKNAME);
    int usernameCount = usernames.size();
    int nicknameCount = nicknames.size();
    if (usernameCount != nicknameCount) {
        LogCleaner.warn(LOG_TAG, "[ImProvider] insertBulkContacts: input bundle " + "username & nickname lists have diff. length!");
        return false;
    }
    ArrayList<String> contactTypeArray = getStringArrayList(values, Imps.Contacts.TYPE);
    ArrayList<String> subscriptionStatusArray = getStringArrayList(values, Imps.Contacts.SUBSCRIPTION_STATUS);
    ArrayList<String> subscriptionTypeArray = getStringArrayList(values, Imps.Contacts.SUBSCRIPTION_TYPE);
    ArrayList<String> quickContactArray = getStringArrayList(values, Imps.Contacts.QUICK_CONTACT);
    ArrayList<String> rejectedArray = getStringArrayList(values, Imps.Contacts.REJECTED);
    int sum = 0;
    final SQLiteDatabase db = getDBHelper().getWritableDatabase();
    db.beginTransaction();
    try {
        long provider = values.getAsLong(Imps.Contacts.PROVIDER);
        long account = values.getAsLong(Imps.Contacts.ACCOUNT);
        long listId = values.getAsLong(Imps.Contacts.CONTACTLIST);
        ContentValues contactValues = new ContentValues();
        contactValues.put(Imps.Contacts.PROVIDER, provider);
        contactValues.put(Imps.Contacts.ACCOUNT, account);
        contactValues.put(Imps.Contacts.CONTACTLIST, listId);
        ContentValues presenceValues = new ContentValues();
        presenceValues.put(Imps.Presence.PRESENCE_STATUS, Imps.Presence.OFFLINE);
        StringBuffer whereClause = new StringBuffer();
        whereClause.append(Imps.Contacts.USERNAME);
        whereClause.append(" = ?");
        whereClause.append(" AND ");
        whereClause.append(Imps.Contacts.ACCOUNT);
        whereClause.append(" = ?");
        whereClause.append(" AND ");
        whereClause.append(Imps.Contacts.PROVIDER);
        whereClause.append(" = ?");
        for (int i = 0; i < usernameCount; i++) {
            String username = usernames.get(i);
            String nickname = nicknames.get(i);
            int type = 0;
            int subscriptionStatus = 0;
            int subscriptionType = 0;
            int quickContact = 0;
            int rejected = 0;
            try {
                type = Integer.parseInt(contactTypeArray.get(i));
                if (subscriptionStatusArray != null) {
                    subscriptionStatus = Integer.parseInt(subscriptionStatusArray.get(i));
                }
                if (subscriptionTypeArray != null) {
                    subscriptionType = Integer.parseInt(subscriptionTypeArray.get(i));
                }
                if (quickContactArray != null) {
                    quickContact = Integer.parseInt(quickContactArray.get(i));
                }
                if (rejectedArray != null) {
                    rejected = Integer.parseInt(rejectedArray.get(i));
                }
            } catch (NumberFormatException ex) {
                LogCleaner.error(LOG_TAG, "insertBulkContacts: caught ", ex);
            }
            /*
                 log("insertBulkContacts[" + i + "] username=" +
                        username + ", nickname=" + nickname + ", type=" + type +
                        ", subscriptionStatus=" + subscriptionStatus + ", subscriptionType=" +
                        subscriptionType + ", qc=" + quickContact);
                */
            contactValues.put(Imps.Contacts.USERNAME, username);
            contactValues.put(Imps.Contacts.NICKNAME, nickname);
            contactValues.put(Imps.Contacts.TYPE, type);
            if (subscriptionStatusArray != null) {
                contactValues.put(Imps.Contacts.SUBSCRIPTION_STATUS, subscriptionStatus);
            }
            if (subscriptionTypeArray != null) {
                contactValues.put(Imps.Contacts.SUBSCRIPTION_TYPE, subscriptionType);
            }
            if (quickContactArray != null) {
                contactValues.put(Imps.Contacts.QUICK_CONTACT, quickContact);
            }
            if (rejectedArray != null) {
                contactValues.put(Imps.Contacts.REJECTED, rejected);
            }
            long rowId;
            /* save this code for when we add constraint (account, username) to the contacts
                   table
                try {
                    rowId = db.insertOrThrow(TABLE_CONTACTS, USERNAME, contactValues);
                } catch (android.database.sqlite.SQLiteConstraintException ex) {
                     log("insertBulkContacts: insert " + username + " caught " + ex);

                    // append username to the selection clause
                    updateSelection.delete(0, updateSelection.length());
                    updateSelection.append(Im.Contacts.USERNAME);
                    updateSelection.append("=?");
                    updateSelectionArgs[0] = username;

                    int updated = db.update(TABLE_CONTACTS, contactValues,
                            updateSelection.toString(), updateSelectionArgs);

                    if (DBG && updated != 1) {
                        log("insertBulkContacts: update " + username + " failed!");
                    }
                }
                */
            String[] queryColumns = { "_id", Contacts.USERNAME, Contacts.ACCOUNT, Contacts.PROVIDER };
            String[] whereArgs = { username, Long.toString(account), Long.toString(provider) };
            Cursor c = db.query(TABLE_CONTACTS, queryColumns, whereClause.toString(), whereArgs, null, null, null, null);
            boolean contactExists = (c != null && c.getCount() > 0);
            if (contactExists) {
                int rowsUpdated = db.update(TABLE_CONTACTS, contactValues, whereClause.toString(), whereArgs);
                // seed the presence for the new contact
                while (c.moveToNext()) {
                    for (int n = 0; n < c.getColumnCount(); n++) LogCleaner.debug(LOG_TAG, c.getColumnName(n) + "=" + c.getString(n));
                    rowId = c.getLong(0);
                    presenceValues.put(Imps.Presence.CONTACT_ID, rowId);
                    String[] presenceWhereArgs = { rowId + "" };
                    try {
                        db.update(TABLE_PRESENCE, presenceValues, Imps.Presence.CONTACT_ID + " = ?", presenceWhereArgs);
                    } catch (Exception ex) {
                        LogCleaner.warn(LOG_TAG, "insertBulkContacts: seeding presence caught " + ex);
                    }
                }
            } else {
                rowId = db.insert(TABLE_CONTACTS, USERNAME, contactValues);
                if (rowId > 0) {
                    sum++;
                    // seed the presence for the new contact
                    log("### seedPresence for contact id " + rowId);
                    presenceValues.put(Imps.Presence.CONTACT_ID, rowId);
                    try {
                        db.insert(TABLE_PRESENCE, null, presenceValues);
                    } catch (Exception ex) {
                        LogCleaner.warn(LOG_TAG, "insertBulkContacts: seeding presence caught " + ex);
                    }
                }
            }
            if (c != null)
                c.close();
            // yield the lock if anyone else is trying to
            // perform a db operation here.
            db.yieldIfContended();
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    // We know that we succeeded becuase endTransaction throws if the transaction failed.
    log("insertBulkContacts: added " + sum + " contacts!");
    return true;
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(net.sqlcipher.database.SQLiteConstraintException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 65 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Zom-Android by zom.

the class ImpsProvider method updateInternal.

private int updateInternal(Uri url, ContentValues values, String userWhere, String[] whereArgs) {
    String tableToChange;
    String idColumnName = null;
    String changedItemId = null;
    String accountStr = null;
    long account = 0;
    String contact = null;
    String packetId = null;
    long threadId = 0;
    int count = 0;
    StringBuilder whereClause = new StringBuilder();
    if (userWhere != null) {
        whereClause.append(userWhere);
    }
    boolean notifyMessagesContentUri = false;
    boolean notifyMessagesByContactContentUri = false;
    boolean notifyMessagesByThreadIdContentUri = false;
    boolean notifyContactListContentUri = false;
    boolean notifyProviderAccountContentUri = false;
    int match = mUrlMatcher.match(url);
    final SQLiteDatabase db = getDBHelper().getWritableDatabase();
    switch(match) {
        case MATCH_PROVIDERS_BY_ID:
            changedItemId = url.getPathSegments().get(1);
        // fall through
        case MATCH_PROVIDERS:
            tableToChange = TABLE_PROVIDERS;
            break;
        case MATCH_ACCOUNTS_BY_ID:
            changedItemId = url.getPathSegments().get(1);
        // fall through
        case MATCH_ACCOUNTS:
            tableToChange = TABLE_ACCOUNTS;
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_ACCOUNT_STATUS:
            changedItemId = url.getPathSegments().get(1);
        // fall through
        case MATCH_ACCOUNTS_STATUS:
            tableToChange = TABLE_ACCOUNT_STATUS;
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_CONTACTS:
        case MATCH_CONTACTS_BAREBONE:
            tableToChange = TABLE_CONTACTS;
            break;
        case MATCH_CONTACTS_BY_PROVIDER:
            tableToChange = TABLE_CONTACTS;
            changedItemId = url.getPathSegments().get(2);
            idColumnName = Imps.Contacts.ACCOUNT;
            break;
        case MATCH_CONTACT:
            tableToChange = TABLE_CONTACTS;
            changedItemId = url.getPathSegments().get(1);
            break;
        case MATCH_CONTACTS_BULK:
            count = updateBulkContacts(values, userWhere);
            // in contacts changes.
            if (count > 0) {
                getContext().getContentResolver().notifyChange(Imps.Contacts.CONTENT_URI, null);
            }
            return count;
        case MATCH_CONTACTLIST:
            tableToChange = TABLE_CONTACT_LIST;
            changedItemId = url.getPathSegments().get(1);
            notifyContactListContentUri = true;
            break;
        case MATCH_CONTACTS_ETAGS:
            tableToChange = TABLE_CONTACTS_ETAG;
            break;
        case MATCH_CONTACTS_ETAG:
            tableToChange = TABLE_CONTACTS_ETAG;
            changedItemId = url.getPathSegments().get(1);
            break;
        case MATCH_MESSAGES:
            tableToChange = TABLE_MESSAGES;
            break;
        case MATCH_MESSAGES_BY_CONTACT:
            tableToChange = TABLE_MESSAGES;
            accountStr = decodeURLSegment(url.getPathSegments().get(1));
            try {
                account = Long.parseLong(accountStr);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            contact = decodeURLSegment(url.getPathSegments().get(2));
            appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", getContactId(db, accountStr, contact));
            notifyMessagesContentUri = true;
            break;
        case MATCH_MESSAGES_BY_THREAD_ID:
            tableToChange = TABLE_MESSAGES;
            try {
                threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1)));
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId);
            notifyMessagesContentUri = true;
            break;
        case MATCH_MESSAGE:
            tableToChange = TABLE_MESSAGES;
            changedItemId = url.getPathSegments().get(1);
            notifyMessagesContentUri = true;
            break;
        case MATCH_OTR_MESSAGES:
            tableToChange = TABLE_IN_MEMORY_MESSAGES;
            break;
        case MATCH_OTR_MESSAGES_BY_CONTACT:
            tableToChange = TABLE_IN_MEMORY_MESSAGES;
            accountStr = decodeURLSegment(url.getPathSegments().get(1));
            try {
                account = Long.parseLong(accountStr);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            contact = decodeURLSegment(url.getPathSegments().get(2));
            appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", getContactId(db, accountStr, contact));
            notifyMessagesByContactContentUri = true;
            break;
        case MATCH_OTR_MESSAGES_BY_THREAD_ID:
            tableToChange = TABLE_IN_MEMORY_MESSAGES;
            try {
                threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1)));
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException();
            }
            appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId);
            notifyMessagesByThreadIdContentUri = true;
            break;
        case MATCH_MESSAGES_BY_PACKET_ID:
            packetId = decodeURLSegment(url.getPathSegments().get(1));
            // FIXME these should be going to memory but they do not
            tableToChange = TABLE_MESSAGES;
            appendWhere(whereClause, Imps.Messages.PACKET_ID, "=", packetId);
            notifyMessagesContentUri = true;
            notifyMessagesByThreadIdContentUri = true;
            threadId = values.getAsLong(Imps.Messages.THREAD_ID);
            // count += db.update(TABLE_IN_MEMORY_MESSAGES, values, whereClause.toString(), whereArgs);
            break;
        case MATCH_OTR_MESSAGES_BY_PACKET_ID:
            packetId = decodeURLSegment(url.getPathSegments().get(1));
            // FIXME these should be going to memory but they do not
            tableToChange = TABLE_IN_MEMORY_MESSAGES;
            appendWhere(whereClause, Imps.Messages.PACKET_ID, "=", packetId);
            notifyMessagesContentUri = true;
            notifyMessagesByThreadIdContentUri = true;
            threadId = values.getAsLong(Imps.Messages.THREAD_ID);
            // count += db.update(TABLE_IN_MEMORY_MESSAGES, values, whereClause.toString(), whereArgs);
            break;
        case MATCH_OTR_MESSAGE:
            tableToChange = TABLE_IN_MEMORY_MESSAGES;
            changedItemId = url.getPathSegments().get(1);
            notifyMessagesContentUri = true;
            break;
        case MATCH_AVATARS:
            tableToChange = TABLE_AVATARS;
            break;
        case MATCH_AVATAR:
            tableToChange = TABLE_AVATARS;
            changedItemId = url.getPathSegments().get(1);
            break;
        case MATCH_AVATAR_BY_PROVIDER:
            tableToChange = TABLE_AVATARS;
            changedItemId = url.getPathSegments().get(2);
            idColumnName = Imps.Avatars.ACCOUNT;
            break;
        case MATCH_CHATS:
            tableToChange = TABLE_CHATS;
            // For updating account stats in account list
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_CHATS_ID:
            tableToChange = TABLE_CHATS;
            changedItemId = url.getPathSegments().get(1);
            idColumnName = Imps.Chats.CONTACT_ID;
            // For updating account stats in account list
            notifyProviderAccountContentUri = true;
            break;
        case MATCH_PRESENCE:
            log("update presence: where='" + userWhere + "'");
            tableToChange = TABLE_PRESENCE;
            break;
        case MATCH_PRESENCE_ID:
            tableToChange = TABLE_PRESENCE;
            changedItemId = url.getPathSegments().get(1);
            idColumnName = Imps.Presence.CONTACT_ID;
            break;
        case MATCH_PRESENCE_BULK:
            tableToChange = null;
            count = updateBulkPresence(values, db, userWhere, whereArgs);
            if (count > 0) {
                getContext().getContentResolver().notifyChange(Imps.Contacts.CONTENT_URI_CHAT_CONTACTS_BY, null);
                getContext().getContentResolver().notifyChange(Imps.Contacts.CONTENT_URI, null);
                notifyContactListContentUri = true;
            }
            break;
        case MATCH_INVITATION:
            tableToChange = TABLE_INVITATIONS;
            changedItemId = url.getPathSegments().get(1);
            break;
        case MATCH_GROUP_MEMBERS_BY_GROUP:
            tableToChange = TABLE_GROUP_MEMBERS;
            String groupId = url.getPathSegments().get(1);
            appendWhere(whereClause, "groupId", "=", groupId);
            break;
        case MATCH_SESSIONS:
            tableToChange = TABLE_SESSION_COOKIES;
            break;
        case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME:
            tableToChange = TABLE_PROVIDER_SETTINGS;
            String providerId = url.getPathSegments().get(1);
            String name = url.getPathSegments().get(2);
            if (values.containsKey(Imps.ProviderSettings.PROVIDER) || values.containsKey(Imps.ProviderSettings.NAME)) {
                throw new SecurityException("Cannot override the value for provider|name");
            }
            appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId);
            appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name);
            break;
        case MATCH_OUTGOING_RMQ_MESSAGES:
            tableToChange = TABLE_OUTGOING_RMQ_MESSAGES;
            break;
        case MATCH_LAST_RMQ_ID:
            tableToChange = TABLE_LAST_RMQ_ID;
            break;
        case MATCH_S2D_RMQ_IDS:
            tableToChange = TABLE_S2D_RMQ_IDS;
            break;
        // ChatSecure-Push
        case MATCH_CSP_ACCOUNTS:
            tableToChange = TABLE_CSP_ACCOUNTS;
            break;
        case MATCH_CSP_DEVICES:
            tableToChange = TABLE_CSP_DEVICES;
            break;
        case MATCH_CSP_TOKENS:
            tableToChange = TABLE_CSP_TOKENS;
            break;
        default:
            throw new UnsupportedOperationException("Cannot update URL: " + url);
    }
    if (idColumnName == null) {
        idColumnName = "_id";
    }
    if (changedItemId != null) {
        appendWhere(whereClause, idColumnName, "=", changedItemId);
    }
    log("update " + url + " WHERE " + whereClause);
    if (tableToChange != null)
        count += db.update(tableToChange, values, whereClause.toString(), whereArgs);
    if (count > 0) {
        ContentResolver resolver = getContext().getContentResolver();
        // we should also notify that contacts changes when presence or chats changed.
        if (match == MATCH_CHATS || match == MATCH_CHATS_ID || match == MATCH_PRESENCE || match == MATCH_PRESENCE_BULK || match == MATCH_PRESENCE_ID || match == MATCH_CONTACTS_BAREBONE) {
            resolver.notifyChange(Imps.Contacts.CONTENT_URI, null);
        }
        if (notifyMessagesContentUri) {
            // log("notify change for " + Imps.Messages.CONTENT_URI);
            resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
        }
        if (notifyMessagesByContactContentUri) {
            resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
            resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null);
        }
        if (notifyMessagesByThreadIdContentUri) {
            resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
            resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null);
        }
        if (notifyContactListContentUri) {
            resolver.notifyChange(Imps.ContactList.CONTENT_URI, null);
        }
        if (notifyProviderAccountContentUri) {
            log("notify change for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT);
            resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null);
        }
    }
    return count;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) ContentResolver(android.content.ContentResolver)

Aggregations

SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)124 Cursor (android.database.Cursor)37 ContentValues (android.content.ContentValues)32 IOException (java.io.IOException)15 LinkedList (java.util.LinkedList)12 NonNull (android.support.annotation.NonNull)7 MergeCursor (android.database.MergeCursor)6 Nullable (android.support.annotation.Nullable)6 Pair (android.util.Pair)6 File (java.io.File)5 SQLiteConstraintException (net.sqlcipher.database.SQLiteConstraintException)5 FileNotFoundException (java.io.FileNotFoundException)4 StreamCorruptedException (java.io.StreamCorruptedException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AudioClipTable (org.storymaker.app.model.AudioClipTable)4 AuthTable (org.storymaker.app.model.AuthTable)4 JobTable (org.storymaker.app.model.JobTable)4 LessonTable (org.storymaker.app.model.LessonTable)4 MediaTable (org.storymaker.app.model.MediaTable)4 ProjectTable (org.storymaker.app.model.ProjectTable)4