def createSplash(self, file_db, portrait=True):
        configxml=parseConfigXML(os.path.join(self.aplication_folder, 'config.xml'))

        if file_db:
            if portrait:
                splash_sizes={
                'screen-hdpi-portrait':(480, 800),
                'screen-ldpi-portrait':(200, 320),
                'screen-mdpi-portrait':(320, 480),
                'screen-xhdpi-portrait':(720, 1280)
                }
            else:
                splash_sizes={
                'screen-hdpi-landscape':(800, 480),
                'screen-ldpi-landscape':(320, 200),
                'screen-mdpi-landscape':(480, 320),
                'screen-xhdpi-landscape':(1280, 720)
                }                
            for x in splash_sizes.keys():
                file_name = '%s.png' % x
                imagem=self._processImg(file_db, splash_sizes[x][0],  splash_sizes[x][1])
                if imagem:
                    end_arquivo=os.path.join(self.aplication_folder, 'res', 'screen', 'android', file_name)
                    print(end_arquivo)
                    imagem.save(end_arquivo)
            if portrait:
                configxml.addSplash(portrait=True)
            else:
                configxml.addSplash(portrait=False)
            configxml.save()
        else:
            print("File don't exist")
    def _prepareTheEnvironment(self, buildHtml=True):
        request = self.request

        print('Prepare Environment')
        if not os.path.exists(self.cordova_app_folder):
            print('Creating Folder: %s' % self.cordova_app_folder)
            os.makedirs(self.cordova_app_folder)
        if platform == "win32" or platform == 'cygwin':
            if not os.path.exists(os.path.join(self.cordova_app_folder, '%s_create_app.bat' % self.aplication_name)):
                print("Creating file %s_create_app.bat" % self.aplication_name)
                with open(os.path.join(self.cordova_app_folder, '%s_create_app.bat' % self.aplication_name), 'w') as file_opened:
                    content = "cd %s\ncordova create %s %s %s" % (
                        self.cordova_app_folder, self.aplication_name, 'com.yoursite.yourapp', self.aplication_name)
                    try:
                        content=content.decode('utf-8')
                    except:
                        pass                    
                    file_opened.write(content)
            print("Executing: %s_create_app" %self.aplication_name)
            subprocess.call([os.path.join(self.cordova_app_folder, '%s_create_app.bat' %
                                          self.aplication_name)], stdout=subprocess.PIPE, shell=True, stdin=subprocess.PIPE)

            if not os.path.exists(os.path.join(self.cordova_app_folder, '%s_platform_browser.bat' % self.aplication_name)):
                print("Creating file %s_platform_browser.bat" % self.aplication_name)
                with open(os.path.join(self.cordova_app_folder, '%s_platform_browser.bat' % self.aplication_name), 'w') as file_opened:
                    content = "cd %s\ncordova platform add browser" %(self.aplication_folder)
                    try:
                        content=content.decode('utf-8')
                    except:
                        pass                    
                    file_opened.write(content)
            print("Executing: %s_platform_browser.bat" %self.aplication_name)
            subprocess.call([os.path.join(self.cordova_app_folder, '%s_platform_browser.bat' %
                                          self.aplication_name)], shell=True)
            configxml=parseConfigXML(os.path.join(self.aplication_folder, 'config.xml'))
            if not configxml.checkPlugin('cordova-plugin-splashscreen'):
                with open(os.path.join(self.cordova_app_folder, '%s_add_plugin_splashscreen.bat' % self.aplication_name), 'w') as file_opened:
                    content = "cd %s\n" %os.path.join(self.aplication_folder)
                    content+="cordova plugin add cordova-plugin-splashscreen\n"
                    try:
                        content=content.decode('utf-8')
                    except:
                        pass
                    file_opened.write(content)
                procs=subprocess.call([os.path.join(self.cordova_app_folder, '%s_add_plugin_splashscreen.bat' %
                                                  self.aplication_name)])

        elif platform == "linux" or platform == "linux2":
            if not os.path.exists(self.aplication_folder):
                subprocess.call(["cordova create %s %s %s" % (os.path.join(self.cordova_app_folder, self.aplication_name), 'com.yoursite.yourapp', self.aplication_name)], cwd=self.cordova_app_folder, shell=True)
                subprocess.call(["cordova platform add browser"], cwd=self.aplication_folder, shell=True)
                subprocess.call(["cordova plugin add cordova-plugin-splashscreen"], cwd=self.aplication_folder, shell=True)

        if buildHtml:
            self._buildhtml()
 def createIcon(self, file_db):
     if file_db:
         icons_sizes={'icon-36-ldpi':(36, 36),
         'icon-48-mdpi':(48, 48),
         'icon-72-hdpi':(72, 72),
         'icon-96-xhdpi':(96, 96)
         }
         for x in icons_sizes.keys():
             file_name = '%s.png' % x
             imagem=self._processImg(file_db, icons_sizes[x][0],  icons_sizes[x][1])
             if imagem:
                 end_arquivo=os.path.join(self.aplication_folder, 'res', 'icon', 'android', file_name)
                 print(end_arquivo)
                 imagem.save(end_arquivo)
         configxml=parseConfigXML(os.path.join(self.aplication_folder, 'config.xml'))
         configxml.addIcons()
         configxml.save()
     else:
         print("File dont exist")
    def createApk(self, level="debug"):
        """
        @level: create apk debug if setted 'debug', create apk release if setted 'release'
        @include_key: is include in apk if keystore exists
        """
        self._buildhtml(for_apk=True)
        if os.path.exists(os.path.join(self.aplication_folder, 'package.json')):
            self._remove_file(os.path.join(self.aplication_folder, 'package.json'))
        configxml=parseConfigXML(os.path.join(self.aplication_folder, 'config.xml'))
        engine=configxml.checkEngine('android')

        outputfilebase=os.path.join(self.aplication_folder,'platforms', 'android', 'build', 'outputs', 'apk')
        if platform == "win32" or platform == 'cygwin':
            if not engine or not os.path.exists(os.path.join(self.aplication_folder,'platforms', 'android')):
                with open(os.path.join(self.cordova_app_folder, '%s_create_apk_build.bat' % self.aplication_name), 'w') as file_opened:
                    content = "cd %s\n" %os.path.join(self.aplication_folder)
                    content+="cordova platform add android\n"
                    try:
                        content=content.decode('utf-8')
                    except:
                        pass
                    file_opened.write(content)
                print("Inserting Android Platform")
                try:
                    apk1=subprocess.call([os.path.join(self.cordova_app_folder, '%s_create_apk_build.bat' %
                                                  self.aplication_name)])
                except Exception as e:
                    print(e)

            with open(os.path.join(self.cordova_app_folder, '%s_create_apk_release.bat' % self.aplication_name), 'w') as file_opened:
                content = "cd %s\n" %os.path.join(self.aplication_folder)
                if level=='release':
                    if self._created_key:
                        outputfile=os.path.join(outputfilebase, 'android-release.apk')
                        content+="cordova build android --verbose --release -- --keystore=\"%s\" --storePassword=%s --alias=%s --password=%s" %(
                            self._created_key[0],self._created_key[1],self._created_key[2], self._created_key[3])
                        # with open(os.path.join(self.aplication_folder, 'platforms', 'android', 'gradle.properties'), 'w') as file_opened2:

                        #     content2="storeFile=%s\nstorePassword=%s\nstoreType=pkcs12\n" %(self._created_key[0], self._created_key[1])
                        #             "keyAlias=%s\nkeyPassword=%(keypass)s" %(self._created_key[2], )
                    else:
                        outputfile=os.path.join(outputfilebase, 'android-release-unsigned.apk')
                        content+="cordova build android --verbose --release\n"
                else:
                    outputfile=os.path.join(outputfilebase, 'android-debug.apk')
                    content+="cordova build android --verbose\n"
                try:
                    content=content.decode('utf-8')
                except:
                    pass                
                file_opened.write(content)

            procs=subprocess.Popen([os.path.join(self.cordova_app_folder, '%s_create_apk_release.bat' %
                                              self.aplication_name)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            print('\n\n\n####################################################')
            print('# Creating APK file')
            print("# PID: ", procs.pid)
            print('#####################################################\n\n\n')
            while True:
                time.sleep(.1) # so i don't kill the cpu on the machine i'm checking
                try:
                    output = procs.stdout.readline()
                except Exception as e:
                    print(e)
                    break
                if output:
                    print(output.strip())
                    if 'BUILD SUCCESSFUL' in str(output.strip()):
                        for proc in psutil.process_iter():
                            if proc.name()=='java.exe':
                                proc.kill()
                        break

            saida=procs.stdout.read()

        elif platform == "linux" or platform == "linux2":
            if not engine:
                subprocess.call(["cordova platform add android"], cwd=self.aplication_folder, shell=True)
            if level=='release':
                if self._created_key:
                    subprocess.call(["cordova build android --verbose --release -- --keystore=\"%s\" --storePassword=%s --alias=%s" %(
                            self._created_key[0],self._created_key[1],self._created_key[2]
                        )], cwd=self.aplication_folder, shell=True)
                else:
                    subprocess.call(['cordova build android --verbose --release'], cwd=self.aplication_folder, shell=True)
            else:
                subprocess.call(['cordova build android --verbose'], cwd=self.aplication_folder, shell=True)
        print("Done!")
    def openServer(self, server=None):
        """
            @server: 'phonegap' or 'cordova'
            In linux the server will have to be opened manually, 
            so the method will try to find the process and the port.
        """
        request = self.request
        if server:
            self.server_chosen = server
        print("Open %s server..." % self.server_chosen)
        procs = self._examine_process()
        if procs:
            self.port = procs['port']
            print('Server is run in door %s' % procs['port'])
        else:
            port = 3000
            def myrange():
                cont=3000
                while cont<4000:
                    cont+=1
                    yield cont
            for y in myrange():
                if y in self.ports:
                    pass
                else:
                    self.ports.append(y)
                    port = y
                    break
            self.port = port
            if platform == "win32" or platform == 'cygwin':
                configxml=parseConfigXML(os.path.join(self.aplication_folder, 'config.xml'))
                engine=configxml.checkEngine('browser')
                if not engine or not os.path.exists(os.path.join(self.aplication_folder,'platforms', 'browser')):
                    with open(os.path.join(self.cordova_app_folder, '%s_platform_add_browser.bat' % self.aplication_name), 'w') as file_opened:
                        content = "cd %s\n" %os.path.join(self.aplication_folder)
                        content+="cordova platform add browser\n"
                        try:
                            content=content.decode('utf-8')
                        except:
                            pass
                        file_opened.write(content)
                    print("Inserting Browser Platform")
                    try:
                        subprocess.call([os.path.join(self.cordova_app_folder, '%s_platform_add_browser.bat' %
                                                      self.aplication_name)])
                    except Exception as e:
                        print(e)
                if self.server_chosen == 'phonegap':
                    with open(os.path.join(self.cordova_app_folder, '%s_server_%s_run.bat' %(self.aplication_name, self.server_chosen)), 'w') as file_opened:
                        content = "cd %s\nphonegap serve -p%s" % (
                            self.aplication_folder, port)
                        try:
                            content=content.decode('utf-8')
                        except:
                            pass
                        file_opened.write(content)
                else:
                    with open(os.path.join(self.cordova_app_folder, '%s_server_%s_run.bat' %(self.aplication_name, self.server_chosen)), 'w') as file_opened:
                        content = "cd %s\ncordova serve %s" % (
                            self.aplication_folder, port)
                        try:
                            content=content.decode('utf-8')
                        except:
                            pass
                        file_opened.write(content)
                processo = subprocess.Popen([os.path.join(
                    self.cordova_app_folder, '%s_server_%s_run.bat' %(self.aplication_name, self.server_chosen))], shell=True)
            elif platform == "linux" or platform == "linux2":

                processo = subprocess.Popen(['%S serve %s' %(self.server_chosen, port)], shell=True, cwd=self.aplication_folder)
            proc = psutil.Process(processo.pid)
            while not proc.children():
                print("Try open server %s run on door %s" % (self.server_chosen, port))
                time.sleep(1)
            cont=50
            while cont<50:
                cont+=1
                print("Try conect (open url) to 'http://%s:%s'..." %(self.request.env.http_host.split(':')[0], port),)
                try:
                    urlopen('http://%s:%s' %(self.request.env.http_host.split(':')[0], port))
                    break
                except Exception as e:
                    print("Fail! be patient! Try again...")
                    time.sleep(3)

            print("\n================================\n  Conected\n-------------------------------\n")