Context-detection API for Android developed as a university project
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

EmailModule.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.util.Log;
  33. import android.view.View;
  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 EmailModule extends Module {
  49. public EmailModule(DataHelper helper) {
  50. super(helper);
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public View getView(final Context context, final int weight) {
  55. final LinearLayout layout = new LinearLayout(context);
  56. layout.setOrientation(LinearLayout.VERTICAL);
  57. final Uri inboxUri = Uri.parse("content://gmail-ls/"
  58. + "conversations/" + context.getSharedPreferences("email",
  59. Context.MODE_WORLD_READABLE).getString("account", "chris87@gmail.com"));
  60. final Cursor cursor = context.getContentResolver().query(inboxUri,
  61. new String[] { "conversation_id" }, null, null, null);
  62. final int convIdIndex = cursor.getColumnIndex("conversation_id");
  63. final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
  64. LayoutParams.WRAP_CONTENT);
  65. params.weight = 1;
  66. final List<Object[]> messages = new ArrayList<Object[]>(weight);
  67. final List<Long> convIds = new ArrayList<Long>();
  68. if (cursor.moveToFirst()) {
  69. do {
  70. convIds.add(cursor.getLong(convIdIndex));
  71. } while (cursor.moveToNext());
  72. }
  73. cursor.close();
  74. Log.e("!!!!!", convIds.toString());
  75. for (Long convId : convIds) {
  76. final Uri uri = inboxUri.buildUpon().appendEncodedPath(String.valueOf(convId)
  77. + "/messages").build();
  78. final Cursor messageCursor = context.getContentResolver().query(uri,
  79. new String[] { "fromAddress", "subject", "messageId" }, null, null, null);
  80. while (messageCursor.getExtras().getString("status").equals("LOADING")) {
  81. messageCursor.requery();
  82. Thread.yield();
  83. }
  84. if (messageCursor.moveToFirst()) {
  85. final int subjectIndex = messageCursor.getColumnIndex("subject");
  86. final int addressIndex = messageCursor.getColumnIndex("fromAddress");
  87. final String body = messageCursor.getString(subjectIndex);
  88. final String address = messageCursor.getString(addressIndex);
  89. final int count = messageCursor.getCount();
  90. messages.add(new Object[] { body, address, count });
  91. }
  92. messageCursor.close();
  93. }
  94. for (Object[] message : messages) {
  95. layout.addView(getView(context, (String) message[0],
  96. (String) message[1], (Integer) message[2]), params);
  97. }
  98. return layout;
  99. }
  100. private View getView(final Context context, String text, String address, int count) {
  101. final View view = View.inflate(context, R.layout.titledimage, null);
  102. view.setClickable(true);
  103. view.setFocusable(true);
  104. view.setOnClickListener(new View.OnClickListener() {
  105. public void onClick(View arg0) {
  106. final Intent intent = new Intent();
  107. intent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
  108. context.startActivity(intent);
  109. }
  110. });
  111. final Uri contactUri = Uri.parse("content://contacts/contact_methods");
  112. final String name = address.replaceAll("^.*\"(.*?)\".*$", "$1")
  113. .replaceAll("(.*), (.*)", "$2 $1");
  114. final String email = address.replaceAll("^.*<(.*?)>.*$", "$1");
  115. final Cursor cursor = context.getContentResolver().query(contactUri,
  116. new String[] { "person", "name" }, "data LIKE ?", new String[] { email }, null);
  117. final TextView title = (TextView) view.findViewById(R.id.title);
  118. final ImageView image = (ImageView) view.findViewById(R.id.image);
  119. if (cursor.moveToFirst()) {
  120. title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
  121. .getColumnIndex("name")) + "</b> (" + count + ")"));
  122. Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
  123. cursor.getLong(cursor.getColumnIndex("person")));
  124. image.setImageBitmap(Contacts.People.loadContactPhoto(context,
  125. uri, R.drawable.blank, null));
  126. } else {
  127. title.setText(Html.fromHtml("<b>" + (name.length() == 0 ? email : name) + "</b> ("
  128. + count + ")"));
  129. }
  130. cursor.close();
  131. final TextView body = (TextView) view.findViewById(R.id.body);
  132. body.setText(text);
  133. return view;
  134. }
  135. }