mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-25 10:05:49 +01:00
Some utility methods + tests
This commit is contained in:
parent
a6bba1b094
commit
4b230412b6
@ -19,6 +19,7 @@ package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
||||
|
||||
@ -47,13 +48,22 @@ public class NotificationUtils {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String formatText(String sender, String subject, String body, int lengthBody, int lengthSubject, Context context) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(StringUtils.truncate(body, lengthBody));
|
||||
builder.append(StringUtils.truncate(subject, lengthSubject));
|
||||
builder.append(StringUtils.formatSender(sender, context));
|
||||
public static String formatSender(String sender, Context context) {
|
||||
if (sender == null || sender.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
return context.getString(R.string.StringUtils_sender, sender);
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
|
||||
@NonNull
|
||||
public static String formatText(String sender, String subject, String body, int lengthBody, int lengthSubject, Context context) {
|
||||
String fBody = StringUtils.truncate(body, lengthBody);
|
||||
String fSubject = StringUtils.truncate(subject, lengthSubject);
|
||||
String fSender = formatSender(sender, context);
|
||||
|
||||
StringBuilder builder = StringUtils.join(" ", fBody, fSubject, fSender);
|
||||
return builder.toString().trim();
|
||||
}
|
||||
|
||||
public static String getPreferredTextFor(CallSpec callSpec) {
|
||||
|
@ -16,11 +16,8 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
public static String truncate(String s, int maxLength){
|
||||
@ -47,12 +44,31 @@ public class StringUtils {
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins the given elements and adds a separator between each element in the resulting string.
|
||||
* There will be no separator at the start or end of the string. There will be no consecutive
|
||||
* separators (even in case an element is null or empty).
|
||||
* @param separator the separator string
|
||||
* @param elements the elements to concatenate to a new string
|
||||
* @return the joined strings, separated by the separator
|
||||
*/
|
||||
@NonNull
|
||||
public static String formatSender(String sender, Context context) {
|
||||
if (sender == null || sender.length() == 0) {
|
||||
return "";
|
||||
public static StringBuilder join(String separator, String... elements) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (elements == null) {
|
||||
return builder;
|
||||
}
|
||||
return context.getString(R.string.StringUtils_sender, sender);
|
||||
boolean hasAdded = false;
|
||||
for (String element : elements) {
|
||||
if (element != null && element.length() > 0) {
|
||||
if (hasAdded) {
|
||||
builder.append(separator);
|
||||
}
|
||||
builder.append(element);
|
||||
hasAdded = true;
|
||||
}
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -0,0 +1,56 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringUtilsTest extends TestBase {
|
||||
private static final String SEP = ":";
|
||||
private static final String E1 = "e1";
|
||||
private static final String E2 = "e2";
|
||||
private static final String E3 = "e3";
|
||||
|
||||
@Test
|
||||
public void testJoinNull() {
|
||||
StringBuilder result = StringUtils.join(SEP, (String[]) null);
|
||||
assertEquals("", result.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinNullElement() {
|
||||
StringBuilder result = StringUtils.join(SEP, (String) null);
|
||||
assertEquals("", result.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinSingleElement() {
|
||||
StringBuilder result = StringUtils.join(SEP, E1);
|
||||
assertEquals(E1, result.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinSingleAndNullElement() {
|
||||
StringBuilder result = StringUtils.join(SEP, E1, null);
|
||||
assertEquals(E1, result.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinTwoElements() {
|
||||
StringBuilder result = StringUtils.join(SEP, E1, E2);
|
||||
assertEquals(E1 + SEP + E2, result.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinTwoElementsAndNull() {
|
||||
StringBuilder result = StringUtils.join(SEP, E1, null, E2);
|
||||
assertEquals(E1 + SEP + E2, result.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinThreeElements() {
|
||||
StringBuilder result = StringUtils.join(SEP, E1, E2, E3);
|
||||
assertEquals(E1 + SEP + E2 + SEP + E3, result.toString());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user