Search in sources :

Example 1 with Android

use of com.twinsoft.convertigo.beans.mobileplatforms.Android in project convertigo by convertigo.

the class LaunchBuild method perform.

public static String perform(MobileResourceHelper mobileResourceHelper, HttpServletRequest request) throws Exception {
    MobilePlatform mobilePlatform = mobileResourceHelper.mobilePlatform;
    MobileApplication mobileApplication = mobileResourceHelper.mobileApplication;
    String finalApplicationName = mobileApplication.getComputedApplicationName();
    File mobileArchiveFile = mobileResourceHelper.makeZipPackage();
    // Login to the mobile builder platform
    String mobileBuilderPlatformURL = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_PLATFORM_URL);
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("application", new String[] { finalApplicationName });
    params.put("platformName", new String[] { mobilePlatform.getName() });
    params.put("platformType", new String[] { mobilePlatform.getType() });
    params.put("auth_token", new String[] { mobileApplication.getComputedAuthenticationToken() });
    // revision and endpoint params
    params.put("revision", new String[] { mobileResourceHelper.getRevision() });
    params.put("endpoint", new String[] { mobileApplication.getComputedEndpoint(request) });
    params.put("appid", new String[] { mobileApplication.getComputedApplicationId() });
    // iOS
    if (mobilePlatform instanceof IOs) {
        IOs ios = (IOs) mobilePlatform;
        String pw, title = ios.getiOSCertificateTitle();
        if (!title.equals("")) {
            pw = ios.getiOSCertificatePw();
        } else {
            title = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_IOS_CERTIFICATE_TITLE);
            pw = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_IOS_CERTIFICATE_PW);
        }
        params.put("iOSCertificateTitle", new String[] { title });
        params.put("iOSCertificatePw", new String[] { pw });
    }
    // Android
    if (mobilePlatform instanceof Android) {
        Android android = (Android) mobilePlatform;
        String certificatePw, keystorePw, title = android.getAndroidCertificateTitle();
        if (!title.equals("")) {
            certificatePw = android.getAndroidCertificatePw();
            keystorePw = android.getAndroidKeystorePw();
        } else {
            title = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_ANDROID_CERTIFICATE_TITLE);
            certificatePw = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_ANDROID_CERTIFICATE_PW);
            keystorePw = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_ANDROID_KEYSTORE_PW);
        }
        params.put("androidCertificateTitle", new String[] { title });
        params.put("androidCertificatePw", new String[] { certificatePw });
        params.put("androidKeystorePw", new String[] { keystorePw });
    }
    // Windows Phone
    if (mobilePlatform instanceof WindowsPhoneKeyProvider) {
        WindowsPhoneKeyProvider windowsPhone = (WindowsPhoneKeyProvider) mobilePlatform;
        String title = windowsPhone.getWinphonePublisherIdTitle();
        if (title.equals("")) {
            title = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_WINDOWSPHONE_PUBLISHER_ID_TITLE);
        }
        params.put("winphonePublisherIdTitle", new String[] { title });
    }
    // Launch the mobile build
    URL url = new URL(mobileBuilderPlatformURL + "/build?" + URLUtils.mapToQuery(params));
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(url.getHost());
    HttpState httpState = new HttpState();
    Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url);
    PostMethod method = new PostMethod(url.toString());
    FileRequestEntity entity = new FileRequestEntity(mobileArchiveFile, null);
    method.setRequestEntity(entity);
    int methodStatusCode = Engine.theApp.httpClient.executeMethod(hostConfiguration, method, httpState);
    String sResult = IOUtils.toString(method.getResponseBodyAsStream(), "UTF-8");
    if (methodStatusCode != HttpStatus.SC_OK) {
        throw new ServiceException("Unable to build application '" + finalApplicationName + "'.\n" + sResult);
    }
    return sResult;
}
Also used : HashMap(java.util.HashMap) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpState(org.apache.commons.httpclient.HttpState) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) URL(java.net.URL) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) MobilePlatform(com.twinsoft.convertigo.beans.core.MobilePlatform) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) File(java.io.File) WindowsPhoneKeyProvider(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhoneKeyProvider)

Example 2 with Android

use of com.twinsoft.convertigo.beans.mobileplatforms.Android in project convertigo by convertigo.

the class Migration7_0_0 method migrate.

public static Element migrate(Document document, Element projectNode) throws EngineException {
    try {
        NodeList mobileDevicesNodeList = XMLUtils.findElements(projectNode, "/mobiledevice");
        if (mobileDevicesNodeList != null) {
            MobileApplication mobileApplication = new MobileApplication();
            Element mobileApplicationElement = mobileApplication.toXml(document);
            projectNode.appendChild(mobileApplicationElement);
            Node[] mobileDeviceNodes = XMLUtils.toNodeArray(mobileDevicesNodeList);
            boolean hasAndroid = false;
            boolean hasIOs = false;
            for (Node mobileDeviceNode : mobileDeviceNodes) {
                Element mobileDevice = (Element) mobileDeviceNode;
                String classname = mobileDevice.getAttribute("classname");
                if (classname == null) {
                // may never arrived
                } else if (classname.equals("com.twinsoft.convertigo.beans.mobiledevices.Android")) {
                    hasAndroid = true;
                } else if (classname.startsWith("com.twinsoft.convertigo.beans.mobiledevices.IP")) {
                    hasIOs = true;
                }
                projectNode.removeChild(mobileDevice);
            }
            if (hasAndroid) {
                mobileApplicationElement.appendChild(new Android().toXml(document));
            }
            if (hasIOs) {
                mobileApplicationElement.appendChild(new IOs().toXml(document));
            }
            if (hasAndroid && hasIOs) {
                mobileApplicationElement.appendChild(new WindowsPhone8().toXml(document));
                mobileApplicationElement.appendChild(new Windows().toXml(document));
            }
            String projectName = "" + XMLUtils.findPropertyValue(projectNode, "name");
            File mobileFolder = new File(Engine.projectDir(projectName) + "/DisplayObjects/mobile");
            if (mobileFolder.exists()) {
                FileUtils.write(new File(mobileFolder, "mobile_project_migrated.txt"), "Your mobile project has been migrated.\n" + "Now, we make per platform configuration and resources.\n" + "You may customize your config.xml (the existing one will never used) and dispatch your existing specific resources per platform (see the 'platforms' folder).\n" + "You can delete this file after reading it.", "UTF-8");
            }
        }
    } catch (Exception e) {
        throw new EngineException("[Migration 7.0.0] Unable to migrate project", e);
    }
    return projectNode;
}
Also used : WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) NodeList(org.w3c.dom.NodeList) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EngineException(com.twinsoft.convertigo.engine.EngineException) Windows(com.twinsoft.convertigo.beans.mobileplatforms.Windows) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) EngineException(com.twinsoft.convertigo.engine.EngineException) IOException(java.io.IOException) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android) MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) File(java.io.File)

Example 3 with Android

use of com.twinsoft.convertigo.beans.mobileplatforms.Android in project convertigo by convertigo.

the class BuildLocally method installCordova.

public Status installCordova() {
    try {
        File resourceFolder = mobilePlatform.getResourceFolder();
        File configFile = new File(resourceFolder, "config.xml");
        Document doc = XMLUtils.loadXml(configFile);
        TwsCachedXPathAPI xpathApi = new TwsCachedXPathAPI();
        Element singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='nodejs-version']");
        String nodeVersion = (singleElement != null) ? singleElement.getAttribute("value") : ProcessUtils.getDefaultNodeVersion();
        nodeDir = ProcessUtils.getNodeDir(nodeVersion, (r, t, x) -> {
            Engine.logEngine.info("Downloading nodejs " + nodeVersion + ": " + Math.round((r * 100f) / t) + "%");
        });
        Engine.logEngine.info("Checking if nodejs and npm are installed.");
        List<String> parameters = new LinkedList<String>();
        parameters.add("--version");
        String npmVersion = runCommand(resourceFolder, "npm", parameters, false);
        Pattern pattern = Pattern.compile("^([0-9])+\\.([0-9])+\\.([0-9])+$");
        Matcher matcher = pattern.matcher(npmVersion);
        if (!matcher.find()) {
            throw new Exception("node.js is not installed ('npm --version' returned '" + npmVersion + "')\nYou must download nodes.js from https://nodejs.org/en/download/");
        }
        Engine.logEngine.info("OK, nodejs (" + nodeVersion + ") and npm (" + npmVersion + ") are installed.");
        singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='prefered-android-build-tools']");
        if (singleElement != null && singleElement.hasAttribute("value")) {
            preferedAndroidBuildTools = singleElement.getAttribute("value");
        }
        Engine.logEngine.info("Checking if this cordova version is already installed.");
        singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='cordova-version']");
        if (singleElement == null) {
            singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/preference[@name='phonegap-version']");
        }
        if (singleElement != null) {
            String cliVersion = singleElement.getAttribute("value");
            if (cliVersion != null) {
                pattern = Pattern.compile("^(?:cli-)?([0-9]+\\.[0-9]+\\.[0-9]+)$");
                matcher = pattern.matcher(cliVersion);
                if (!matcher.find()) {
                    throw new Exception("The cordova version is specified but its value has not the right format.");
                }
                // Remove 'cli-' from 'cli-x.x.x'
                cliVersion = matcher.group(1);
                String cordovaInstallPath = BuildLocally.cordovaInstallsPath + File.separator + "cordova" + cliVersion;
                File cordovaBinFile = new File(cordovaInstallPath + File.separator + "node_modules" + File.separator + "cordova" + File.separator + "bin" + File.separator + "cordova");
                // If cordova is not installed
                if (!cordovaBinFile.exists()) {
                    Engine.logEngine.info("Installing cordova " + cliVersion + " This can take some time....");
                    File cordovaInstallDir = new File(cordovaInstallPath);
                    cordovaInstallDir.mkdir();
                    parameters = new LinkedList<String>();
                    parameters.add("--prefix");
                    parameters.add(cordovaInstallDir.getAbsolutePath());
                    parameters.add("--unsafe-perm=true");
                    parameters.add("install");
                    parameters.add("cordova@" + cliVersion);
                    this.runCommand(cordovaInstallDir, "npm", parameters, true);
                }
                Engine.logEngine.info("Cordova (" + cliVersion + ") is now installed.");
                this.cordovaBinPath = cordovaBinFile.getAbsolutePath();
            } else {
                Engine.logEngine.info("The phonegap-version prefrence version is not specified in config.xml.");
                throw new Exception("The phonegap-version prefrence version is not specified in config.xml.");
            }
        } else {
            throw new Exception("The phonegap-version preference not found in config.xml.");
        }
    } catch (Throwable e) {
        Engine.logEngine.info("Error when installing Cordova: " + e);
        logException(e, "Error when installing Cordova");
        return Status.CANCEL;
    }
    return Status.OK;
}
Also used : TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) Attr(org.w3c.dom.Attr) Matcher(java.util.regex.Matcher) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) NamedNodeMap(org.w3c.dom.NamedNodeMap) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) Windows(com.twinsoft.convertigo.beans.mobileplatforms.Windows) Engine(com.twinsoft.convertigo.engine.Engine) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) File(java.io.File) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android) List(java.util.List) Element(org.w3c.dom.Element) NodeIterator(org.w3c.dom.traversal.NodeIterator) MobileResourceHelper(com.twinsoft.convertigo.engine.admin.services.mobiles.MobileResourceHelper) XMLUtils(com.twinsoft.convertigo.engine.util.XMLUtils) MobilePlatform(com.twinsoft.convertigo.beans.core.MobilePlatform) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) EngineException(com.twinsoft.convertigo.engine.EngineException) Collections(java.util.Collections) ProcessUtils(com.twinsoft.convertigo.engine.util.ProcessUtils) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) File(java.io.File) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Example 4 with Android

use of com.twinsoft.convertigo.beans.mobileplatforms.Android in project convertigo by convertigo.

the class BuildLocally method processConfigXMLResources.

/**
 * Explore "config.xml", handle plugins and copy needed resources to appropriate platforms folders.
 * @param wwwDir
 * @param platform
 * @param cordovaDir
 */
private void processConfigXMLResources(File wwwDir, File cordovaDir) throws Throwable {
    try {
        File configFile = new File(cordovaDir, "config.xml");
        Document doc = XMLUtils.loadXml(configFile);
        TwsCachedXPathAPI xpathApi = new TwsCachedXPathAPI();
        // Changes icons and splashs src in config.xml file because it was moved to the parent folder
        NodeIterator nodeIterator = xpathApi.selectNodeIterator(doc, "//*[local-name()='splash' or local-name()='icon']");
        Element singleElement = (Element) nodeIterator.nextNode();
        while (singleElement != null) {
            String src = singleElement.getAttribute("src");
            src = "www/" + src;
            File file = new File(cordovaDir, src);
            if (file.exists()) {
                singleElement.setAttribute("src", src);
            }
            singleElement = (Element) nodeIterator.nextNode();
        }
        // ANDROID
        if (mobilePlatform instanceof Android) {
            singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/name");
            if (singleElement != null) {
                String name = singleElement.getTextContent();
                name = name.replace("\\", "\\\\");
                name = name.replace("'", "\\'");
                name = name.replace("\"", "\\\"");
                singleElement.setTextContent(name);
            }
        }
        // WINPHONE
        if (mobilePlatform instanceof WindowsPhone8) {
            // Without these width and height the local build doesn't work but with these the remote build doesn't work
            singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/platform[@name='wp8']/icon[not(@role)]");
            if (singleElement != null) {
                singleElement.setAttribute("width", "99");
                singleElement.setAttribute("height", "99");
            }
            singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/platform[@name='wp8']/icon[@role='background']");
            if (singleElement != null) {
                singleElement.setAttribute("width", "159");
                singleElement.setAttribute("height", "159");
            }
            // /widget/platform[@name='wp8']/splash
            singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/platform/splash");
            if (singleElement != null) {
                singleElement.setAttribute("width", "768");
                singleElement.setAttribute("height", "1280");
            }
            singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/plugin[@name='phonegap-plugin-push']/param[@name='SENDER_ID']");
            if (singleElement != null) {
                // Remote build needs a node named 'param' and local build needs a node named 'variable'
                singleElement.getParentNode().appendChild(cloneNode(singleElement, "variable"));
                singleElement.getParentNode().removeChild(singleElement);
            }
        }
        // XMLUtils.saveXml(doc, configFile.getAbsolutePath());
        // We have to add the root config.xml all our app's config.xml preferences.
        // Cordova will use this file to generates the platform specific config.xml
        // Get preferences from current config.xml
        NodeIterator preferences = xpathApi.selectNodeIterator(doc, "//preference");
        // File configFile = new File(cordovaDir, "config.xml");
        // doc = XMLUtils.loadXml(configFile);  // The root config.xml
        NodeList preferencesList = doc.getElementsByTagName("preference");
        // Remove old preferences
        while (preferencesList.getLength() > 0) {
            Element pathNode = (Element) preferencesList.item(0);
            // Remove empty lines
            Node prev = pathNode.getPreviousSibling();
            if (prev != null && prev.getNodeType() == Node.TEXT_NODE && prev.getNodeValue().trim().length() == 0) {
                doc.getDocumentElement().removeChild(prev);
            }
            doc.getDocumentElement().removeChild(pathNode);
        }
        for (Element preference = (Element) preferences.nextNode(); preference != null; preference = (Element) preferences.nextNode()) {
            String name = preference.getAttribute("name");
            String value = preference.getAttribute("value");
            Element elt = doc.createElement("preference");
            elt.setAttribute("name", name);
            elt.setAttribute("value", value);
            Engine.logEngine.info("Adding preference'" + name + "' with value '" + value + "'");
            doc.getDocumentElement().appendChild(elt);
        }
        Engine.logEngine.trace("New config.xml is: " + XMLUtils.prettyPrintDOM(doc));
        File resXmlFile = new File(cordovaDir, "config.xml");
        // FileUtils.deleteQuietly(resXmlFile);
        XMLUtils.saveXml(doc, resXmlFile.getAbsolutePath());
    // Last part, as all resources has been copied to the correct location, we can remove
    // our "www/res" directory before packaging to save build time and size...
    // FileUtils.deleteDirectory(new File(wwwDir, "res"));
    } catch (Exception e) {
        logException(e, "Unable to process config.xml in your project, check the file's validity");
    }
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) File(java.io.File) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android)

Example 5 with Android

use of com.twinsoft.convertigo.beans.mobileplatforms.Android in project convertigo by convertigo.

the class BuildLocally method getAbsolutePathOfBuiltFile.

/**
 * Return the absolute path of built application file
 * @param mobilePlatform
 * @param buildMode
 * @return
 */
protected File getAbsolutePathOfBuiltFile(MobilePlatform mobilePlatform, String buildMode) {
    String cordovaPlatform = mobilePlatform.getCordovaPlatform();
    String builtPath = File.separator + "platforms" + File.separator + cordovaPlatform + File.separator;
    String buildMd = buildMode.equals("debug") ? "Debug" : "Release";
    String extension = "";
    File f = new File(getCordovaDir(), builtPath);
    if (f.exists()) {
        // Android
        if (mobilePlatform instanceof Android) {
            builtPath = builtPath + "ant-build" + File.separator;
            File f2 = new File(getCordovaDir(), builtPath);
            if (!f2.exists()) {
                builtPath = File.separator + "platforms" + File.separator + cordovaPlatform + File.separator + "build" + File.separator + "outputs" + File.separator + "apk" + File.separator;
            }
            extension = "apk";
        // iOS
        } else if (mobilePlatform instanceof IOs) {
            extension = "xcodeproj";
        // Windows Phone 8
        } else if (mobilePlatform instanceof WindowsPhone8) {
            builtPath = builtPath + "Bin" + File.separator + buildMd + File.separator;
            extension = "xap";
        // Windows 8
        } else if (mobilePlatform instanceof Windows) {
        // TODO : Handle Windows 8
        } else {
            return null;
        }
    }
    f = new File(getCordovaDir(), builtPath);
    if (f.exists()) {
        String[] filesNames = f.list();
        int i = filesNames.length - 1;
        boolean find = false;
        while (i > 0 && !find && !extension.isEmpty()) {
            String fileName = filesNames[i];
            if (fileName.endsWith(extension)) {
                builtPath += fileName;
                find = true;
            }
            i--;
        }
    } else {
        builtPath = File.separator + "platforms" + File.separator + cordovaPlatform + File.separator;
    }
    return new File(getCordovaDir(), builtPath);
}
Also used : WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) Windows(com.twinsoft.convertigo.beans.mobileplatforms.Windows) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) File(java.io.File) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android)

Aggregations

Android (com.twinsoft.convertigo.beans.mobileplatforms.Android)8 IOs (com.twinsoft.convertigo.beans.mobileplatforms.IOs)7 File (java.io.File)7 WindowsPhone8 (com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8)6 MobileApplication (com.twinsoft.convertigo.beans.core.MobileApplication)5 Element (org.w3c.dom.Element)5 Node (org.w3c.dom.Node)5 MobilePlatform (com.twinsoft.convertigo.beans.core.MobilePlatform)4 Windows (com.twinsoft.convertigo.beans.mobileplatforms.Windows)4 EngineException (com.twinsoft.convertigo.engine.EngineException)4 TwsCachedXPathAPI (com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)4 IOException (java.io.IOException)4 Document (org.w3c.dom.Document)4 NodeList (org.w3c.dom.NodeList)4 LinkedList (java.util.LinkedList)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 NodeIterator (org.w3c.dom.traversal.NodeIterator)3 Engine (com.twinsoft.convertigo.engine.Engine)2 MobileResourceHelper (com.twinsoft.convertigo.engine.admin.services.mobiles.MobileResourceHelper)2