1 /* 2 * Copyright (C) 2015 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 */ 16 package com.android.messaging.datamodel.data; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.net.Uri; 21 22 import com.android.messaging.R; 23 import com.android.messaging.datamodel.binding.Binding; 24 import com.android.messaging.datamodel.binding.BindingBase; 25 import com.android.messaging.datamodel.media.BindableMediaRequest; 26 import com.android.messaging.datamodel.media.MediaRequest; 27 import com.android.messaging.datamodel.media.MediaResourceManager; 28 import com.android.messaging.datamodel.media.MediaResourceManager.MediaResourceLoadListener; 29 import com.android.messaging.datamodel.media.VCardRequestDescriptor; 30 import com.android.messaging.datamodel.media.VCardResource; 31 import com.android.messaging.datamodel.media.VCardResourceEntry; 32 import com.android.messaging.util.Assert; 33 import com.android.messaging.util.AvatarUriUtil; 34 import com.android.messaging.util.ContactUtil; 35 36 import java.util.List; 37 38 /** 39 * Data class for visualizing and loading data for a VCard contact. 40 */ 41 public class VCardContactItemData extends PersonItemData 42 implements MediaResourceLoadListener<VCardResource> { 43 private final Context mContext; 44 private final Uri mVCardUri; 45 private String mDetails; 46 private final Binding<BindableMediaRequest<VCardResource>> mBinding = 47 BindingBase.createBinding(this); 48 private VCardResource mVCardResource; 49 50 private static final Uri sDefaultAvatarUri = 51 AvatarUriUtil.createAvatarUri(null, null, null, null); 52 53 /** 54 * Constructor. This parses data from the given MessagePartData describing the vcard 55 */ VCardContactItemData(final Context context, final MessagePartData messagePartData)56 public VCardContactItemData(final Context context, final MessagePartData messagePartData) { 57 this(context, messagePartData.getContentUri()); 58 Assert.isTrue(messagePartData.isVCard()); 59 } 60 61 /** 62 * Constructor. This parses data from the given VCard Uri 63 */ VCardContactItemData(final Context context, final Uri vCardUri)64 public VCardContactItemData(final Context context, final Uri vCardUri) { 65 mContext = context; 66 mDetails = mContext.getString(R.string.loading_vcard); 67 mVCardUri = vCardUri; 68 } 69 70 @Override getAvatarUri()71 public Uri getAvatarUri() { 72 if (hasValidVCard()) { 73 final List<VCardResourceEntry> vcards = mVCardResource.getVCards(); 74 Assert.isTrue(vcards.size() > 0); 75 if (vcards.size() == 1) { 76 return vcards.get(0).getAvatarUri(); 77 } 78 } 79 return sDefaultAvatarUri; 80 } 81 82 @Override getDisplayName()83 public String getDisplayName() { 84 if (hasValidVCard()) { 85 final List<VCardResourceEntry> vcards = mVCardResource.getVCards(); 86 Assert.isTrue(vcards.size() > 0); 87 if (vcards.size() == 1) { 88 return vcards.get(0).getDisplayName(); 89 } else { 90 return mContext.getResources().getQuantityString( 91 R.plurals.vcard_multiple_display_name, vcards.size(), vcards.size()); 92 } 93 } 94 return null; 95 } 96 97 @Override getDetails()98 public String getDetails() { 99 return mDetails; 100 } 101 102 @Override getClickIntent()103 public Intent getClickIntent() { 104 return null; 105 } 106 107 @Override getContactId()108 public long getContactId() { 109 return ContactUtil.INVALID_CONTACT_ID; 110 } 111 112 @Override getLookupKey()113 public String getLookupKey() { 114 return null; 115 } 116 117 @Override getNormalizedDestination()118 public String getNormalizedDestination() { 119 return null; 120 } 121 getVCardResource()122 public VCardResource getVCardResource() { 123 return hasValidVCard() ? mVCardResource : null; 124 } 125 getVCardUri()126 public Uri getVCardUri() { 127 return hasValidVCard() ? mVCardUri : null; 128 } 129 hasValidVCard()130 public boolean hasValidVCard() { 131 return isBound() && mVCardResource != null; 132 } 133 134 @Override bind(final String bindingId)135 public void bind(final String bindingId) { 136 super.bind(bindingId); 137 138 // Bind and request the VCard from media resource manager. 139 mBinding.bind(new VCardRequestDescriptor(mVCardUri).buildAsyncMediaRequest(mContext, this)); 140 MediaResourceManager.get().requestMediaResourceAsync(mBinding.getData()); 141 } 142 143 @Override unbind(final String bindingId)144 public void unbind(final String bindingId) { 145 super.unbind(bindingId); 146 mBinding.unbind(); 147 if (mVCardResource != null) { 148 mVCardResource.release(); 149 mVCardResource = null; 150 } 151 } 152 153 @Override equals(final Object o)154 public boolean equals(final Object o) { 155 if (this == o) { 156 return true; 157 } 158 159 if (!(o instanceof VCardContactItemData)) { 160 return false; 161 } 162 163 final VCardContactItemData lhs = (VCardContactItemData) o; 164 return mVCardUri.equals(lhs.mVCardUri); 165 } 166 167 @Override onMediaResourceLoaded(final MediaRequest<VCardResource> request, final VCardResource resource, final boolean isCached)168 public void onMediaResourceLoaded(final MediaRequest<VCardResource> request, 169 final VCardResource resource, final boolean isCached) { 170 Assert.isTrue(mVCardResource == null); 171 mBinding.ensureBound(); 172 mDetails = mContext.getString(R.string.vcard_tap_hint); 173 mVCardResource = resource; 174 mVCardResource.addRef(); 175 notifyDataUpdated(); 176 } 177 178 @Override onMediaResourceLoadError(final MediaRequest<VCardResource> request, final Exception exception)179 public void onMediaResourceLoadError(final MediaRequest<VCardResource> request, 180 final Exception exception) { 181 mBinding.ensureBound(); 182 mDetails = mContext.getString(R.string.failed_loading_vcard); 183 notifyDataFailed(exception); 184 } 185 } 186