Return Create MMS service and route MMS to Emulator
1: package com.voipplus.mmsclient.testMMS;
2:
3: import android.content.ContentValues;
4: import android.content.Context;
5: import android.content.res.AssetManager;
6: import android.net.Uri;
7: import android.provider.Telephony;
8: import android.telephony.SmsManager;
9: import android.telephony.SmsMessage;
10: import android.util.Log;
11:
12: import java.io.File;
13: import java.io.IOException;
14: import java.io.InputStream;
15: import java.util.Random;
16:
17:
18: import android.provider.Telephony.Mms;
19: import android.provider.Telephony.Mms.Part;
20: import android.provider.Telephony.MmsSms;
21: import android.provider.Telephony.MmsSms.PendingMessages;
22: import android.provider.Telephony.Sms;
23: import android.provider.Telephony.Sms.Intents;
24: import android.telephony.SmsMessage;
25: import android.text.TextUtils;
26: import android.util.Log;
27: import android.provider.Telephony.Mms.Addr;
28: import android.provider.Telephony.Mms.Rate;
29: import android.provider.Telephony.Mms.Sent;
30: import android.provider.Telephony.Mms.Draft;
31: import android.provider.Telephony.Mms.Inbox;
32: import android.provider.Telephony.Mms.Outbox;
33: import android.provider.Telephony.Mms.Part;
34:
35:
36: public class MMSTestGenerator {
37:
38: private static final String TAG = "MMSTestGenerator";
39:
40: public static void generateTestMMS(Context context, String senderNumber, com.voipplus.mmsclient.mms.MMSType mmsType) {
41: Log.d(TAG, "generateTestMMS: Generating test MMS of type: " + mmsType);
42: Uri mmsUri = null;
43: try {
44: // Create a new MMS message
45: ContentValues values = new ContentValues();
46: values.put(Telephony.Mms.MESSAGE_BOX, Telephony.Mms.MESSAGE_BOX_INBOX);
47: values.put(Telephony.Mms.SUBJECT, "Test MMS Subject");
48: values.put(Telephony.Mms.DATE, System.currentTimeMillis() / 1000L);
49: values.put(Telephony.Mms.READ, 1);
50: values.put(Telephony.Mms.SEEN, 1);
51: values.put(Telephony.Mms.LOCKED, 0);
52: values.put(Telephony.Mms.THREAD_ID, new Random().nextLong());
53: /* values.put(Telephony.Mms.MESSAGE_TYPE, Telephony.Mms.MESSAGE_TYPE_SEND_REQ);
54: values.put(Telephony.Mms.STATUS, Telephony.Mms.STATUS_RETRIEVED);
55: values.put(Telephony.Mms.ADDRESS, senderNumber);*/
56: mmsUri = context.getContentResolver().insert(Telephony.Mms.CONTENT_URI, values);
57: String mmsId = mmsUri.getLastPathSegment();
58: addAddress(context, mmsId, senderNumber);
59:
60: // Insert the MMS message into the MMS provider
61: Uri uri = context.getContentResolver().insert(Telephony.Mms.CONTENT_URI, values);
62:
63: switch (mmsType) {
64: case TEXT_ONLY:
65: addTextPart(context, mmsId);
66: break;
67: case TEXT_AND_IMAGE:
68: addTextPart(context, mmsId);
69: addMediaPart(context, mmsId, "tst1.png", "image/png");
70: break;
71: case MIXED_MEDIA_WITH_VIDEO:
72: addTextPart(context, mmsId);
73: addMediaPart(context, mmsId, "tst1.mp4", "video/mp4");
74: break;
75: case MIXED_MEDIA_WITH_SOUND:
76: addTextPart(context, mmsId);
77: addMediaPart(context, mmsId, "tst1.mp3", "audio/mpeg");
78: break;
79: case MIXED_MEDIA_WITH_VIDEO_AND_SOUND:
80: addTextPart(context, mmsId);
81: addMediaPart(context, mmsId, "tst1.mp4", "video/mp4");
82: addMediaPart(context, mmsId, "tst1.mp3", "audio/mpeg");
83: break;
84: }
85:
86: Log.d(TAG, "generateTestMMS: Test MMS generated successfully");
87: } catch (Exception e) {
88: Log.e(TAG, "generateTestMMS: Error generating test MMS", e);
89: }
90: }
91: private static void addAddress(Context context, String mmsId, String address) {
92: ContentValues addrValues = new ContentValues();
93: addrValues.put(Telephony.Mms.Addr.MSG_ID, mmsId);
94: addrValues.put(Telephony.Mms.Addr.ADDRESS, address);
95: addrValues.put(Telephony.Mms.Addr.TYPE, 137); // 137 is the type for "from"address
96: addrValues.put(Telephony.Mms.Addr.CHARSET, 106); // UTF-8
97: Uri addrUri = Uri.parse("content://mms/" + mmsId + "/addr");
98: context.getContentResolver().insert(addrUri, addrValues);
99: }
100:
101: private static void addTextPart(Context context, String mmsUri) {
102: AssetManager assetManager = context.getAssets();
103: InputStream inputStream = null;
104: try {
105: inputStream = assetManager.open("tst1.txt"); // Open the file from assets
106: int size = inputStream.available();
107: byte[] buffer = new byte[size];
108: inputStream.read(buffer);
109: String text = new String(buffer);
110:
111: ContentValues partValues = new ContentValues();
112: partValues.put(Telephony.Mms.Part.MSG_ID, mmsUri);
113: partValues.put(Telephony.Mms.Part.SEQ, 0);
114: partValues.put(Telephony.Mms.Part.CONTENT_TYPE, "text/plain");
115: partValues.put(Telephony.Mms.Part.TEXT, text);
116: context.getContentResolver().insert(Telephony.Mms.Part.CONTENT_URI, partValues);
117: } catch (IOException e) {
118: Log.e(TAG, "addTextPart: Error adding textpart", e);
119: } finally {
120: if (inputStream != null) {
121: try {
122: inputStream.close();
123: } catch (IOException e) {
124: Log.e(TAG, "addTextPart: Error closing input stream", e);
125: }
126: }
127: }
128: }
129:
130: private static <T> void addMediaPart(Context context, String mmsId, String assetFileName, String contentType) {
131: AssetManager assetManager = context.getAssets();
132: InputStream inputStream = null;
133: try {
134: inputStream = assetManager.open(assetFileName);
135: byte[] mediaData = new byte[inputStream.available()];
136: inputStream.read(mediaData);
137:
138: ContentValues partValues = new ContentValues();
139: partValues.put(Telephony.Mms.Part.MSG_ID, mmsId);partValues.put(Telephony.Mms.Part.SEQ, 1);
140: partValues.put(Telephony.Mms.Part.CONTENT_TYPE, contentType);
141: partValues.put("_data", mediaData);
142: partValues.put(Telephony.Mms.Part.FILENAME, new File(assetFileName).getName());
143: context.getContentResolver().insert(Telephony.Mms.Part.CONTENT_URI, partValues);
144: } catch (IOException e) {
145: Log.e(TAG, "addMediaPart: Error adding media part", e);
146: } finally {
147: if (inputStream != null) {
148: try {
149: inputStream.close();
150: } catch (IOException e) {
151: Log.e(TAG, "addMediaPart: Error closing input stream", e);
152: }
153: }
154: }
155: }
156: }
157:
Return
AndroidMosaic context:
- 2025 year: Resolve class conflicts #AndroidMosaic
- 2025 year: Application level parameters in configuration #AndroidMosaic
- 2025 year: Create test with junit and pass or dismiss test before application launch #AndroidMosaic
- 2025 year: Ask special permission and listen answer #AndroidMosaic
- 2025 year: Service lifecycle #AndroidMosaic
- 2025 year: AtomicInteger and accept service callback in UI thread, stop progressBar that was run in UiThread #AndroidMosaic
- 2025 year: Generate random data and pass it to RecyclerView with Adapter #AndroidMosaic
- 2025 year: Pass data to new form with Intent and getParcelableExtra #AndroidMosaic
- 2025 year: Access to Contact list #AndroidMosaic
- 2025 year: Using Query and Cursor to read data sequence and fill DataAdapter for RecyclerView #AndroidMosaic
- 2025 year: Custom TabSwithcer #AndroidMosaic
- 2025 year: Custom Dropdownlist with default value based on Spinner #AndroidMosaic
- 2025 year: Create MMS service and route MMS to Emulator #AndroidMosaic
- 2025 year: Vector drawable - fastest way to create icons #AndroidMosaic
- 2025 year: Technique to create Button with own rectangle shape #AndroidMosaic
- 2025 year: Simple HTTP service #AndroidMosaic
- 2025 year: Simple key-value service with Local Storage #AndroidMosaic
- 2025 year: Cryptographic local storage key-value service #AndroidMosaic
- 2025 year: WebRTC service #AndroidMosaic
- 2025 year: SIP service #AndroidMosaic
- 2025 year: Global objects with references to all application services #AndroidMosaic
Comments (
)

Link to this page:
http://www.vb-net.com/AndroidMosaic/MMSTestGenerator.htm
|