patchAndroidManifestForSuperwall function

PatchedManifest patchAndroidManifestForSuperwall(
  1. String manifestContent
)

Idempotently inserts superwallAndroidActivityXml just after the opening <application ...> tag in manifestContent. Pure — does no IO so it can be unit-tested against canned manifest strings.

Implementation

PatchedManifest patchAndroidManifestForSuperwall(String manifestContent) {
  // Idempotency: any existing reference to the SuperwallPaywallActivity means
  // we've already patched (or the developer pasted it manually). Bail.
  if (manifestContent.contains('SuperwallPaywallActivity')) {
    return PatchedManifest(
      manifestContent,
      AndroidManifestPatchResult.alreadyRegistered,
    );
  }

  final appTagStart = manifestContent.indexOf('<application');
  if (appTagStart == -1) {
    return PatchedManifest(
      manifestContent,
      AndroidManifestPatchResult.malformedManifest,
    );
  }
  // Find the closing `>` of the `<application ...>` opening tag. A real
  // Flutter manifest always has children inside it, so we don't special-case
  // a self-closing `<application .../>`.
  final appTagEnd = manifestContent.indexOf('>', appTagStart);
  if (appTagEnd == -1) {
    return PatchedManifest(
      manifestContent,
      AndroidManifestPatchResult.malformedManifest,
    );
  }

  final insertion = '\n        $superwallAndroidActivityXml';
  final patched =
      manifestContent.substring(0, appTagEnd + 1) +
      insertion +
      manifestContent.substring(appTagEnd + 1);

  return PatchedManifest(patched, AndroidManifestPatchResult.added);
}