Skip to content

Commit df85a3a

Browse files
Merge pull request #2773 from nextcloud/bugfix/use-remote-id-for-home-pin-action
BugFix - Use remoteId for Pin to Home action
2 parents ee24bca + 9cef6bf commit df85a3a

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package it.niedermann.owncloud.notes.edit;
88

9-
import static java.lang.Boolean.TRUE;
109
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
1110
import static it.niedermann.owncloud.notes.shared.util.WidgetUtil.pendingIntentFlagCompat;
1211

@@ -154,6 +153,10 @@ private void loadExistingNote(long noteId) {
154153

155154
isNew = false;
156155
note = originalNote = repo.getNoteById(noteId);
156+
if (note == null) {
157+
Log_OC.d(TAG, "remoteNoteId will be used to get note");
158+
note = repo.getNoteByRemoteId(noteId);
159+
}
157160
}
158161

159162
private void createNewNote() {
@@ -306,26 +309,43 @@ public boolean onOptionsItemSelected(MenuItem item) {
306309
shareNote();
307310
return false;
308311
} else if (itemId == MENU_ID_PIN) {
309-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
310-
final var context = requireContext();
311-
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
312-
final var pinShortcutInfo = new ShortcutInfoCompat.Builder(context, String.valueOf(note.getId()))
313-
.setShortLabel(note.getTitle())
314-
.setIcon(IconCompat.createWithResource(context.getApplicationContext(), TRUE.equals(note.getFavorite()) ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_border_grey_ccc_24dp))
315-
.setIntent(new Intent(getActivity(), EditNoteActivity.class).putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getId()).setAction(ACTION_SHORTCUT))
316-
.build();
317-
318-
ShortcutManagerCompat.requestPinShortcut(context, pinShortcutInfo, PendingIntent.getBroadcast(context, 0, ShortcutManagerCompat.createShortcutResultIntent(context, pinShortcutInfo), pendingIntentFlagCompat(0)).getIntentSender());
319-
} else {
320-
Log.i(TAG, "RequestPinShortcut is not supported");
321-
}
322-
}
323-
312+
pinNoteToHome();
324313
return true;
325314
}
326315
return super.onOptionsItemSelected(item);
327316
}
328317

318+
private void pinNoteToHome() {
319+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
320+
return;
321+
}
322+
323+
if (!ShortcutManagerCompat.isRequestPinShortcutSupported(requireContext())) {
324+
Log.i(TAG, "RequestPinShortcut is not supported");
325+
return;
326+
}
327+
328+
final var iconId = note.getFavorite() ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_border_grey_ccc_24dp;
329+
final var icon = IconCompat.createWithResource(requireContext().getApplicationContext(), iconId);
330+
final var intent = new Intent(getActivity(), EditNoteActivity.class)
331+
.putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getRemoteId())
332+
.setAction(ACTION_SHORTCUT);
333+
final var noteId = String.valueOf(note.getRemoteId());
334+
335+
final var pinShortcutInfo = new ShortcutInfoCompat.Builder(requireContext(), noteId)
336+
.setShortLabel(note.getTitle())
337+
.setIcon(icon)
338+
.setIntent(intent)
339+
.build();
340+
341+
final var broadcastIntent = ShortcutManagerCompat.createShortcutResultIntent(requireContext(), pinShortcutInfo);
342+
final var intentFlag = pendingIntentFlagCompat(0);
343+
final var intentSender = PendingIntent
344+
.getBroadcast(requireContext(), 0, broadcastIntent, intentFlag)
345+
.getIntentSender();
346+
ShortcutManagerCompat.requestPinShortcut(requireContext(), pinShortcutInfo, intentSender);
347+
}
348+
329349
protected void shareNote() {
330350
if (note == null) {
331351
Log_OC.w(TAG, "Note is null in shareNote");

app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ public Note getNoteById(long id) {
355355
return db.getNoteDao().getNoteById(id);
356356
}
357357

358+
public Note getNoteByRemoteId(long id) {
359+
return db.getNoteDao().getNoteByRemoteId(id);
360+
}
361+
358362
public LiveData<Integer> count$(long accountId) {
359363
return db.getNoteDao().count$(accountId);
360364
}

app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public interface NoteDao {
5353
@Query(getNoteById)
5454
Note getNoteById(long id);
5555

56+
@Query("SELECT * FROM NOTE WHERE remoteId = :id")
57+
Note getNoteByRemoteId(long id);
58+
5659
@Query("SELECT remoteId FROM NOTE WHERE id = :id")
5760
Long getRemoteId(long id);
5861

0 commit comments

Comments
 (0)