Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for using this framework with QuickContactBadges #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/com/loopj/android/image/SmartQuickContactBadge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.loopj.android.image;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.QuickContactBadge;
import android.util.AttributeSet;

public class SmartQuickContactBadge extends QuickContactBadge {
private static final int LOADING_THREADS = 10;
private static ExecutorService threadPool = Executors.newFixedThreadPool(LOADING_THREADS);

private SmartImageTask currentTask;


public SmartQuickContactBadge(Context context) {
super(context);
}

public SmartQuickContactBadge(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SmartQuickContactBadge(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


// Helpers to set image by URL
public void setImageUrl(String url) {
setImage(new WebImage(url));
}

public void setImageUrl(String url, final Integer fallbackResource) {
setImage(new WebImage(url), fallbackResource);
}

public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource) {
setImage(new WebImage(url), fallbackResource, loadingResource);
}


// Helpers to set image by contact address book id
public void setImageContact(long contactId) {
setImage(new ContactImage(contactId));
}

public void setImageContact(long contactId, final Integer fallbackResource) {
setImage(new ContactImage(contactId), fallbackResource);
}

public void setImageContact(long contactId, final Integer fallbackResource, final Integer loadingResource) {
setImage(new ContactImage(contactId), fallbackResource, fallbackResource);
}


// Set image using SmartImage object
public void setImage(final SmartImage image) {
setImage(image, null, null);
}

public void setImage(final SmartImage image, final Integer fallbackResource) {
setImage(image, fallbackResource, fallbackResource);
}

public void setImage(final SmartImage image, final Integer fallbackResource, final Integer loadingResource) {
// Set a loading resource
if(loadingResource != null){
setImageResource(loadingResource);
}

// Cancel any existing tasks for this image view
if(currentTask != null) {
currentTask.cancel();
currentTask = null;
}

// Set up the new task
currentTask = new SmartImageTask(getContext(), image);
currentTask.setOnCompleteHandler(new SmartImageTask.OnCompleteHandler() {
@Override
public void onComplete(Bitmap bitmap) {
if(bitmap != null) {
setImageBitmap(bitmap);
} else {
// Set fallback resource
if(fallbackResource != null) {
setImageResource(fallbackResource);
}
}
}
});

// Run the task in a threadpool
threadPool.execute(currentTask);
}

public static void cancelAllTasks() {
threadPool.shutdownNow();
threadPool = Executors.newFixedThreadPool(LOADING_THREADS);
}
}