-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChatActivity.java
executable file
·148 lines (133 loc) · 5.13 KB
/
ChatActivity.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
package com.cloudilly.anonymous;
import java.util.ArrayList;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.cloudilly.anonymous.sdk.Cloudilly;
public class ChatActivity extends AppCompatActivity implements Cloudilly.Delegate {
private Cloudilly cloudilly;
private ListView chatsListView;
private EditText inputEditText;
private Button sendButton;
private ArrayAdapter<String> messageListAdapter;
ArrayList<String> messageList= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
String app= "<INSERT YOUR APP NAME>";
String access= "<INSERT YOUR ACCESS KEY>";
cloudilly= new Cloudilly(app, access, getApplicationContext(), new Cloudilly.CallBack() {
@Override
public void onCompleted(Exception ex, JSONObject dict) {
updateView("CONNECTING...");
cloudilly.connect();
}
});
cloudilly.addDelegate(this);
chatsListView= (ListView)findViewById(R.id.chatsListView);
inputEditText= (EditText)findViewById(R.id.inputEditText);
sendButton= (Button)findViewById(R.id.sendButton);
inputEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled= false;
if(actionId== EditorInfo.IME_ACTION_SEND) { handled= true; fireSend(); }
return handled;
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { fireSend(); }
});
messageListAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, messageList);
chatsListView.setAdapter(messageListAdapter);
}
protected void updateView(final String msg) {
if(msg== null || messageListAdapter== null) { return; }
runOnUiThread(new Runnable() {
@Override
public void run() {
messageList.add(msg);
messageListAdapter.notifyDataSetChanged();
}
});
}
private void fireSend() {
final String msg= inputEditText.getText().toString();
if(msg.length()== 0) { return; }
InputMethodManager imm= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputEditText.getWindowToken(), 0);
postMessage(inputEditText.getText().toString());
inputEditText.setText("");
}
private void postMessage(String msg) {
try {
JSONObject payload= new JSONObject();
payload.put("msg", msg);
cloudilly.post("public", payload, new Cloudilly.CallBack() {
@Override
public void onCompleted(Exception ex, JSONObject dict) {
Log.e("CLOUDILLY", "@@@@@@ POST: " + dict.toString());
try {
if (dict.get("status").toString().equalsIgnoreCase("fail")) {
Log.e("CLOUDILLY", "ERROR: Oops. Something wrong");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch(JSONException e) { e.printStackTrace(); }
}
@Override
public void socketConnected(JSONObject dict) {
Log.e("CLOUDILLY", "@@@@@@ CONNECTED: " + dict.toString());
try {
if(dict.get("status").toString().equalsIgnoreCase("fail")) { Log.e("CLOUDILLY", "ERROR: Oops. Something wrong"); return; }
updateView("CONNECTED AS " + dict.get("device").toString().toUpperCase());
cloudilly.join("public", new Cloudilly.CallBack() {
@Override
public void onCompleted(Exception ex, JSONObject dict) {
Log.e("CLOUDILLY", "@@@@@@ JOIN: " + dict.toString());
try {
if(dict.get("status").toString().equalsIgnoreCase("fail")) { Log.e("CLOUDILLY", "ERROR: Oops. Something wrong"); return; }
updateView("DEVICES PRESENT IN PUBLIC: " + dict.getInt("total_devices"));
} catch (JSONException e) { e.printStackTrace(); }
}
});
} catch (JSONException e) { e.printStackTrace(); }
}
@Override
public void socketDisconnected() {
Log.e("CLOUDILLY", "@@@@@@ DISCONNECTED");
updateView("DISCONNECTED");
}
@Override
public void socketReceivedDevice(JSONObject dict) {
Log.e("CLOUDILLY", "@@@@@@ RECEIVED DEVICE: " + dict.toString());
try {
String device= dict.get("device").toString().toUpperCase();
String action= dict.getInt("timestamp")== 0 ? "JOINED" : "LEFT";
updateView(device + " " + action + " PUBLIC");
} catch (JSONException e) { e.printStackTrace(); }
}
@Override
public void socketReceivedPost(JSONObject dict) {
Log.e("CLOUDILLY", "@@@@@@ RECEIVED POST: " + dict.toString());
try {
updateView(dict.get("device").toString().toUpperCase() + ": " + dict.getJSONObject("payload").get("msg"));
} catch (JSONException e) { e.printStackTrace(); }
}
}