1 /* 2 * Copyright (C) 2009 Myriad Group AG 3 * Copyright (C) 2009 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.im.app; 19 20 import com.android.im.R; 21 import com.android.im.plugin.BrandingResourceIDs; 22 import com.android.im.provider.Imps; 23 24 import android.graphics.drawable.Drawable; 25 import android.widget.LinearLayout; 26 import android.widget.ImageView; 27 import android.widget.TextView; 28 import android.content.Context; 29 import android.content.ContentResolver; 30 import android.content.res.Resources; 31 import android.database.Cursor; 32 import android.content.res.ColorStateList; 33 import android.view.View; 34 import android.util.Log; 35 36 public class ProviderListItem extends LinearLayout { 37 private static final String TAG = "IM"; 38 private static final boolean LOCAL_DEBUG = false; 39 40 private LandingPage mActivity; 41 private ImageView mProviderIcon; 42 private ImageView mStatusIcon; 43 private TextView mProviderName; 44 private TextView mLoginName; 45 private TextView mChatView; 46 private View mUnderBubble; 47 private Drawable mBubbleDrawable, mDefaultBackground; 48 49 private int mProviderIdColumn; 50 private int mProviderFullnameColumn; 51 private int mActiveAccountIdColumn; 52 private int mActiveAccountUserNameColumn; 53 private int mAccountPresenceStatusColumn; 54 private int mAccountConnectionStatusColumn; 55 56 private ColorStateList mProviderNameColors; 57 private ColorStateList mLoginNameColors; 58 private ColorStateList mChatViewColors; 59 ProviderListItem(Context context, LandingPage activity)60 public ProviderListItem(Context context, LandingPage activity) { 61 super(context); 62 mActivity = activity; 63 } 64 init(Cursor c)65 public void init(Cursor c) { 66 mProviderIcon = (ImageView) findViewById(R.id.providerIcon); 67 mStatusIcon = (ImageView) findViewById(R.id.statusIcon); 68 mProviderName = (TextView) findViewById(R.id.providerName); 69 mLoginName = (TextView) findViewById(R.id.loginName); 70 mChatView = (TextView) findViewById(R.id.conversations); 71 mUnderBubble = findViewById(R.id.underBubble); 72 mBubbleDrawable = getResources().getDrawable(R.drawable.bubble); 73 mDefaultBackground = getResources().getDrawable(R.drawable.default_background); 74 75 mProviderIdColumn = c.getColumnIndexOrThrow(Imps.Provider._ID); 76 mProviderFullnameColumn = c.getColumnIndexOrThrow(Imps.Provider.FULLNAME); 77 mActiveAccountIdColumn = c.getColumnIndexOrThrow( 78 Imps.Provider.ACTIVE_ACCOUNT_ID); 79 mActiveAccountUserNameColumn = c.getColumnIndexOrThrow( 80 Imps.Provider.ACTIVE_ACCOUNT_USERNAME); 81 mAccountPresenceStatusColumn = c.getColumnIndexOrThrow( 82 Imps.Provider.ACCOUNT_PRESENCE_STATUS); 83 mAccountConnectionStatusColumn = c.getColumnIndexOrThrow( 84 Imps.Provider.ACCOUNT_CONNECTION_STATUS); 85 86 mProviderNameColors = mProviderName.getTextColors(); 87 mLoginNameColors = mLoginName.getTextColors(); 88 mChatViewColors = mChatView.getTextColors(); 89 } 90 bindView(Cursor cursor)91 public void bindView(Cursor cursor) { 92 Resources r = getResources(); 93 ImageView providerIcon = mProviderIcon; 94 ImageView statusIcon = mStatusIcon; 95 TextView providerName = mProviderName; 96 TextView loginName = mLoginName; 97 TextView chatView = mChatView; 98 99 int providerId = cursor.getInt(mProviderIdColumn); 100 String providerDisplayName = cursor.getString(mProviderFullnameColumn); 101 102 ImApp app = ImApp.getApplication(mActivity); 103 BrandingResources brandingRes = app.getBrandingResource(providerId); 104 providerIcon.setImageDrawable( 105 brandingRes.getDrawable(BrandingResourceIDs.DRAWABLE_LOGO)); 106 107 mUnderBubble.setBackgroundDrawable(mDefaultBackground); 108 statusIcon.setVisibility(View.GONE); 109 110 providerName.setTextColor(mProviderNameColors); 111 loginName.setTextColor(mLoginNameColors); 112 chatView.setTextColor(mChatViewColors); 113 114 if (!cursor.isNull(mActiveAccountIdColumn)) { 115 mLoginName.setVisibility(View.VISIBLE); 116 providerName.setVisibility(View.VISIBLE); 117 providerName.setText(providerDisplayName); 118 119 long accountId = cursor.getLong(mActiveAccountIdColumn); 120 int connectionStatus = cursor.getInt(mAccountConnectionStatusColumn); 121 122 String secondRowText; 123 124 chatView.setVisibility(View.GONE); 125 126 switch (connectionStatus) { 127 case Imps.ConnectionStatus.CONNECTING: 128 secondRowText = r.getString(R.string.signing_in_wait); 129 break; 130 131 case Imps.ConnectionStatus.ONLINE: 132 int presenceIconId = getPresenceIconId(cursor); 133 statusIcon.setImageDrawable( 134 brandingRes.getDrawable(presenceIconId)); 135 statusIcon.setVisibility(View.VISIBLE); 136 ContentResolver cr = mActivity.getContentResolver(); 137 138 int count = getConversationCount(cr, accountId); 139 if (count > 0) { 140 mUnderBubble.setBackgroundDrawable(mBubbleDrawable); 141 chatView.setVisibility(View.VISIBLE); 142 chatView.setText(r.getString(R.string.conversations, count)); 143 144 providerName.setTextColor(0xff000000); 145 loginName.setTextColor(0xff000000); 146 chatView.setTextColor(0xff000000); 147 } else { 148 chatView.setVisibility(View.GONE); 149 } 150 151 secondRowText = cursor.getString(mActiveAccountUserNameColumn); 152 break; 153 154 default: 155 secondRowText = cursor.getString(mActiveAccountUserNameColumn); 156 break; 157 } 158 159 loginName.setText(secondRowText); 160 161 } else { 162 // No active account, show add account 163 mLoginName.setVisibility(View.GONE); 164 mChatView.setVisibility(View.GONE); 165 mProviderName.setText(providerDisplayName); 166 } 167 } 168 getConversationCount(ContentResolver cr, long accountId)169 private int getConversationCount(ContentResolver cr, long accountId) { 170 // TODO: this is code used to get Google Talk's chat count. Not sure if this will work 171 // for IMPS chat count. 172 StringBuilder where = new StringBuilder(); 173 where.append(Imps.Chats.CONTACT_ID); 174 where.append(" in (select _id from contacts where "); 175 where.append(Imps.Contacts.ACCOUNT); 176 where.append("="); 177 where.append(accountId); 178 where.append(")"); 179 180 Cursor cursor = cr.query(Imps.Chats.CONTENT_URI, null, where.toString(), null, null); 181 182 try { 183 return cursor.getCount(); 184 } finally { 185 cursor.close(); 186 } 187 } 188 getPresenceIconId(Cursor cursor)189 private int getPresenceIconId(Cursor cursor) { 190 int presenceStatus = cursor.getInt(mAccountPresenceStatusColumn); 191 192 if (LOCAL_DEBUG) log("getPresenceIconId: presenceStatus=" + presenceStatus); 193 194 switch (presenceStatus) { 195 case Imps.Presence.AVAILABLE: 196 return BrandingResourceIDs.DRAWABLE_PRESENCE_ONLINE; 197 198 case Imps.Presence.IDLE: 199 case Imps.Presence.AWAY: 200 return BrandingResourceIDs.DRAWABLE_PRESENCE_AWAY; 201 202 case Imps.Presence.DO_NOT_DISTURB: 203 return BrandingResourceIDs.DRAWABLE_PRESENCE_BUSY; 204 205 case Imps.Presence.INVISIBLE: 206 return BrandingResourceIDs.DRAWABLE_PRESENCE_INVISIBLE; 207 208 default: 209 return BrandingResourceIDs.DRAWABLE_PRESENCE_OFFLINE; 210 } 211 } 212 log(String msg)213 private void log(String msg) { 214 Log.d(TAG, msg); 215 } 216 } 217