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.8KB

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