Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / customize.py
index 84b5fe6..eb2d7b6 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# Copyright (c) 2013 Intel Corporation. All rights reserved.
+# Copyright (c) 2013,2014 Intel Corporation. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
@@ -25,10 +25,13 @@ from handle_xml import AddElementAttributeAndText
 from handle_xml import EditElementAttribute
 from handle_xml import EditElementValueByNodeName
 from handle_permissions import HandlePermissions
+from util import CleanDir, CreateAndCopyDir, GetBuildDir
 from xml.dom import minidom
 
+
 TEMPLATE_DIR_NAME = 'template'
 
+
 def VerifyPackageName(value):
   regex = r'^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$'
   descrpt = 'Each part of package'
@@ -64,7 +67,7 @@ def ReplaceInvalidChars(value, mode='default'):
     invalid_chars = '\/:.*?"<>|- '
   for c in invalid_chars:
     if mode == 'apkname' and c in value:
-      print("Illegal character: '%s' is replaced with '_'" % c)
+      print('Illegal character: "%s" replaced with "_"' % c)
     value = value.replace(c, '_')
   return value
 
@@ -115,25 +118,28 @@ def Prepare(app_info, compressor):
   """Copy the Android template project to a new app project
      named app_info.app_name
   """
-  # create new app_dir in xwalk_dir
+  # create new app_dir in temp dir
   app_name = app_info.android_name
-  app_dir = os.path.join(xwalk_dir, app_name)
+  app_dir = GetBuildDir(app_name)
   app_package = app_info.package
   app_root = app_info.app_root
   template_app_dir = os.path.join(xwalk_dir, TEMPLATE_DIR_NAME)
 
   # 1) copy template project to app_dir
-  if os.path.exists(app_dir):
-    shutil.rmtree(app_dir)
+  CleanDir(app_dir)
+  if not os.path.isdir(template_app_dir):
+    print('Error: The template directory could not be found (%s).' %
+          template_app_dir)
+    sys.exit(7)
   shutil.copytree(template_app_dir, app_dir)
 
   # 2) replace app_dir 'src' dir with template 'src' dir
-  shutil.rmtree(os.path.join(app_dir, 'src'))
+  CleanDir(os.path.join(app_dir, 'src'))
   template_src_root = os.path.join(template_app_dir, 'src', 'org', 'xwalk',
                                    'app', 'template')
 
   # 3) Create directory tree from app package (org.xyz.foo -> src/org/xyz/foo)
-  #    and copy AppTemplateActivity.java to <app_name>Activity.java 
+  #    and copy AppTemplateActivity.java to <app_name>Activity.java
   template_activity_file = os.path.join(template_src_root,
                                         'AppTemplateActivity.java')
   if not os.path.isfile(template_activity_file):
@@ -151,8 +157,7 @@ def Prepare(app_info, compressor):
   # 4) Copy all HTML source from app_root to app_dir
   if app_root:
     app_assets_dir = os.path.join(app_dir, 'assets', 'www')
-    if os.path.isdir(app_assets_dir):
-      shutil.rmtree(app_assets_dir)
+    CleanDir(app_assets_dir)
     shutil.copytree(app_root, app_assets_dir)
     if compressor:
       CompressSourceFiles(app_assets_dir, compressor)
@@ -168,7 +173,8 @@ def EncodingUnicodeValue(value):
 
 
 def CustomizeStringXML(name, description):
-  strings_path = os.path.join(xwalk_dir, name, 'res', 'values', 'strings.xml')
+  strings_path = os.path.join(GetBuildDir(name), 'res', 'values',
+                              'strings.xml')
   if not os.path.isfile(strings_path):
     print ('Please make sure strings_xml'
            ' exists under template folder.')
@@ -185,7 +191,7 @@ def CustomizeStringXML(name, description):
 
 
 def CustomizeThemeXML(name, fullscreen, manifest):
-  theme_path = os.path.join(xwalk_dir, name, 'res', 'values-v14', 'theme.xml')
+  theme_path = os.path.join(GetBuildDir(name), 'res', 'values-v14', 'theme.xml')
   if not os.path.isfile(theme_path):
     print('Error: theme.xml is missing in the build tool.')
     sys.exit(6)
@@ -211,6 +217,7 @@ def CustomizeXML(app_info, description, icon_dict, manifest, permissions):
   orientation = app_info.orientation
   package = app_info.package
   app_name = app_info.app_name
+  app_dir = GetBuildDir(name)
   # Chinese character with unicode get from 'manifest.json' will cause
   # 'UnicodeEncodeError' when finally wrote to 'AndroidManifest.xml'.
   app_name = EncodingUnicodeValue(app_name)
@@ -219,7 +226,7 @@ def CustomizeXML(app_info, description, icon_dict, manifest, permissions):
   # append a space before '@' or '?' to fix that.
   if app_name.startswith('@') or app_name.startswith('?'):
     app_name = ' ' + app_name
-  manifest_path = os.path.join(xwalk_dir, name, 'AndroidManifest.xml')
+  manifest_path = os.path.join(app_dir, 'AndroidManifest.xml')
   if not os.path.isfile(manifest_path):
     print ('Please make sure AndroidManifest.xml'
            ' exists under template folder.')
@@ -251,7 +258,7 @@ def CustomizeXML(app_info, description, icon_dict, manifest, permissions):
     EditElementAttribute(xmldoc, 'application', 'android:icon',
                          '@drawable/%s' % icon_name)
 
-  file_handle = open(os.path.join(xwalk_dir, name, 'AndroidManifest.xml'), 'w')
+  file_handle = open(os.path.join(app_dir, 'AndroidManifest.xml'), 'w')
   xmldoc.writexml(file_handle, encoding='utf-8')
   file_handle.close()
 
@@ -268,7 +275,7 @@ def ReplaceString(file_path, src, dest):
 
 def SetVariable(file_path, string_line, variable, value):
   function_string = ('%sset%s(%s);\n' %
-                    ('        ', variable, value))
+                     ('        ', variable, value))
   temp_file_path = file_path + '.backup'
   file_handle = open(temp_file_path, 'w+')
   for line in open(file_path):
@@ -282,12 +289,12 @@ def SetVariable(file_path, string_line, variable, value):
 def CustomizeJava(app_info, app_url, app_local_path, keep_screen_on):
   name = app_info.android_name
   package = app_info.package
-  app_pkg_dir = os.path.join(xwalk_dir, name, 'src',
-                             package.replace('.', os.path.sep))
+  app_dir = GetBuildDir(name)
+  app_pkg_dir = os.path.join(app_dir, 'src', package.replace('.', os.path.sep))
   dest_activity = os.path.join(app_pkg_dir, name + 'Activity.java')
   ReplaceString(dest_activity, 'org.xwalk.app.template', package)
   ReplaceString(dest_activity, 'AppTemplate', name)
-  manifest_file = os.path.join(xwalk_dir, name, 'assets/www', 'manifest.json')
+  manifest_file = os.path.join(app_dir, 'assets', 'www', 'manifest.json')
   if os.path.isfile(manifest_file):
     ReplaceString(
         dest_activity,
@@ -299,8 +306,7 @@ def CustomizeJava(app_info, app_url, app_local_path, keep_screen_on):
         ReplaceString(dest_activity, 'file:///android_asset/www/index.html',
                       app_url)
     elif app_local_path:
-      if os.path.isfile(os.path.join(xwalk_dir, name, 'assets/www',
-                                     app_local_path)):
+      if os.path.isfile(os.path.join(app_dir, 'assets', 'www', app_local_path)):
         ReplaceString(dest_activity, 'file:///android_asset/www/index.html',
                       'app://' + package + '/' + app_local_path)
       else:
@@ -312,6 +318,10 @@ def CustomizeJava(app_info, app_url, app_local_path, keep_screen_on):
     SetVariable(dest_activity,
                 'public void onCreate(Bundle savedInstanceState)',
                 'RemoteDebugging', 'true')
+  if app_info.use_animatable_view:
+    SetVariable(dest_activity,
+                'public void onCreate(Bundle savedInstanceState)',
+                'UseAnimatableView', 'true')
   if app_info.fullscreen_flag:
     SetVariable(dest_activity,
                 'super.onCreate(savedInstanceState)',
@@ -329,7 +339,7 @@ def CopyExtensionFile(extension_name, suffix, src_path, dest_path):
   dest_extension_path = os.path.join(dest_path, extension_name)
   if os.path.exists(dest_extension_path):
     # TODO: Refine it by renaming it internally.
-    print('Error: duplicated extension names "%s" are found. Please rename it.'
+    print('Error: duplicate extension names were found (%s). Please rename it.'
           % extension_name)
     sys.exit(9)
   else:
@@ -364,12 +374,12 @@ def CustomizeExtensions(app_info, extensions):
   if not extensions:
     return
   name = app_info.android_name
-  apk_path = os.path.join(xwalk_dir, name)
-  apk_assets_path = os.path.join(apk_path, 'assets')
+  app_dir = GetBuildDir(name)
+  apk_assets_path = os.path.join(app_dir, 'assets')
   extensions_string = 'xwalk-extensions'
 
   # Set up the target directories and files.
-  dest_jar_path = os.path.join(apk_path, extensions_string)
+  dest_jar_path = os.path.join(app_dir, extensions_string)
   os.mkdir(dest_jar_path)
   dest_js_path = os.path.join(apk_assets_path, extensions_string)
   os.mkdir(dest_js_path)
@@ -381,7 +391,7 @@ def CustomizeExtensions(app_info, extensions):
   extension_json_list = []
   for source_path in extension_paths:
     if not os.path.exists(source_path):
-      print('Error: can\'t find the extension directory \'%s\'.' % source_path)
+      print('Error: can not find the extension directory \'%s\'.' % source_path)
       sys.exit(9)
     # Remove redundant separators to avoid empty basename.
     source_path = os.path.normpath(source_path)
@@ -397,7 +407,7 @@ def CustomizeExtensions(app_info, extensions):
     file_name = extension_name + '.json'
     src_file = os.path.join(source_path, file_name)
     if not os.path.isfile(src_file):
-      print('Error: %s is not found in %s.' % (file_name, source_path))
+      print('Error: %s was not found in %s.' % (file_name, source_path))
       sys.exit(9)
     else:
       src_file_handle = open(src_file)
@@ -416,7 +426,7 @@ def CustomizeExtensions(app_info, extensions):
       json_output['jsapi'] = js_path_prefix + json_output['jsapi']
       extension_json_list.append(json_output)
       # Merge the permissions of extensions into AndroidManifest.xml.
-      manifest_path = os.path.join(xwalk_dir, name, 'AndroidManifest.xml')
+      manifest_path = os.path.join(app_dir, 'AndroidManifest.xml')
       xmldoc = minidom.parse(manifest_path)
       if ('permissions' in json_output):
         # Get used permission list to avoid repetition as "--permissions"
@@ -449,13 +459,14 @@ def CustomizeExtensions(app_info, extensions):
 def GenerateCommandLineFile(app_info, xwalk_command_line):
   if xwalk_command_line == '':
     return
-  assets_path = os.path.join(xwalk_dir, app_info.android_name, 'assets')
+  assets_path = os.path.join(GetBuildDir(app_info.android_name), 'assets')
   file_path = os.path.join(assets_path, 'xwalk-command-line')
   command_line_file = open(file_path, 'w')
   command_line_file.write('xwalk ' + xwalk_command_line)
 
 
 def CustomizeIconByDict(name, app_root, icon_dict):
+  app_dir = GetBuildDir(name)
   icon_name = None
   drawable_dict = {'ldpi': [1, 37], 'mdpi': [37, 72], 'hdpi': [72, 96],
                    'xhdpi': [96, 120], 'xxhdpi': [120, 144],
@@ -473,7 +484,7 @@ def CustomizeIconByDict(name, app_root, icon_dict):
     for kd, vd in drawable_dict.items():
       for item in icon_list:
         if item[0] >= vd[0] and item[0] < vd[1]:
-          drawable_path = os.path.join(xwalk_dir, name, 'res', 'drawable-' + kd)
+          drawable_path = os.path.join(app_dir, 'res', 'drawable-' + kd)
           if not os.path.exists(drawable_path):
             os.makedirs(drawable_path)
           icon = os.path.join(app_root, item[1])
@@ -492,7 +503,7 @@ def CustomizeIconByDict(name, app_root, icon_dict):
 
 def CustomizeIconByOption(name, icon):
   if os.path.isfile(icon):
-    drawable_path = os.path.join(xwalk_dir, name, 'res', 'drawable')
+    drawable_path = os.path.join(GetBuildDir(name), 'res', 'drawable')
     if not os.path.exists(drawable_path):
       os.makedirs(drawable_path)
     icon_file = os.path.basename(icon)
@@ -501,7 +512,7 @@ def CustomizeIconByOption(name, icon):
     icon_name = os.path.splitext(icon_file)[0]
     return icon_name
   else:
-    print('Error: "%s" does not exist.')
+    print('Error: "%s" does not exist.' % icon)
     sys.exit(6)
 
 
@@ -561,6 +572,9 @@ def main():
   parser.add_option('--enable-remote-debugging', action='store_true',
                     dest='enable_remote_debugging', default=False,
                     help='Enable remote debugging.')
+  parser.add_option('--use-animatable-view', action='store_true',
+                    dest='use_animatable_view', default=False,
+                    help='Enable using animatable view (TextureView).')
   parser.add_option('-f', '--fullscreen', action='store_true',
                     dest='fullscreen', default=False,
                     help='Make application fullscreen.')
@@ -581,6 +595,8 @@ def main():
           'Crosswalk is powered by Chromium and supports Chromium command line.'
           'For example, '
           '--xwalk-command-line=\'--chromium-command-1 --xwalk-command-2\'')
+  info = ('Create an Android project directory at this location. ')
+  parser.add_option('--project-dir', help=info)
   parser.add_option('--xwalk-command-line', default='', help=info)
   info = ('Minify and obfuscate javascript and css.'
           '--compressor: compress javascript and css.'
@@ -618,9 +634,18 @@ def main():
                  options.permissions, options.app_url, options.app_local_path,
                  options.keep_screen_on, options.extensions, None,
                  options.xwalk_command_line, options.compressor)
+
+    # build project is now in /tmp/<name>. Copy to project_dir
+    if options.project_dir:
+      src_dir = GetBuildDir(app_info.android_name)
+      dest_dir = os.path.join(options.project_dir, app_info.android_name)
+      CreateAndCopyDir(src_dir, dest_dir, True)
+
   except SystemExit as ec:
     print('Exiting with error code: %d' % ec.code)
     return ec.code
+  finally:
+    CleanDir(GetBuildDir(app_info.android_name))
   return 0