1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.incallui.rtt.impl; 18 19 import android.support.annotation.NonNull; 20 import android.support.annotation.Nullable; 21 import android.text.TextWatcher; 22 import com.android.incallui.rtt.protocol.Constants; 23 import com.google.common.base.Splitter; 24 import java.util.ArrayList; 25 import java.util.Iterator; 26 import java.util.List; 27 28 /** Message class that holds one RTT chat content. */ 29 final class RttChatMessage { 30 31 private static final Splitter SPLITTER = Splitter.on(Constants.BUBBLE_BREAKER); 32 33 boolean isRemote; 34 public boolean hasAvatar; 35 private final StringBuilder content = new StringBuilder(); 36 private boolean isFinished; 37 38 public boolean isFinished() { 39 return isFinished; 40 } 41 42 public void finish() { 43 isFinished = true; 44 } 45 46 public void append(String text) { 47 for (int i = 0; i < text.length(); i++) { 48 char c = text.charAt(i); 49 if (c != '\b') { 50 content.append(c); 51 } else if (content.length() > 0) { 52 content.deleteCharAt(content.length() - 1); 53 } 54 } 55 } 56 57 public String getContent() { 58 return content.toString(); 59 } 60 61 /** 62 * Generates delta change to a text. 63 * 64 * <p>This is used to track text change of input. See more details in {@link 65 * TextWatcher#onTextChanged} 66 * 67 * <p>e.g. "hello world" -> "hello" : "\b\b\b\b\b\b" 68 * 69 * <p>"hello world" -> "hello mom!" : "\b\b\b\b\bmom!" 70 * 71 * <p>"hello world" -> "hello d" : "\b\b\b\b\bd" 72 * 73 * <p>"hello world" -> "hello new world" : "\b\b\b\b\bnew world" 74 */ 75 static String getChangedString(CharSequence s, int start, int before, int count) { 76 StringBuilder modify = new StringBuilder(); 77 char c = '\b'; 78 int oldLength = s.length() - count + before; 79 for (int i = 0; i < oldLength - start; i++) { 80 modify.append(c); 81 } 82 modify.append(s, start, start + count); 83 if (start + count < s.length()) { 84 modify.append(s, start + count, s.length()); 85 } 86 return modify.toString(); 87 } 88 89 /** Convert remote input text into an array of {@code RttChatMessage}. */ 90 static RttChatMessage[] getRemoteRttChatMessage( 91 @Nullable RttChatMessage currentMessage, @NonNull String text) { 92 Iterator<String> splitText = SPLITTER.split(text).iterator(); 93 List<RttChatMessage> messageList = new ArrayList<>(); 94 95 String firstMessageContent = splitText.next(); 96 RttChatMessage firstMessage = currentMessage; 97 if (firstMessage == null) { 98 firstMessage = new RttChatMessage(); 99 firstMessage.isRemote = true; 100 } 101 firstMessage.append(firstMessageContent); 102 if (splitText.hasNext() || text.endsWith(Constants.BUBBLE_BREAKER)) { 103 firstMessage.finish(); 104 } 105 messageList.add(firstMessage); 106 107 while (splitText.hasNext()) { 108 String singleMessageContent = splitText.next(); 109 if (singleMessageContent.isEmpty()) { 110 continue; 111 } 112 RttChatMessage message = new RttChatMessage(); 113 message.append(singleMessageContent); 114 message.isRemote = true; 115 if (splitText.hasNext()) { 116 message.finish(); 117 } 118 messageList.add(message); 119 } 120 121 return messageList.toArray(new RttChatMessage[0]); 122 } 123 } 124