Android Open Source - ribbit Recipients Activity






From Project

Back to project page ribbit.

License

The source code is released under:

MIT License

If you think the Android project ribbit 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

package com.lleshiart.ribbit;
/*w w w  .  ja  v a 2 s .  com*/
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class RecipientsActivity extends ListActivity {
  
  public static final String TAG = RecipientsActivity.class.getSimpleName();
  
  protected ParseRelation<ParseUser> mFriendsRelation;
  protected List<ParseUser> mFriends;
  protected ParseUser mCurrentUser;
  
  protected MenuItem mSendMenuItem;
  
  protected Uri mMediaUri;
  protected String mFileType;
  protected String mMessageText;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_recipients);

    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
    mMediaUri = getIntent().getData();
    mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);
    mMessageText = getIntent().getExtras().getString(ParseConstants.KEY_MESSAGE);
  }
  
  @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++;
        }
        
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
            RecipientsActivity.this, 
            android.R.layout.simple_list_item_checked, 
            usernames);
        setListAdapter(adapter);
        }
        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();
        }
      }
    });
  }

  @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.findItem(R.id.action_send);
    return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id) {
      case R.id.action_send:
        ParseObject message = createMessage();
        if (message == null) {
          // error
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle(R.string.error_selecting_file_title)
            .setMessage(R.string.error_selecting_file)
            .setPositiveButton(android.R.string.ok, null);
          AlertDialog dialog = builder.create();
          dialog.show();
        }
        else {
          send(message);
          finish();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
  }
  
  @Override
  public boolean onPreparePanel(int featureId, View view, Menu menu) {
    super.onPreparePanel(featureId, view, menu);
    return true;
  }
  
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    
    if (l.getCheckedItemCount() > 0) {
      mSendMenuItem.setVisible(true);
    }
    else {
      mSendMenuItem.setVisible(false);
    }
  }
  
  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)) {
      // Send the text message!
      message.put(ParseConstants.KEY_MESSAGE, mMessageText);
      return message;
    }
    else {
      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;
      }
    }
  }
  
  protected ArrayList<String> getRecipientIds() {
    ArrayList<String> recipientIds = new ArrayList<String>();
    for (int i = 0; i < getListView().getCount(); i++) {
      if (getListView().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) {
          // Sucess!
          Toast.makeText(RecipientsActivity.this, R.string.success_message, Toast.LENGTH_LONG).show();
        }
        else {
          AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
          builder.setTitle(R.string.error_selecting_file_title)
            .setMessage(R.string.error_sending_message)
            .setPositiveButton(android.R.string.ok, null);
          AlertDialog dialog = builder.create();
          dialog.show();
        }
      }
    });
  }
}




Java Source Code List

com.lleshiart.ribbit.EditFriendsActivity.java
com.lleshiart.ribbit.FileHelper.java
com.lleshiart.ribbit.ForgotPasswordActivity.java
com.lleshiart.ribbit.FriendsFragment.java
com.lleshiart.ribbit.ImageResizer.java
com.lleshiart.ribbit.InboxFragment.java
com.lleshiart.ribbit.LoginActivity.java
com.lleshiart.ribbit.MainActivity.java
com.lleshiart.ribbit.MessageAdapter.java
com.lleshiart.ribbit.NewMessageActivity.java
com.lleshiart.ribbit.ParseConstants.java
com.lleshiart.ribbit.RecipientsActivity.java
com.lleshiart.ribbit.SectionsPagerAdapter.java
com.lleshiart.ribbit.SignUpActivity.java
com.lleshiart.ribbit.ViewImageActivity.java