|
4 | 4 | import android.content.Intent; |
5 | 5 | import android.content.pm.ResolveInfo; |
6 | 6 | import android.net.Uri; |
| 7 | + |
7 | 8 | import androidx.annotation.NonNull; |
8 | 9 | import androidx.annotation.Nullable; |
| 10 | +import androidx.annotation.VisibleForTesting; |
| 11 | + |
9 | 12 | import android.util.Log; |
10 | 13 |
|
11 | 14 | import java.util.List; |
12 | 15 |
|
13 | 16 | class IterableActionRunner { |
14 | | - private static final String TAG = "IterableActionRunner"; |
15 | | - |
16 | | - /** |
17 | | - * Execute an {@link IterableAction} as a response to push action |
18 | | - * @param context Context |
19 | | - * @param action The original action object |
20 | | - * @return `true` if the action was handled, `false` if it was not |
21 | | - */ |
22 | | - static boolean executeAction(@NonNull Context context, @Nullable IterableAction action, @NonNull IterableActionSource source) { |
23 | | - if (action == null) { |
24 | | - return false; |
25 | | - } |
26 | 17 |
|
27 | | - IterableActionContext actionContext = new IterableActionContext(action, source); |
| 18 | + @VisibleForTesting |
| 19 | + static IterableActionRunnerImpl instance = new IterableActionRunnerImpl(); |
28 | 20 |
|
29 | | - if (action.isOfType(IterableAction.ACTION_TYPE_OPEN_URL)) { |
30 | | - return openUri(context, Uri.parse(action.getData()), actionContext); |
31 | | - } else { |
32 | | - return callCustomActionIfSpecified(action, actionContext); |
33 | | - } |
| 21 | + static boolean executeAction(@NonNull Context context, @Nullable IterableAction action, @NonNull IterableActionSource source) { |
| 22 | + return instance.executeAction(context, action, source); |
34 | 23 | } |
35 | 24 |
|
36 | | - /** |
37 | | - * Handle {@link IterableAction#ACTION_TYPE_OPEN_URL} action type |
38 | | - * Calls {@link IterableUrlHandler} for custom handling by the app. If the handle does not exist |
39 | | - * or returns `false`, the SDK tries to find an activity that can open this URL. |
40 | | - * @param context Context |
41 | | - * @param uri The URL to open |
42 | | - * @param actionContext The original action object |
43 | | - * @return `true` if the action was handled, or an activity was found for this URL |
44 | | - * `false` if the handler did not handle this URL and no activity was found to open it with |
45 | | - */ |
46 | | - private static boolean openUri(@NonNull Context context, @NonNull Uri uri, @NonNull IterableActionContext actionContext) { |
47 | | - if (IterableApi.sharedInstance.config.urlHandler != null) { |
48 | | - if (IterableApi.sharedInstance.config.urlHandler.handleIterableURL(uri, actionContext)) { |
49 | | - return true; |
| 25 | + static class IterableActionRunnerImpl { |
| 26 | + private static final String TAG = "IterableActionRunner"; |
| 27 | + |
| 28 | + /** |
| 29 | + * Execute an {@link IterableAction} as a response to push action |
| 30 | + * |
| 31 | + * @param context Context |
| 32 | + * @param action The original action object |
| 33 | + * @return `true` if the action was handled, `false` if it was not |
| 34 | + */ |
| 35 | + boolean executeAction(@NonNull Context context, @Nullable IterableAction action, @NonNull IterableActionSource source) { |
| 36 | + if (action == null) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + IterableActionContext actionContext = new IterableActionContext(action, source); |
| 41 | + |
| 42 | + if (action.isOfType(IterableAction.ACTION_TYPE_OPEN_URL)) { |
| 43 | + return openUri(context, Uri.parse(action.getData()), actionContext); |
| 44 | + } else { |
| 45 | + return callCustomActionIfSpecified(action, actionContext); |
50 | 46 | } |
51 | 47 | } |
52 | 48 |
|
53 | | - // Handle URL: check for deep links within the app |
54 | | - Intent intent = new Intent(Intent.ACTION_VIEW); |
55 | | - intent.setData(uri); |
56 | | - |
57 | | - List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0); |
58 | | - if (resolveInfos.size() > 1) { |
59 | | - for (ResolveInfo resolveInfo : resolveInfos) { |
60 | | - if (resolveInfo.activityInfo.packageName.equals(context.getPackageName())) { |
61 | | - Log.d(TAG, "The deep link will be handled by the app: " + resolveInfo.activityInfo.packageName); |
62 | | - intent.setPackage(resolveInfo.activityInfo.packageName); |
63 | | - break; |
| 49 | + /** |
| 50 | + * Handle {@link IterableAction#ACTION_TYPE_OPEN_URL} action type |
| 51 | + * Calls {@link IterableUrlHandler} for custom handling by the app. If the handle does not exist |
| 52 | + * or returns `false`, the SDK tries to find an activity that can open this URL. |
| 53 | + * |
| 54 | + * @param context Context |
| 55 | + * @param uri The URL to open |
| 56 | + * @param actionContext The original action object |
| 57 | + * @return `true` if the action was handled, or an activity was found for this URL |
| 58 | + * `false` if the handler did not handle this URL and no activity was found to open it with |
| 59 | + */ |
| 60 | + private boolean openUri(@NonNull Context context, @NonNull Uri uri, @NonNull IterableActionContext actionContext) { |
| 61 | + if (IterableApi.sharedInstance.config.urlHandler != null) { |
| 62 | + if (IterableApi.sharedInstance.config.urlHandler.handleIterableURL(uri, actionContext)) { |
| 63 | + return true; |
64 | 64 | } |
65 | 65 | } |
66 | | - } |
67 | 66 |
|
68 | | - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 67 | + // Handle URL: check for deep links within the app |
| 68 | + Intent intent = new Intent(Intent.ACTION_VIEW); |
| 69 | + intent.setData(uri); |
69 | 70 |
|
70 | | - if (intent.resolveActivity(context.getPackageManager()) != null) { |
71 | | - context.startActivity(intent); |
72 | | - return true; |
73 | | - } else { |
74 | | - IterableLogger.e(TAG, "Could not find activities to handle deep link:" + uri); |
75 | | - return false; |
| 71 | + List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0); |
| 72 | + if (resolveInfos.size() > 1) { |
| 73 | + for (ResolveInfo resolveInfo : resolveInfos) { |
| 74 | + if (resolveInfo.activityInfo.packageName.equals(context.getPackageName())) { |
| 75 | + Log.d(TAG, "The deep link will be handled by the app: " + resolveInfo.activityInfo.packageName); |
| 76 | + intent.setPackage(resolveInfo.activityInfo.packageName); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 83 | + |
| 84 | + if (intent.resolveActivity(context.getPackageManager()) != null) { |
| 85 | + context.startActivity(intent); |
| 86 | + return true; |
| 87 | + } else { |
| 88 | + IterableLogger.e(TAG, "Could not find activities to handle deep link:" + uri); |
| 89 | + return false; |
| 90 | + } |
76 | 91 | } |
77 | | - } |
78 | 92 |
|
79 | | - /** |
80 | | - * Handle custom actions passed from push notifications |
81 | | - * @param action {@link IterableAction} object that contains action payload |
82 | | - * @return `true` if the action is valid and was handled by the handler |
83 | | - * `false` if the action is invalid or the handler returned `false` |
84 | | - */ |
85 | | - private static boolean callCustomActionIfSpecified(@NonNull IterableAction action, @NonNull IterableActionContext actionContext) { |
86 | | - if (action.getType() != null && !action.getType().isEmpty()) { |
87 | | - // Call custom action handler |
88 | | - if (IterableApi.sharedInstance.config.customActionHandler != null) { |
89 | | - return IterableApi.sharedInstance.config.customActionHandler.handleIterableCustomAction(action, actionContext); |
| 93 | + /** |
| 94 | + * Handle custom actions passed from push notifications |
| 95 | + * |
| 96 | + * @param action {@link IterableAction} object that contains action payload |
| 97 | + * @return `true` if the action is valid and was handled by the handler |
| 98 | + * `false` if the action is invalid or the handler returned `false` |
| 99 | + */ |
| 100 | + private boolean callCustomActionIfSpecified(@NonNull IterableAction action, @NonNull IterableActionContext actionContext) { |
| 101 | + if (action.getType() != null && !action.getType().isEmpty()) { |
| 102 | + // Call custom action handler |
| 103 | + if (IterableApi.sharedInstance.config.customActionHandler != null) { |
| 104 | + return IterableApi.sharedInstance.config.customActionHandler.handleIterableCustomAction(action, actionContext); |
| 105 | + } |
90 | 106 | } |
| 107 | + return false; |
91 | 108 | } |
92 | | - return false; |
93 | 109 | } |
94 | 110 |
|
95 | 111 | } |
0 commit comments