Android Open Source - MMSUtility M M S Activity






From Project

Back to project page MMSUtility.

License

The source code is released under:

Apache License

If you think the Android project MMSUtility 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.sreid.mmsutility;
//  ww w  .  j a v  a 2 s  .com
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;


public class MMSActivity extends Activity {

    private EditText mMessage;
    private Button mSendButton;
    private Spinner mContact;
    private ArrayList<MMSContact> contacts;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mms);
        mMessage = (EditText) findViewById(R.id.textEditorPart);
        mSendButton = (Button) findViewById(R.id.sendMMSButton);
        mContact = (Spinner) findViewById(R.id.contactList);

        //set contact list contents

        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //first verifies that a selection has been made in the contact list
                //then verifies that the message is not empty
                //then uses a singleton class to send the message as an mms
            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mm, menu);
        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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void getContactList() {
        //gets contact list and puts names in the spot

        contacts = new ArrayList<MMSContact>();
        MMSContact tempContact = null;
        String phoneNumber = null;

        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String GIVEN_NAME = ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME;
        String LAST_NAME = ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;



        ContentResolver contentResolver = getContentResolver();

        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
        if (cursor.getCount() > 0){
            while (cursor.moveToNext()) {

                String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
                String name = cursor.getString(cursor.getColumnIndex(GIVEN_NAME)) + " " + cursor.getString(cursor.getColumnIndex(LAST_NAME));


                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));

                if(hasPhoneNumber > 0) {
                    tempContact = new MMSContact();
                    tempContact.setName(name);

                    //Get the mobile number
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);

                    while (phoneCursor.moveToNext()) {
                        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        switch (type) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                tempContact.setPhoneNumber(phoneNumber);
                                tempContact.setPhoneType("MMS");
                                break;
                            default:
                                break;
                        }
                    }
                    phoneCursor.close();

                    if(!tempContact.getName().isEmpty() && !tempContact.getPhoneNumber().isEmpty()) {
                        contacts.add(tempContact);
                    }
                }
            }
            cursor.close();
        }
        //contacts are implicitly returned by adding to class ArrayList
    }
    
    /**
     * gets the APN information for the phone so that MMS messages can be sent
     * @return apn -> apn to be used
     */
    private String getAPN() {

      //path to preferred APNs
      final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/MMSC");

      //receiving cursor to preferred APN table
      Cursor c = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);

      //moving the cursor to beginning of the table
      c.moveToFirst();

      //now the cursor points to the first preferred APN and we can get some
      //information about it
      //for example first preferred APN id    
      int index = c.getColumnIndex("_id");    //getting index of required column
      Short id = c.getShort(index);           //getting APN's id from

      //we can get APN name by the same way
      index = c.getColumnIndex("name");
      return c.getString(index); 
    }
    
    /**
     * gets the APN proxy used for MMS messages
     * @return proxy -> used for sending MMS messages
     */
    private String getProxy() {
      //path to preferred APNs
      final Uri PREFERRED_PROXY_URI = Uri.parse("content://telephony/carriers/MMSPROXY");

      //receiving cursor to preferred APN table
      Cursor c = getContentResolver().query(PREFERRED_PROXY_URI, null, null, null, null);

      //moving the cursor to beginning of the table
      c.moveToFirst();

      //now the cursor points to the first preferred APN and we can get some
      //information about it
      //for example first preferred APN id    
      int index = c.getColumnIndex("_id");    //getting index of required column
      Short id = c.getShort(index);           //getting APN's id from

      //we can get APN name by the same way
      index = c.getColumnIndex("name");
      return c.getString(index);
    }
    
    /**
     * get the port used by the carrier for MMS messages
     * @return port number -> (as a string) to be used
     */
    private String getPort() {
      //path to preferred APNs
      final Uri PREFERRED_PORT_URI = Uri.parse("content://telephony/carriers/MMSPORT");

      //receiving cursor to preferred APN table
      Cursor c = getContentResolver().query(PREFERRED_PORT_URI, null, null, null, null);

      //moving the cursor to beginning of the table
      c.moveToFirst();

      //now the cursor points to the first preferred APN and we can get some
      //information about it
      //for example first preferred APN id    
      int index = c.getColumnIndex("_id");    //getting index of required column
      Short id = c.getShort(index);           //getting APN's id from

      //we can get APN name by the same way
      index = c.getColumnIndex("name");
      return c.getString(index);
    }

}




Java Source Code List

com.sreid.mmsutility.MMSActivity.java
com.sreid.mmsutility.MMSContact.java
com.sreid.mmsutility.MMSUtility.java