<commit_before>(function() {
  // properties

  const path = require("path");
  const xmlHelper = require("../lib/xmlHelper.js");

  // entry
  module.exports = {
    writePreferences: writePreferences
  };

  // injects config.xml preferences into AndroidManifest.xml file.
  function writePreferences(context, preferences) {
    // read manifest
    const manifest = getManifest(context);

    // update manifest
    manifest.file = updateBranchMetaData(manifest.file, preferences);
    manifest.file = updateBranchReferrerTracking(manifest.file);
    manifest.file = updateLaunchOptionToSingleTask(
      manifest.file,
      manifest.mainActivityIndex
    );
    manifest.file = updateBranchURIScheme(
      manifest.file,
      manifest.mainActivityIndex,
      preferences
    );
    manifest.file = updateBranchAppLinks(
      manifest.file,
      manifest.mainActivityIndex,
      preferences
    );

    // save manifest
    xmlHelper.writeJsonAsXml(manifest.path, manifest.file);
  }

  // get AndroidManifest.xml information
  function getManifest(context) {
    let pathToManifest;
    let manifest;

    try {
      // cordova platform add android@6.0.0
      pathToManifest = path.join(
        context.opts.projectRoot,
        "platforms",
        "android",
        "AndroidManifest.xml"
      );
      manifest = xmlHelper.readXmlAsJson(pathToManifest);
    } catch (e) {
      try {
        // cordova platform add android@7.0.0
        pathToManifest = path.join(
          context.opts.projectRoot,
          "platforms",
          "android",
          "app",
          "src",
          "main",
          "AndroidManifest.xml"
        );
        manifest = xmlHelper.readXmlAsJson(pathToManifest);
      } catch (e) {
        throw new Error(`BRANCH SDK: Cannot read AndroidManfiest.xml ${e}`);
      }
    }
    const mainActivityIndex = getMainLaunchActivityIndex(
      manifest.manifest.application[0].activity
    );

    return {
      file: manifest,
      path: pathToManifest,
      mainActivityIndex: mainActivityIndex
    };
  }

  // adds to <application> for Branch init and testmode:
  //    <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_icCccJIpd7GlYY5oOmoEtpafuDiuyXhT" />
  //    <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />
  function updateBranchMetaData(manifest, preferences) {
    let metadatas = manifest.manifest.application[0]["meta-data"] || [];
    const metadata = [];
    const keys = ["io.branch.sdk.BranchKey", "io.branch.sdk.TestMode"];
    const vals = [
      preferences.branchKey,
      preferences.androidTestMode || "false"
    ];

    // remove old
    for (var i = 0; i < keys.length; i++) {
      metadatas = removeBasedOnAndroidName(metadatas, keys[i]);
    }

    // add new
    for (i = 0; i < keys.length; i++) {
      const key = keys[i];
      const val = vals[i];
      metadata.push({
        $: {
          "android:name": key,
          "android:value": val
        }
      });
    }
    manifest.manifest.application[0]["meta-data"] = metadatas.concat(metadata);

    return manifest;
  }

  // adds to <application> for install referrer tracking (optional)
  //    <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
  //       <intent-filter>
  //           <action android:name="com.android.vending.INSTALL_REFERRER" />
  //       </intent-filter>
  //    </receiver>
  function updateBranchReferrerTracking(manifest) {
    let receivers = manifest.manifest.application[0].receiver || [];
    const androidName = "io.branch.referral.InstallListener";

    // remove old
    receivers = removeBasedOnAndroidName(receivers, androidName);

    // add new
    manifest.manifest.application[0].receiver = receivers.concat([
      {
        $: {
          "android:name": androidName,
          "android:exported": true
        },
        "intent-filter": [
          {
            action: [
              {
                $: {
                  "android:name": "com.android.vending.INSTALL_REFERRER"
                }
              }
            ]
          }
        ]
      }
    ]);

    return manifest;
  }

  // adds to main <activity>:
  //    android:launchMode="singleTask"
  function updateLaunchOptionToSingleTask(manifest, mainActivityIndex) {
    manifest.manifest.application[0].activity[mainActivityIndex].$[
      "android:launchMode"
    ] =
      "singleTask";
    return manifest;
  }

  // adds to main <activity> for URI Scheme
  //    <intent-filter android:name="io.branch.sdk.UriScheme">
  //        <data android:scheme="ethantest" />
  //        <action android:name="android.intent.action.VIEW" />
  //        <category android:name="android.intent.category.DEFAULT" />
  //        <category android:name="android.intent.category.BROWSABLE" />
  //    </intent-filter>
  function updateBranchURIScheme(manifest, mainActivityIndex, preferences) {
    let intentFilters =
      manifest.manifest.application[0].activity[mainActivityIndex][
        "intent-filter"
      ] || [];<commit_msg>remove install referrer broadcast receiver (not just stop adding but remove if it exists)<commit_after>(function() {
  // properties

  const path = require("path");
  const xmlHelper = require("../lib/xmlHelper.js");

  // entry
  module.exports = {
    writePreferences: writePreferences
  };

  // injects config.xml preferences into AndroidManifest.xml file.
  function writePreferences(context, preferences) {
    // read manifest
    const manifest = getManifest(context);

    // update manifest
    manifest.file = updateBranchMetaData(manifest.file, preferences);
    manifest.file = removeDeprecatedInstalReferrerBroadcastReceiver(manifest.file);
    manifest.file = updateLaunchOptionToSingleTask(
      manifest.file,
      manifest.mainActivityIndex
    );
    manifest.file = updateBranchURIScheme(
      manifest.file,
      manifest.mainActivityIndex,
      preferences
    );
    manifest.file = updateBranchAppLinks(
      manifest.file,
      manifest.mainActivityIndex,
      preferences
    );

    // save manifest
    xmlHelper.writeJsonAsXml(manifest.path, manifest.file);
  }

  // get AndroidManifest.xml information
  function getManifest(context) {
    let pathToManifest;
    let manifest;

    try {
      // cordova platform add android@6.0.0
      pathToManifest = path.join(
        context.opts.projectRoot,
        "platforms",
        "android",
        "AndroidManifest.xml"
      );
      manifest = xmlHelper.readXmlAsJson(pathToManifest);
    } catch (e) {
      try {
        // cordova platform add android@7.0.0
        pathToManifest = path.join(
          context.opts.projectRoot,
          "platforms",
          "android",
          "app",
          "src",
          "main",
          "AndroidManifest.xml"
        );
        manifest = xmlHelper.readXmlAsJson(pathToManifest);
      } catch (e) {
        throw new Error(`BRANCH SDK: Cannot read AndroidManfiest.xml ${e}`);
      }
    }
    const mainActivityIndex = getMainLaunchActivityIndex(
      manifest.manifest.application[0].activity
    );

    return {
      file: manifest,
      path: pathToManifest,
      mainActivityIndex: mainActivityIndex
    };
  }

  // adds to <application> for Branch init and testmode:
  //    <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_icCccJIpd7GlYY5oOmoEtpafuDiuyXhT" />
  //    <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />
  function updateBranchMetaData(manifest, preferences) {
    let metadatas = manifest.manifest.application[0]["meta-data"] || [];
    const metadata = [];
    const keys = ["io.branch.sdk.BranchKey", "io.branch.sdk.TestMode"];
    const vals = [
      preferences.branchKey,
      preferences.androidTestMode || "false"
    ];

    // remove old
    for (var i = 0; i < keys.length; i++) {
      metadatas = removeBasedOnAndroidName(metadatas, keys[i]);
    }

    // add new
    for (i = 0; i < keys.length; i++) {
      const key = keys[i];
      const val = vals[i];
      metadata.push({
        $: {
          "android:name": key,
          "android:value": val
        }
      });
    }
    manifest.manifest.application[0]["meta-data"] = metadatas.concat(metadata);

    return manifest;
  }

  function removeDeprecatedInstalReferrerBroadcastReceiver(manifest) { 
    let receivers = manifest.manifest.application[0].receiver || [];  
    const androidName = "io.branch.referral.InstallListener";
    manifest.manifest.application[0].receiver = removeBasedOnAndroidName(receivers, androidName);

    return manifest;  
  }

  // adds to main <activity>:
  //    android:launchMode="singleTask"
  function updateLaunchOptionToSingleTask(manifest, mainActivityIndex) {
    manifest.manifest.application[0].activity[mainActivityIndex].$[
      "android:launchMode"
    ] =
      "singleTask";
    return manifest;
  }

  // adds to main <activity> for URI Scheme
  //    <intent-filter android:name="io.branch.sdk.UriScheme">
  //        <data android:scheme="ethantest" />
  //        <action android:name="android.intent.action.VIEW" />
  //        <category android:name="android.intent.category.DEFAULT" />
  //        <category android:name="android.intent.category.BROWSABLE" />
  //    </intent-filter>
  function updateBranchURIScheme(manifest, mainActivityIndex, preferences) {
    let intentFilters =
      manifest.manifest.application[0].activity[mainActivityIndex][
        "intent-filter"
      ] || [];
{'fraction_non_alphanumeric': 0.12282175099727063, 'fraction_numerical': 0.003569179088809574, 'mean_word_length': 2.576201201201201, 'pattern_counts': {'":': 8, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7239822', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2763, 'n_words': 505}