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
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.incallui.rtt.impl;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextWatcher;
import com.android.incallui.rtt.protocol.Constants;
import com.google.common.base.Splitter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/** Message class that holds one RTT chat content. */
final class RttChatMessage {
private static final Splitter SPLITTER = Splitter.on(Constants.BUBBLE_BREAKER);
boolean isRemote;
public boolean hasAvatar;
private final StringBuilder content = new StringBuilder();
private boolean isFinished;
public boolean isFinished() {
return isFinished;
}
public void finish() {
isFinished = true;
}
public void append(String text) {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c != '\b') {
content.append(c);
} else if (content.length() > 0) {
content.deleteCharAt(content.length() - 1);
}
}
}
public String getContent() {
return content.toString();
}
/**
* Generates delta change to a text.
*
* <p>This is used to track text change of input. See more details in {@link
* TextWatcher#onTextChanged}
*
* <p>e.g. "hello world" -> "hello" : "\b\b\b\b\b\b"
*
* <p>"hello world" -> "hello mom!" : "\b\b\b\b\bmom!"
*
* <p>"hello world" -> "hello d" : "\b\b\b\b\bd"
*
* <p>"hello world" -> "hello new world" : "\b\b\b\b\bnew world"
*/
static String getChangedString(CharSequence s, int start, int before, int count) {
StringBuilder modify = new StringBuilder();
char c = '\b';
int oldLength = s.length() - count + before;
for (int i = 0; i < oldLength - start; i++) {
modify.append(c);
}
modify.append(s, start, start + count);
if (start + count < s.length()) {
modify.append(s, start + count, s.length());
}
return modify.toString();
}
/** Convert remote input text into an array of {@code RttChatMessage}. */
static RttChatMessage[] getRemoteRttChatMessage(
@Nullable RttChatMessage currentMessage, @NonNull String text) {
Iterator<String> splitText = SPLITTER.split(text).iterator();
List<RttChatMessage> messageList = new ArrayList<>();
String firstMessageContent = splitText.next();
RttChatMessage firstMessage = currentMessage;
if (firstMessage == null) {
firstMessage = new RttChatMessage();
firstMessage.isRemote = true;
}
firstMessage.append(firstMessageContent);
if (splitText.hasNext() || text.endsWith(Constants.BUBBLE_BREAKER)) {
firstMessage.finish();
}
messageList.add(firstMessage);
while (splitText.hasNext()) {
String singleMessageContent = splitText.next();
if (singleMessageContent.isEmpty()) {
continue;
}
RttChatMessage message = new RttChatMessage();
message.append(singleMessageContent);
message.isRemote = true;
if (splitText.hasNext()) {
message.finish();
}
messageList.add(message);
}
return messageList.toArray(new RttChatMessage[0]);
}
}
|