-
Notifications
You must be signed in to change notification settings - Fork 31
/
PhoneComponent.java
64 lines (49 loc) · 1.76 KB
/
PhoneComponent.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
package io.kimo.lib.faker.component.text;
import android.content.Context;
import java.lang.*;
import io.kimo.lib.faker.R;
import io.kimo.lib.faker.api.PhoneAPI;
import io.kimo.lib.faker.component.FakerTextComponent;
import io.kimo.lib.faker.component.number.NumberComponent;
public class PhoneComponent extends FakerTextComponent implements PhoneAPI {
private String areaCodeMask, countryCodeMask;
private NumberComponent numberComponent;
public PhoneComponent(Context context) {
super(context);
numberComponent = new NumberComponent(context);
areaCodeMask = context.getResources().getString(R.string.area_code_phone_mask);
countryCodeMask = context.getResources().getString(R.string.country_code_phone_mask);
}
@Override
public String randomText() {
int method = (int)(Math.random() * 10);
switch (method % 2) {
case 0:
return phoneWithAreaCode();
case 1:
return phoneWithCountryCode();
default:
return "";
}
}
private String numbersInMask(String mask, char maskPlaceholder) {
StringBuilder phoneInMask = new StringBuilder();
for(int i = 0; i < mask.length(); i++) {
char currentChar = mask.charAt(i);
if(currentChar == maskPlaceholder) {
phoneInMask.append(numberComponent.positiveDigit());
} else {
phoneInMask.append(currentChar);
}
}
return phoneInMask.toString();
}
@Override
public String phoneWithAreaCode() {
return numbersInMask(areaCodeMask, '#');
}
@Override
public String phoneWithCountryCode() {
return numbersInMask(countryCodeMask, '#');
}
}