-
Notifications
You must be signed in to change notification settings - Fork 89
/
MainActivity.java
152 lines (127 loc) · 5.64 KB
/
MainActivity.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
package com.example.scaledrone.chat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.scaledrone.lib.Listener;
import com.scaledrone.lib.Member;
import com.scaledrone.lib.Room;
import com.scaledrone.lib.RoomListener;
import com.scaledrone.lib.Scaledrone;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements RoomListener {
// replace this with a real channelID from Scaledrone dashboard
private String channelID = "CHANNEL_ID_FROM_YOUR_SCALEDRONE_DASHBOARD";
private String roomName = "observable-room";
private EditText editText;
private Scaledrone scaledrone;
private MessageAdapter messageAdapter;
private ListView messagesView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
messageAdapter = new MessageAdapter(this);
messagesView = (ListView) findViewById(R.id.messages_view);
messagesView.setAdapter(messageAdapter);
MemberData data = new MemberData(getRandomName(), getRandomColor());
scaledrone = new Scaledrone(channelID, data);
scaledrone.connect(new Listener() {
@Override
public void onOpen() {
System.out.println("Scaledrone connection open");
scaledrone.subscribe(roomName, MainActivity.this);
}
@Override
public void onOpenFailure(Exception ex) {
System.err.println(ex);
}
@Override
public void onFailure(Exception ex) {
System.err.println(ex);
}
@Override
public void onClosed(String reason) {
System.err.println(reason);
}
});
}
public void sendMessage(View view) {
String message = editText.getText().toString();
if (message.length() > 0) {
scaledrone.publish(roomName, message);
editText.getText().clear();
}
}
@Override
public void onOpen(Room room) {
System.out.println("Conneted to room");
}
@Override
public void onOpenFailure(Room room, Exception ex) {
System.err.println(ex);
}
@Override
public void onMessage(Room room, com.scaledrone.lib.Message receivedMessage) {
final ObjectMapper mapper = new ObjectMapper();
try {
final MemberData data = mapper.treeToValue(receivedMessage.getMember().getClientData(), MemberData.class);
boolean belongsToCurrentUser = receivedMessage.getClientID().equals(scaledrone.getClientID());
final Message message = new Message(receivedMessage.getData().asText(), data, belongsToCurrentUser);
runOnUiThread(new Runnable() {
@Override
public void run() {
messageAdapter.add(message);
messagesView.setSelection(messagesView.getCount() - 1);
}
});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
private String getRandomName() {
String[] adjs = {"autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", "summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter", "patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue", "billowing", "broken", "cold", "damp", "falling", "frosty", "green", "long", "late", "lingering", "bold", "little", "morning", "muddy", "old", "red", "rough", "still", "small", "sparkling", "throbbing", "shy", "wandering", "withered", "wild", "black", "young", "holy", "solitary", "fragrant", "aged", "snowy", "proud", "floral", "restless", "divine", "polished", "ancient", "purple", "lively", "nameless"};
String[] nouns = {"waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning", "snow", "lake", "sunset", "pine", "shadow", "leaf", "dawn", "glitter", "forest", "hill", "cloud", "meadow", "sun", "glade", "bird", "brook", "butterfly", "bush", "dew", "dust", "field", "fire", "flower", "firefly", "feather", "grass", "haze", "mountain", "night", "pond", "darkness", "snowflake", "silence", "sound", "sky", "shape", "surf", "thunder", "violet", "water", "wildflower", "wave", "water", "resonance", "sun", "wood", "dream", "cherry", "tree", "fog", "frost", "voice", "paper", "frog", "smoke", "star"};
return (
adjs[(int) Math.floor(Math.random() * adjs.length)] +
"_" +
nouns[(int) Math.floor(Math.random() * nouns.length)]
);
}
private String getRandomColor() {
Random r = new Random();
StringBuffer sb = new StringBuffer("#");
while(sb.length() < 7){
sb.append(Integer.toHexString(r.nextInt()));
}
return sb.toString().substring(0, 7);
}
}
class MemberData {
private String name;
private String color;
public MemberData(String name, String color) {
this.name = name;
this.color = color;
}
public MemberData() {
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
@Override
public String toString() {
return "MemberData{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
}