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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.database.Cursor;
  26. import android.net.Uri;
  27. import android.provider.Contacts;
  28. import android.provider.Contacts.People;
  29. import android.text.Html;
  30. import android.view.View;
  31. import android.view.ViewGroup;
  32. import android.widget.ImageView;
  33. import android.widget.LinearLayout;
  34. import android.widget.LinearLayout.LayoutParams;
  35. import android.widget.TextView;
  36. import uk.co.md87.android.contexthome.Module;
  37. import uk.co.md87.android.contexthome.R;
  38. /**
  39. * A module which displays SMS messages.
  40. *
  41. * @author chris
  42. */
  43. public class SmsModule implements Module {
  44. private static final Uri INBOX_URI = Uri.parse("content://sms/inbox");
  45. /** {@inheritDoc} */
  46. @Override
  47. public View getView(final Context context, final int weight) {
  48. final LinearLayout layout = new LinearLayout(context);
  49. layout.setOrientation(LinearLayout.VERTICAL);
  50. final Cursor cursor = context.getContentResolver().query(INBOX_URI,
  51. new String[] { "_id", "date", "body", "address" }, null, null, "date DESC");
  52. final int bodyIndex = cursor.getColumnIndex("body");
  53. final int addressIndex = cursor.getColumnIndex("address");
  54. final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
  55. LayoutParams.WRAP_CONTENT);
  56. params.weight = 1;
  57. boolean success = cursor.moveToFirst();
  58. for (int i = 0; i < weight && success; i++) {
  59. final String body = cursor.getString(bodyIndex);
  60. final String address = cursor.getString(addressIndex);
  61. layout.addView(getView(context, layout, body, address), params);
  62. success = cursor.moveToNext();
  63. }
  64. cursor.close();
  65. return layout;
  66. }
  67. private View getView(Context context, ViewGroup layout, String text, String address) {
  68. final View view = View.inflate(context, R.layout.titledimage, null);
  69. final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
  70. Uri.encode(address));
  71. final Cursor cursor = context.getContentResolver().query(contactUri,
  72. new String[] { Contacts.Phones.PERSON_ID,
  73. Contacts.Phones.DISPLAY_NAME }, null, null, null);
  74. final TextView title = (TextView) view.findViewById(R.id.title);
  75. final ImageView image = (ImageView) view.findViewById(R.id.image);
  76. if (cursor.moveToFirst()) {
  77. title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
  78. .getColumnIndex(Contacts.Phones.DISPLAY_NAME)) + "</b>"));
  79. Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
  80. cursor.getLong(cursor.getColumnIndex(Contacts.Phones.PERSON_ID)));
  81. image.setImageBitmap(Contacts.People.loadContactPhoto(context,
  82. uri, R.drawable.icon, null));
  83. } else {
  84. title.setText(Html.fromHtml("<b>" + address + "</b>"));
  85. }
  86. cursor.close();
  87. final TextView body = (TextView) view.findViewById(R.id.body);
  88. body.setText(text);
  89. return view;
  90. }
  91. }