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.

EmailModule.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.widget.ImageView;
  32. import android.widget.LinearLayout;
  33. import android.widget.LinearLayout.LayoutParams;
  34. import android.widget.TextView;
  35. import uk.co.md87.android.contexthome.Module;
  36. import uk.co.md87.android.contexthome.R;
  37. /**
  38. * A module which displays SMS messages.
  39. *
  40. * @author chris
  41. */
  42. public class EmailModule implements Module {
  43. private static final Uri INBOX_URI = Uri.parse("content://gmail-ls/"
  44. + "conversations/chris87@gmail.com");
  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[] { "conversation_id" }, null, null, null);
  52. final int convIdIndex = cursor.getColumnIndex("conversation_id");
  53. final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
  54. LayoutParams.WRAP_CONTENT);
  55. params.weight = 1;
  56. boolean success = cursor.moveToFirst();
  57. for (int i = 0; i < weight && success; i++) {
  58. final long convId = cursor.getLong(convIdIndex);
  59. final Uri uri = INBOX_URI.buildUpon().appendEncodedPath(String.valueOf(convId)
  60. + "/messages").build();
  61. final Cursor messageCursor = context.getContentResolver().query(uri,
  62. new String[] { "fromAddress", "subject", "messageId" }, null, null, null);
  63. messageCursor.moveToFirst();
  64. final int subjectIndex = messageCursor.getColumnIndex("subject");
  65. final int addressIndex = messageCursor.getColumnIndex("fromAddress");
  66. final String body = messageCursor.getString(subjectIndex);
  67. final String address = messageCursor.getString(addressIndex);
  68. final int count = messageCursor.getCount();
  69. layout.addView(getView(context, body, address, count), params);
  70. messageCursor.close();
  71. success = cursor.moveToNext();
  72. }
  73. cursor.close();
  74. return layout;
  75. }
  76. private View getView(Context context, String text, String address, int count) {
  77. final View view = View.inflate(context, R.layout.titledimage, null);
  78. final Uri contactUri = Uri.parse("content://contacts/contact_methods");
  79. final String name = address.replaceAll("^.*\"(.*?)\".*$", "$1")
  80. .replaceAll("(.*), (.*)", "$2 $1");
  81. final String email = address.replaceAll("^.*<(.*?)>.*$", "$1");
  82. final Cursor cursor = context.getContentResolver().query(contactUri,
  83. new String[] { "person", "name" }, "data LIKE ?", new String[] { email }, null);
  84. final TextView title = (TextView) view.findViewById(R.id.title);
  85. final ImageView image = (ImageView) view.findViewById(R.id.image);
  86. if (cursor.moveToFirst()) {
  87. title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
  88. .getColumnIndex("name")) + "</b> (" + count + ")"));
  89. Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
  90. cursor.getLong(cursor.getColumnIndex("person")));
  91. image.setImageBitmap(Contacts.People.loadContactPhoto(context,
  92. uri, R.drawable.blank, null));
  93. } else {
  94. title.setText(Html.fromHtml("<b>" + (name.length() == 0 ? email : name) + "</b> ("
  95. + count + ")"));
  96. }
  97. cursor.close();
  98. final TextView body = (TextView) view.findViewById(R.id.body);
  99. body.setText(text);
  100. return view;
  101. }
  102. }