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