blob: 335ba1daaf8bf1457f587f9fdfa2ffb04278643a [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
36 private final int mSelectedLayoutId;
37
38 private final long mCurrentId;
39
40 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId,
41 int selectedViewId) {
42 super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION,
43 Email.CONTACT_ID + " =?", new String[] {
44 String.valueOf(contactId)
45 }, null), 0);
46 mLayoutInflater = LayoutInflater.from(context);
47 mLayoutId = viewId;
48 mSelectedLayoutId = selectedViewId;
49 mCurrentId = currentId;
50 }
51
52 @Override
53 public long getItemId(int position) {
54 Cursor c = getCursor();
55 c.moveToPosition(position);
56 return c.getLong(EmailQuery.DATA_ID);
57 }
58
59 public RecipientEntry getRecipientEntry(int position) {
60 Cursor c = getCursor();
61 c.moveToPosition(position);
62 return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c
63 .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c
64 .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI));
65 }
66
67 @Override
68 public View getView(int position, View convertView, ViewGroup parent) {
Mindy Pereira67ad4892011-06-02 12:21:22 -070069 Cursor cursor = getCursor();
70 cursor.moveToPosition(position);
Mindy Pereira97d77682011-06-01 10:48:55 -070071 if (convertView == null) {
Mindy Pereira67ad4892011-06-02 12:21:22 -070072 convertView = newView(cursor.getLong(EmailQuery.DATA_ID) == mCurrentId);
Mindy Pereira97d77682011-06-01 10:48:55 -070073 }
Mindy Pereira67ad4892011-06-02 12:21:22 -070074 bindView(convertView, convertView.getContext(), cursor);
Mindy Pereira97d77682011-06-01 10:48:55 -070075 return convertView;
76 }
77
Mindy Pereira67ad4892011-06-02 12:21:22 -070078 // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine
79 // somehow?
Mindy Pereira97d77682011-06-01 10:48:55 -070080 @Override
81 public void bindView(View view, Context context, Cursor cursor) {
Mindy Pereira67ad4892011-06-02 12:21:22 -070082 int position = cursor.getPosition();
83
84 TextView display = (TextView) view.findViewById(android.R.id.text1);
85 ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
86 RecipientEntry entry = getRecipientEntry(position);
87 if (position == 0) {
88 display.setText(cursor.getString(EmailQuery.NAME));
89 display.setVisibility(View.VISIBLE);
90 // TODO: see if this needs to be done outside the main thread
91 // as it may be too slow to get immediately.
92 imageView.setImageURI(entry.getPhotoThumbnailUri());
93 imageView.setVisibility(View.VISIBLE);
94 } else {
95 display.setVisibility(View.GONE);
96 imageView.setVisibility(View.GONE);
97 }
98 TextView destination = (TextView) view.findViewById(android.R.id.text2);
Mindy Pereira97d77682011-06-01 10:48:55 -070099 destination.setText(cursor.getString(EmailQuery.ADDRESS));
100 }
101
102 @Override
103 public View newView(Context context, Cursor cursor, ViewGroup parent) {
104 return newView(false);
105 }
106
107 private View newView(boolean isSelected) {
108 return isSelected ? mLayoutInflater.inflate(mSelectedLayoutId, null) : mLayoutInflater
109 .inflate(mLayoutId, null);
110 }
111}