summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/simulator/impl/RttChatBot.java
blob: b2860e3878a0471a19492ff71d6b2756bf7a776f (plain)
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
/*
 * 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.dialer.simulator.impl;

import android.annotation.TargetApi;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.support.annotation.MainThread;
import android.telecom.Connection.RttTextStream;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.incallui.rtt.protocol.Constants;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/** Chat bot to generate remote RTT chat messages. */
@TargetApi(28)
public class RttChatBot {

  interface Callback {
    void type(String text);
  }

  private static final int START_SENDING = 1;
  private static final int SEND_MESSAGE = 2;

  private static final String[] CANDIDATE_MESSAGES =
      new String[] {
        "To RTT or not to RTT, that is the question...",
        "Making TTY great again!",
        "I would be more comfortable with real \"Thyme\" chatting."
            + " I don't know how to end this pun",
        "お疲れ様でした",
        "The FCC has mandated that I respond... I will do so begrudgingly",
        "😂😂😂💯"
      };

  private final MessageHandler messageHandler;
  private final HandlerThread handlerThread;

  RttChatBot(RttTextStream rttTextStream) {
    handlerThread = new HandlerThread("RttChatBot");
    handlerThread.start();
    messageHandler = new MessageHandler(handlerThread.getLooper(), rttTextStream);
  }

  @MainThread
  public void start() {
    Assert.isMainThread();
    LogUtil.enterBlock("RttChatBot.start");
    messageHandler.sendEmptyMessage(START_SENDING);
  }

  @MainThread
  public void stop() {
    Assert.isMainThread();
    LogUtil.enterBlock("RttChatBot.stop");
    if (handlerThread != null && handlerThread.isAlive()) {
      handlerThread.quit();
    }
  }

  private static class MessageHandler extends Handler {
    private final RttTextStream rttTextStream;
    private final Random random = new Random();
    private final List<String> messageQueue = new ArrayList<>();
    private int currentTypingPosition = -1;
    private String currentTypingMessage = null;

    MessageHandler(Looper looper, RttTextStream rttTextStream) {
      super(looper);
      this.rttTextStream = rttTextStream;
    }

    @Override
    public void handleMessage(android.os.Message msg) {
      switch (msg.what) {
        case START_SENDING:
          sendMessage(obtainMessage(SEND_MESSAGE, nextTyping()));
          break;
        case SEND_MESSAGE:
          String message = (String) msg.obj;
          try {
            rttTextStream.write(message);
          } catch (IOException e) {
            LogUtil.e("RttChatBot.MessageHandler", "write message", e);
          }
          if (Constants.BUBBLE_BREAKER.equals(message)) {
            // Wait 1-11s between two messages.
            sendMessageDelayed(
                obtainMessage(SEND_MESSAGE, nextTyping()), 1000 * (1 + random.nextInt(10)));
          } else {
            // Wait up to 2s between typing.
            sendMessageDelayed(obtainMessage(SEND_MESSAGE, nextTyping()), 200 * random.nextInt(10));
          }
          break;
        default: // fall out
      }
    }

    private String nextTyping() {
      if (currentTypingPosition < 0 || currentTypingMessage == null) {
        if (messageQueue.isEmpty()) {
          String text = CANDIDATE_MESSAGES[random.nextInt(CANDIDATE_MESSAGES.length)];
          messageQueue.add(text);
        }
        currentTypingMessage = messageQueue.remove(0);
        currentTypingPosition = 0;
      }
      if (currentTypingPosition < currentTypingMessage.length()) {
        int size = random.nextInt(currentTypingMessage.length() - currentTypingPosition + 1);
        String messageToType =
            currentTypingMessage.substring(currentTypingPosition, currentTypingPosition + size);
        currentTypingPosition = currentTypingPosition + size;
        return messageToType;
      } else {
        currentTypingPosition = -1;
        currentTypingMessage = null;
        return Constants.BUBBLE_BREAKER;
      }
    }
  }
}