#831 Migrate from SQLiteOpenHelper to Room

Rewrite NoSuchElementException for Widgets
This commit is contained in:
Stefan Niedermann 2020-10-09 21:22:58 +02:00
parent 5cfe9d5ea8
commit e1c80fe2c8
5 changed files with 21 additions and 26 deletions

View file

@ -67,6 +67,8 @@ import it.niedermann.owncloud.notes.shared.model.ISyncCallback;
import it.niedermann.owncloud.notes.shared.model.NavigationCategory;
import it.niedermann.owncloud.notes.shared.util.NoteUtil;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
@ -295,7 +297,7 @@ public abstract class NotesDatabase extends RoomDatabase {
notifyWidgets();
serverSyncHelper.scheduleSync(ssoAccount, true);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (SDK_INT >= O) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
shortcutManager.getPinnedShortcuts().forEach((shortcut) -> {
@ -326,7 +328,7 @@ public abstract class NotesDatabase extends RoomDatabase {
void updateDynamicShortcuts(long accountId) {
new Thread(() -> {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
if (SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = context.getApplicationContext().getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
if (!shortcutManager.isRateLimitingActive()) {

View file

@ -11,8 +11,6 @@ import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import java.util.NoSuchElementException;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
@ -44,8 +42,8 @@ public class NoteListWidget extends AppWidgetProvider {
DarkModeSetting darkTheme;
for (int appWidgetId : appWidgetIds) {
try {
final NotesListWidgetData data = db.getWidgetNotesListDao().getNoteListWidgetData(appWidgetId);
final NotesListWidgetData data = db.getWidgetNotesListDao().getNoteListWidgetData(appWidgetId);
if (data != null) {
final Account localAccount = db.getAccountDao().getAccount(data.getAccountId());
String category = null;
@ -128,7 +126,7 @@ public class NoteListWidget extends AppWidgetProvider {
}
awm.updateAppWidget(appWidgetId, views);
} catch (NoSuchElementException e) {
} else {
Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget");
}
}

View file

@ -10,8 +10,6 @@ import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import java.util.NoSuchElementException;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.edit.BaseNoteFragment;
@ -29,9 +27,8 @@ public class SingleNoteWidget extends AppWidgetProvider {
final NotesDatabase db = NotesDatabase.getInstance(context);
for (int appWidgetId : appWidgetIds) {
try {
final SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
final SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
if (data != null) {
templateIntent.putExtra(BaseNoteFragment.PARAM_ACCOUNT_ID, data.getAccountId());
final PendingIntent templatePendingIntent = PendingIntent.getActivity(context, appWidgetId, templateIntent,
@ -57,7 +54,7 @@ public class SingleNoteWidget extends AppWidgetProvider {
awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv);
}
awm.updateAppWidget(appWidgetId, views);
} catch (NoSuchElementException e) {
} else {
Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget");
}
}

View file

@ -17,6 +17,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.exception.ExceptionHandler;
import it.niedermann.owncloud.notes.main.MainActivity;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.NoteWithCategory;
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;
public class SingleNoteWidgetConfigurationActivity extends MainActivity {
@ -42,7 +43,7 @@ public class SingleNoteWidgetConfigurationActivity extends MainActivity {
@Override
public void onNoteClick(int position, View v) {
final Note note = (Note) adapter.getItem(position);
final NoteWithCategory note = (NoteWithCategory) adapter.getItem(position);
final Bundle extras = getIntent().getExtras();
if (extras == null) {
@ -56,8 +57,8 @@ public class SingleNoteWidgetConfigurationActivity extends MainActivity {
db.getWidgetSingleNoteDao().createOrUpdateSingleNoteWidgetData(
new SingleNoteWidgetData(
appWidgetId,
note.getAccountId(),
note.getId(),
note.getNote().getAccountId(),
note.getNote().getId(),
NotesApplication.getAppTheme(this).getModeId()
)
);

View file

@ -11,8 +11,6 @@ import android.widget.RemoteViewsService;
import com.yydcdut.markdown.MarkdownProcessor;
import com.yydcdut.markdown.syntax.text.TextFactory;
import java.util.NoSuchElementException;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
@ -43,14 +41,13 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa
db = NotesDatabase.getInstance(context);
markdownProcessor = new MarkdownProcessor(this.context);
markdownProcessor.factory(TextFactory.create());
try {
SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
final SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
if (data != null) {
darkModeActive = NotesApplication.isDarkThemeActive(context, DarkModeSetting.fromModeID(data.getThemeMode()));
} catch (NoSuchElementException e) {
} else {
Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet.");
} finally {
markdownProcessor.config(MarkDownUtil.getMarkDownConfiguration(this.context, darkModeActive).build());
}
markdownProcessor.config(MarkDownUtil.getMarkDownConfiguration(this.context, darkModeActive).build());
}
@Override
@ -60,8 +57,8 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa
@Override
public void onDataSetChanged() {
try {
final SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
final SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
if (data != null) {
final long noteId = data.getNoteId();
Log.v(TAG, "Fetch note with id " + noteId);
note = db.getNoteDao().getNote(data.getAccountId(), noteId);
@ -69,7 +66,7 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa
if (note == null) {
Log.e(TAG, "Error: note not found");
}
} catch (NoSuchElementException e) {
} else {
Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet.");
}
}