AI2 Component  (Version nb184)
EmailAddressAdapter.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import android.content.ContentResolver;
10 import android.content.Context;
11 import android.database.Cursor;
12 import android.database.DatabaseUtils;
13 import android.provider.Contacts.ContactMethods;
14 import android.provider.Contacts.People;
15 import android.text.TextUtils;
16 import android.text.util.Rfc822Token;
17 import android.view.View;
18 import android.widget.ResourceCursorAdapter;
19 import android.widget.TextView;
20 import android.util.Log;
21 
24 
36 public class EmailAddressAdapter extends ResourceCursorAdapter {
37 
38  private static final boolean DEBUG = false;
39  private static final String TAG = "EmailAddressAdapter";
40 
41  public static final int PRE_HONEYCOMB_NAME_INDEX = 1;
42  public static final int PRE_HONEYCOMB_DATA_INDEX = 2;
43 
44  private static String SORT_ORDER;
45 
46  private ContentResolver contentResolver;
47 
48  private Context context;
49 
50  private static final String[] PRE_HONEYCOMB_PROJECTION = {
51  ContactMethods._ID, // 0
52  ContactMethods.NAME, // 1
53  ContactMethods.DATA, // 2
54  };
55 
56  private static final String[] POST_HONEYCOMB_PROJECTION = HoneycombMR1Util.getEmailAdapterProjection();
57 
58  public EmailAddressAdapter(Context context) {
59  super(context, android.R.layout.simple_dropdown_item_1line, null);
60  contentResolver = context.getContentResolver();
61  this.context = context;
64  } else {
65  SORT_ORDER = People.TIMES_CONTACTED + " DESC, " + People.NAME;
66  }
67  }
68 
69  @Override
70  public final String convertToString(Cursor cursor) {
71 
72  int POST_HONEYCOMB_NAME_INDEX = cursor.getColumnIndex(HoneycombMR1Util.getDisplayName());
73  int POST_HONEYCOMB_EMAIL_INDEX = cursor.getColumnIndex(HoneycombMR1Util.getEmailAddress());
74  String name = "";
75  String address = "";
76 
78  name = cursor.getString(POST_HONEYCOMB_NAME_INDEX);
79  address = cursor.getString(POST_HONEYCOMB_EMAIL_INDEX);
80  } else {
81  name = cursor.getString(PRE_HONEYCOMB_NAME_INDEX);
82  address = cursor.getString(PRE_HONEYCOMB_DATA_INDEX);
83  }
84 
85  return new Rfc822Token(name, address, null).toString();
86  }
87 
88  private final String makeDisplayString(Cursor cursor) {
89 
90  int POST_HONEYCOMB_NAME_INDEX = cursor.getColumnIndex(HoneycombMR1Util.getDisplayName());
91  int POST_HONEYCOMB_EMAIL_INDEX = cursor.getColumnIndex(HoneycombMR1Util.getEmailAddress());
92  StringBuilder s = new StringBuilder();
93  boolean flag = false;
94  String name = "";
95  String address = "";
96 
98  name = cursor.getString(POST_HONEYCOMB_NAME_INDEX);
99  address = cursor.getString(POST_HONEYCOMB_EMAIL_INDEX);
100  } else {
101  name = cursor.getString(PRE_HONEYCOMB_NAME_INDEX);
102  address = cursor.getString(PRE_HONEYCOMB_DATA_INDEX);
103  }
104 
105  if (!TextUtils.isEmpty(name)) {
106  s.append(name);
107  flag = true;
108  }
109 
110  if (flag) {
111  s.append(" <");
112  }
113 
114  s.append(address);
115 
116  if (flag) {
117  s.append(">");
118  }
119 
120  return s.toString();
121  }
122 
123  @Override
124  public final void bindView(View view, Context context, Cursor cursor) {
125  ((TextView) view).setText(makeDisplayString(cursor));
126  }
127 
128  @Override
129  public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
130 
131  String where = null;
132  android.net.Uri db = null;
133  StringBuilder s = new StringBuilder();
134 
135  if (constraint != null) {
136  String filter = DatabaseUtils.sqlEscapeString(constraint.toString() + '%');
137 
140  s.append("(" + HoneycombMR1Util.getDataMimeType() + "='" + HoneycombMR1Util.getEmailType() + "')");
141  s.append(" AND ");
142  s.append("(display_name LIKE ");
143  s.append(filter);
144  s.append(")");
145  } else {
146  db = ContactMethods.CONTENT_EMAIL_URI;
147  s.append("(name LIKE ");
148  s.append(filter);
149  s.append(") OR (display_name LIKE ");
150  s.append(filter);
151  s.append(")");
152  }
153  }
154  where = s.toString();
155 
156  // Note(hal): This lists the column names in the table being accessed, since they aren't
157  // obvious to me from the documentation
158  if (DEBUG) {
159  Cursor c = context.getContentResolver().query(db, null, null, null, null, null);
160  Log.d(TAG, "listing columns");
161  for (int i = 0; i<c.getColumnCount(); i++) {
162  Log.d(TAG, "column " + i + "=" + c.getColumnName(i));
163  }
164  }
165 
167  return contentResolver.query(db, POST_HONEYCOMB_PROJECTION,
168  where, null, SORT_ORDER);
169  } else {
170  return contentResolver.query(db, PRE_HONEYCOMB_PROJECTION,
171  where, null, SORT_ORDER);
172  }
173  }
174 }
175 
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getDataMimeType
static String getDataMimeType()
Definition: HoneycombMR1Util.java:185
com.google.appinventor.components.runtime.EmailAddressAdapter.convertToString
final String convertToString(Cursor cursor)
Definition: EmailAddressAdapter.java:70
com.google.appinventor.components.runtime.EmailAddressAdapter
Definition: EmailAddressAdapter.java:36
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getDisplayName
static String getDisplayName()
Definition: HoneycombMR1Util.java:171
com.google.appinventor.components.runtime.EmailAddressAdapter.PRE_HONEYCOMB_NAME_INDEX
static final int PRE_HONEYCOMB_NAME_INDEX
Definition: EmailAddressAdapter.java:41
com.google.appinventor.components
com.google.appinventor.components.runtime.util.HoneycombMR1Util
Definition: HoneycombMR1Util.java:29
com.google.appinventor.components.runtime.EmailAddressAdapter.runQueryOnBackgroundThread
Cursor runQueryOnBackgroundThread(CharSequence constraint)
Definition: EmailAddressAdapter.java:129
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getEmailAdapterProjection
static String[] getEmailAdapterProjection()
Definition: HoneycombMR1Util.java:98
com.google.appinventor.components.runtime.EmailAddressAdapter.PRE_HONEYCOMB_DATA_INDEX
static final int PRE_HONEYCOMB_DATA_INDEX
Definition: EmailAddressAdapter.java:42
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getDataContentUri
static Uri getDataContentUri()
Definition: HoneycombMR1Util.java:51
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getTimesContacted
static String getTimesContacted()
Definition: HoneycombMR1Util.java:212
com.google.appinventor.components.runtime.EmailAddressAdapter.bindView
final void bindView(View view, Context context, Cursor cursor)
Definition: EmailAddressAdapter.java:124
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getEmailType
static String getEmailType()
Definition: HoneycombMR1Util.java:164
com.google
com.google.appinventor.components.runtime.EmailAddressAdapter.EmailAddressAdapter
EmailAddressAdapter(Context context)
Definition: EmailAddressAdapter.java:58
com
com.google.appinventor.components.runtime.util.HoneycombMR1Util.getEmailAddress
static String getEmailAddress()
Definition: HoneycombMR1Util.java:178
com.google.appinventor
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_HONEYCOMB_MR1
static final int LEVEL_HONEYCOMB_MR1
Definition: SdkLevel.java:29