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.

ContactsModule.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.database.Cursor;
  27. import android.net.Uri;
  28. import android.os.Handler;
  29. import android.provider.Contacts.People;
  30. import android.provider.Contacts.Photos;
  31. import android.view.View;
  32. import android.view.ViewGroup;
  33. import android.widget.ImageView;
  34. import android.widget.LinearLayout;
  35. import java.util.ArrayList;
  36. import java.util.Collections;
  37. import java.util.Comparator;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. import uk.co.md87.android.contexthome.DataHelper;
  42. import uk.co.md87.android.contexthome.Module;
  43. import uk.co.md87.android.contexthome.R;
  44. /**
  45. * A module which displays contact shortcuts.
  46. *
  47. * @author chris
  48. */
  49. public class ContactsModule extends Module implements Comparator<Long> {
  50. public ContactsModule(DataHelper helper) {
  51. super(helper);
  52. }
  53. /** {@inheritDoc} */
  54. @Override
  55. public void addViews(final ViewGroup parent, final Context context, final int weight) {
  56. final View view = View.inflate(context, R.layout.scroller, null);
  57. final LinearLayout layout = (LinearLayout) view.findViewById(R.id.content);
  58. final View.OnClickListener listener = new View.OnClickListener() {
  59. public void onClick(View view) {
  60. Intent intent = new Intent(Intent.ACTION_VIEW);
  61. intent.setType(People.CONTENT_ITEM_TYPE);
  62. intent.setData((Uri) view.getTag());
  63. context.startActivity(intent);
  64. recordAction(getMap(ContentUris.parseId((Uri) view.getTag())));
  65. }
  66. };
  67. final Handler handler = new Handler();
  68. new Thread(new Runnable() {
  69. public void run() {
  70. final Cursor cursor = context.getContentResolver().query(Photos.CONTENT_URI,
  71. new String[] { "person" }, "exists_on_server != 0", null, null);
  72. final int column = cursor.getColumnIndex("person");
  73. final List<Long> hits = new ArrayList<Long>(cursor.getCount());
  74. if (cursor.moveToFirst()) {
  75. do {
  76. hits.add(cursor.getLong(column));
  77. } while (cursor.moveToNext());
  78. }
  79. Collections.sort(hits, ContactsModule.this);
  80. final List<View> views = new ArrayList<View>(hits.size());
  81. for (Long id : hits) {
  82. views.add(getView(context, listener, id));
  83. }
  84. handler.post(new Runnable() {
  85. public void run() {
  86. for (View myView : views) {
  87. layout.addView(myView, 52, 52);
  88. }
  89. }
  90. });
  91. }
  92. }).start();
  93. parent.addView(view);
  94. }
  95. private final View getView(final Context context, View.OnClickListener listener,
  96. final long id) {
  97. final ImageView image = new ImageView(context);
  98. image.setFocusable(true);
  99. image.setClickable(true);
  100. Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
  101. image.setTag(uri);
  102. image.setImageBitmap(People.loadContactPhoto(context,
  103. uri, R.drawable.blank, null));
  104. image.setOnClickListener(listener);
  105. image.setBackgroundResource(R.drawable.grid_selector);
  106. image.setPadding(2, 2, 2, 2);
  107. return image;
  108. }
  109. public Map<String, String> getMap(final Long id) {
  110. final Map<String, String> params = new HashMap<String, String>();
  111. params.put("contactid", String.valueOf(id));
  112. return params;
  113. }
  114. public int compare(final Long arg0, final Long arg1) {
  115. return getScore(getMap(arg1)) - getScore(getMap(arg0));
  116. }
  117. }