com.easemob.chatuidemo.activity.NewGroupActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.easemob.chatuidemo.activity.NewGroupActivity.java

Source

/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 * <p/>
 * 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
 * http://www.apache.org/licenses/LICENSE-2.0
 * 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.
 */
package com.easemob.chatuidemo.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.boshu.utils.Model;
import com.lifeisle.android.R;
import com.lifeisle.jekton.util.network.AutoLoginRequest;

import org.json.JSONObject;

import java.util.Map;

public class NewGroupActivity extends BaseActivity {
    private EditText groupNameEditText;
    //private ProgressDialog progressDialog;
    private EditText introductionEditText;
    private CheckBox checkBox;
    private CheckBox memberCheckbox;
    private LinearLayout openInviteContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_group);
        groupNameEditText = (EditText) findViewById(R.id.edit_group_name);
        introductionEditText = (EditText) findViewById(R.id.edit_group_introduction);
        checkBox = (CheckBox) findViewById(R.id.cb_public);
        memberCheckbox = (CheckBox) findViewById(R.id.cb_member_inviter);
        openInviteContainer = (LinearLayout) findViewById(R.id.ll_open_invite);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    openInviteContainer.setVisibility(View.INVISIBLE);
                } else {
                    openInviteContainer.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    /**
     * @param v
     */
    public void save(View v) {
        String str6 = getResources().getString(R.string.Group_name_cannot_be_empty);
        String name = groupNameEditText.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Intent intent = new Intent(this, AlertDialog.class);
            intent.putExtra("msg", str6);
            startActivity(intent);
        } else {
            // 
            startActivityForResult(new Intent(this, GroupPickContactsActivity.class).putExtra("groupName", name),
                    0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String st1 = getResources().getString(R.string.Is_to_create_a_group_chat);
        final String st2 = getResources().getString(R.string.Failed_to_create_groups);
        if (resultCode == RESULT_OK) {
            //
            /*   progressDialog = new ProgressDialog(this);
               progressDialog.setMessage(st1);
               progressDialog.setCanceledOnTouchOutside(false);
               progressDialog.show();*/

            /*  new Thread(new Runnable() {
            @Override
            public void run() {*/
            // sdk
            String groupName = groupNameEditText.getText().toString().trim();
            String desc = introductionEditText.getText().toString();
            String[] members = data.getStringArrayExtra("newmembers");
            StringBuffer sb = new StringBuffer();
            ;
            for (int i = 0; i < members.length; i++) {

                String sb_child;
                if (i != members.length - 1) {
                    sb_child = members[i] + ",";
                } else {
                    sb_child = members[i];
                }
                sb.append(sb_child);
            }

            try {

                if (checkBox.isChecked()) {
                    setGroup(groupName, desc, "1", sb.toString(), "0");
                    //  progressDialog.dismiss();

                    //???
                    //???????
                    //  EMGroupManager.getInstance().createPublicGroup(groupName, desc, members, true, 200);
                } else {
                    if (memberCheckbox.isChecked()) {
                        setGroup(groupName, desc, "0", sb.toString(), "1");
                    } else {
                        setGroup(groupName, desc, "0", sb.toString(), "0");

                    }
                    //?o
                    //EMGroupManager.getInstance().createPrivateGroup(groupName, desc, members, memberCheckbox.isChecked(), 200);
                }
                /* runOnUiThread(new Runnable() {
                     public void run() {
                         progressDialog.dismiss();
                         setResult(RESULT_OK);
                         finish();
                     }
                 });*/
            } catch (final Exception e) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        //progressDialog.dismiss();
                        Toast.makeText(NewGroupActivity.this, st2 + e.getLocalizedMessage(), 1).show();
                    }
                });
            }

            /*      }
              }).start();*/
        }
    }

    public void back(View view) {
        finish();
    }

    public void setGroup(final String groupName, final String groupIntroduce, final String open,
            final String number, final String allow_invite) {
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setMessage("");
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        Log.i("FriMsg", groupName + groupIntroduce + open + number + allow_invite);
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        requestQueue.start();

        requestQueue.add(new AutoLoginRequest(this, Request.Method.POST, Model.PathLoad,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i("FriMsg", response.toString());
                        try {
                            if (response.getString("status").equals("0")) {
                                Toast.makeText(NewGroupActivity.this, "?", 0).show();
                                NewGroupActivity.this.finish();
                            }

                        } catch (Exception e) {
                            Log.e("Activity_boshu_FriMsg", "?");
                        }
                        pd.dismiss();

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i("FriMsg", error.getMessage());
                        pd.dismiss();

                    }
                }) {
            @Override
            protected void setParams(Map<String, String> params) {
                params.put("sys", "msg");
                params.put("ctrl", "msger");
                params.put("action", "crt_group");
                params.put("group_name", groupName);
                params.put("introduce", groupIntroduce);
                params.put("maxusers", "5000");
                params.put("public", open);
                params.put("approval", "1");
                params.put("members", number);
                params.put("allow_invite", allow_invite);

            }
        });
    }
}