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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 uk.co.md87.android.contexthome.DataHelper;
  34. import uk.co.md87.android.contexthome.Module;
  35. import uk.co.md87.android.contexthome.R;
  36. /**
  37. * A module which displays contact shortcuts.
  38. *
  39. * @author chris
  40. */
  41. public class ContactsModule extends Module {
  42. public ContactsModule(DataHelper helper) {
  43. super(helper);
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public View getView(final Context context, final int weight) {
  48. final View view = View.inflate(context, R.layout.scroller, null);
  49. final LinearLayout layout = (LinearLayout) view.findViewById(R.id.content);
  50. final View.OnClickListener listener = new View.OnClickListener() {
  51. public void onClick(View view) {
  52. Intent intent = new Intent(Intent.ACTION_VIEW);
  53. intent.setType(People.CONTENT_ITEM_TYPE);
  54. intent.setData((Uri) view.getTag());
  55. context.startActivity(intent);
  56. }
  57. };
  58. final Cursor cursor = context.getContentResolver().query(Photos.CONTENT_URI,
  59. new String[] { "person" }, "exists_on_server != 0", null, null);
  60. final int column = cursor.getColumnIndex("person");
  61. if (cursor.moveToFirst()) {
  62. do {
  63. layout.addView(getView(context, listener, cursor.getLong(column)), 52, 52);
  64. } while (cursor.moveToNext());
  65. }
  66. return view;
  67. }
  68. private final View getView(final Context context, View.OnClickListener listener,
  69. final long id) {
  70. final ImageView image = new ImageView(context);
  71. image.setFocusable(true);
  72. image.setClickable(true);
  73. Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
  74. image.setTag(uri);
  75. image.setImageBitmap(People.loadContactPhoto(context,
  76. uri, R.drawable.blank, null));
  77. image.setOnClickListener(listener);
  78. image.setBackgroundResource(R.drawable.grid_selector);
  79. image.setPadding(2, 2, 2, 2);
  80. return image;
  81. }
  82. }