-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
ProfileName.java
168 lines (133 loc) · 4.79 KB
/
ProfileName.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
package org.thoughtcrime.securesms.profiles;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.annimon.stream.Stream;
import org.thoughtcrime.securesms.util.cjkv.CJKVUtil;
import org.whispersystems.signalservice.api.crypto.ProfileCipher;
import java.nio.charset.StandardCharsets;
public final class ProfileName implements Parcelable {
public static final ProfileName EMPTY = new ProfileName("", "");
private static final int MAX_PART_LENGTH = (ProfileCipher.NAME_PADDED_LENGTH - 1) / 2;
private final String givenName;
private final String familyName;
private final String joinedName;
private ProfileName(@Nullable String givenName, @Nullable String familyName) {
this.givenName = givenName == null ? "" : givenName;
this.familyName = familyName == null ? "" : familyName;
this.joinedName = getJoinedName(this.givenName, this.familyName);
}
private ProfileName(Parcel in) {
this(in.readString(), in.readString());
}
public @NonNull
String getGivenName() {
return givenName;
}
public @NonNull
String getFamilyName() {
return familyName;
}
@VisibleForTesting
boolean isProfileNameCJKV() {
return isCJKV(givenName, familyName);
}
public boolean isEmpty() {
return joinedName.isEmpty();
}
public boolean isGivenNameEmpty() {
return givenName.isEmpty();
}
public @NonNull String serialize() {
if (isGivenNameEmpty()) {
return "";
}
return String.format("%s\0%s", givenName, familyName);
}
@Override
public @NonNull String toString() {
return joinedName;
}
/**
* Deserializes a profile name, trims if exceeds the limits.
*/
public static @NonNull ProfileName fromSerialized(@Nullable String profileName) {
if (profileName == null) {
return EMPTY;
}
String[] parts = profileName.split("\0");
if (parts.length == 0) {
return EMPTY;
} else if (parts.length == 1) {
return fromParts(parts[0], "");
} else {
return fromParts(parts[0], parts[1]);
}
}
/**
* Creates a profile name, trimming chars until it fits the limits.
*/
public static @NonNull ProfileName fromParts(@Nullable String givenName, @Nullable String familyName) {
givenName = givenName == null ? "" : givenName;
familyName = familyName == null ? "" : familyName;
givenName = trimToFit(givenName .trim());
familyName = trimToFit(familyName.trim());
return new ProfileName(givenName, familyName);
}
/**
* Trims a name string to fit into the byte length requirement.
*/
public static @NonNull String trimToFit(@Nullable String name) {
if (name == null) return "";
// At least one byte per char, so shorten string to reduce loop
if (name.length() > ProfileName.MAX_PART_LENGTH) {
name = name.substring(0, ProfileName.MAX_PART_LENGTH);
}
// Remove one char at a time until fits in byte allowance
while (name.getBytes(StandardCharsets.UTF_8).length > ProfileName.MAX_PART_LENGTH) {
name = name.substring(0, name.length() - 1);
}
return name;
}
private static @NonNull String getJoinedName(@NonNull String givenName, @NonNull String familyName) {
if (givenName.isEmpty() && familyName.isEmpty()) return "";
else if (givenName.isEmpty()) return familyName;
else if (familyName.isEmpty()) return givenName;
else if (isCJKV(givenName, familyName)) return String.format("%s %s",
familyName,
givenName);
else return String.format("%s %s",
givenName,
familyName);
}
private static boolean isCJKV(@NonNull String givenName, @NonNull String familyName) {
if (givenName.isEmpty() && familyName.isEmpty()) {
return false;
} else {
return Stream.of(givenName, familyName)
.filterNot(String::isEmpty)
.reduce(true, (a, s) -> a && CJKVUtil.isCJKV(s));
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(givenName);
dest.writeString(familyName);
}
public static final Creator<ProfileName> CREATOR = new Creator<ProfileName>() {
@Override
public ProfileName createFromParcel(Parcel in) {
return new ProfileName(in);
}
@Override
public ProfileName[] newArray(int size) {
return new ProfileName[size];
}
};
}