# All files / src/platformTools/android manifestParser.js

18.84% Statements 26/138
0% Branches 0/88
0% Functions 0/12
18.25% Lines 23/126

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    2292x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x
    2x

    2x

    2x

    2x

    2x

    2x
     import path from 'path';
    import os from 'os';
    import fs from 'fs';
    import net from 'net';
    import chalk from 'chalk';
    import shell from 'shelljs';
    import child_process from 'child_process';
    import inquirer from 'inquirer';
    import {
        logTask,
        logError,
        getAppFolder,
        isPlatformActive,
        getAppVersion,
        getAppTitle,
        getAppVersionCode,
        writeCleanFile,
        getAppId,
        getAppTemplateFolder,
        getBuildFilePath,
        getEntryFile,
        logWarning,
        logDebug,
        getConfigProp,
        logInfo,
        logSuccess,
        getBuildsFolder,
    } from '../../common';
    import { copyBuildsFolder } from '../../projectTools/projectParser'
    import { copyFolderContentsRecursiveSync, copyFileSync, mkdirSync, readObjectSync } from '../../systemTools/fileutils';
    import { getMergedPlugin, parsePlugins } from '../../pluginTools';

    const _findChildNode = (tag, name, node) => {
        if (!node) {
            logWarning('_findChildNode: Node is undefined');
            return;
        }
        if (!name && !PROHIBITED_DUPLICATE_TAGS.includes(tag)) return null; // Can't determine reused child nodes without unique name identifier
        for (let i = 0; i < node.children.length; i++) {
            const ch = node.children[i];
            if (ch.tag === tag) {
                if ((ch['android:name'] === name) || PROHIBITED_DUPLICATE_TAGS.includes(tag)) return ch;
            }
        }
        return null;
    };

    const _convertToXML = manifestObj => _parseNode(manifestObj, 0);

    const _parseNode = (n, level) => {
        let output = '';
        let space = '';
        for (let i = 0; i < level; i++) {
            space += '    ';
        }

        let nodeKeysCount = 0;
        Object.keys(n).forEach((v) => {
            if (!SYSTEM_TAGS.includes(v)) nodeKeysCount++;
        });
        const isSingleLine = nodeKeysCount < 2;

        if (!n.tag) {
            logWarning('Each node must have tag key!');
            return;
        }

        if (n) {
            const endLine = isSingleLine ? ' ' : '\n';
            output += `${space}<${n.tag}${endLine}`;
            for (const k in n) {
                if (!SYSTEM_TAGS.includes(k)) {
                    output += `${isSingleLine ? '' : `${space}  `}${k}="${n[k]}"${endLine}`;
                }
            }
        } else {
            output += `${space}<${n.tag}`;
        }
        if (n.children && n.children.length) {
            if (isSingleLine) {
                output += '>\n';
            } else {
                output += `${space}>\n`;
            }

            const nextLevel = level += 1;
            n.children.forEach((v) => {
                output += _parseNode(v, nextLevel);
            });
            output += `${space}</${n.tag}>\n`;
        } else {
            output += `${isSingleLine ? '' : space}/>\n`;
        }
        return output;
    };

    const _mergeNodeParameters = (node, nodeParamsExt) => {
        if (!nodeParamsExt) {
            logWarning('_mergeNodeParameters: nodeParamsExt value is null');
            return;
        }
        if (!node) {
            logWarning('_mergeNodeParameters: node value is null');
            return;
        }

        for (const k in nodeParamsExt) {
            if (!SYSTEM_TAGS.includes(k)) node[k] = nodeParamsExt[k];
        }
    };

    const PROHIBITED_DUPLICATE_TAGS = ['intent-filter'];
    const SYSTEM_TAGS = ['tag', 'children'];

    const _mergeNodeChildren = (node, nodeChildrenExt = []) => {
        // console.log('_mergeNodeChildren', node, 'OVERRIDE', nodeChildrenExt);
        if (!node) {
            logWarning('_mergeNodeChildren: Node is undefined');
            return;
        }
        if (!node.children) node.children = [];
        nodeChildrenExt.forEach((v) => {
            const nameExt = v['android:name'];
            if (v.tag) {
                const childNode = _findChildNode(v.tag, nameExt, node);
                if (childNode) {
                    console.log('_mergeNodeChildren: FOUND EXISTING NODE TO MERGE', nameExt, v.tag);
                    _mergeNodeParameters(childNode, v);
                    _mergeNodeChildren(childNode, v.children);
                } else {
                    console.log('_mergeNodeChildren: NO android:name found. adding to children', nameExt, v.tag);
                    node.children.push(v);
                }
            }
        });
    };

    export const parseAndroidManifestSync = (c, platform) => {
        logTask(`parseAndroidManifestSync:${platform}`);
        const pluginConfig = {};
        try {
            const baseManifestFilePath = path.join(c.paths.rnv.dir, `src/platformTools/android/supportFiles/AndroidManifest_${platform}.json`);
            const baseManifestFile = readObjectSync(baseManifestFilePath);
            const appFolder = getAppFolder(c, platform);
            const application = _findChildNode('application', '.MainApplication', baseManifestFile);

            baseManifestFile.package = getAppId(c, platform);

            // projectConfig/plugins.json PLUGIN CONFIG ROOT OVERRIDES
            const pluginConfigAndroid = c.buildConfig?.platforms?.[platform]?.AndroidManifest;
            if (pluginConfigAndroid) {
                const applicationExt = _findChildNode('application', '.MainApplication', pluginConfigAndroid);
                _mergeNodeParameters(application, applicationExt);
                if (applicationExt.children) {
                    _mergeNodeChildren(application, applicationExt.children);
                }
            }

            // projectConfig/plugins.json PLUGIN CONFIG OVERRIDES
            parsePlugins(c, platform, (plugin, pluginPlat, key) => {
                if (pluginPlat && pluginPlat.AndroidManifest) {
                    _mergeNodeChildren(baseManifestFile, pluginPlat.AndroidManifest.children);
                    // const pluginApplication = _findChildNode('application', '.MainApplication', pluginPlat.AndroidManifest);
                    // if (pluginApplication) {
                    //     _mergeNodeParameters(application, pluginApplication);
                    //
                    //     _mergeNodeChildren(application, pluginApplication.children);
                    // }
                }
            });

            // appConfig PERMISSIONS OVERRIDES
            let prms = '';
            const configPermissions = c.buildConfig?.permissions;

            const includedPermissions = getConfigProp(c, platform, 'includedPermissions') || getConfigProp(c, platform, 'permissions');
            if (includedPermissions && configPermissions) {
                const platPerm = configPermissions[platform] ? platform : 'android';
                const pc = configPermissions[platPerm];
                if (includedPermissions[0] === '*') {
                    for (const k in pc) {
                        prms += `\n   <uses-permission android:name="${pc[k].key}" />`;
                        const key = pc[k].key || k;
                        baseManifestFile.children.push({
                            tag: 'uses-permission',
                            'android:name': key
                        });
                    }
                } else {
                    includedPermissions.forEach((v) => {
                        if (pc[v]) {
                            prms += `\n   <uses-permission android:name="${pc[v].key}" />`;
                            const key = pc[v].key || k;
                            baseManifestFile.children.push({
                                tag: 'uses-permission',
                                'android:name': key
                            });
                        }
                    });
                }
            }

            const manifestXml = _convertToXML(baseManifestFile);
            // get correct source of manifest
            const manifestFile = 'app/src/main/AndroidManifest.xml';

            writeCleanFile(getBuildFilePath(c, platform, manifestFile), path.join(appFolder, manifestFile), [
                { pattern: '{{PLUGIN_MANIFEST_FILE}}', override: manifestXml },
                { pattern: '{{PERMISIONS}}', override: prms },
                { pattern: '{{APPLICATION_ID}}', override: baseManifestFile.package }
            ]);

            return;
        } catch (e) {
            logError(e);
        }
    };

    export const injectPluginManifestSync = (c, plugin, key, pkg) => {
        const className = pkg ? pkg.split('.').pop() : null;
        let packageParams = '';
        if (plugin.packageParams) {
            packageParams = plugin.packageParams.join(',');
        }

        const pathFixed = plugin.path ? `${plugin.path}` : `node_modules/${key}/android`;
        const modulePath = `../../${pathFixed}`;
    };