forked from googlearchive/AndroidChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
130 lines (111 loc) · 4.69 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
package com.firebase.androidchat;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import java.util.Random;
public class MainActivity extends ListActivity {
// TODO: change this to your own Firebase URL
private static final String FIREBASE_URL = "https://android-chat.firebaseio-demo.com";
private String mUsername;
private Firebase mFirebaseRef;
private ValueEventListener mConnectedListener;
private ChatListAdapter mChatListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Make sure we have a mUsername
setupUsername();
setTitle("Chatting as " + mUsername);
// Setup our Firebase mFirebaseRef
mFirebaseRef = new Firebase(FIREBASE_URL).child("chat");
// Setup our input methods. Enter key on the keyboard or pushing the send button
EditText inputText = (EditText) findViewById(R.id.messageInput);
inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
sendMessage();
}
return true;
}
});
findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}
@Override
public void onStart() {
super.onStart();
// Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
final ListView listView = getListView();
// Tell our list adapter that we only want 50 messages at a time
mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, mUsername);
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
// Finally, a little indication of connection status
mConnectedListener = mFirebaseRef.getRoot().child(".info/connected").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean connected = (Boolean) dataSnapshot.getValue();
if (connected) {
Toast.makeText(MainActivity.this, "Connected to Firebase", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Disconnected from Firebase", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
// No-op
}
});
}
@Override
public void onStop() {
super.onStop();
mFirebaseRef.getRoot().child(".info/connected").removeEventListener(mConnectedListener);
mChatListAdapter.cleanup();
}
private void setupUsername() {
SharedPreferences prefs = getApplication().getSharedPreferences("ChatPrefs", 0);
mUsername = prefs.getString("username", null);
if (mUsername == null) {
Random r = new Random();
// Assign a random user name if we don't have one saved.
mUsername = "JavaUser" + r.nextInt(100000);
prefs.edit().putString("username", mUsername).commit();
}
}
private void sendMessage() {
EditText inputText = (EditText) findViewById(R.id.messageInput);
String input = inputText.getText().toString();
if (!input.equals("")) {
// Create our 'model', a Chat object
Chat chat = new Chat(input, mUsername);
// Create a new, auto-generated child of that chat location, and save our chat data there
mFirebaseRef.push().setValue(chat);
inputText.setText("");
}
}
}