|
6 | 6 | */ |
7 | 7 | package it.niedermann.owncloud.notes.edit; |
8 | 8 |
|
9 | | -import static java.lang.Boolean.TRUE; |
10 | 9 | import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT; |
11 | 10 | import static it.niedermann.owncloud.notes.shared.util.WidgetUtil.pendingIntentFlagCompat; |
12 | 11 |
|
@@ -154,6 +153,10 @@ private void loadExistingNote(long noteId) { |
154 | 153 |
|
155 | 154 | isNew = false; |
156 | 155 | 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 | + } |
157 | 160 | } |
158 | 161 |
|
159 | 162 | private void createNewNote() { |
@@ -306,26 +309,43 @@ public boolean onOptionsItemSelected(MenuItem item) { |
306 | 309 | shareNote(); |
307 | 310 | return false; |
308 | 311 | } 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(); |
324 | 313 | return true; |
325 | 314 | } |
326 | 315 | return super.onOptionsItemSelected(item); |
327 | 316 | } |
328 | 317 |
|
| 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 | + |
329 | 349 | protected void shareNote() { |
330 | 350 | if (note == null) { |
331 | 351 | Log_OC.w(TAG, "Note is null in shareNote"); |
|
0 commit comments