Skip to content

Commit a4c6687

Browse files
committed
Whatsapp App CLone
Featured with ChatActivity Page along with MainActivity page showing All recipents of previous Chats and With separate class of UserList
1 parent 4469bff commit a4c6687

File tree

4 files changed

+422
-0
lines changed

4 files changed

+422
-0
lines changed

whatsapp/ChatActivity.java

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.lenovo.whatsapp;
2+
3+
import android.content.Intent;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.os.Bundle;
6+
import android.util.Log;
7+
import android.view.View;
8+
import android.widget.ArrayAdapter;
9+
import android.widget.EditText;
10+
import android.widget.ListView;
11+
import android.widget.Toast;
12+
13+
import com.parse.FindCallback;
14+
import com.parse.ParseException;
15+
import com.parse.ParseObject;
16+
import com.parse.ParseQuery;
17+
import com.parse.ParseUser;
18+
import com.parse.SaveCallback;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.StringTokenizer;
23+
24+
public class ChatActivity extends AppCompatActivity {
25+
26+
String activeUser = "";
27+
28+
ArrayList<String> messages = new ArrayList<>();
29+
30+
ArrayAdapter arrayAdapter;
31+
32+
public void sendChat(View view) {
33+
34+
final EditText chatEditText = (EditText) findViewById(R.id.chatEditText);
35+
36+
ParseObject message = new ParseObject("Message");
37+
38+
final String messageContent = chatEditText.getText().toString();
39+
40+
message.put("sender", ParseUser.getCurrentUser().getUsername());
41+
message.put("recipient", activeUser);
42+
message.put("message", messageContent);
43+
44+
chatEditText.setText("");
45+
46+
message.saveInBackground(new SaveCallback() {
47+
@Override
48+
public void done(ParseException e) {
49+
50+
if (e == null) {
51+
52+
messages.add(messageContent);
53+
54+
arrayAdapter.notifyDataSetChanged();
55+
56+
}
57+
58+
}
59+
});
60+
61+
}
62+
63+
@Override
64+
protected void onCreate(Bundle savedInstanceState) {
65+
super.onCreate(savedInstanceState);
66+
setContentView(R.layout.activity_chat);
67+
68+
Intent intent = getIntent();
69+
70+
activeUser = intent.getStringExtra("username");
71+
72+
setTitle("Chat with " + activeUser);
73+
74+
ListView chatListView = (ListView) findViewById(R.id.chatListView);
75+
76+
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, messages);
77+
78+
chatListView.setAdapter(arrayAdapter);
79+
80+
ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("Message");
81+
82+
query1.whereEqualTo("sender", ParseUser.getCurrentUser().getUsername());
83+
query1.whereEqualTo("recipient", activeUser);
84+
85+
ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("Message");
86+
87+
query2.whereEqualTo("recipient", ParseUser.getCurrentUser().getUsername());
88+
query2.whereEqualTo("sender", activeUser);
89+
90+
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
91+
92+
queries.add(query1);
93+
queries.add(query2);
94+
95+
ParseQuery<ParseObject> query = ParseQuery.or(queries);
96+
97+
query.orderByAscending("createdAt");
98+
99+
query.findInBackground(new FindCallback<ParseObject>() {
100+
@Override
101+
public void done(List<ParseObject> objects, ParseException e) {
102+
103+
if (e == null) {
104+
105+
if (objects.size() > 0) {
106+
107+
messages.clear();
108+
109+
for (ParseObject message : objects) {
110+
111+
String messageContent = message.getString("message");
112+
113+
if (!message.getString("sender").equals(ParseUser.getCurrentUser().getUsername())) {
114+
115+
messageContent = "---------> " + messageContent;
116+
117+
}
118+
119+
Log.i("Info", messageContent);
120+
121+
messages.add(messageContent);
122+
123+
}
124+
125+
arrayAdapter.notifyDataSetChanged();
126+
127+
}
128+
129+
}
130+
131+
}
132+
});
133+
134+
135+
136+
137+
}
138+
}

whatsapp/MainActivity.java

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.lenovo.whatsapp;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.util.Log;
7+
import android.view.View;
8+
import android.widget.Button;
9+
import android.widget.EditText;
10+
import android.widget.Switch;
11+
import android.widget.TextView;
12+
import android.widget.Toast;
13+
14+
import com.parse.LogInCallback;
15+
import com.parse.Parse;
16+
import com.parse.ParseAnalytics;
17+
import com.parse.ParseAnonymousUtils;
18+
import com.parse.ParseException;
19+
import com.parse.ParseUser;
20+
import com.parse.SaveCallback;
21+
import com.parse.SignUpCallback;
22+
23+
24+
public class MainActivity extends AppCompatActivity {
25+
26+
Boolean loginModeActive = false;
27+
28+
public void redirectIfLoggedIn() {
29+
30+
if (ParseUser.getCurrentUser() != null) {
31+
32+
Intent intent = new Intent(getApplicationContext(), UserListActivity.class);
33+
startActivity(intent);
34+
35+
}
36+
37+
}
38+
39+
public void toggleLoginMode(View view) {
40+
41+
Button loginSignupButton = (Button) findViewById(R.id.loginSignupButton);
42+
43+
TextView toggleLoginModeTextView = (TextView) findViewById(R.id.toggleLoginModeTextView);
44+
45+
if (loginModeActive) {
46+
47+
loginModeActive = false;
48+
loginSignupButton.setText("Sign Up");
49+
toggleLoginModeTextView.setText("Or, log in");
50+
51+
52+
} else {
53+
54+
loginModeActive = true;
55+
loginSignupButton.setText("Log In");
56+
toggleLoginModeTextView.setText("Or, sign up");
57+
58+
}
59+
60+
}
61+
62+
public void signupLogin(View view) {
63+
64+
EditText usernameEditText = (EditText) findViewById(R.id.usernameEditText);
65+
66+
EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
67+
68+
if (loginModeActive) {
69+
70+
ParseUser.logInInBackground(usernameEditText.getText().toString(), passwordEditText.getText().toString(), new LogInCallback() {
71+
@Override
72+
public void done(ParseUser user, ParseException e) {
73+
74+
if (e == null) {
75+
76+
Log.i("Info", "user logged in");
77+
78+
redirectIfLoggedIn();
79+
80+
} else {
81+
82+
String message = e.getMessage();
83+
84+
if (message.toLowerCase().contains("java")) {
85+
86+
message = e.getMessage().substring(e.getMessage().indexOf(" "));
87+
88+
}
89+
90+
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
91+
92+
}
93+
94+
}
95+
});
96+
97+
98+
} else {
99+
100+
ParseUser user = new ParseUser();
101+
102+
user.setUsername(usernameEditText.getText().toString());
103+
104+
user.setPassword(passwordEditText.getText().toString());
105+
106+
user.signUpInBackground(new SignUpCallback() {
107+
@Override
108+
public void done(ParseException e) {
109+
110+
if (e == null) {
111+
112+
Log.i("Info", "user signed up");
113+
114+
redirectIfLoggedIn();
115+
116+
} else {
117+
118+
String message = e.getMessage();
119+
120+
if (message.toLowerCase().contains("java")) {
121+
122+
message = e.getMessage().substring(e.getMessage().indexOf(" "));
123+
124+
}
125+
126+
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
127+
}
128+
129+
}
130+
});
131+
132+
}
133+
}
134+
135+
136+
@Override
137+
protected void onCreate(Bundle savedInstanceState) {
138+
super.onCreate(savedInstanceState);
139+
setContentView(R.layout.activity_main);
140+
141+
setTitle("Whatsapp Login");
142+
143+
redirectIfLoggedIn();
144+
145+
ParseAnalytics.trackAppOpenedInBackground(getIntent());
146+
}
147+
148+
}

whatsapp/StarterApplication.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.lenovo.whatsapp;
2+
3+
import android.app.Application;
4+
import android.util.Log;
5+
6+
import com.parse.Parse;
7+
import com.parse.ParseACL;
8+
import com.parse.ParseException;
9+
import com.parse.ParseObject;
10+
import com.parse.ParseUser;
11+
import com.parse.SaveCallback;
12+
13+
14+
public class StarterApplication extends Application {
15+
16+
@Override
17+
public void onCreate() {
18+
super.onCreate();
19+
20+
// Enable Local Datastore.
21+
Parse.enableLocalDatastore(this);
22+
23+
// Add your initialization code here
24+
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
25+
.applicationId("7b0fd2ddb7c3e089718fcbb29c230c2924cb9512")
26+
.clientKey("09cc80b858a85f4e7e99d66e0963d364bf0bbda9")
27+
.server("http://18.222.190.207:80/parse/")
28+
.build()
29+
);
30+
31+
ParseObject object = new ParseObject("ExampleObject");
32+
object.put("myNumber", "123");
33+
object.put("myString", "rob");
34+
35+
object.saveInBackground(new SaveCallback () {
36+
@Override
37+
public void done(ParseException ex) {
38+
if (ex == null) {
39+
Log.i("Parse Result", "Successful!");
40+
} else {
41+
Log.i("Parse Result", "Failed" + ex.toString());
42+
}
43+
}
44+
});
45+
46+
// ParseUser.enableAutomaticUser();
47+
48+
ParseACL defaultACL = new ParseACL();
49+
defaultACL.setPublicReadAccess(true);
50+
defaultACL.setPublicWriteAccess(true);
51+
ParseACL.setDefaultACL(defaultACL, true);
52+
53+
}
54+
}

0 commit comments

Comments
 (0)