Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android:name="CordovaApp" vs. android:name="MainActivity" #1

Closed
regnete opened this issue Oct 5, 2015 · 6 comments
Closed

android:name="CordovaApp" vs. android:name="MainActivity" #1

regnete opened this issue Oct 5, 2015 · 6 comments

Comments

@regnete
Copy link

regnete commented Oct 5, 2015

Thank you for this great plugin.

With (the newest) Cordova CLI 5.3.1 and Cordova-Android 4.1.0 some preferences are not applied to the android manifest. We tracked this down to the following root cause:

In https://github.com/dpa99c/cordova-custom-config/blob/master/hooks/applyCustomConfig.js#L30 and the following lines you assume android:name="CordovaApp", but the cordova guys renamed the activity to 'MainActivity'.

This issue may be relevant for cli users only, I haven't tried to reproduce it on phonegap build service. The provided code changes should work with cli and pgb.

To support both activity names you could apply the following changes to your code:

  1. Change the android mapping config property "parent" from String to Array of String. This way you are able to provide alternative xpaths to resolve the parent.
    /*  Global object that defines the available custom preferences for each platform.
     Maps a config.xml preference to a specific target file, parent elements (first match will be used), and destination attribute or element
     */
    var preferenceMappingData = {
        'android': {
            'android-manifest-hardwareAccelerated': {target: 'AndroidManifest.xml', parent: ['./'], destination: 'android:hardwareAccelerated'},
            'android-installLocation': {target: 'AndroidManifest.xml', parent: ['./'], destination: 'android:installLocation'},
            'android-activity-hardwareAccelerated': {target: 'AndroidManifest.xml', parent: ['application'], destination: 'android:hardwareAccelerated'},
            'android-configChanges': {target: 'AndroidManifest.xml', parent: ['application/activity[@android:name=\'CordovaApp\']', 'application/activity[@android:name=\'MainActivity\']'], destination: 'android:configChanges'},
            'android-launchMode': {target: 'AndroidManifest.xml', parent: ['application/activity[@android:name=\'CordovaApp\']', 'application/activity[@android:name=\'MainActivity\']'], destination: 'android:launchMode'},
            'android-theme': {target: 'AndroidManifest.xml', parent: ['application/activity[@android:name=\'CordovaApp\']', 'application/activity[@android:name=\'MainActivity\']'], destination: 'android:theme'},
            'android-windowSoftInputMode': {target: 'AndroidManifest.xml', parent: ['application/activity[@android:name=\'CordovaApp\']', 'application/activity[@android:name=\'MainActivity\']'], destination: 'android:windowSoftInputMode'}
        },
        'ios': {}
    };
  1. Iterate of the alternative parents and use the first match:
    function updateAndroidManifest(targetFilePath, configItems) {
        var tempManifest = fileUtils.parseElementtreeSync(targetFilePath),
            root = tempManifest.getroot();

        _.each(configItems, function (item) {
            // if parent is not found on the root, child/grandchild nodes are searched
            var parentEl,
                data = item.data,
                childSelector = item.destination,
                childEl;

            _.each(item.parent, function(parentSelector){
                if(parentEl)
                    return;
                parentEl = root.find(parentSelector) || root.find('*/' + parentSelector);
            });

            if(!parentEl) {
                return;
            }

            if(item.type === 'preference') {
                parentEl.attrib[childSelector] = data.attrib['value'];
            } else {
                // since there can be multiple uses-permission elements, we need to select them by unique name
                if(childSelector === 'uses-permission') {
                    childSelector += '[@android:name=\'' + data.attrib['android:name'] + '\']';
                }

                childEl = parentEl.find(childSelector);
                // if child element doesnt exist, create new element
                if(!childEl) {
                    childEl = new et.Element(item.destination);
                    parentEl.append(childEl);
                }

                // copy all config.xml data except for the generated _id property
                _.each(data, function (prop, propName) {
                    if(propName !== '_id') {
                        childEl[propName] = prop;
                    }
                });
            }
        });
        fs.writeFileSync(targetFilePath, tempManifest.write({indent: 4}), 'utf-8');
    }

That's it. Now your plugin supports 'old' and 'new' activity names.

Sorry for not providing a pull request for this, but I'm a litlle in hurry these days.

Felix

@dpa99c
Copy link
Owner

dpa99c commented Oct 5, 2015

Hi Felix,

Thanks for this; just checked and my Cordova CLI was not the latest, so I was blissfully unaware that the guys at Cordova had helpfully changed the activity name. I'll integrate your suggested code changes (or a variant of them) when I have some time shortly.

Cheers,
Dave

@regnete
Copy link
Author

regnete commented Oct 5, 2015

Hi Dave,

thats good news for issue #1.

Felix

Von: Dave Alden [mailto:notifications@github.com]
Gesendet: Montag, 5. Oktober 2015 22:09
An: dpa99c/cordova-custom-config
Cc: Felix Schauerte
Betreff: Re: [cordova-custom-config] android:name="CordovaApp" vs. android:name="MainActivity" (#1)

Hi Felix,

Thanks for this; just checked and my Cordova CLI was not the latest, so I was blissfully unaware that the guys at Cordova had helpfully changed the activity name. I'll integrate your suggested code changes (or a variant of them) when I have some time shortly.

Cheers,
Dave


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-145653130.

@dpa99c
Copy link
Owner

dpa99c commented Oct 5, 2015

Fixed in v1.1.1

@dpa99c dpa99c closed this as completed Oct 5, 2015
@regnete
Copy link
Author

regnete commented Oct 6, 2015

@dpa99c
Copy link
Owner

dpa99c commented Oct 6, 2015

Oh, missed one. That's why programming shouldn't be done after drinking beer ;-) Guess it will be fixed in v1.1.2 :-)

@dpa99c dpa99c reopened this Oct 6, 2015
@dpa99c dpa99c closed this as completed in f5a053c Oct 6, 2015
@regnete
Copy link
Author

regnete commented Oct 14, 2015

Works like a charm now. Tx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants