4626b7711ac1f37bcbed3c429aa6875200143ddd
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / customize_launch_screen.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import os
8 import shutil
9 import sys
10
11 from manifest_json_parser import ManifestJsonParser
12
13
14 def CopyToPathWithName(root, name, final_path, rename):
15   if name == '':
16     return False
17   origin_path = os.path.join(root, name)
18   if not os.path.exists(origin_path):
19     print('Error: \'' + origin_path + '\' not found.')
20     sys.exit(6)
21   if not os.path.exists(final_path):
22     os.makedirs(final_path)
23   # Get the extension.
24   # Need to take care of special case, such as 'img.9.png'
25   name_components = name.split('.')
26   name_components[0] = rename
27   new_name = '.'.join(name_components)
28   final_path_with_name = os.path.join(final_path, new_name)
29   shutil.copyfile(origin_path, final_path_with_name)
30   return True
31
32
33 def CopyDrawables(image_dict, orientation, sanitized_name, name, app_root):
34   drawable = os.path.join(sanitized_name, 'res', 'drawable')
35   if orientation == 'landscape':
36     drawable = drawable + '-land'
37   elif orientation == 'portrait':
38     drawable = drawable + '-port'
39   drawable_ldpi = drawable + '-ldpi'
40   drawable_mdpi = drawable + '-mdpi'
41   drawable_hdpi = drawable + '-hdpi'
42   drawable_xhdpi = drawable + '-xhdpi'
43
44   image_075x = image_dict.get('0.75x', '')
45   image_1x = image_dict.get('1x', '')
46   image_15x = image_dict.get('1.5x', '')
47   image_2x = image_dict.get('2x', '')
48
49   # Copy all supported images: 0.75x, 1x, 1.5x, 2x.
50   has_image = False
51   if image_075x:
52     if CopyToPathWithName(app_root, image_075x, drawable_ldpi, name):
53       has_image = True
54   if image_1x:
55     if CopyToPathWithName(app_root, image_1x, drawable_mdpi, name):
56       has_image = True
57   if image_15x:
58     if CopyToPathWithName(app_root, image_15x, drawable_hdpi, name):
59       has_image = True
60   if image_2x:
61     if CopyToPathWithName(app_root, image_2x, drawable_xhdpi, name):
62       has_image = True
63
64   # If no supported images found, find the closest one as 1x.
65   if not has_image:
66     closest = ''
67     delta = sys.maxsize
68     for(k, v) in image_dict.items():
69       items = k.split('x')
70       if len(items) == 2:
71         float_value = sys.maxsize
72         try:
73           float_value = float(items[0])
74         except ValueError:
75           continue
76         if abs(float_value - 1) < delta:
77           closest = v
78           if CopyToPathWithName(app_root, closest, drawable_mdpi, name):
79             delta = float_value
80
81
82 def CustomizeDrawable(image, orientation, sanitized_name, app_root, name):
83   # Parse the image.
84   # The format of image: 'image-1x.png [1x], image-75x.png 0.75x,
85   #                       image-15x.png 1.5x, image-2x.png 2x'
86   image_list = image.split(',')
87
88   # The first image: 'image-1x.png', the density is not provided.
89   image_pair_1 = image_list[0].strip()
90   items = image_pair_1.split(' ')
91   image_1x = ''
92   if len(items) == 1:
93     image_1x = items[0]
94     image_list.pop(0)
95   # The dictionary which contains the image pair.
96   image_dict = {'1x': image_1x}
97
98   for image_pair in image_list:
99     items = image_pair.strip().split(' ')
100     if len(items) >= 2:
101       x = items[len(items) - 1]
102       image_item = items[0]
103       image_dict[x] = image_item
104
105   CopyDrawables(image_dict, orientation, sanitized_name, name, app_root)
106
107
108 def CustomizeForeground(image, orientation, sanitized_name, app_root):
109   CustomizeDrawable(image, orientation, sanitized_name,
110                     app_root, "launchscreen_img")
111
112
113 def CustomizeBackground(background_color,
114                         background_image,
115                         orientation,
116                         sanitized_name,
117                         app_root):
118   background_path = os.path.join(sanitized_name, 'res',
119                                  'drawable', 'launchscreen_bg.xml')
120   if not os.path.isfile(background_path):
121     print('Error: launchscreen_bg.xml is missing in the build tool.')
122     sys.exit(6)
123
124   has_background = False
125   background_file = open(background_path, 'r')
126   content = background_file.read()
127   background_file.close()
128   # Fill the background_color.
129   if background_color:
130     content = content.replace('#000000', background_color, 1)
131     has_background = True
132   # Fill the background_image.
133   if background_image:
134     CustomizeDrawable(background_image, orientation, sanitized_name,
135                       app_root, "launchscreen_bg_img")
136     # Only set Background Image once for each orientation.
137     tmp = '<item>\n' \
138           '  <bitmap\n' \
139           '    android:src=\"@drawable/launchscreen_bg_img\"\n' \
140           '    android:tileMode=\"repeat\" />\n' \
141           '</item>\n'
142     content = content.replace('<!-- Background Image -->', tmp, 1)
143     has_background = True
144   if has_background:
145     background_file = file(background_path, 'w')
146     background_file.write(content)
147     background_file.close()
148   return has_background
149
150
151 def CustomizeByOrientation(parser, orientation, sanitized_name, app_root):
152   background_color = parser.GetLaunchScreenBackgroundColor(orientation)
153   background_image = parser.GetLaunchScreenBackgroundImage(orientation)
154   image = parser.GetLaunchScreenImage(orientation)
155   # Customize background: background_color, background_image.
156   has_background = CustomizeBackground(background_color, background_image,
157                                        orientation, sanitized_name, app_root)
158   # Customize foreground: image.
159   CustomizeForeground(image, orientation, sanitized_name, app_root)
160   return has_background
161
162
163 def CustomizeLaunchScreen(app_manifest, sanitized_name):
164   if not app_manifest:
165     return False
166   parser = ManifestJsonParser(os.path.expanduser(app_manifest))
167   app_root = os.path.dirname(app_manifest)
168   default = CustomizeByOrientation(parser, 'default',
169                                    sanitized_name, app_root)
170   portrait = CustomizeByOrientation(parser, 'portrait',
171                                     sanitized_name, app_root)
172   landscape = CustomizeByOrientation(parser, 'landscape',
173                                      sanitized_name, app_root)
174   return default or portrait or landscape