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

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