-
Notifications
You must be signed in to change notification settings - Fork 2
/
autodimension.gradle
335 lines (317 loc) · 11.1 KB
/
autodimension.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import java.util.regex.Matcher
import java.util.regex.Pattern
/**
* How to use:
* https://github.com/nvmanh/android-auto-generate-dimension
* author: nvmanh, manhduongvtt@gmail.com
*/
public class AutoFactory extends DefaultTask {
@TaskAction
def create() {}
}
public enum DimenType {
FROM_LAYOUT, FROM_DIMEN_FILE, AUTO_CREATE
}
public class DimenFactory extends DefaultTask {
@Input
int[] dimens = [320, 360, 384, 411, 480, 540, 600, 720, 800, 1024, 1080, 1280, 1440, 2560, 3840]
@Input
int fromDimen = 300
@Input
double positiveMaxDP = 200
@Input
double positiveMaxSP = 60
@Input
double negativeMaxDP = 60
@Input
double negativeMaxSP = 20
int maxSize = 810
@Input
DimenType type = DimenType.FROM_LAYOUT
@Input
String dimenFileName = "values/dimen.xml"
String resFolder = project.getProjectDir().getPath() + "/src/main/res/"
String layoutFolder = project.getProjectDir().getPath() + "/src/main/res/layout"
String layoutLandFolder = project.getProjectDir().getPath() + "/src/main/res/layout-land"
@TaskAction
def create() {
switch (type) {
case DimenType.FROM_LAYOUT:
createDimenFromLayout()
break;
case DimenType.AUTO_CREATE:
autoCreateDimen()
break;
case DimenType.FROM_DIMEN_FILE:
createDimenFromDimenFile()
break
default:
break
}
}
def createDimenFromDimenFile() {
println "Start convert dimen from value file to other screen size"
String path = resFolder + dimenFileName
Map<String, String> pairs = new HashMap<>()
try {
File fXmlFile = new File(path)
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance()
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder()
Document doc = dBuilder.parse(fXmlFile)
doc.getDocumentElement().normalize()
NodeList nList = doc.getElementsByTagName("dimen")
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp)
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode
if(eElement.getTextContent().contains("@dimen")) continue
pairs.put(eElement.getAttribute("name"), eElement.getTextContent())
}
}
Map<String, Double> dps = new HashMap<>()
Map<String, Double> sps = new HashMap<>()
for (Map.Entry<String, String> entry : pairs) {
String mVal = entry.getValue()
mVal = mVal.replaceAll("dp", "").replaceAll("sp", "")
double val = java.lang.Double.valueOf(mVal)
if (entry.getValue().contains("dp")) {
dps.put(entry.getKey(), val)
} else {
sps.put(entry.getKey(), val)
}
}
//Sort
dps = new TreeMap<>(dps)
sps = new TreeMap<>(sps)
for (int dimen : dimens) {
String folder = resFolder + "values-sw" + (int) dimen + "dp"
String fileName = folder + "/auto_dimens.xml"
new File(folder).mkdir()
new File(fileName).createNewFile()
PrintWriter printWriter = new PrintWriter(fileName)
printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
printWriter.println("<resources>")
for (Map.Entry<String, Double> entry : dps) {
double ratio = entry.getValue() / fromDimen
double sdp = ratio * dimen
printWriter.printf("\t<dimen name=\"%s\">%.2fdp</dimen>\r\n", entry.getKey(), sdp)
}
for (Map.Entry<String, Double> entry : sps) {
double ratio = entry.getValue() / fromDimen
double sdp = ratio * dimen
printWriter.printf("\t<dimen name=\"%s\">%.2fsp</dimen>\r\n", entry.getKey(), sdp)
}
printWriter.println("</resources>")
printWriter.close()
}
} catch (Exception e) {
// println e.getMessage()
e.printStackTrace()
}
}
def autoCreateDimen() {
println "Auto create dimension file and values"
for (int dimen : dimens) {
String folder = resFolder + "values-sw" + (int) dimen + "dp"
String fileName = folder + "/auto_dimens_positive.xml"
new File(folder).mkdir()
new File(fileName).createNewFile()
PrintWriter printWriter = new PrintWriter(fileName);
printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
printWriter.println("<resources>")
for (int i = 1; i <= positiveMaxDP; i++) {
double ratio = i / fromDimen
double sdp = ratio * dimen
printWriter.printf("\t<dimen name=\"dp_%d\">%.2fdp</dimen>\r\n", i, sdp)
}
for (int i = 1; i <= positiveMaxSP; i++) {
double ratio = i / dimens[0]
double sdp = ratio * dimen
printWriter.printf("\t<dimen name=\"sp_%d\">%.2fsp</dimen>\r\n", i, sdp)
}
printWriter.println("</resources>")
printWriter.close()
}
for (int dimen : dimens) {
String folder = resFolder + "values-sw" + (int) dimen + "dp"
String fileName = folder + "/auto_dimens_negative.xml"
new File(folder).mkdir()
new File(fileName).createNewFile()
PrintWriter printWriter = new PrintWriter(fileName)
printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
printWriter.println("<resources>")
for (int i = 1; i <= negativeMaxDP; i++) {
double ratio = i / fromDimen
double sdp = ratio * dimen
printWriter.printf("\t<dimen name=\"dp_minus%d\">%.2fdp</dimen>\r\n", i, -sdp)
}
for (int i = 1; i <= negativeMaxSP; i++) {
double ratio = i / fromDimen
double sdp = ratio * dimen
printWriter.printf("\t<dimen name=\"sp_minus%d\">%.2fsp</dimen>\r\n", i, -sdp)
}
printWriter.println("</resources>")
printWriter.close()
}
}
def createDimenFromLayout() {
println "Create dimen from res/layout dimension values"
Map<String, String> map = new HashMap<>()
addToMap(layoutFolder, map)
//addToMap(layoutLandFolder)
Set<String> holder = new HashSet<>()
for (Map.Entry<String, String> entry : map.entrySet()) {
Set<String> childHolder = new HashSet<>()
String val = entry.getValue()
getDpValues(val, childHolder)
holder.addAll(childHolder)
for (String s : childHolder) {
int valInt = java.lang.Integer.parseInt(s.replaceAll("sp", "").replaceAll("dp", ""))
if (s.contains("dp")) {
if (valInt < 0) {
val = val.replaceAll(s, "@dimen/dp_minus" + (-valInt))
} else {
val = val.replaceAll(s, "@dimen/dp_" + valInt)
}
} else if (s.contains("sp")) {
if (valInt < 0) {
val = val.replaceAll(s, "@dimen/sp_minute" + (-valInt))
} else {
val = val.replaceAll(s, "@dimen/sp_" + valInt)
}
}
}
saveToFile(entry.getKey(), val)
}
Set<Integer> dps = new HashSet<>()
Set<Integer> sps = new HashSet<>()
for (String s : holder) {
if (s.contains("dp")) {
dps.add(java.lang.Integer.valueOf(s.replaceAll("dp", "")))
} else if (s.contains("sp")) {
sps.add(java.lang.Integer.valueOf(s.replaceAll("sp", "")))
}
}
LinkedList<Integer> listDps = new ArrayList<>(dps)
Collections.sort(listDps, new Comparator<Integer>() {
@Override
int compare(Integer i1, Integer i2) {
return i1 - i2
}
});
LinkedList<Integer> listSps = new ArrayList<>(sps)
Collections.sort(listSps, new Comparator<Integer>() {
@Override
int compare(Integer i1, Integer i2) {
return i1 - i2
}
});
for (int dimen : dimens) {
String folder = resFolder + "values-sw" + (int) dimen + "dp"
String fileName = folder + "/auto_dimens.xml"
new File(folder).mkdirs()
new File(fileName).createNewFile()
PrintWriter printWriter = new PrintWriter(fileName)
printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
printWriter.println("<resources>")
for (int i = 0; i < listDps.size(); i++) {
int iVal = listDps.get(i)
double ratio = iVal / fromDimen
double sdp = ratio * dimen
if (iVal < 0) {
printWriter.printf("\t<dimen name=\"dp_minus%d\">%.2fdp</dimen>\r\n", -iVal, sdp)
} else {
printWriter.printf("\t<dimen name=\"dp_%d\">%.2fdp</dimen>\r\n", iVal, sdp)
}
}
for (int i = 0; i < listSps.size(); i++) {
int iVal = listSps.get(i)
double ratio = iVal / fromDimen
double sdp = ratio * dimen
if (iVal < 0) {
printWriter.printf("\t<dimen name=\"sp_minus%d\">%.2fsp</dimen>\r\n", -iVal, sdp)
} else {
printWriter.printf("\t<dimen name=\"sp_%d\">%.2fsp</dimen>\r\n", iVal, sdp)
}
}
printWriter.println("</resources>")
printWriter.close()
}
}
void saveToFile(String path, String content) {
try {
File file = new File(path)
FileWriter fw = new FileWriter(file.getAbsoluteFile())
BufferedWriter bw = new BufferedWriter(fw)
if (file.exists()) {
bw.append(content)
} else {
bw.write(content)
}
bw.close()
} catch (Exception e) {
e.printStackTrace()
}
}
void getDpValues(String string, Set<String> holder) {
String[] vals = string.split("\n")
Pattern pattern = Pattern.compile(".*\\\"(.*)\\\".*")
for (String val : vals) {
Matcher matcher = pattern.matcher(val)
if (!matcher.find()) continue;
for (int i = 1; i <= matcher.groupCount(); i++) {
String mVal = matcher.group(i)
if (mVal != null &&
(mVal.contains("dp") || mVal.contains("sp")) &&
!mVal.contains("@dimen")) {
holder.add(mVal)
}
}
}
}
void addToMap(String path, Map<String, String> map) {
File file = new File(path)
if (!file.exists()) return
if (file.isDirectory()) {
File[] files = file.listFiles()
for (File f : files) {
addToMap(f.getAbsolutePath(), map)
}
} else {
String content = fileToString(path)
println path
map.put(path, content)
}
}
String fileToString(String path) {
try {
File f = new File(path)
FileInputStream inp = new FileInputStream(f)
byte[] bf = new byte[(int) f.length()]
inp.read(bf);
return new String(bf, "UTF-8")
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
return null
}
}
task createDimen(type: DimenFactory) {}
createDimen {
dimens = [320, 360, 384, 411, 480, 540, 600, 720, 800, 1024, 1080, 1280, 1440, 2560, 3840];
fromDimen = 360
positiveMaxDP = 200 //change to 600 or any other value if needed
positiveMaxSP = 60 //change to 600 or any other value if needed
negativeMaxDP = 60 //change to 600 or any other value if needed
negativeMaxSP = 20 //change to 600 or any other value if needed
type = DimenType.FROM_DIMEN_FILE
dimenFileName = 'values-sw360dp/auto_dimens.xml' // name of file, for type = DimenType.FROM_DIMEN_FILE only
}