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 util import GetBuildDir
13 def CopyToPathWithName(root, name, final_path, rename):
16 origin_path = os.path.join(root, name)
17 if not os.path.exists(origin_path):
18 print('Error: \'' + origin_path + '\' not found.')
20 if not os.path.exists(final_path):
21 os.makedirs(final_path)
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)
32 def CopyDrawables(image_dict, orientation, sanitized_name, name, app_root):
33 drawable = os.path.join(GetBuildDir(sanitized_name), 'res',
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.maxsize
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(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.')
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 = open(background_path, 'w')
146 background_file.write(content)
147 background_file.close()
148 return has_background
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
163 def CustomizeLaunchScreen(manifest, sanitized_name):
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