|  | 
| 17 | 17 | import android.os.Looper; | 
| 18 | 18 | import androidx.annotation.ChecksSdkIntAtLeast; | 
| 19 | 19 | import androidx.annotation.NonNull; | 
|  | 20 | +import androidx.annotation.Nullable; | 
| 20 | 21 | import io.flutter.plugins.quickactions.Messages.AndroidQuickActionsApi; | 
| 21 | 22 | import io.flutter.plugins.quickactions.Messages.FlutterError; | 
| 22 |  | -import io.flutter.plugins.quickactions.Messages.Result; | 
| 23 | 23 | import io.flutter.plugins.quickactions.Messages.ShortcutItemMessage; | 
| 24 | 24 | import java.util.ArrayList; | 
| 25 | 25 | import java.util.List; | 
| @@ -48,89 +48,87 @@ public Activity getActivity() { | 
| 48 | 48 | 
 | 
| 49 | 49 |   @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N_MR1) | 
| 50 | 50 |   boolean isVersionAllowed() { | 
| 51 |  | -    // We already know that this functionality does not work for anything | 
| 52 |  | -    // lower than API 25 so we chose not to return error. Instead we do nothing. | 
| 53 | 51 |     return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1; | 
| 54 | 52 |   } | 
| 55 | 53 | 
 | 
| 56 | 54 |   @Override | 
| 57 |  | -  public void setShortcutItems( | 
| 58 |  | -      @NonNull List<ShortcutItemMessage> itemsList, @NonNull Result<Void> result) { | 
| 59 |  | -    if (isVersionAllowed()) { | 
| 60 |  | -      ShortcutManager shortcutManager = | 
| 61 |  | -          (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | 
| 62 |  | -      List<ShortcutInfo> shortcuts = ShortcutItemMessageToShortcutInfo(itemsList); | 
| 63 |  | -      Executor uiThreadExecutor = new UiThreadExecutor(); | 
| 64 |  | -      ThreadPoolExecutor executor = | 
| 65 |  | -          new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); | 
| 66 |  | - | 
| 67 |  | -      executor.execute( | 
| 68 |  | -          () -> { | 
| 69 |  | -            boolean dynamicShortcutsSet = false; | 
| 70 |  | -            try { | 
| 71 |  | -              shortcutManager.setDynamicShortcuts(shortcuts); | 
| 72 |  | -              dynamicShortcutsSet = true; | 
| 73 |  | -            } catch (Exception e) { | 
| 74 |  | -              // Leave dynamicShortcutsSet as false | 
| 75 |  | -            } | 
| 76 |  | - | 
| 77 |  | -            final boolean didSucceed = dynamicShortcutsSet; | 
| 78 |  | - | 
| 79 |  | -            // TODO(camsim99): Move re-dispatch below to background thread when Flutter 2.8+ is | 
| 80 |  | -            // stable. | 
| 81 |  | -            uiThreadExecutor.execute( | 
| 82 |  | -                () -> { | 
| 83 |  | -                  if (didSucceed) { | 
| 84 |  | -                    result.success(null); | 
| 85 |  | -                  } else { | 
| 86 |  | -                    result.error( | 
| 87 |  | -                        new FlutterError( | 
| 88 |  | -                            "quick_action_setshortcutitems_failure", | 
| 89 |  | -                            "Exception thrown when setting dynamic shortcuts", | 
| 90 |  | -                            null)); | 
| 91 |  | -                  } | 
| 92 |  | -                }); | 
| 93 |  | -          }); | 
|  | 55 | +  public void setShortcutItems(@NonNull List<ShortcutItemMessage> itemsList) { | 
|  | 56 | +    // We already know that this functionality does not work for anything | 
|  | 57 | +    // lower than API 25 so we chose not to return error. Instead we do nothing. | 
|  | 58 | +    if (!isVersionAllowed()) { | 
|  | 59 | +      return; | 
| 94 | 60 |     } | 
| 95 |  | -    return; | 
|  | 61 | +    ShortcutManager shortcutManager = | 
|  | 62 | +        (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | 
|  | 63 | +    List<ShortcutInfo> shortcuts = shortcutItemMessageToShortcutInfo(itemsList); | 
|  | 64 | +    Executor uiThreadExecutor = new UiThreadExecutor(); | 
|  | 65 | +    ThreadPoolExecutor executor = | 
|  | 66 | +        new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); | 
|  | 67 | + | 
|  | 68 | +    executor.execute( | 
|  | 69 | +        () -> { | 
|  | 70 | +          boolean dynamicShortcutsSet = false; | 
|  | 71 | +          try { | 
|  | 72 | +            shortcutManager.setDynamicShortcuts(shortcuts); | 
|  | 73 | +            dynamicShortcutsSet = true; | 
|  | 74 | +          } catch (Exception e) { | 
|  | 75 | +            // Leave dynamicShortcutsSet as false | 
|  | 76 | +          } | 
|  | 77 | + | 
|  | 78 | +          final boolean didSucceed = dynamicShortcutsSet; | 
|  | 79 | + | 
|  | 80 | +          // TODO(camsim99): Move re-dispatch below to background thread when Flutter 2.8+ is | 
|  | 81 | +          // stable. | 
|  | 82 | +          uiThreadExecutor.execute( | 
|  | 83 | +              () -> { | 
|  | 84 | +                if (!didSucceed) { | 
|  | 85 | +                  throw new FlutterError( | 
|  | 86 | +                      "quick_action_setshortcutitems_failure", | 
|  | 87 | +                      "Exception thrown when setting dynamic shortcuts", | 
|  | 88 | +                      null); | 
|  | 89 | +                } | 
|  | 90 | +              }); | 
|  | 91 | +        }); | 
| 96 | 92 |   } | 
| 97 | 93 | 
 | 
| 98 | 94 |   @Override | 
| 99 |  | -  public void clearShortcutItems(@NonNull Result<Void> result) { | 
| 100 |  | -    if (isVersionAllowed()) { | 
| 101 |  | -      ShortcutManager shortcutManager = | 
| 102 |  | -          (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | 
| 103 |  | -      shortcutManager.removeAllDynamicShortcuts(); | 
| 104 |  | -      result.success(null); | 
|  | 95 | +  public void clearShortcutItems() { | 
|  | 96 | +    // We already know that this functionality does not work for anything | 
|  | 97 | +    // lower than API 25 so we chose not to return error. Instead we do nothing. | 
|  | 98 | +    if (!isVersionAllowed()) { | 
|  | 99 | +      return; | 
| 105 | 100 |     } | 
|  | 101 | +    ShortcutManager shortcutManager = | 
|  | 102 | +        (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | 
|  | 103 | +    shortcutManager.removeAllDynamicShortcuts(); | 
| 106 | 104 |   } | 
| 107 | 105 | 
 | 
| 108 | 106 |   @Override | 
| 109 |  | -  public void getLaunchAction(@NonNull Result<String> result) { | 
| 110 |  | -    if (isVersionAllowed()) { | 
| 111 |  | -      ShortcutManager shortcutManager = | 
| 112 |  | -          (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | 
| 113 |  | -      if (activity == null) { | 
| 114 |  | -        result.error( | 
| 115 |  | -            new FlutterError( | 
| 116 |  | -                "quick_action_getlaunchaction_no_activity", | 
| 117 |  | -                "There is no activity available when launching action", | 
| 118 |  | -                null)); | 
| 119 |  | -        return; | 
| 120 |  | -      } | 
| 121 |  | -      final Intent intent = activity.getIntent(); | 
| 122 |  | -      final String launchAction = intent.getStringExtra(EXTRA_ACTION); | 
| 123 |  | -      if (launchAction != null && !launchAction.isEmpty()) { | 
| 124 |  | -        shortcutManager.reportShortcutUsed(launchAction); | 
| 125 |  | -        intent.removeExtra(EXTRA_ACTION); | 
| 126 |  | -      } | 
| 127 |  | -      result.success(launchAction); | 
|  | 107 | +  public @Nullable String getLaunchAction() { | 
|  | 108 | +    // We already know that this functionality does not work for anything | 
|  | 109 | +    // lower than API 25 so we chose not to return error. Instead we do nothing. | 
|  | 110 | +    if (!isVersionAllowed()) { | 
|  | 111 | +      return null; | 
|  | 112 | +    } | 
|  | 113 | +    ShortcutManager shortcutManager = | 
|  | 114 | +        (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | 
|  | 115 | +    if (activity == null) { | 
|  | 116 | +      throw new FlutterError( | 
|  | 117 | +          "quick_action_getlaunchaction_no_activity", | 
|  | 118 | +          "There is no activity available when launching action", | 
|  | 119 | +          null); | 
|  | 120 | +    } | 
|  | 121 | +    final Intent intent = activity.getIntent(); | 
|  | 122 | +    final String launchAction = intent.getStringExtra(EXTRA_ACTION); | 
|  | 123 | +    if (launchAction != null && !launchAction.isEmpty()) { | 
|  | 124 | +      shortcutManager.reportShortcutUsed(launchAction); | 
|  | 125 | +      intent.removeExtra(EXTRA_ACTION); | 
| 128 | 126 |     } | 
| 129 |  | -    return; | 
|  | 127 | +    return launchAction; | 
| 130 | 128 |   } | 
| 131 | 129 | 
 | 
| 132 | 130 |   @TargetApi(Build.VERSION_CODES.N_MR1) | 
| 133 |  | -  private List<ShortcutInfo> ShortcutItemMessageToShortcutInfo( | 
|  | 131 | +  private List<ShortcutInfo> shortcutItemMessageToShortcutInfo( | 
| 134 | 132 |       @NonNull List<ShortcutItemMessage> shortcuts) { | 
| 135 | 133 |     final List<ShortcutInfo> shortcutInfos = new ArrayList<>(); | 
| 136 | 134 | 
 | 
|  | 
0 commit comments