BaseDeepLinkDelegate { public @NonNull DeepLinkResult createResult( Activity activity, Intent sourceIntent, DeepLinkEntry deepLinkEntry ) { validateInput(activity, sourceIntent); Uri uri = sourceIntent.getData(); if (uri == null) { return new DeepLinkResult( false, null, "No Uri in given activity's intent.", null, null, deepLinkEntry); } String uriString = uri.toString(); if (deepLinkEntry == null) { return new DeepLinkResult(false, null, "DeepLinkEntry cannot be null", null, null, null); } DeepLinkUri deepLinkUri = DeepLinkUri.parse(uriString); Map<String, String> parameterMap = new HashMap<>(deepLinkEntry.getParameters(deepLinkUri)); for (String queryParameter : deepLinkUri.queryParameterNames()) { for (String queryParameterValue : deepLinkUri.queryParameterValues(queryParameter)) { if (parameterMap.containsKey(queryParameter)) { Log.w(TAG, "Duplicate parameter name in path and query param: " + queryParameter); } parameterMap.put(queryParameter, queryParameterValue); } } parameterMap.put(DeepLink.URI, uri.toString()); Bundle parameters; if (sourceIntent.getExtras() != null) { parameters = new Bundle(sourceIntent.getExtras()); } else { parameters = new Bundle(); } for (Map.Entry<String, String> parameterEntry : parameterMap.entrySet()) { parameters.putString(parameterEntry.getKey(), parameterEntry.getValue()); } try { Class<?> c = deepLinkEntry.getActivityClass(); Intent newIntent = null; TaskStackBuilder taskStackBuilder = null; if (deepLinkEntry.getType() == DeepLinkEntry.Type.CLASS) { newIntent = new Intent(activity, c); } else { Method method; DeepLinkResult errorResult = new DeepLinkResult(false, uriString, "Could not deep link to method: " + deepLinkEntry.getMethod() + " intents length == 0", null, null, deepLinkEntry); try { method = c.getMethod(deepLinkEntry.getMethod(), Context.class); if (method.getReturnType().equals(TaskStackBuilder.class)) { taskStackBuilder = (TaskStackBuilder) method.invoke(c, activity); if (taskStackBuilder.getIntentCount() == 0) { return errorResult; } newIntent = taskStackBuilder.editIntentAt(taskStackBuilder.getIntentCount() - 1); } else { newIntent = (Intent) method.invoke(c, activity); } } catch (NoSuchMethodException exception) { method = c.getMethod(deepLinkEntry.getMethod(), Context.class, Bundle.class); if (method.getReturnType().equals(TaskStackBuilder.class)) { taskStackBuilder = (TaskStackBuilder) method.invoke(c, activity, parameters); if (taskStackBuilder.getIntentCount() == 0) { return errorResult; } newIntent = taskStackBuilder.editIntentAt(taskStackBuilder.getIntentCount() - 1); } else { newIntent = (Intent) method.invoke(c, activity, parameters); } } } if (newIntent == null) { return new DeepLinkResult(false, uriString, "Destination Intent is null!", null, taskStackBuilder, deepLinkEntry); } if (newIntent.getAction() == null) { newIntent.setAction(sourceIntent.getAction()); } if (newIntent.getData() == null) { newIntent.setData(sourceIntent.getData()); } newIntent.putExtras(parameters); newIntent.putExtra(DeepLink.IS_DEEP_LINK, true); newIntent.putExtra(DeepLink.REFERRER_URI, uri); if (activity.getCallingActivity() != null) { newIntent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); } return new DeepLinkResult(true, uriString, "", newIntent, taskStackBuilder, deepLinkEntry); } catch (NoSuchMethodException exception) { return new DeepLinkResult(false, uriString, "Dee3p link to non-existent method: " + deepLinkEntry.getMethod(), null, null, deepLinkEntry); } catch (IllegalAccessException exception) { return new DeepLinkResult(false, uriString, "Could not deep link to method: " + deepLinkEntry.getMethod(), null, null, deepLinkEntry); } catch (InvocationTargetException exception) { return new DeepLinkResult(false, uriString, "Could not deep link to method: " + deepLinkEntry.getMethod(), null, null, deepLinkEntry); } } BaseDeepLinkDelegate(List<? extends BaseRegistry> registries);  BaseDeepLinkDelegate(List<? extends BaseRegistry> registries, ErrorHandler errorHandler);  BaseDeepLinkDelegate(
    List<? extends BaseRegistry> registries,
    Map<String, String> configurablePathSegmentReplacements
  );  BaseDeepLinkDelegate(
    List<? extends BaseRegistry> registries,
    Map<String, String> configurablePathSegmentReplacements,
    ErrorHandler errorHandler
  ); List<? extends BaseRegistry> getRegistries(); DeepLinkResult dispatchFrom(Activity activity); DeepLinkResult dispatchFrom(Activity activity, Intent sourceIntent); @NonNull DeepLinkResult createResult(
    Activity activity, Intent sourceIntent, DeepLinkEntry deepLinkEntry
  ); boolean supportsUri(String uriString);  }
@Test public void testCreateResultNullIntent() { DeepLinkEntry entry = deepLinkEntry("airbnb: TestDeepLinkDelegate testDelegate = getOneRegistryTestDelegate(Arrays.asList(new DeepLinkEntry[]{entry}), null); Activity activity = mock(Activity.class); when(activity.getIntent()) .thenReturn(null); String message = null; try { testDelegate.createResult(activity, null, null); } catch (NullPointerException e) { message = e.getMessage(); } assertThat(message).isEqualTo("sourceIntent == null"); }
@Test public void testCreateResultAllNullData() { DeepLinkEntry entry = deepLinkEntry("airbnb: TestDeepLinkDelegate testDelegate = getOneRegistryTestDelegate(Arrays.asList(new DeepLinkEntry[]{entry}), null); Intent intent = mock(Intent.class); when(intent.getData()) .thenReturn(null); Activity activity = mock(Activity.class); when(activity.getIntent()) .thenReturn(intent); DeepLinkResult result = testDelegate.createResult(activity, intent, null); assertThat(result).isEqualTo(new DeepLinkResult( false, null, "No Uri in given activity's intent.", null, null, null)); }
@Test public void testCreateResultAllNull() { DeepLinkEntry entry = deepLinkEntry("airbnb: TestDeepLinkDelegate testDelegate = getOneRegistryTestDelegate(Arrays.asList(new DeepLinkEntry[]{entry}), null); String message = null; try { testDelegate.createResult(null, null, null); } catch (NullPointerException e) { message = e.getMessage(); } assertThat(message).isEqualTo("activity == null"); }