如何使聊天屏幕与收件人一起工作

时间:2015-06-28 13:10:44

标签: android parse-platform chat

我一直在https://github.com/codepath/android_guides/wiki/Building-Simple-Chat-Client-with-Parse关注如何使用parse.com构建一个简单的聊天客户端的本教程。我已完成教程,现在当您发送消息时,它会将消息发送给拥有该应用的任何人。如何制作以便用户可以选择收件人?我的聊天客户端代码与上面教程中的代码相同。

以下是我的RecipientsActivity代码

public class RecipientsActivity extends Activity {

    public static final String TAG = RecipientsActivity.class.getSimpleName();

    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;
    protected List<ParseUser> mFriends;
    protected MenuItem mSendMenuItem;
    protected Uri mMediaUri;
    protected String mFileType;
    protected GridView mGridView;
    protected String mTextMessage;
    protected int mMemeImage;
    protected String mTextTop;
    protected String mTextBottom;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.user_grid);
        // Show the Up button in the action bar.
        setupActionBar();

        mGridView = (GridView) findViewById(R.id.friendsGrid);
        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        mGridView.setOnItemClickListener(mOnItemClickListener);

        TextView emptyTextView = (TextView) findViewById(android.R.id.empty);
        mGridView.setEmptyView(emptyTextView);

        mFileType = getIntent().getExtras().getString(
                ParseConstants.KEY_FILE_TYPE);
        if (mFileType.equals(ParseConstants.TYPE_TEXT_MESSAGE)) {
            mTextMessage = getIntent().getExtras().getString(
                    ParseConstants.KEY_TEXT);
        }

        else if (mFileType.equals(ParseConstants.TYPE_MEME)) {


            mMemeImage = getIntent().getExtras().getInt(
                    ParseConstants.KEY_MEME_IMAGE);
            mTextTop = getIntent().getExtras().getString(
                    ParseConstants.KEY_MEME_TOP_TEXT);
            mTextBottom = getIntent().getExtras().getString(
                    ParseConstants.KEY_MEME_BOTTOM_TEXT);
        }

        else {
            mMediaUri = getIntent().getData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser
                .getRelation(ParseConstants.KEY_FRIENDS_RELATION);

        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
        query.addAscendingOrder(ParseConstants.KEY_USERNAME);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                setProgressBarIndeterminateVisibility(false);

                if (e == null) {
                    mFriends = friends;

                    String[] usernames = new String[mFriends.size()];
                    int i = 0;
                    for (ParseUser user : mFriends) {
                        usernames[i] = user.getUsername();
                        i++;
                    }

                    if (mGridView.getAdapter() == null) {
                        UserAdapter adapter = new UserAdapter(
                                RecipientsActivity.this, mFriends);
                        mGridView.setAdapter(adapter);
                    } else {
                        ((UserAdapter) mGridView.getAdapter()).refill(mFriends);
                    }
                } else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            RecipientsActivity.this);
                    builder.setMessage(e.getMessage())
                            .setTitle(R.string.error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    /**
     * Set up the {@link android.app.ActionBar}.
     */
    private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.recipients, menu);
        mSendMenuItem = menu.getItem(0);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.action_send:
            ParseObject message = createMessage();
            if (message == null) {
                // error
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.error_selecting_file)
                        .setTitle(R.string.error_selecting_file_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                send(message);
                finish();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected ParseObject createMessage() {
        ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
        message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser()
                .getObjectId());
        message.put(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser()
                .getUsername());
        message.put(ParseConstants.KEY_RECIPIENT_IDS, getRecipientIds());
        message.put(ParseConstants.KEY_FILE_TYPE, mFileType);


        if (mFileType.equals(ParseConstants.TYPE_TEXT_MESSAGE)) {
            message.put(ParseConstants.KEY_TEXT, mTextMessage);
        } else if (mFileType.equals(ParseConstants.TYPE_MEME)) {
            message.put(ParseConstants.KEY_MEME_IMAGE, mMemeImage);
            message.put(ParseConstants.KEY_MEME_TOP_TEXT, mTextTop);
            message.put(ParseConstants.KEY_MEME_BOTTOM_TEXT, mTextBottom);
        } 

        else {
            // Process and attach media file
            byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);

            if (fileBytes == null) {
                return null;
            } else {
                if (mFileType.equals(ParseConstants.TYPE_IMAGE)) {
                    fileBytes = FileHelper.reduceImageForUpload(fileBytes);
                }

                String fileName = FileHelper.getFileName(this, mMediaUri,
                        mFileType);
                ParseFile file = new ParseFile(fileName, fileBytes);
                message.put(ParseConstants.KEY_FILE, file);

                return message;
            }
        }
        return message;
    }

    protected ArrayList<String> getRecipientIds() {
        ArrayList<String> recipientIds = new ArrayList<String>();
        for (int i = 0; i < mGridView.getCount(); i++) {
            if (mGridView.isItemChecked(i)) {
                recipientIds.add(mFriends.get(i).getObjectId());
            }
        }
        return recipientIds;
    }

    protected void send(ParseObject message) {
        message.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    // success!
                    Toast.makeText(RecipientsActivity.this,
                            R.string.success_message, Toast.LENGTH_LONG).show();
                    sendPushNotifications();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            RecipientsActivity.this);
                    builder.setMessage(R.string.error_sending_message)
                            .setTitle(R.string.error_selecting_file_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    protected OnItemClickListener mOnItemClickListener = new OnItemClickListener() {


@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        if (mGridView.getCheckedItemCount() > 0) {
            mSendMenuItem.setVisible(true);
        } else {
            mSendMenuItem.setVisible(false);
        }

        ImageView checkImageView = (ImageView) view
                .findViewById(R.id.checkImageView);

        if (mGridView.isItemChecked(position)) {
            // add the recipient
            checkImageView.setVisibility(View.VISIBLE);
        } else {
            // remove the recipient
            checkImageView.setVisibility(View.INVISIBLE);
        }
    }
};

protected void sendPushNotifications() {
    ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
    query.whereContainedIn(ParseConstants.KEY_USER_ID, getRecipientIds());

    // send push notification
    ParsePush push = new ParsePush();
    push.setQuery(query);
    push.setMessage(getString(R.string.push_message, ParseUser
            .getCurrentUser().getUsername()));
    push.sendInBackground();
 }
}

0 个答案:

没有答案
相关问题