34033d631f832e62e878b5c43060c98291c3d2bd
[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 util import GetBuildDir
12
13 def CopyToPathWithName(root, name, final_path, rename):
14   if name == '':
15     return False
16   origin_path = os.path.join(root, name)
17   if not os.path.exists(origin_path):
18     print('Error: \'' + origin_path + '\' not found.')
19     sys.exit(6)
20   if not os.path.exists(final_path):
21     os.makedirs(final_path)
22   # Get the extension.
23   # Need to take care of special case, such as 'img.9.png'
24   name_components = name.split('.')
25   name_components[0] = rename
26   new_name = '.'.join(name_components)
27   final_path_with_name = os.path.join(final_path, new_name)
28   shutil.copyfile(origin_path, final_path_with_name)
29   return True
30
31
32 def CopyDrawables(image_dict, orientation, sanitized_name, name, app_root):
33   drawable = os.path.join(GetBuildDir(sanitized_name), 'res',
34                           '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(GetBuildDir(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 = open(background_path, 'w')
146     background_file.write(content)
147     background_file.close()
148   return has_background
149
150
151 def CustomizeByOrientation(manifest, orientation, sanitized_name, app_root):
152   background_color = manifest.GetLaunchScreenBackgroundColor(orientation)
153   background_image = manifest.GetLaunchScreenBackgroundImage(orientation)
154   image = manifest.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(manifest, sanitized_name):
164   if manifest is None:
165     return False
166   app_root = os.path.dirname(manifest.input_path)
167   default = CustomizeByOrientation(manifest, 'default',
168                                    sanitized_name, app_root)
169   portrait = CustomizeByOrientation(manifest, 'portrait',
170                                     sanitized_name, app_root)
171   landscape = CustomizeByOrientation(manifest, 'landscape',
172                                      sanitized_name, app_root)
173   return default or portrait or landscape