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.
12 def CopyToPathWithName(root, name, final_path, rename):
15 origin_path = os.path.join(root, name)
16 if not os.path.exists(origin_path):
17 print('Error: \'' + origin_path + '\' not found.')
19 if not os.path.exists(final_path):
20 os.makedirs(final_path)
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)
31 def CopyDrawables(image_dict, orientation, sanitized_name, name, app_root):
32 drawable = os.path.join(tempfile.gettempdir(), sanitized_name, 'res',
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'
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', '')
48 # Copy all supported images: 0.75x, 1x, 1.5x, 2x.
51 if CopyToPathWithName(app_root, image_075x, drawable_ldpi, name):
54 if CopyToPathWithName(app_root, image_1x, drawable_mdpi, name):
57 if CopyToPathWithName(app_root, image_15x, drawable_hdpi, name):
60 if CopyToPathWithName(app_root, image_2x, drawable_xhdpi, name):
63 # If no supported images found, find the closest one as 1x.
67 for(k, v) in image_dict.items():
70 float_value = sys.maxsize
72 float_value = float(items[0])
75 if abs(float_value - 1) < delta:
77 if CopyToPathWithName(app_root, closest, drawable_mdpi, name):
81 def CustomizeDrawable(image, orientation, sanitized_name, app_root, name):
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(',')
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(' ')
94 # The dictionary which contains the image pair.
95 image_dict = {'1x': image_1x}
97 for image_pair in image_list:
98 items = image_pair.strip().split(' ')
100 x = items[len(items) - 1]
101 image_item = items[0]
102 image_dict[x] = image_item
104 CopyDrawables(image_dict, orientation, sanitized_name, name, app_root)
107 def CustomizeForeground(image, orientation, sanitized_name, app_root):
108 CustomizeDrawable(image, orientation, sanitized_name,
109 app_root, "launchscreen_img")
112 def CustomizeBackground(background_color,
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.')
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.
129 content = content.replace('#000000', background_color, 1)
130 has_background = True
131 # Fill the background_image.
133 CustomizeDrawable(background_image, orientation, sanitized_name,
134 app_root, "launchscreen_bg_img")
135 # Only set Background Image once for each orientation.
138 ' android:src=\"@drawable/launchscreen_bg_img\"\n' \
139 ' android:tileMode=\"repeat\" />\n' \
141 content = content.replace('<!-- Background Image -->', tmp, 1)
142 has_background = True
144 background_file = open(background_path, 'w')
145 background_file.write(content)
146 background_file.close()
147 return has_background
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
162 def CustomizeLaunchScreen(manifest, sanitized_name):
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