Set initial sizes for ContentValues and catch potential NullPointerException

This commit is contained in:
Stefan Niedermann 2020-06-08 09:12:55 +02:00 committed by Niedermann IT-Dienstleistungen
parent 212ac7f638
commit 6ef22884dc

View file

@ -109,7 +109,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
*/
long addNote(long accountId, CloudNote note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(11);
if (note instanceof DBNote) {
DBNote dbNote = (DBNote) note;
if (dbNote.getId() > 0) {
@ -450,7 +450,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
note.setFavorite(!note.isFavorite());
note.setStatus(DBStatus.LOCAL_EDITED);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(2);
values.put(key_status, note.getStatus().getTitle());
values.put(key_favorite, note.isFavorite() ? "1" : "0");
db.update(table_notes, values, key_id + " = ?", new String[]{String.valueOf(note.getId())});
@ -474,7 +474,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
note.setCategory(category);
note.setStatus(DBStatus.LOCAL_EDITED);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(2);
values.put(key_status, note.getStatus().getTitle());
int id = getCategoryIdByTitle(note.getAccountId(), note.getCategory());
values.put(key_category, id);
@ -489,7 +489,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
private long addCategory(long accountId, @NonNull String title) {
validateAccountId(accountId);
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(2);
values.put(key_category_account_id, accountId);
values.put(key_category_title, title);
return db.insert(table_category, null, values);
@ -513,13 +513,14 @@ public class NotesDatabase extends AbstractNotesDatabase {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(newContent, getContext()), newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(newContent), oldNote.getScrollY());
}
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(7);
values.put(key_status, newNote.getStatus().getTitle());
values.put(key_title, newNote.getTitle());
values.put(key_category, getCategoryIdByTitle(newNote.getAccountId(), newNote.getCategory()));
values.put(key_modified, newNote.getModified().getTimeInMillis() / 1000);
values.put(key_content, newNote.getContent());
values.put(key_excerpt, newNote.getExcerpt());
values.put(key_scroll_y, newNote.getScrollY());
int rows = db.update(table_notes, values, key_id + " = ? AND (" + key_content + " != ? OR " + key_category + " != ?)", new String[]{String.valueOf(newNote.getId()), newNote.getContent(), newNote.getCategory()});
removeEmptyCategory(accountId);
// if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
@ -538,6 +539,13 @@ public class NotesDatabase extends AbstractNotesDatabase {
}
}
public void updateScrollY(long noteId, int scrollY) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(1);
values.put(key_scroll_y, scrollY);
db.update(table_notes, values, key_id + " = ? ", new String[]{String.valueOf(scrollY)});
}
/**
* Updates a single Note with data from the server, (if it was not modified locally).
* Thereby, an optimistic concurrency control is realized in order to prevent conflicts arising due to parallel changes from the UI and synchronization.
@ -551,7 +559,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
SQLiteDatabase db = this.getWritableDatabase();
// First, update the remote ID, since this field cannot be changed in parallel, but have to be updated always.
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(8);
values.put(key_remote_id, remoteNote.getRemoteId());
db.update(table_notes, values, key_id + " = ?", new String[]{String.valueOf(id)});
@ -594,7 +602,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
*/
public void deleteNoteAndSync(SingleSignOnAccount ssoAccount, long id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(1);
values.put(key_status, DBStatus.LOCAL_DELETED.getTitle());
db.update(table_notes,
values,
@ -605,6 +613,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = getContext().getSystemService(ShortcutManager.class);
if(shortcutManager != null) {
shortcutManager.getPinnedShortcuts().forEach((shortcut) -> {
String shortcutId = id + "";
if (shortcut.getId().equals(shortcutId)) {
@ -612,6 +621,9 @@ public class NotesDatabase extends AbstractNotesDatabase {
shortcutManager.disableShortcuts(Collections.singletonList(shortcutId), getContext().getResources().getString(R.string.note_has_been_deleted));
}
});
} else {
Log.e(TAG, ShortcutManager.class.getSimpleName() + "is null.");
}
}
}
@ -700,7 +712,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
*/
public void addAccount(@NonNull String url, @NonNull String username, @NonNull String accountName, @NonNull Capabilities capabilities) throws SQLiteConstraintException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(4);
values.put(key_url, url);
values.put(key_username, username);
values.put(key_account_name, accountName);
@ -810,7 +822,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
}
final SQLiteDatabase db = this.getWritableDatabase();
final ContentValues values = new ContentValues();
final ContentValues values = new ContentValues(2);
values.put(key_color, color);
values.put(key_text_color, textColor);
@ -838,7 +850,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
}
if (apiVersions.length() > 0) {
final SQLiteDatabase db = this.getWritableDatabase();
final ContentValues values = new ContentValues();
final ContentValues values = new ContentValues(1);
values.put(key_api_version, apiVersion);
final int updatedRows = db.update(table_accounts, values, key_id + " = ?", new String[]{String.valueOf(accountId)});
if (updatedRows == 1) {
@ -890,7 +902,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
void updateETag(long accountId, String etag) {
validateAccountId(accountId);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(1);
values.put(key_etag, etag);
final int updatedRows = db.update(table_accounts, values, key_id + " = ?", new String[]{String.valueOf(accountId)});
if (updatedRows == 1) {
@ -903,7 +915,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
public void updateCapabilitiesETag(long accountId, String capabilitiesETag) {
validateAccountId(accountId);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(1);
values.put(key_capabilities_etag, capabilitiesETag);
final int updatedRows = db.update(table_accounts, values, key_id + " = ?", new String[]{String.valueOf(accountId)});
if (updatedRows == 1) {
@ -919,7 +931,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
throw new IllegalArgumentException("modified must be greater or equal 0");
}
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values = new ContentValues(1);
values.put(key_modified, modified);
final int updatedRows = db.update(table_accounts, values, key_id + " = ?", new String[]{String.valueOf(accountId)});
if (updatedRows == 1) {
@ -959,7 +971,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
public void createOrUpdateSingleNoteWidgetData(@NonNull SingleNoteWidgetData data) throws SQLException {
validateAccountId(data.getAccountId());
final SQLiteDatabase db = getWritableDatabase();
final ContentValues values = new ContentValues();
final ContentValues values = new ContentValues(4);
values.put(key_id, data.getAppWidgetId());
values.put(key_account_id, data.getAccountId());
values.put(key_note_id, data.getNoteId());
@ -993,7 +1005,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
public void createOrUpdateNoteListWidgetData(@NonNull NoteListsWidgetData data) throws SQLException {
validateAccountId(data.getAccountId());
final SQLiteDatabase db = getWritableDatabase();
final ContentValues values = new ContentValues();
final ContentValues values = new ContentValues(5);
if (data.getMode() != MODE_DISPLAY_CATEGORY && data.getCategoryId() != null) {
throw new UnsupportedOperationException("Cannot create a widget with a categoryId when mode is not " + MODE_DISPLAY_CATEGORY);
}