-
Notifications
You must be signed in to change notification settings - Fork 49
/
CommonUtils.java
245 lines (235 loc) · 6.42 KB
/
CommonUtils.java
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
package chan.util;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.SystemClock;
import chan.annotation.Public;
import com.mishiranu.dashchan.util.Log;
import java.lang.reflect.Array;
import java.util.Collection;
import org.json.JSONException;
import org.json.JSONObject;
@Public
public class CommonUtils {
@Public
public static boolean sleepMaxRealtime(long startRealtime, long interval) {
long time = interval - (SystemClock.elapsedRealtime() - startRealtime);
if (time <= 0) {
return false;
}
try {
Thread.sleep(time);
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return true;
}
}
@Public
public static String optJsonString(JSONObject jsonObject, String name) {
return optJsonString(jsonObject, name, null);
}
@Public
public static String optJsonString(JSONObject jsonObject, String name, String fallback) {
try {
return getJsonString(jsonObject, name);
} catch (JSONException e) {
return fallback;
}
}
@Public
public static String getJsonString(JSONObject jsonObject, String name) throws JSONException {
if (jsonObject.has(name) && jsonObject.isNull(name)) {
return null;
} else {
return jsonObject.getString(name);
}
}
@Public
public static String restoreCloudFlareProtectedEmails(String string) {
int index = 0;
StringBuilder builder = null;
while (true) {
index = builder != null ? builder.indexOf("/cdn-cgi/l/email-protection", index)
: string.indexOf("/cdn-cgi/l/email-protection");
if (index >= 0) {
if (builder == null) {
builder = new StringBuilder(string);
}
int index1 = builder.lastIndexOf("<", index);
int index2 = builder.lastIndexOf("<a", index);
int index3 = builder.indexOf(">", index);
int index4 = builder.indexOf("</a>", index);
if (index1 == index2 && index3 > index) {
int index5 = builder.indexOf("\"", index);
if (index5 == -1) {
index5 = builder.indexOf("'", index);
}
if (index5 == -1) {
index5 = builder.indexOf(" ", index);
}
if (index5 == -1) {
break;
}
String url = builder.substring(index, index5);
int index6 = url.indexOf('#');
String hash = null;
boolean replaceTag = false;
if (index6 >= 0) {
hash = url.substring(index6 + 1);
} else {
index6 = builder.indexOf("data-cfemail=", index1);
if (index6 >= 0 && index6 < index3) {
index6 += 14;
index5 = builder.indexOf("\"", index6);
if (index5 == -1) {
index5 = builder.indexOf("'", index6);
}
if (index5 == -1) {
index5 = builder.indexOf(" ", index6--);
}
if (index5 == -1) {
break;
}
hash = builder.substring(index6, index5);
replaceTag = true;
}
}
if (hash != null && hash.length() % 2 == 0) {
int x = Integer.parseInt(hash.substring(0, 2), 16);
StringBuilder email = new StringBuilder(hash.length() / 2 - 1);
for (int i = 2; i < hash.length(); i += 2) {
int b = Integer.parseInt(hash.substring(i, i + 2), 16);
email.append((char) (b ^ x));
}
if (replaceTag) {
builder.replace(index1, index4 + 4, email.toString());
index = index1 + email.length();
continue;
} else {
builder.replace(index, index5, "mailto:" + email.toString());
index = builder.indexOf(">", index) + 1;
continue;
}
}
}
if (index4 == -1) {
break;
}
index = index4 + 4;
} else {
break;
}
}
if (builder != null) {
while (true) {
int start = builder.indexOf("<script data-cfhash");
if (start >= 0) {
int end = builder.indexOf("</script>");
if (end > start) {
end += 9;
builder.delete(start, end);
continue;
}
}
break;
}
string = builder.toString();
}
return string;
}
@Public
public static Bitmap trimBitmap(Bitmap bitmap, int backgroundColor) {
if (bitmap == null) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[Math.max(width, height)];
int actualLeft = 0;
int actualRight = width;
int actualTop = 0;
int actualBottom = height;
OUT: for (int i = 0; i < width; i++) {
bitmap.getPixels(pixels, 0, 1, i, 0, 1, height);
for (int j = 0; j < height; j++) {
if (pixels[j] != backgroundColor) {
actualLeft = i;
break OUT;
}
}
}
OUT: for (int i = width - 1; i >= 0; i--) {
bitmap.getPixels(pixels, 0, 1, i, 0, 1, height);
for (int j = 0; j < height; j++) {
if (pixels[j] != backgroundColor) {
actualRight = i + 1;
break OUT;
}
}
}
OUT: for (int i = 0; i < height; i++) {
bitmap.getPixels(pixels, 0, width, 0, i, width, 1);
for (int j = 0; j < width; j++) {
if (pixels[j] != backgroundColor) {
actualTop = i;
break OUT;
}
}
}
OUT: for (int i = height - 1; i >= 0; i--) {
bitmap.getPixels(pixels, 0, width, 0, i, width, 1);
for (int j = 0; j < width; j++) {
if (pixels[j] != backgroundColor) {
actualBottom = i + 1;
break OUT;
}
}
}
if (actualLeft != 0 || actualTop != 0 || actualRight != width || actualBottom != height) {
if (actualRight > actualLeft && actualBottom > actualTop) {
Bitmap newBitmap = Bitmap.createBitmap(actualRight - actualLeft, actualBottom - actualTop,
Bitmap.Config.ARGB_8888);
new Canvas(newBitmap).drawBitmap(bitmap, -actualLeft, -actualTop, null);
return newBitmap;
}
return null;
}
return bitmap;
}
@Public
public static void writeLog(Object... data) {
Log.persistent().write(data);
}
public static <T> T[] removeNullItems(T[] array, Class<T> itemClass) {
if (array != null) {
int nullItems = 0;
for (T item : array) {
if (item == null) {
nullItems++;
}
}
if (nullItems == array.length) {
array = null;
} else if (nullItems > 0) {
@SuppressWarnings("unchecked")
T[] newArray = (T[]) Array.newInstance(itemClass, array.length - nullItems);
int i = 0;
for (T item : array) {
if (item != null) {
newArray[i++] = item;
}
}
array = newArray;
}
}
return array;
}
public static <T> T[] toArray(Collection<? extends T> collection, Class<T> itemClass) {
if (collection != null && !collection.isEmpty()) {
@SuppressWarnings("unchecked")
T[] array = (T[]) Array.newInstance(itemClass, collection.size());
return collection.toArray(array);
}
return null;
}
}