This repository was archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathUtils.java
More file actions
51 lines (42 loc) · 1.43 KB
/
Utils.java
File metadata and controls
51 lines (42 loc) · 1.43 KB
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
package com.jaychang.st;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
class Utils {
static int dp2px(Context context, int dp) {
float density = context.getApplicationContext().getResources().getDisplayMetrics().density;
return (int) (dp * density + 0.5f);
}
static void openURL(Context context, String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
static List<Integer> indexesOf(String src, String target) {
List<Integer> positions = new ArrayList<>();
for (int index = src.indexOf(target);
index >= 0;
index = src.indexOf(target, index + 1)) {
positions.add(index);
}
return positions;
}
static List<Range> ranges(String src, String pattern) {
return ranges(src, Pattern.compile(pattern));
}
static List<Range> ranges(String src, Pattern pattern) {
List<Range> ranges = new ArrayList<>();
Matcher matcher = pattern.matcher(src);
while (matcher.find()) {
Range range = Range.create(matcher.start(), matcher.end());
ranges.add(range);
}
return ranges;
}
}