Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SmsModule.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.contexthome.modules;
  23. import android.content.ContentUris;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.database.Cursor;
  27. import android.net.Uri;
  28. import android.os.Handler;
  29. import android.provider.Contacts;
  30. import android.provider.Contacts.People;
  31. import android.text.Html;
  32. import android.view.View;
  33. import android.view.ViewGroup;
  34. import android.widget.ImageView;
  35. import android.widget.LinearLayout;
  36. import android.widget.LinearLayout.LayoutParams;
  37. import android.widget.TextView;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import uk.co.md87.android.contexthome.DataHelper;
  41. import uk.co.md87.android.contexthome.Module;
  42. import uk.co.md87.android.contexthome.R;
  43. /**
  44. * A module which displays SMS messages.
  45. *
  46. * @author chris
  47. */
  48. public class SmsModule extends Module {
  49. private static final Uri INBOX_URI = Uri.parse("content://sms/inbox");
  50. public SmsModule(DataHelper helper) {
  51. super(helper);
  52. }
  53. /** {@inheritDoc} */
  54. @Override
  55. public void addViews(final ViewGroup parent, final Context context, final int weight) {
  56. final Handler handler = new Handler();
  57. new Thread(new Runnable() {
  58. public void run() {
  59. final Cursor cursor = context.getContentResolver().query(INBOX_URI,
  60. new String[] { "thread_id", "date", "body", "address" }, null, null, "date DESC");
  61. final int idIndex = cursor.getColumnIndex("thread_id");
  62. final int bodyIndex = cursor.getColumnIndex("body");
  63. final int addressIndex = cursor.getColumnIndex("address");
  64. final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
  65. LayoutParams.WRAP_CONTENT);
  66. params.weight = 1;
  67. final List<View> views = new ArrayList<View>(weight);
  68. boolean success = cursor.moveToFirst();
  69. for (int i = 0; i < weight && success; i++) {
  70. final String body = cursor.getString(bodyIndex);
  71. final String address = cursor.getString(addressIndex);
  72. final View view = getView(context, body, address, cursor.getLong(idIndex));
  73. handler.post(new Runnable() {
  74. public void run() {
  75. parent.addView(view);
  76. }
  77. });
  78. success = cursor.moveToNext();
  79. }
  80. cursor.close();
  81. }
  82. }).start();
  83. }
  84. private View getView(final Context context, String text,
  85. String address, final long threadId) {
  86. final View view = View.inflate(context, R.layout.titledimage, null);
  87. view.setClickable(true);
  88. view.setFocusable(true);
  89. view.setOnClickListener(new View.OnClickListener() {
  90. public void onClick(View arg0) {
  91. final Intent intent = new Intent();
  92. intent.setAction(Intent.ACTION_VIEW);
  93. intent.setData(Uri.parse("content://mms-sms/conversations/" + threadId));
  94. context.startActivity(intent);
  95. }
  96. });
  97. final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
  98. Uri.encode(address));
  99. final Cursor cursor = context.getContentResolver().query(contactUri,
  100. new String[] { Contacts.Phones.PERSON_ID,
  101. Contacts.Phones.DISPLAY_NAME }, null, null, null);
  102. final TextView title = (TextView) view.findViewById(R.id.title);
  103. final ImageView image = (ImageView) view.findViewById(R.id.image);
  104. if (cursor.moveToFirst()) {
  105. title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
  106. .getColumnIndex(Contacts.Phones.DISPLAY_NAME)) + "</b>"));
  107. Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
  108. cursor.getLong(cursor.getColumnIndex(Contacts.Phones.PERSON_ID)));
  109. image.setImageBitmap(Contacts.People.loadContactPhoto(context,
  110. uri, R.drawable.blank, null));
  111. } else {
  112. title.setText(Html.fromHtml("<b>" + address + "</b>"));
  113. }
  114. cursor.close();
  115. final TextView body = (TextView) view.findViewById(R.id.body);
  116. body.setText(text);
  117. return view;
  118. }
  119. }