Skip to content

Commit d8e3264

Browse files
author
Stephen Dimmick
committed
Chats are kinda working, but only first message
I'm able to connect to my Firebase and send a message, but subsequent messages seem to fail. Need to talk to Greg.
1 parent 8f5c51d commit d8e3264

23 files changed

+363
-0
lines changed

AndroidManifest.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.sdimmick.firebasedemo"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="8"
9+
android:targetSdkVersion="17" />
10+
11+
<uses-permission android:name="android.permission.INTERNET"/>
12+
13+
<application
14+
android:allowBackup="true"
15+
android:icon="@drawable/ic_launcher"
16+
android:label="@string/app_name"
17+
android:theme="@style/AppTheme" >
18+
19+
<activity android:name=".LoginActivity">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
26+
<activity android:name=".ChatActivity" />
27+
</application>
28+
29+
</manifest>

ic_launcher-web.png

50.4 KB
Loading

libs/android-support-v4.jar

473 KB
Binary file not shown.
Binary file not shown.

proguard-project.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# To enable ProGuard in your project, edit project.properties
2+
# to define the proguard.config property as described in that file.
3+
#
4+
# Add project specific ProGuard rules here.
5+
# By default, the flags in this file are appended to flags specified
6+
# in ${sdk.dir}/tools/proguard/proguard-android.txt
7+
# You can edit the include path and order by changing the ProGuard
8+
# include property in project.properties.
9+
#
10+
# For more details, see
11+
# http://developer.android.com/guide/developing/tools/proguard.html
12+
13+
# Add any project specific keep options here:
14+
15+
# If your project uses WebView with JS, uncomment the following
16+
# and specify the fully qualified class name to the JavaScript interface
17+
# class:
18+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19+
# public *;
20+
#}

project.properties

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system edit
7+
# "ant.properties", and override values to adapt the script to your
8+
# project structure.
9+
#
10+
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11+
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12+
13+
# Project target.
14+
target=android-17
15+
android.library.reference.1=../actionbarsherlock

res/drawable-hdpi/ic_launcher.png

7.51 KB
Loading

res/drawable-hdpi/ic_send.png

1.4 KB
Loading

res/drawable-mdpi/ic_launcher.png

3.71 KB
Loading

res/drawable-mdpi/ic_send.png

1.26 KB
Loading

res/drawable-xhdpi/ic_launcher.png

12.2 KB
Loading

res/drawable-xhdpi/ic_send.png

1.65 KB
Loading

res/drawable-xxhdpi/ic_launcher.png

24.1 KB
Loading

res/drawable/button_background.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:state_pressed="true" android:drawable="@color/button_pressed" />
4+
<item android:drawable="@android:color/transparent" />
5+
</selector>

res/layout/activity_chat.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:padding="10dp"
6+
tools:context=".ChatActivity" >
7+
8+
<ScrollView
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
android:layout_alignParentTop="true" >
12+
13+
<LinearLayout
14+
android:id="@+id/activity_chat_message_layout"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
android:orientation="vertical" />
18+
19+
</ScrollView>
20+
21+
<!-- chat input -->
22+
<EditText
23+
android:id="@+id/activity_chat_input"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:layout_alignParentBottom="true"
27+
android:layout_alignParentLeft="true"
28+
android:layout_toLeftOf="@+id/activity_chat_send"
29+
android:inputType="textCapSentences"
30+
android:hint="@string/type_here_to_chat" />
31+
32+
<!-- send button -->
33+
<ImageButton
34+
android:id="@+id/activity_chat_send"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:layout_alignParentBottom="true"
38+
android:layout_alignParentRight="true"
39+
android:src="@drawable/ic_send"
40+
android:background="@drawable/button_background"
41+
android:contentDescription="@string/send_message" />
42+
43+
</RelativeLayout>

res/layout/activity_login.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingBottom="@dimen/activity_vertical_margin"
6+
android:paddingLeft="@dimen/activity_horizontal_margin"
7+
android:paddingRight="@dimen/activity_horizontal_margin"
8+
android:paddingTop="@dimen/activity_vertical_margin"
9+
android:gravity="center"
10+
android:orientation="vertical"
11+
tools:context=".LoginActivity" >
12+
13+
<!-- username -->
14+
<EditText
15+
android:id="@+id/activity_login_username"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
android:layout_margin="20dp"
19+
android:inputType="textPersonName"
20+
android:hint="@string/enter_your_name" />
21+
22+
<!-- submit -->
23+
<Button
24+
android:id="@+id/activity_login_continue"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:text="@string/continue_"/>
28+
29+
</LinearLayout>

res/layout/chat_message.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="wrap_content"
4+
style="@android:style/TextAppearance.Medium" />

res/values/colors.xml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<color name="button_pressed">#87C8E6</color>
3+
</resources>

res/values/dimens.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
3+
<!-- Default screen margins, per the Android Design guidelines. -->
4+
<dimen name="activity_horizontal_margin">16dp</dimen>
5+
<dimen name="activity_vertical_margin">16dp</dimen>
6+
7+
</resources>

res/values/strings.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Firebase SDK Demo</string>
4+
<string name="action_settings">Settings</string>
5+
<string name="enter_your_name">Enter your name</string>
6+
<string name="continue_">Continue</string>
7+
<string name="login">Login</string>
8+
<string name="chat">Chat</string>
9+
<string name="type_here_to_chat">Type here to chat</string>
10+
<string name="send_message">Send message</string>
11+
<string name="chat_message">%1$s says %2$s</string>
12+
</resources>

res/values/styles.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme, dependent on API level. This theme is replaced
5+
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
6+
-->
7+
<style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar">
8+
<!--
9+
Theme customizations available in newer API levels can go in
10+
res/values-vXX/styles.xml, while customizations related to
11+
backward-compatibility can go here.
12+
-->
13+
</style>
14+
15+
<!-- Application theme. -->
16+
<style name="AppTheme" parent="AppBaseTheme">
17+
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
18+
</style>
19+
20+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.sdimmick.firebasedemo;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.actionbarsherlock.app.SherlockActivity;
7+
import com.firebase.client.ChildEventListener;
8+
import com.firebase.client.DataSnapshot;
9+
import com.firebase.client.Firebase;
10+
import com.firebase.client.FirebaseException;
11+
12+
import android.content.Context;
13+
import android.content.Intent;
14+
import android.os.Bundle;
15+
import android.util.Log;
16+
import android.view.LayoutInflater;
17+
import android.view.View;
18+
import android.view.ViewGroup;
19+
import android.widget.EditText;
20+
import android.widget.ImageButton;
21+
import android.widget.TextView;
22+
import android.widget.Toast;
23+
24+
public class ChatActivity extends SherlockActivity implements ChildEventListener {
25+
private static final String FIREBASE_URL = "https://android-sdk-demo.firebaseio.com/";
26+
private static final String TAG = ChatActivity.class.getSimpleName();
27+
private static final String USERNAME_KEY = "username";
28+
private static final String NAME = "name";
29+
private static final String MESSAGE = "message";
30+
31+
private String mUsername;
32+
private Firebase mFirebase;
33+
private ViewGroup mChatMessagesLayout;
34+
private EditText mChatMessageView;
35+
private ImageButton mSendButton;
36+
37+
private String mMessageUsername;
38+
private String mMessageContents;
39+
40+
public static Intent createIntent(Context context, String username) {
41+
Intent intent = new Intent(context, ChatActivity.class);
42+
intent.putExtra(USERNAME_KEY, username);
43+
return intent;
44+
}
45+
46+
@Override
47+
protected void onCreate(Bundle savedInstanceState) {
48+
super.onCreate(savedInstanceState);
49+
setContentView(R.layout.activity_chat);
50+
setTitle(R.string.chat);
51+
52+
initializeFirebase();
53+
54+
mUsername = getIntent().getStringExtra(USERNAME_KEY);
55+
56+
mChatMessagesLayout = (ViewGroup) findViewById(R.id.activity_chat_message_layout);
57+
mChatMessageView = (EditText) findViewById(R.id.activity_chat_input);
58+
mSendButton = (ImageButton) findViewById(R.id.activity_chat_send);
59+
60+
mSendButton.setOnClickListener(new View.OnClickListener() {
61+
@Override
62+
public void onClick(View v) {
63+
sendMessage();
64+
}
65+
});
66+
}
67+
68+
private void initializeFirebase() {
69+
// Initialize the Firebase client
70+
try {
71+
mFirebase = new Firebase(FIREBASE_URL);
72+
mFirebase.addChildEventListener(this);
73+
} catch (FirebaseException e) {
74+
Log.e(TAG, "Error initializing Firebase client", e);
75+
Toast.makeText(this, "Error initializing Firebase Client", Toast.LENGTH_SHORT).show();
76+
}
77+
}
78+
79+
private void sendMessage() {
80+
String message = mChatMessageView.getText().toString().trim();
81+
82+
Map<String, Object> firebaseMessage = new HashMap<String, Object>();
83+
firebaseMessage.put(NAME, mUsername);
84+
firebaseMessage.put(MESSAGE, message);
85+
86+
try {
87+
mFirebase.updateChildren(firebaseMessage);
88+
} catch (FirebaseException e) {
89+
Log.e(TAG, "Error sending message", e);
90+
Toast.makeText(this, "Error sending message", Toast.LENGTH_SHORT).show();
91+
}
92+
93+
mChatMessageView.setText("");
94+
}
95+
96+
/* ChildEventListener callbacks */
97+
98+
@Override
99+
public void onCancelled() {
100+
101+
}
102+
103+
@Override
104+
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
105+
if (snapshot.getName().equals(NAME)) {
106+
mMessageUsername = snapshot.getValue().toString();
107+
} else if (snapshot.getName().equals(MESSAGE)) {
108+
mMessageContents = snapshot.getValue().toString();
109+
}
110+
111+
if (mMessageUsername != null && mMessageContents != null) {
112+
// We have a complete message. Display it.
113+
LayoutInflater inflater = LayoutInflater.from(this);
114+
TextView chatView = (TextView) inflater.inflate(R.layout.chat_message, null, false);
115+
chatView.setText(getString(R.string.chat_message, mMessageUsername, mMessageContents));
116+
mChatMessagesLayout.addView(chatView);
117+
118+
mMessageUsername = null;
119+
mMessageContents = null;
120+
}
121+
}
122+
123+
@Override
124+
public void onChildChanged(DataSnapshot snapshot, String previousChildName) {
125+
126+
}
127+
128+
@Override
129+
public void onChildMoved(DataSnapshot snapshot, String previousChildName) {
130+
131+
}
132+
133+
@Override
134+
public void onChildRemoved(DataSnapshot snapshot) {
135+
136+
}
137+
138+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.sdimmick.firebasedemo;
2+
3+
import com.actionbarsherlock.app.SherlockActivity;
4+
5+
import android.os.Bundle;
6+
import android.content.Intent;
7+
import android.view.View;
8+
import android.widget.Button;
9+
import android.widget.EditText;
10+
11+
public class LoginActivity extends SherlockActivity {
12+
private EditText mUsernameView;
13+
private Button mContinueButton;
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.activity_login);
19+
setTitle(R.string.login);
20+
21+
mUsernameView = (EditText) findViewById(R.id.activity_login_username);
22+
mContinueButton = (Button) findViewById(R.id.activity_login_continue);
23+
24+
mContinueButton.setOnClickListener(new View.OnClickListener() {
25+
@Override
26+
public void onClick(View v) {
27+
String username = mUsernameView.getText().toString().trim();
28+
if (username.length() > 0) {
29+
// We have a username. Start the chat activity.
30+
Intent intent = ChatActivity.createIntent(LoginActivity.this, username);
31+
startActivity(intent);
32+
finish();
33+
}
34+
}
35+
});
36+
}
37+
38+
}

0 commit comments

Comments
 (0)