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.
11 from manifest_json_parser import ManifestJsonParser
14 def CopyToPathWithName(root, name, final_path, rename):
17 origin_path = os.path.join(root, name)
18 if not os.path.exists(origin_path):
19 print('Error: \'' + origin_path + '\' not found.')
21 if not os.path.exists(final_path):
22 os.makedirs(final_path)
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)
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'
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', '')
49 # Copy all supported images: 0.75x, 1x, 1.5x, 2x.
52 if CopyToPathWithName(app_root, image_075x, drawable_ldpi, name):
55 if CopyToPathWithName(app_root, image_1x, drawable_mdpi, name):
58 if CopyToPathWithName(app_root, image_15x, drawable_hdpi, name):
61 if CopyToPathWithName(app_root, image_2x, drawable_xhdpi, name):
64 # If no supported images found, find the closest one as 1x.
68 for(k, v) in image_dict.items():
71 float_value = sys.maxint
73 float_value = float(items[0])
76 if abs(float_value - 1) < delta:
78 if CopyToPathWithName(app_root, closest, drawable_mdpi, name):
82 def CustomizeDrawable(image, orientation, sanitized_name, app_root, name):
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(',')
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(' ')
95 # The dictionary which contains the image pair.
96 image_dict = {'1x': image_1x}
98 for image_pair in image_list:
99 items = image_pair.strip().split(' ')
101 x = items[len(items) - 1]
102 image_item = items[0]
103 image_dict[x] = image_item
105 CopyDrawables(image_dict, orientation, sanitized_name, name, app_root)
108 def CustomizeForeground(image, orientation, sanitized_name, app_root):
109 CustomizeDrawable(image, orientation, sanitized_name,
110 app_root, "launchscreen_img")
113 def CustomizeBackground(background_color,
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.')
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.
130 content = content.replace('#000000', background_color, 1)
131 has_background = True
132 # Fill the background_image.
134 CustomizeDrawable(background_image, orientation, sanitized_name,
135 app_root, "launchscreen_bg_img")
136 # Only set Background Image once for each orientation.
139 ' android:src=\"@drawable/launchscreen_bg_img\"\n' \
140 ' android:tileMode=\"repeat\" />\n' \
142 content = content.replace('<!-- Background Image -->', tmp, 1)
143 has_background = True
145 background_file = file(background_path, 'w')
146 background_file.write(content)
147 background_file.close()
148 return has_background
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
163 def CustomizeLaunchScreen(app_manifest, sanitized_name):
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