Android Open Source - AndroidHappyChat Social Talk Activity

From Project

Back to project page AndroidHappyChat.


The source code is released under:

Apache License

If you think the Android project AndroidHappyChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

 * Copyright (c) 2013 Google Inc./*from   ww w.  j a  v a 2  s  .c  om*/
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

 * Sample app for CloudMessage API of Mobile Backend Starter. It subscribes to
 * any posts that includes specified keywords.
public class SocialTalkActivity extends CloudBackendActivity {

  // data formatter for formatting createdAt property
  private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss ", Locale.US);

  // UI components
  private TextView tvPosts;
  private EditText etMessage;

  // a list of posts on the UI
  List<CloudEntity> posts = new LinkedList<CloudEntity>();

  // initialize UI
  protected void onCreate(Bundle savedInstanceState) {
    tvPosts = (TextView) findViewById(;
    etMessage = (EditText) findViewById(;

  protected void onPostCreate() {

    // create handler for Cloud Messages
    CloudCallbackHandler<List<CloudEntity>> handler = new CloudCallbackHandler<List<CloudEntity>>() {
      public void onComplete(List<CloudEntity> messages) {
        for (CloudEntity ce : messages) {
          posts.add(0, ce);

    // receive all posts that includes "#dog" or "#cat" hashtags
    getCloudBackend().subscribeToCloudMessage("#dog", handler, 50);
    getCloudBackend().subscribeToCloudMessage("#cat", handler, 50);

  // convert posts into string and update UI
  private void updateTimelineUI() {
    final StringBuilder sb = new StringBuilder();
    for (CloudEntity post : posts) {
      sb.append(sdf.format(post.getCreatedAt()) + getCreatorName(post) + ": " + post.get("message")
          + "\n");

  // removing the domain name part from email address
  private String getCreatorName(CloudEntity b) {
    if (b.getCreatedBy() != null) {
      return " " + b.getCreatedBy().replaceFirst("@.*", "");
    } else {
      return "<anonymous>";

  // post a new message to all subscribers of each hashtags
  public void onSendButtonPressed(View view) {
    String msg = etMessage.getText().toString();
    for (String token : msg.split(" ")) {
      if (token.startsWith("#")) {
        CloudEntity cm = getCloudBackend().createCloudMessage(token);
        cm.put("message", msg);

Java Source Code List