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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Context;
  24. import android.content.Intent;
  25. import android.database.Cursor;
  26. import android.net.Uri;
  27. import android.os.Handler;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import android.widget.LinearLayout.LayoutParams;
  31. import java.util.ArrayList;
  32. import java.util.Collections;
  33. import java.util.Comparator;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. import uk.co.md87.android.contexthome.DataHelper;
  38. import uk.co.md87.android.contexthome.Module;
  39. import uk.co.md87.android.contexthome.R;
  40. /**
  41. * A module which displays SMS messages.
  42. *
  43. * @author chris
  44. */
  45. public class EmailModule extends Module implements Comparator<Email> {
  46. public EmailModule(DataHelper helper) {
  47. super(helper);
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. public void addViews(final ViewGroup parent, final Context context, final int weight) {
  52. final Handler handler = new Handler();
  53. new Thread(new Runnable() {
  54. public void run() {
  55. final Uri inboxUri = Uri.parse("content://gmail-ls/"
  56. + "conversations/" + context.getSharedPreferences("email",
  57. Context.MODE_WORLD_READABLE).getString("account", "chris87@gmail.com"));
  58. final Cursor cursor = context.getContentResolver().query(inboxUri,
  59. new String[] { "conversation_id" }, null, null, null);
  60. while (cursor.getExtras().getString("status").equals("LOADING")) {
  61. cursor.requery();
  62. Thread.yield();
  63. }
  64. final int convIdIndex = cursor.getColumnIndex("conversation_id");
  65. final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
  66. LayoutParams.WRAP_CONTENT);
  67. params.weight = 1;
  68. final List<Email> messages = new ArrayList<Email>(weight);
  69. if (cursor.moveToFirst()) {
  70. do {
  71. final View view = View.inflate(context, R.layout.titledimage, null);
  72. view.setClickable(true);
  73. view.setFocusable(true);
  74. view.setOnClickListener(new View.OnClickListener() {
  75. public void onClick(View arg0) {
  76. final Intent intent = new Intent();
  77. intent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
  78. context.startActivity(intent);
  79. recordAction(getMap((Email) arg0.getTag()));
  80. }
  81. });
  82. final Email message = new Email(handler, context,
  83. cursor.getLong(convIdIndex), view);
  84. view.setTag(message);
  85. messages.add(message);
  86. } while (cursor.moveToNext());
  87. }
  88. cursor.close();
  89. Collections.sort(messages, EmailModule.this);
  90. handler.post(new Runnable() {
  91. public void run() {
  92. for (Email message : messages) {
  93. parent.addView(message.getView(), params);
  94. }
  95. }
  96. });
  97. }
  98. }).start();
  99. }
  100. public Map<String, String> getMap(final Email email) {
  101. final Map<String, String> params = new HashMap<String, String>();
  102. if (email.getId() >= 0) {
  103. params.put("contactid", String.valueOf(email.getId()));
  104. }
  105. params.put("contactemail", email.getAddress());
  106. return params;
  107. }
  108. public int compare(final Email arg0, final Email arg1) {
  109. return getScore(getMap(arg1)) - getScore(getMap(arg0));
  110. }
  111. }