-
Notifications
You must be signed in to change notification settings - Fork 156
/
Util.java
executable file
·465 lines (413 loc) · 14.9 KB
/
Util.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright 2009 Andrew Shu
*
* This file is part of "reddit is fun".
*
* "reddit is fun" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "reddit is fun" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "reddit is fun". If not, see <http://www.gnu.org/licenses/>.
*/
package com.andrewshu.android.reddit;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import android.app.Activity;
import android.net.Uri;
import android.text.style.URLSpan;
import android.util.Log;
public class Util {
private static final String TAG = "Util";
public static ArrayList<String> extractUris(URLSpan[] spans) {
int size = spans.length;
ArrayList<String> accumulator = new ArrayList<String>();
for (int i = 0; i < size; i++) {
accumulator.add(spans[i].getURL());
}
return accumulator;
}
/**
* Convert HTML tags so they will be properly recognized by
* android.text.Html.fromHtml()
* @param html unescaped HTML
* @return converted HTML
*/
public static String convertHtmlTags(String html) {
// Handle <code>
html = html.replaceAll("<code>", "<tt>").replaceAll("</code>", "</tt>");
// Handle <pre>
int preIndex = html.indexOf("<pre>");
int preEndIndex = -6; // -"</pre>".length()
StringBuilder bodyConverted = new StringBuilder();
while (preIndex != -1) {
// get the text between previous </pre> and next <pre>.
bodyConverted = bodyConverted.append(html.substring(preEndIndex + 6, preIndex));
preEndIndex = html.indexOf("</pre>", preIndex);
// Replace newlines with <br> inside the <pre></pre>
// Retain <pre> tags since android.text.Html.fromHtml() will ignore them anyway.
bodyConverted = bodyConverted.append(html.substring(preIndex, preEndIndex).replaceAll("\n", "<br>"))
.append("</pre>");
preIndex = html.indexOf("<pre>", preEndIndex);
}
html = bodyConverted.append(html.substring(preEndIndex + 6)).toString();
// Handle <li>
html = html.replaceAll("<li>", "* ").replaceAll("</li>", "<br>");
// Handle <strong> and <em>, which are normally <b> and <i> respectively, but reversed in Android.
// ANDROID BUG: http://code.google.com/p/android/issues/detail?id=3473
html = html.replaceAll("<strong>", "<b>").replaceAll("</strong>", "</b>")
.replaceAll("<em>", "<i>").replaceAll("</em>", "</i>");
return html;
}
/**
* To the second, not millisecond like reddit
* @param timeSeconds
* @return
*/
public static String getTimeAgo(long utcTimeSeconds) {
long systime = System.currentTimeMillis() / 1000;
long diff = systime - utcTimeSeconds;
if (diff <= 0)
return "very recently";
else if (diff < 60) {
if (diff == 1)
return "1 second ago";
else
return diff + " seconds ago";
}
else if (diff < 3600) {
if ((diff / 60) == 1)
return "1 minute ago";
else
return (diff / 60) + " minutes ago";
}
else if (diff < 86400) { // 86400 seconds in a day
if ((diff / 3600) == 1)
return "1 hour ago";
else
return (diff / 3600) + " hours ago";
}
else if (diff < 604800) { // 86400 * 7
if ((diff / 86400) == 1)
return "1 day ago";
else
return (diff / 86400) + " days ago";
}
else if (diff < 2592000) { // 86400 * 30
if ((diff / 604800) == 1)
return "1 week ago";
else
return (diff / 604800) + " weeks ago";
}
else if (diff < 31536000) { // 86400 * 365
if ((diff / 2592000) == 1)
return "1 month ago";
else
return (diff / 2592000) + " months ago";
}
else {
if ((diff / 31536000) == 1)
return "1 year ago";
else
return (diff / 31536000) + " years ago";
}
}
public static String getTimeAgo(double utcTimeSeconds) {
return getTimeAgo((long)utcTimeSeconds);
}
public static String showNumComments(int comments) {
if (comments == 1) {
return "1 comment";
} else {
return comments + " comments";
}
}
public static String showNumPoints(int score) {
if (score == 1) {
return "1 point";
} else {
return score + " points";
}
}
static String absolutePathToURL(String path) {
if (path.startsWith("/"))
return "http://www.reddit.com" + path;
return path;
}
public static String nameToId(String name) {
// indexOf('_') == -1 if not found; -1 + 1 == 0
return name.substring(name.indexOf('_') + 1);
}
public static boolean isHttpStatusOK(HttpResponse response) {
if (response == null || response.getStatusLine() == null) {
return false;
}
return response.getStatusLine().getStatusCode() == 200;
}
// ===============
// Theme
// ===============
public static boolean isLightTheme(int theme) {
return theme == R.style.Reddit_Light_Medium || theme == R.style.Reddit_Light_Large || theme == R.style.Reddit_Light_Larger || theme == R.style.Reddit_Light_Huge;
}
public static boolean isDarkTheme(int theme) {
return theme == R.style.Reddit_Dark_Medium || theme == R.style.Reddit_Dark_Large || theme == R.style.Reddit_Dark_Larger || theme == R.style.Reddit_Dark_Huge;
}
public static int getInvertedTheme(int theme) {
switch (theme) {
case R.style.Reddit_Light_Medium:
return R.style.Reddit_Dark_Medium;
case R.style.Reddit_Light_Large:
return R.style.Reddit_Dark_Large;
case R.style.Reddit_Light_Larger:
return R.style.Reddit_Dark_Larger;
case R.style.Reddit_Light_Huge:
return R.style.Reddit_Dark_Huge;
case R.style.Reddit_Dark_Medium:
return R.style.Reddit_Light_Medium;
case R.style.Reddit_Dark_Large:
return R.style.Reddit_Light_Large;
case R.style.Reddit_Dark_Larger:
return R.style.Reddit_Light_Larger;
case R.style.Reddit_Dark_Huge:
return R.style.Reddit_Light_Huge;
default:
return R.style.Reddit_Light_Medium;
}
}
public static int getThemeResourceFromPrefs(String themePref, String textSizePref) {
if (Constants.PREF_THEME_LIGHT.equals(themePref)) {
if (Constants.PREF_TEXT_SIZE_MEDIUM.equals(textSizePref))
return R.style.Reddit_Light_Medium;
else if (Constants.PREF_TEXT_SIZE_LARGE.equals(textSizePref))
return R.style.Reddit_Light_Large;
else if (Constants.PREF_TEXT_SIZE_LARGER.equals(textSizePref))
return R.style.Reddit_Light_Larger;
else if (Constants.PREF_TEXT_SIZE_HUGE.equals(textSizePref))
return R.style.Reddit_Light_Huge;
} else /* if (Constants.PREF_THEME_DARK.equals(themePref)) */ {
if (Constants.PREF_TEXT_SIZE_MEDIUM.equals(textSizePref))
return R.style.Reddit_Dark_Medium;
else if (Constants.PREF_TEXT_SIZE_LARGE.equals(textSizePref))
return R.style.Reddit_Dark_Large;
else if (Constants.PREF_TEXT_SIZE_LARGER.equals(textSizePref))
return R.style.Reddit_Dark_Larger;
else if (Constants.PREF_TEXT_SIZE_HUGE.equals(textSizePref))
return R.style.Reddit_Dark_Huge;
}
return R.style.Reddit_Light_Medium;
}
/**
* Return the theme and textSize String prefs
*/
public static String[] getPrefsFromThemeResource(int theme) {
switch (theme) {
case R.style.Reddit_Light_Medium:
return new String[] { Constants.PREF_THEME_LIGHT, Constants.PREF_TEXT_SIZE_MEDIUM };
case R.style.Reddit_Light_Large:
return new String[] { Constants.PREF_THEME_LIGHT, Constants.PREF_TEXT_SIZE_LARGE };
case R.style.Reddit_Light_Larger:
return new String[] { Constants.PREF_THEME_LIGHT, Constants.PREF_TEXT_SIZE_LARGER };
case R.style.Reddit_Light_Huge:
return new String[] { Constants.PREF_THEME_LIGHT, Constants.PREF_TEXT_SIZE_HUGE };
case R.style.Reddit_Dark_Medium:
return new String[] { Constants.PREF_THEME_DARK, Constants.PREF_TEXT_SIZE_MEDIUM };
case R.style.Reddit_Dark_Large:
return new String[] { Constants.PREF_THEME_DARK, Constants.PREF_TEXT_SIZE_LARGE };
case R.style.Reddit_Dark_Larger:
return new String[] { Constants.PREF_THEME_DARK, Constants.PREF_TEXT_SIZE_LARGER };
case R.style.Reddit_Dark_Huge:
return new String[] { Constants.PREF_THEME_DARK, Constants.PREF_TEXT_SIZE_HUGE };
default:
return new String[] { Constants.PREF_THEME_LIGHT, Constants.PREF_TEXT_SIZE_MEDIUM };
}
}
public static int getTextAppearanceResource(int themeResource, int androidTextAppearanceStyle) {
switch (themeResource) {
case R.style.Reddit_Light_Medium:
case R.style.Reddit_Dark_Medium:
switch (androidTextAppearanceStyle) {
case android.R.style.TextAppearance_Small:
return R.style.TextAppearance_Medium_Small;
case android.R.style.TextAppearance_Medium:
return R.style.TextAppearance_Medium_Medium;
case android.R.style.TextAppearance_Large:
return R.style.TextAppearance_Medium_Large;
default:
return R.style.TextAppearance_Medium_Medium;
}
case R.style.Reddit_Light_Large:
case R.style.Reddit_Dark_Large:
switch (androidTextAppearanceStyle) {
case android.R.style.TextAppearance_Small:
return R.style.TextAppearance_Large_Small;
case android.R.style.TextAppearance_Medium:
return R.style.TextAppearance_Large_Medium;
case android.R.style.TextAppearance_Large:
return R.style.TextAppearance_Large_Large;
default:
return R.style.TextAppearance_Large_Medium;
}
case R.style.Reddit_Light_Larger:
case R.style.Reddit_Dark_Larger:
switch (androidTextAppearanceStyle) {
case android.R.style.TextAppearance_Small:
return R.style.TextAppearance_Larger_Small;
case android.R.style.TextAppearance_Medium:
return R.style.TextAppearance_Larger_Medium;
case android.R.style.TextAppearance_Large:
return R.style.TextAppearance_Larger_Large;
default:
return R.style.TextAppearance_Larger_Medium;
}
case R.style.Reddit_Light_Huge:
case R.style.Reddit_Dark_Huge:
switch (androidTextAppearanceStyle) {
case android.R.style.TextAppearance_Small:
return R.style.TextAppearance_Huge_Small;
case android.R.style.TextAppearance_Medium:
return R.style.TextAppearance_Huge_Medium;
case android.R.style.TextAppearance_Large:
return R.style.TextAppearance_Huge_Large;
default:
return R.style.TextAppearance_Huge_Medium;
}
default:
return R.style.TextAppearance_Medium_Medium;
}
}
// =======================
// Mail Notification
// =======================
static long getMillisFromMailNotificationPref(String pref) {
if (Constants.PREF_MAIL_NOTIFICATION_SERVICE_OFF.equals(pref)) {
return 0;
} else if (Constants.PREF_MAIL_NOTIFICATION_SERVICE_5MIN.equals(pref)) {
return 5 * 60 * 1000;
} else if (Constants.PREF_MAIL_NOTIFICATION_SERVICE_30MIN.equals(pref)) {
return 30 * 60 * 1000;
} else if (Constants.PREF_MAIL_NOTIFICATION_SERVICE_1HOUR.equals(pref)) {
return 1 * 3600 * 1000;
} else if (Constants.PREF_MAIL_NOTIFICATION_SERVICE_6HOURS.equals(pref)) {
return 6 * 3600 * 1000;
} else /* if (Constants.PREF_MAIL_NOTIFICATION_SERVICE_1DAY.equals(pref)) */ {
return 24 * 3600 * 1000;
}
}
// ===============
// Transitions
// ===============
static void overridePendingTransition(Method activity_overridePendingTransition, Activity act, int enterAnim, int exitAnim) {
// only available in Android 2.0 (SDK Level 5) and later
if (activity_overridePendingTransition != null) {
try {
activity_overridePendingTransition.invoke(act, enterAnim, exitAnim);
} catch (Exception ex) {
if (Constants.LOGGING) Log.e(TAG, "overridePendingTransition", ex);
}
}
}
// ===============
// Uri
// ===============
static Uri createCommentUri(String linkId, String commentId, int commentContext) {
return Uri.parse(new StringBuilder("http://www.reddit.com/comments/")
.append(linkId)
.append("/z/")
.append(commentId)
.append("?context=")
.append(commentContext)
.toString());
}
static Uri createCommentUri(ThingInfo commentThingInfo, int commentContext) {
if (commentThingInfo.getContext() != null)
return Uri.parse(absolutePathToURL(commentThingInfo.getContext()));
if (commentThingInfo.getLink_id() != null)
return createCommentUri(nameToId(commentThingInfo.getLink_id()), commentThingInfo.getId(), commentContext);
return null;
}
static Uri createProfileUri(String username) {
return Uri.parse(new StringBuilder("http://www.reddit.com/user/")
.append(username)
.toString());
}
static Uri createSubmitUri(String subreddit) {
if (Constants.FRONTPAGE_STRING.equals(subreddit))
return Uri.parse("http://www.reddit.com/submit");
return Uri.parse(new StringBuilder("http://www.reddit.com/r/")
.append(subreddit)
.append("/submit")
.toString());
}
static Uri createSubmitUri(ThingInfo thingInfo) {
return createSubmitUri(thingInfo.getSubreddit());
}
static Uri createSubredditUri(String subreddit) {
if (Constants.FRONTPAGE_STRING.equals(subreddit))
return Uri.parse("http://www.reddit.com/");
return Uri.parse(new StringBuilder("http://www.reddit.com/r/")
.append(subreddit)
.toString());
}
static Uri createSubredditUri(ThingInfo thingInfo) {
return createSubredditUri(thingInfo.getSubreddit());
}
static Uri createThreadUri(String subreddit, String threadId) {
return Uri.parse(new StringBuilder("http://www.reddit.com/r/")
.append(subreddit)
.append("/comments/")
.append(threadId)
.toString());
}
static Uri createThreadUri(ThingInfo threadThingInfo) {
return createThreadUri(threadThingInfo.getSubreddit(), threadThingInfo.getId());
}
static boolean isRedditUri(Uri uri) {
if (uri == null) return false;
String host = uri.getHost();
return host != null && (host.equals("reddit.com") || host.endsWith(".reddit.com"));
}
static boolean isRedditShortenedUri(Uri uri) {
if (uri == null) return false;
String host = uri.getHost();
return host != null && host.equals("redd.it");
}
/**
* Creates mobile version of <code>uri</code> if applicable.
*
* @return original uri if no mobile version of uri is known
*/
static Uri optimizeMobileUri(Uri uri) {
if (isWikipediaUri(uri)) {
uri = createMobileWikpediaUri(uri);
}
return uri;
}
/**
* @return if uri points to a non-mobile wikpedia uri.
*/
static boolean isWikipediaUri(Uri uri) {
if (uri == null) return false;
String host = uri.getHost();
return host != null && host.endsWith(".wikipedia.org") && !host.contains(".m.wikipedia.org");
}
/**
* @return mobile version of a wikipedia uri
*/
static Uri createMobileWikpediaUri(Uri uri) {
String uriString = uri.toString();
return Uri.parse(uriString.replace(".wikipedia.org/", ".m.wikipedia.org/"));
}
static boolean isYoutubeUri(Uri uri) {
if (uri == null) return false;
String host = uri.getHost();
return host != null && host.endsWith(".youtube.com");
}
}