Upstream version 6.34.113.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / customize.py
index c8eac2e..675b67b 100755 (executable)
@@ -114,8 +114,8 @@ def CustomizeThemeXML(sanitized_name, fullscreen, launch_screen_img):
 
 
 def CustomizeXML(sanitized_name, package, app_versionCode, app_version,
-                 description, name, orientation, icon, fullscreen,
-                 launch_screen_img, permissions):
+                 description, name, orientation, icon_dict, fullscreen,
+                 launch_screen_img, permissions, app_root):
   manifest_path = os.path.join(sanitized_name, 'AndroidManifest.xml')
   if not os.path.isfile(manifest_path):
     print ('Please make sure AndroidManifest.xml'
@@ -143,19 +143,9 @@ def CustomizeXML(sanitized_name, package, app_versionCode, app_version,
   if orientation:
     EditElementAttribute(xmldoc, 'activity', 'android:screenOrientation',
                          orientation)
-  if icon and os.path.isfile(icon):
-    drawable_path = os.path.join(sanitized_name, 'res', 'drawable')
-    if not os.path.exists(drawable_path):
-      os.makedirs(drawable_path)
-    icon_file = os.path.basename(icon)
-    icon_file = ReplaceInvalidChars(icon_file)
-    shutil.copyfile(icon, os.path.join(drawable_path, icon_file))
-    icon_name = os.path.splitext(icon_file)[0]
-    EditElementAttribute(xmldoc, 'application',
-                         'android:icon', '@drawable/%s' % icon_name)
-  elif icon and (not os.path.isfile(icon)):
-    print ('Please make sure the icon file does exist!')
-    sys.exit(6)
+  if CustomizeIcon(sanitized_name, app_root, icon_dict):
+    EditElementAttribute(xmldoc, 'application', 'android:icon',
+                         '@drawable/icon')
 
   file_handle = open(os.path.join(sanitized_name, 'AndroidManifest.xml'), 'w')
   xmldoc.writexml(file_handle, encoding='utf-8')
@@ -172,21 +162,21 @@ def ReplaceString(file_path, src, dest):
   file_handle.close()
 
 
-def SetVariable(file_path, variable, value):
+def SetVariable(file_path, string_line, variable, value):
   function_string = ('%sset%s(%s);\n' %
                     ('        ', variable, value))
   temp_file_path = file_path + '.backup'
   file_handle = open(temp_file_path, 'w+')
   for line in open(file_path):
     file_handle.write(line)
-    if (line.find('public void onCreate(Bundle savedInstanceState)') >= 0):
+    if (line.find(string_line) >= 0):
       file_handle.write(function_string)
   file_handle.close()
   shutil.move(temp_file_path, file_path)
 
 
 def CustomizeJava(sanitized_name, package, app_url, app_local_path,
-                  enable_remote_debugging):
+                  enable_remote_debugging, display_as_fullscreen):
   root_path =  os.path.join(sanitized_name, 'src',
                             package.replace('.', os.path.sep))
   dest_activity = os.path.join(root_path, sanitized_name + 'Activity.java')
@@ -214,7 +204,13 @@ def CustomizeJava(sanitized_name, package, app_url, app_local_path,
         sys.exit(8)
 
   if enable_remote_debugging:
-    SetVariable(dest_activity, 'RemoteDebugging', 'true')
+    SetVariable(dest_activity,
+                'public void onCreate(Bundle savedInstanceState)',
+                'RemoteDebugging', 'true')
+  if display_as_fullscreen:
+    SetVariable(dest_activity,
+                'super.onCreate(savedInstanceState)',
+                'IsFullscreen', 'true')
 
 
 def CopyExtensionFile(extension_name, suffix, src_path, dest_path):
@@ -337,19 +333,53 @@ def CustomizeExtensions(sanitized_name, name, extensions):
     extension_json_file.close()
 
 
+def CustomizeIcon(sanitized_name, app_root, icon_dict):
+  icon_exist = False
+  drawable_dict = {'ldpi':[1, 37], 'mdpi':[37, 72], 'hdpi':[72, 96],
+                   'xhdpi':[96, 120], 'xxhdpi':[120, 144]}
+  if not icon_dict:
+    return icon_exist
+
+  try:
+    icon_dict = dict((int(k), v) for k, v in icon_dict.items())
+  except ValueError:
+    print('The key of icon in the manifest file should be a number.')
+
+  if len(icon_dict) > 0:
+    icon_list = sorted(icon_dict.iteritems(), key = lambda d:d[0])
+    for kd, vd in drawable_dict.iteritems():
+      for item in icon_list:
+        if item[0] >= vd[0] and item[0] < vd[1]:
+          drawable_path = os.path.join(sanitized_name, 'res', 'drawable-' + kd)
+          if not os.path.exists(drawable_path):
+            os.makedirs(drawable_path)
+          icon = os.path.join(app_root, item[1])
+          if icon and os.path.isfile(icon):
+            icon_name = os.path.basename(icon)
+            icon_suffix = icon_name.split('.')[-1]
+            shutil.copyfile(icon, os.path.join(drawable_path,
+                                               'icon.' + icon_suffix))
+            icon_exist = True
+          elif icon and (not os.path.isfile(icon)):
+            print ('Error: Please make sure \"' + icon + '\" does exist!')
+            sys.exit(6)
+          break
+  return icon_exist
+
+
 def CustomizeAll(app_versionCode, description, icon, permissions, app_url,
                  app_root, app_local_path, enable_remote_debugging,
-                 fullscreen_flag, extensions, launch_screen_img,
+                 display_as_fullscreen, extensions, launch_screen_img,
                  package='org.xwalk.app.template', name='AppTemplate',
-                 app_version='1.0.0', orientation='unspecified'):
+                 app_version='1.0.0', orientation='unspecified') :
   sanitized_name = ReplaceInvalidChars(name, 'apkname')
   try:
     Prepare(sanitized_name, package, app_root)
     CustomizeXML(sanitized_name, package, app_versionCode, app_version,
-                 description, name, orientation, icon, fullscreen_flag,
-                 launch_screen_img, permissions)
+                 description, name, orientation, icon, display_as_fullscreen,
+                 launch_screen_img, permissions, app_root)
     CustomizeJava(sanitized_name, package, app_url, app_local_path,
-                  enable_remote_debugging)
+                  enable_remote_debugging, display_as_fullscreen)
     CustomizeExtensions(sanitized_name, name, extensions)
   except SystemExit as ec:
     print('Exiting with error code: %d' % ec.code)
@@ -370,8 +400,6 @@ def main():
   info = ('The application description. Such as:'
           '--description=YourApplicationdDescription')
   parser.add_option('--description', help=info)
-  info = ('The path of icon. Such as: --icon=/path/to/your/customized/icon')
-  parser.add_option('--icon', help=info)
   info = ('The permission list. Such as: --permissions="geolocation"'
           'For more permissions, such as:'
           '--permissions="geolocation:permission2"')
@@ -408,7 +436,21 @@ def main():
                     help='The fallback image for launch_screen')
   options, _ = parser.parse_args()
   try:
-    CustomizeAll(options.app_versionCode, options.description, options.icon,
+    icon_dict = {144: 'icons/icon_144.png',
+                 72: 'icons/icon_72.png',
+                 96: 'icons/icon_96.png',
+                 48: 'icons/icon_48.png'}
+    if options.name == None:
+      options.name = 'Example'
+    if options.app_root == None:
+      options.app_root = os.path.join('test_data', 'manifest')
+    if options.package == None:
+      options.package = 'org.xwalk.app.template'
+    if options.orientation == None:
+      options.orientation = 'unspecified'
+    if options.app_version == None:
+      options.app_version = '1.0.0'
+    CustomizeAll(options.app_versionCode, options.description, icon_dict,
                  options.permissions, options.app_url, options.app_root,
                  options.app_local_path, options.enable_remote_debugging,
                  options.fullscreen, options.extensions,