FileDocCategorySizeDatePackage
ContactsAsyncHelper.javaAPI DocAndroid 1.5 API12402Wed May 06 22:41:56 BST 2009android.pim

ContactsAsyncHelper

public class ContactsAsyncHelper extends android.os.Handler
Helper class for async access of images.

Fields Summary
private static final boolean
DBG
private static final String
LOG_TAG
private static final int
EVENT_LOAD_IMAGE
private static final int
DEFAULT_TOKEN
private static android.os.Handler
sThreadHandler
private static ContactsAsyncHelper
sInstance
Constructors Summary
private ContactsAsyncHelper()
Private constructor for static class

        HandlerThread thread = new HandlerThread("ContactsAsyncWorker");
        thread.start();
        sThreadHandler = new WorkerHandler(thread.getLooper());
    
Methods Summary
public voidhandleMessage(android.os.Message msg)
Called when loading is done.

        WorkerArgs args = (WorkerArgs) msg.obj;
        switch (msg.arg1) {
            case EVENT_LOAD_IMAGE:
                boolean imagePresent = false;

                // if the image has been loaded then display it, otherwise set default.
                // in either case, make sure the image is visible.
                if (args.result != null) {
                    args.view.setVisibility(View.VISIBLE);
                    args.view.setImageDrawable((Drawable) args.result);
                    // make sure the cached photo data is updated.
                    if (args.info != null) {
                        args.info.cachedPhoto = (Drawable) args.result;
                    }
                    imagePresent = true;
                } else if (args.defaultResource != -1) {
                    args.view.setVisibility(View.VISIBLE);
                    args.view.setImageResource(args.defaultResource);
                }
                
                // Note that the data is cached.
                if (args.info != null) {
                    args.info.isCachedPhotoCurrent = true;
                }
                
                // notify the listener if it is there.
                if (args.listener != null) {
                    if (DBG) Log.d(LOG_TAG, "Notifying listener: " + args.listener.toString() + 
                            " image: " + args.uri + " completed");
                    args.listener.onImageLoadComplete(msg.what, args.cookie, args.view,
                            imagePresent);
                }
                break;
            default:    
        }
    
public static final voidupdateImageViewWithContactPhotoAsync(android.content.Context context, android.widget.ImageView imageView, android.net.Uri person, int placeholderImageResource)
Convenience method for calls that do not want to deal with listeners and tokens.

        // Added additional Cookie field in the callee.
        updateImageViewWithContactPhotoAsync (null, DEFAULT_TOKEN, null, null, context, 
                imageView, person, placeholderImageResource);
    
public static final voidupdateImageViewWithContactPhotoAsync(com.android.internal.telephony.CallerInfo info, android.content.Context context, android.widget.ImageView imageView, android.net.Uri person, int placeholderImageResource)
Convenience method for calls that do not want to deal with listeners and tokens, but have a CallerInfo object to cache the image to.

        // Added additional Cookie field in the callee.
        updateImageViewWithContactPhotoAsync (info, DEFAULT_TOKEN, null, null, context, 
                imageView, person, placeholderImageResource);
    
public static final voidupdateImageViewWithContactPhotoAsync(com.android.internal.telephony.CallerInfo info, int token, android.pim.ContactsAsyncHelper$OnImageLoadCompleteListener listener, java.lang.Object cookie, android.content.Context context, android.widget.ImageView imageView, android.net.Uri person, int placeholderImageResource)
Start an image load, attach the result to the specified CallerInfo object. Note, when the query is started, we make the ImageView INVISIBLE if the placeholderImageResource value is -1. When we're given a valid (!= -1) placeholderImageResource value, we make sure the image is visible.

        
        // in case the source caller info is null, the URI will be null as well.
        // just update using the placeholder image in this case.
        if (person == null) {
            if (DBG) Log.d(LOG_TAG, "target image is null, just display placeholder.");
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(placeholderImageResource);
            return;
        }
        
        // Added additional Cookie field in the callee to handle arguments
        // sent to the callback function.
        
        // setup arguments
        WorkerArgs args = new WorkerArgs();
        args.cookie = cookie;
        args.context = context;
        args.view = imageView;
        args.uri = person;
        args.defaultResource = placeholderImageResource;
        args.listener = listener;
        args.info = info;
        
        // setup message arguments
        Message msg = sThreadHandler.obtainMessage(token);
        msg.arg1 = EVENT_LOAD_IMAGE;
        msg.obj = args;
        
        if (DBG) Log.d(LOG_TAG, "Begin loading image: " + args.uri + 
                ", displaying default image for now.");
        
        // set the default image first, when the query is complete, we will
        // replace the image with the correct one.
        if (placeholderImageResource != -1) {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(placeholderImageResource);
        } else {
            imageView.setVisibility(View.INVISIBLE);
        }
        
        // notify the thread to begin working
        sThreadHandler.sendMessage(msg);