blob: 792fdccb7d44b7a6ddc74e61108e6625a78c9c6c [file] [log] [blame]
Mindy Pereira84dd9a82011-06-01 11:13:08 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mindy Pereira97d77682011-06-01 10:48:55 -070016
17package com.android.ex.chips;
18
19import com.android.ex.chips.BaseRecipientAdapter.EmailQuery;
20
21import android.content.Context;
22import android.database.Cursor;
23import android.provider.ContactsContract.CommonDataKinds.Email;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.CursorAdapter;
Mindy Pereira67ad4892011-06-02 12:21:22 -070028import android.widget.ImageView;
Mindy Pereira97d77682011-06-01 10:48:55 -070029import android.widget.TextView;
30
31public class RecipientAlternatesAdapter extends CursorAdapter {
32 private final LayoutInflater mLayoutInflater;
33
34 private final int mLayoutId;
35
Mindy Pereira97d77682011-06-01 10:48:55 -070036 private final long mCurrentId;
37
Mindy Pereira007a76b2011-06-14 11:39:36 -070038 private int mCheckedItemPosition = -1;
39
Mindy Pereira50863912011-06-16 18:54:10 -070040 private OnCheckedItemChangedListener mCheckedItemChangedListener;
41
42 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId,
43 OnCheckedItemChangedListener listener) {
Mindy Pereira97d77682011-06-01 10:48:55 -070044 super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION,
45 Email.CONTACT_ID + " =?", new String[] {
46 String.valueOf(contactId)
47 }, null), 0);
48 mLayoutInflater = LayoutInflater.from(context);
49 mLayoutId = viewId;
Mindy Pereira97d77682011-06-01 10:48:55 -070050 mCurrentId = currentId;
Mindy Pereira50863912011-06-16 18:54:10 -070051 mCheckedItemChangedListener = listener;
Mindy Pereira97d77682011-06-01 10:48:55 -070052 }
53
54 @Override
55 public long getItemId(int position) {
56 Cursor c = getCursor();
57 c.moveToPosition(position);
58 return c.getLong(EmailQuery.DATA_ID);
59 }
60
61 public RecipientEntry getRecipientEntry(int position) {
62 Cursor c = getCursor();
63 c.moveToPosition(position);
64 return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c
65 .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c
66 .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI));
67 }
68
69 @Override
70 public View getView(int position, View convertView, ViewGroup parent) {
Mindy Pereira67ad4892011-06-02 12:21:22 -070071 Cursor cursor = getCursor();
72 cursor.moveToPosition(position);
Mindy Pereira97d77682011-06-01 10:48:55 -070073 if (convertView == null) {
Mindy Pereira007a76b2011-06-14 11:39:36 -070074 convertView = newView();
75 }
76 if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) {
77 mCheckedItemPosition = position;
Mindy Pereira50863912011-06-16 18:54:10 -070078 if (mCheckedItemChangedListener != null) {
79 mCheckedItemChangedListener.onCheckedItemChanged(mCheckedItemPosition);
80 }
Mindy Pereira97d77682011-06-01 10:48:55 -070081 }
Mindy Pereira67ad4892011-06-02 12:21:22 -070082 bindView(convertView, convertView.getContext(), cursor);
Mindy Pereira97d77682011-06-01 10:48:55 -070083 return convertView;
84 }
85
Mindy Pereira67ad4892011-06-02 12:21:22 -070086 // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine
87 // somehow?
Mindy Pereira97d77682011-06-01 10:48:55 -070088 @Override
89 public void bindView(View view, Context context, Cursor cursor) {
Mindy Pereira67ad4892011-06-02 12:21:22 -070090 int position = cursor.getPosition();
91
92 TextView display = (TextView) view.findViewById(android.R.id.text1);
93 ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
94 RecipientEntry entry = getRecipientEntry(position);
95 if (position == 0) {
96 display.setText(cursor.getString(EmailQuery.NAME));
97 display.setVisibility(View.VISIBLE);
98 // TODO: see if this needs to be done outside the main thread
99 // as it may be too slow to get immediately.
100 imageView.setImageURI(entry.getPhotoThumbnailUri());
101 imageView.setVisibility(View.VISIBLE);
102 } else {
103 display.setVisibility(View.GONE);
104 imageView.setVisibility(View.GONE);
105 }
106 TextView destination = (TextView) view.findViewById(android.R.id.text2);
Mindy Pereira97d77682011-06-01 10:48:55 -0700107 destination.setText(cursor.getString(EmailQuery.ADDRESS));
108 }
109
110 @Override
111 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Mindy Pereira007a76b2011-06-14 11:39:36 -0700112 return newView();
Mindy Pereira97d77682011-06-01 10:48:55 -0700113 }
114
Mindy Pereira007a76b2011-06-14 11:39:36 -0700115 private View newView() {
116 return mLayoutInflater.inflate(mLayoutId, null);
117 }
118
Mindy Pereira50863912011-06-16 18:54:10 -0700119 /*package*/ static interface OnCheckedItemChangedListener {
120 public void onCheckedItemChanged(int position);
Mindy Pereira97d77682011-06-01 10:48:55 -0700121 }
122}