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.ui.contact;
17 
18 import android.content.Context;
19 import android.net.Uri;
20 
21 import com.android.ex.chips.PhotoManager;
22 import com.android.ex.chips.RecipientEntry;
23 import com.android.messaging.Factory;
24 import com.android.messaging.R;
25 import com.android.messaging.datamodel.data.ParticipantData;
26 import com.android.messaging.datamodel.media.AvatarRequestDescriptor;
27 import com.android.messaging.datamodel.media.BindableMediaRequest;
28 import com.android.messaging.datamodel.media.ImageResource;
29 import com.android.messaging.datamodel.media.MediaRequest;
30 import com.android.messaging.datamodel.media.MediaResourceManager;
31 import com.android.messaging.datamodel.media.MediaResourceManager.MediaResourceLoadListener;
32 import com.android.messaging.util.AvatarUriUtil;
33 import com.android.messaging.util.LogUtil;
34 import com.android.messaging.util.ThreadUtil;
35 
36 /**
37  * An implementation of {@link PhotoManager} that hooks up the chips UI's photos with our own
38  * {@link MediaResourceManager} for retrieving and caching contact avatars.
39  */
40 public class ContactRecipientPhotoManager implements PhotoManager {
41     private static final String IMAGE_BYTES_REQUEST_STATIC_BINDING_ID = "imagebytes";
42     private final Context mContext;
43     private final int mIconSize;
44     private final ContactListItemView.HostInterface mClivHostInterface;
45 
ContactRecipientPhotoManager(final Context context, final ContactListItemView.HostInterface clivHostInterface)46     public ContactRecipientPhotoManager(final Context context,
47             final ContactListItemView.HostInterface clivHostInterface) {
48         mContext = context;
49         mIconSize = context.getResources().getDimensionPixelSize(
50                 R.dimen.compose_message_chip_height) - context.getResources().getDimensionPixelSize(
51                         R.dimen.compose_message_chip_padding) * 2;
52         mClivHostInterface = clivHostInterface;
53     }
54 
55     /**
56      * {@inheritDoc}
57      */
58     @Override
populatePhotoBytesAsync(final RecipientEntry entry, final PhotoManagerCallback callback)59     public void populatePhotoBytesAsync(final RecipientEntry entry,
60             final PhotoManagerCallback callback) {
61         // Post all media resource request to the main thread.
62         ThreadUtil.getMainThreadHandler().post(new Runnable() {
63             @Override
64             public void run() {
65                 final Uri avatarUri = AvatarUriUtil.createAvatarUri(
66                         ParticipantData.getFromRecipientEntry(entry));
67                 final AvatarRequestDescriptor descriptor =
68                         new AvatarRequestDescriptor(avatarUri, mIconSize, mIconSize);
69                 final BindableMediaRequest<ImageResource> req = descriptor.buildAsyncMediaRequest(
70                         mContext,
71                         new MediaResourceLoadListener<ImageResource>() {
72                     @Override
73                     public void onMediaResourceLoaded(final MediaRequest<ImageResource> request,
74                             final ImageResource resource, final boolean isCached) {
75                         entry.setPhotoBytes(resource.getBytes());
76                         callback.onPhotoBytesAsynchronouslyPopulated();
77                     }
78 
79                     @Override
80                     public void onMediaResourceLoadError(final MediaRequest<ImageResource> request,
81                             final Exception exception) {
82                         LogUtil.e(LogUtil.BUGLE_TAG, "Photo bytes loading failed due to " +
83                                 exception + " request key=" + request.getKey());
84 
85                         // Fall back to the default avatar image.
86                         callback.onPhotoBytesAsyncLoadFailed();
87                     }});
88 
89                 // Statically bind the request since it's not bound to any specific piece of UI.
90                 req.bind(IMAGE_BYTES_REQUEST_STATIC_BINDING_ID);
91 
92                 Factory.get().getMediaResourceManager().requestMediaResourceAsync(req);
93             }
94         });
95     }
96 }
97