package cn.net.caoyao.cpzshandset.common.bitmap;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import cn.net.caoyao.cpzshandset.common.bitmap.core.BitmapCache;
import cn.net.caoyao.cpzshandset.common.bitmap.core.BitmapDisplayConfig;
import cn.net.caoyao.cpzshandset.common.bitmap.core.BitmapProcess;
import cn.net.caoyao.cpzshandset.common.bitmap.display.Displayer;
import cn.net.caoyao.cpzshandset.common.bitmap.display.SimpleDisplayer;
import cn.net.caoyao.cpzshandset.common.bitmap.display.Utils;
import cn.net.caoyao.cpzshandset.common.bitmap.download.Downloader;
import cn.net.caoyao.cpzshandset.common.bitmap.download.SimpleDownloader;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;

public class MBitmapService {
	private AWonderBitmapConfig mConfig;
	private BitmapCache mImageCache;
	private BitmapProcess mBitmapProcess;
	private boolean mExitTasksEarly = false;
	private boolean mPauseWork = false;
	private final Object mPauseWorkLock = new Object();
	private Context mContext;
	private boolean mInit = false;
	private ExecutorService bitmapLoadAndDisplayExecutor;
	private static MBitmapService mBitmapService;
	private HashMap<String, BitmapDisplayConfig> configMap = new HashMap();
	private static AssetManager am;

	private MBitmapService(Context context) {
		this.mContext = context;
		this.mConfig = new AWonderBitmapConfig(context);
		configDiskCachePath(Utils.getDiskCacheDir(context, "afinalCache").getAbsolutePath());
		configDisplayer(new SimpleDisplayer());
		configDownlader(new SimpleDownloader());
	}
	
	public static MBitmapService getInstance(Context context){
		if (mBitmapService == null ) {
			//$$$$$ 10-13
			mBitmapService = new MBitmapService(context);
		}
		return mBitmapService;
	}

	public static synchronized MBitmapService create(MBitmapService mAWonderBitmap11,Context ctx) {
		am = ctx.getResources().getAssets(); 
		return mAWonderBitmap11;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap11,Context ctx, String diskCachePath) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap11.configDiskCachePath(diskCachePath);
		mAWonderBitmap11.init();
		return mAWonderBitmap11;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap1,Context ctx, String diskCachePath, float memoryCacheSizePercent) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap1.configDiskCachePath(diskCachePath);
		mAWonderBitmap1.configMemoryCachePercent(memoryCacheSizePercent);
		mAWonderBitmap1.init();

		return mAWonderBitmap1;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap1,Context ctx, String diskCachePath, int memoryCacheSize) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap1.configDiskCachePath(diskCachePath);
		mAWonderBitmap1.configMemoryCacheSize(memoryCacheSize);
		mAWonderBitmap1.init();
		
		return mAWonderBitmap1;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap1,Context ctx, String diskCachePath, float memoryCacheSizePercent,
			int threadSize) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap1.configDiskCachePath(diskCachePath);
		mAWonderBitmap1.configBitmapLoadThreadSize(threadSize);
		mAWonderBitmap1.configMemoryCachePercent(memoryCacheSizePercent);
		mAWonderBitmap1.init();
		
		return mAWonderBitmap1;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap1,Context ctx, String diskCachePath, int memoryCacheSize, int threadSize) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap1.configDiskCachePath(diskCachePath);
		mAWonderBitmap1.configBitmapLoadThreadSize(threadSize);
		mAWonderBitmap1.configMemoryCacheSize(memoryCacheSize);
		mAWonderBitmap1.init();
		return mAWonderBitmap1;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap1,Context ctx, String diskCachePath, float memoryCacheSizePercent,
			int diskCacheSize, int threadSize) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap1.configDiskCachePath(diskCachePath);
		mAWonderBitmap1.configBitmapLoadThreadSize(threadSize);
		mAWonderBitmap1.configMemoryCachePercent(memoryCacheSizePercent);
		mAWonderBitmap1.configDiskCacheSize(diskCacheSize);
		mAWonderBitmap1.init();

		return mAWonderBitmap1;
	}

	public static MBitmapService create(MBitmapService mAWonderBitmap1,Context ctx, String diskCachePath, int memoryCacheSize, int diskCacheSize,
			int threadSize) {
		am = ctx.getResources().getAssets(); 
		mAWonderBitmap1.configDiskCachePath(diskCachePath);
		mAWonderBitmap1.configBitmapLoadThreadSize(threadSize);
		mAWonderBitmap1.configMemoryCacheSize(memoryCacheSize);
		mAWonderBitmap1.configDiskCacheSize(diskCacheSize);
		mAWonderBitmap1.init();

		return mAWonderBitmap1;
	}
	

	public MBitmapService configLoadingImage(Bitmap bitmap) {
		this.mConfig.defaultDisplayConfig.setLoadingBitmap(bitmap);
		return this;
	}

	public MBitmapService configLoadingImage(int resId) {
		this.mConfig.defaultDisplayConfig
				.setLoadingBitmap(BitmapFactory.decodeResource(this.mContext.getResources(), resId));
		return this;
	}

	public MBitmapService configLoadfailImage(Bitmap bitmap) {
		this.mConfig.defaultDisplayConfig.setLoadfailBitmap(bitmap);
		return this;
	}

	public MBitmapService configLoadfailImage(int resId) {
		this.mConfig.defaultDisplayConfig
				.setLoadfailBitmap(BitmapFactory.decodeResource(this.mContext.getResources(), resId));
		return this;
	}

	public MBitmapService configBitmapMaxHeight(int bitmapHeight) {
		this.mConfig.defaultDisplayConfig.setBitmapHeight(bitmapHeight);
		return this;
	}

	public MBitmapService configBitmapMaxWidth(int bitmapWidth) {
		this.mConfig.defaultDisplayConfig.setBitmapWidth(bitmapWidth);
		return this;
	}

	public MBitmapService configDownlader(Downloader downlader) {
		this.mConfig.downloader = downlader;
		return this;
	}

	public MBitmapService configDisplayer(Displayer displayer) {
		this.mConfig.displayer = displayer;
		return this;
	}

	public MBitmapService configDiskCachePath(String strPath) {
		if (!TextUtils.isEmpty(strPath)) {
			this.mConfig.cachePath = strPath;
		}
		return this;
	}

	public MBitmapService configMemoryCacheSize(int size) {
		this.mConfig.memCacheSize = size;
		return this;
	}

	public MBitmapService configMemoryCachePercent(float percent) {
		this.mConfig.memCacheSizePercent = percent;
		return this;
	}

	public MBitmapService configDiskCacheSize(int size) {
		this.mConfig.diskCacheSize = size;
		return this;
	}

	public MBitmapService configBitmapLoadThreadSize(int size) {
		if (size >= 1)
			this.mConfig.poolSize = size;
		return this;
	}

	public MBitmapService configRecycleImmediately(boolean recycleImmediately) {
		this.mConfig.recycleImmediately = recycleImmediately;
		return this;
	}

	private MBitmapService init() {
		if (!this.mInit) {
			BitmapCache.ImageCacheParams imageCacheParams = new BitmapCache.ImageCacheParams(this.mConfig.cachePath);
			if ((this.mConfig.memCacheSizePercent > 0.05D) && (this.mConfig.memCacheSizePercent < 0.8D)) {
				imageCacheParams.setMemCacheSizePercent(this.mContext, this.mConfig.memCacheSizePercent);
			} else if (this.mConfig.memCacheSize > 2097152) {
				imageCacheParams.setMemCacheSize(this.mConfig.memCacheSize);
			} else {
				imageCacheParams.setMemCacheSizePercent(this.mContext, 0.3F);
			}

			if (this.mConfig.diskCacheSize > 5242880) {
				imageCacheParams.setDiskCacheSize(this.mConfig.diskCacheSize);
			}
			imageCacheParams.setRecycleImmediately(this.mConfig.recycleImmediately);

			this.mImageCache = new BitmapCache(imageCacheParams);

			this.bitmapLoadAndDisplayExecutor = Executors.newFixedThreadPool(this.mConfig.poolSize,
					new ThreadFactory() {
						public Thread newThread(Runnable r) {
							Thread t = new Thread(r);

							t.setPriority(4);
							return t;
						}
					});
			this.mBitmapProcess = new BitmapProcess(this.mConfig.downloader, this.mImageCache);

			this.mInit = true;
		}

		return this;
	}

	public void display(View imageView, String url) {
		doDisplay(imageView, url, null, false);
	}
	
public Bitmap justDownloadImage( String url) {
		
		if (!this.mInit) {
			init();
		}

		if ((TextUtils.isEmpty(url))) {
			return null;
		}

		Bitmap bitmap = null;

		if (this.mImageCache != null) {
			bitmap = this.mImageCache.getBitmapFromMemoryCache(url);
		}

		if (bitmap != null) {
			//本地已经存在的图片
			LogUtil.e(TConfig.Echo, "本地已存在的图片：" + url);
		} else{
			//本地不存在，去服务器下载图片到本地
//			SimpleDownloader downloader = new SimpleDownloader();
//			downloader.download(url);
			//
			bitmap = MBitmapService.this.processBitmap(url, this.mConfig.defaultDisplayConfig);
			LogUtil.i(TConfig.Echo, "bitmap:"+bitmap + "  从SD卡中获取图片：" + url);
		}
		return bitmap;
	}
//	public void displayCirImage(View imageView, String url) {
//		doDisplay(imageView, url, null,true);
//	}
	public void displayCirImage(final  View imageView, final String url, Bitmap loadingBitmap, Bitmap loadfailBitmap) {
		BitmapDisplayConfig displayConfig = (BitmapDisplayConfig) this.configMap
				.get(String.valueOf(loadingBitmap) + "_" + String.valueOf(loadfailBitmap));
		if (displayConfig == null) {
			displayConfig = getDisplayConfig();
			displayConfig.setLoadingBitmap(loadingBitmap);
			displayConfig.setLoadfailBitmap(loadfailBitmap);
			this.configMap.put(String.valueOf(loadingBitmap) + "_" + String.valueOf(loadfailBitmap), displayConfig);
		}
		doDisplay(imageView, url, displayConfig,true);
	}
	//加载Asset目录下的图片
	public void displayLocalImage(View imageView, String url) {
		if (!TextUtils.isEmpty(url)) {
			loadImageSync(imageView, url);
		}
	}
	
	private void loadImageSync(View view, String background) {
		WeakReference<View> imageViewReference = new WeakReference<View>(view);
		View imageView = imageViewReference.get();
			Bitmap aBitmap = loadImageBitmap(background);
			if (aBitmap != null) {
				imageView.setBackgroundDrawable(new BitmapDrawable(aBitmap));
			}
	}
	
	public Bitmap loadImageBitmap(String background) {
		// TODO Auto-generated method stub
		try {
			background = background.substring(9, background.length());
			InputStream is = am.open(background); 

			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(is, null, o);

			int REQUIRED_SIZE = 100;
			int width_tmp = o.outWidth;
			int height_tmp = o.outHeight;
			int scale = 1;

			while ((width_tmp / 2 >= REQUIRED_SIZE) && (height_tmp / 2 >= REQUIRED_SIZE)) {
				width_tmp /= 2;
				height_tmp /= 2;
				scale *= 2;
			}

			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			InputStream is2 = am.open(background); 
			Bitmap bitmap2 = BitmapFactory.decodeStream(is2, null, o2);
			
			return bitmap2;
		} catch (IOException e) {
			// TODO: handle exception
			return null;
		}
	}

	public void display(View imageView, String url, int imageWidth, int imageHeight) {
		BitmapDisplayConfig displayConfig = (BitmapDisplayConfig) this.configMap.get(imageWidth + "_" + imageHeight);
		if (displayConfig == null) {
			displayConfig = getDisplayConfig();
			displayConfig.setBitmapHeight(imageHeight);
			displayConfig.setBitmapWidth(imageWidth);
			this.configMap.put(imageWidth + "_" + imageHeight, displayConfig);
		}

		doDisplay(imageView, url, displayConfig,false);
	}

	public void display(View imageView, String url, Bitmap loadingBitmap) {
		BitmapDisplayConfig displayConfig = (BitmapDisplayConfig) this.configMap.get(String.valueOf(loadingBitmap));
		if (displayConfig == null) {
			displayConfig = getDisplayConfig();
			displayConfig.setLoadingBitmap(loadingBitmap);
			this.configMap.put(String.valueOf(loadingBitmap), displayConfig);
		}

		doDisplay(imageView, url, displayConfig,false);
	}

	public void display(View imageView, String url, Bitmap loadingBitmap, Bitmap loadfailBitmap) {
		BitmapDisplayConfig displayConfig = (BitmapDisplayConfig) this.configMap
				.get(String.valueOf(loadingBitmap) + "_" + String.valueOf(loadfailBitmap));
		if (displayConfig == null) {
			displayConfig = getDisplayConfig();
			displayConfig.setLoadingBitmap(loadingBitmap);
			displayConfig.setLoadfailBitmap(loadfailBitmap);
			this.configMap.put(String.valueOf(loadingBitmap) + "_" + String.valueOf(loadfailBitmap), displayConfig);
		}

		doDisplay(imageView, url, displayConfig,false);
	}
	public void displayNotCompress(View imageView, String url, Bitmap loadingBitmap, Bitmap loadfailBitmap) {
		BitmapDisplayConfig displayConfig = (BitmapDisplayConfig) this.configMap
				.get(String.valueOf(loadingBitmap) + "_" + String.valueOf(loadfailBitmap));
//		LogUtil.d(TConfig.ECHO_LOGIN_TAG, " displayConfig: "+displayConfig);
		if (displayConfig == null) {
			displayConfig = getDisplayConfig();
			displayConfig.setLoadingBitmap(loadingBitmap);
			displayConfig.setLoadfailBitmap(loadfailBitmap);
			displayConfig.setBitmapWidth(0);
			displayConfig.setBitmapHeight(0);
//			displayConfig
//			LogUtil.d(TConfig.ECHO_LOGIN_TAG, "ScreenHeight:"+displayConfig.getScreenHeight() +"  screenWidth:" + displayConfig.getScreenWidth());
			this.configMap.put(String.valueOf(loadingBitmap) + "_" + String.valueOf(loadfailBitmap), displayConfig);
		}

		doDisplay(imageView, url, displayConfig,false);
	}

	public void display(View imageView, String url, int imageWidth, int imageHeight, Bitmap loadingBitmap,
			Bitmap laodfailBitmap) {
		BitmapDisplayConfig displayConfig = (BitmapDisplayConfig) this.configMap.get(imageWidth + "_" + imageHeight
				+ "_" + String.valueOf(loadingBitmap) + "_" + String.valueOf(laodfailBitmap));
		if (displayConfig == null) {
			displayConfig = getDisplayConfig();
			displayConfig.setBitmapHeight(imageHeight);
			displayConfig.setBitmapWidth(imageWidth);
			displayConfig.setLoadingBitmap(loadingBitmap);
			displayConfig.setLoadfailBitmap(laodfailBitmap);
			this.configMap.put(imageWidth + "_" + imageHeight + "_" + String.valueOf(loadingBitmap) + "_"
					+ String.valueOf(laodfailBitmap), displayConfig);
		}

		doDisplay(imageView, url, displayConfig,false);
	}

	public void display(View imageView, String url, BitmapDisplayConfig config) {
		doDisplay(imageView, url, config,false);
	}

	private void doDisplay(View imageView, String url, BitmapDisplayConfig displayConfig,boolean b) {
		if (!this.mInit) {
			init();
		}

		if ((TextUtils.isEmpty(url)) || (imageView == null)) {
			return;
		}

		if (displayConfig == null) {
			displayConfig = this.mConfig.defaultDisplayConfig;
		}
		Bitmap bitmap = null;

		if (this.mImageCache != null) {
			bitmap = this.mImageCache.getBitmapFromMemoryCache(url);
		}

		if (bitmap != null) {
			if ((imageView instanceof ImageView))
				((ImageView) imageView).setImageBitmap(bitmap);
			else {
				imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
			}

		} else if (checkImageTask(url, imageView)) {
			BitmapLoadAndDisplayTask task = new BitmapLoadAndDisplayTask(imageView, displayConfig,b);

			AsyncDrawable asyncDrawable = new AsyncDrawable(this.mContext.getResources(),
					displayConfig.getLoadingBitmap(), task);

			if ((imageView instanceof ImageView)) {
				((ImageView) imageView).setImageDrawable(asyncDrawable);
			} else {
				imageView.setBackgroundDrawable(asyncDrawable);
			}

			task.executeOnExecutor(this.bitmapLoadAndDisplayExecutor, new Object[] { url });
		}
	}

	private BitmapDisplayConfig getDisplayConfig() {
		BitmapDisplayConfig config = new BitmapDisplayConfig();
		config.setAnimation(this.mConfig.defaultDisplayConfig.getAnimation());
		config.setAnimationType(this.mConfig.defaultDisplayConfig.getAnimationType());
		config.setBitmapHeight(this.mConfig.defaultDisplayConfig.getBitmapHeight());
		config.setBitmapWidth(this.mConfig.defaultDisplayConfig.getBitmapWidth());
		config.setLoadfailBitmap(this.mConfig.defaultDisplayConfig.getLoadfailBitmap());
		config.setLoadingBitmap(this.mConfig.defaultDisplayConfig.getLoadingBitmap());
		return config;
	}

	private void clearCacheInternalInBackgroud() {
		if (this.mImageCache != null)
			this.mImageCache.clearCache();
	}

	private void clearDiskCacheInBackgroud() {
//		MyLog.d(SplashScreen.testClear, "---clearDiskCacheInBackgroud----" + (this.mImageCache != null));
		if (this.mImageCache != null)
			this.mImageCache.clearDiskCache();
	}

	private void clearCacheInBackgroud(String key) {
		if (this.mImageCache != null)
			this.mImageCache.clearCache(key);
	}

	private void clearDiskCacheInBackgroud(String key) {
		if (this.mImageCache != null)
			this.mImageCache.clearDiskCache(key);
	}

	private void closeCacheInternalInBackgroud() {
		if (this.mImageCache != null) {
			this.mImageCache.close();
			this.mImageCache = null;
			mBitmapService = null;
		}
	}

	private Bitmap processBitmap(String uri, BitmapDisplayConfig config) {
		if (this.mBitmapProcess != null) {
			return this.mBitmapProcess.getBitmap(uri, config);
		}
		return null;
	}

	public Bitmap getBitmapFromCache(String key) {
		Bitmap bitmap = getBitmapFromMemoryCache(key);
		if (bitmap == null) {
			bitmap = getBitmapFromDiskCache(key);
		}
		return bitmap;
	}

	public Bitmap getBitmapFromMemoryCache(String key) {
		return this.mImageCache.getBitmapFromMemoryCache(key);
	}

	public Bitmap getBitmapFromDiskCache(String key) {
		return getBitmapFromDiskCache(key, null);
	}

	public Bitmap getBitmapFromDiskCache(String key, BitmapDisplayConfig config) {
		return this.mBitmapProcess.getFromDisk(key, config);
	}

	public void setExitTasksEarly(boolean exitTasksEarly) {
		this.mExitTasksEarly = exitTasksEarly;
	}

	public void onResume() {
		setExitTasksEarly(false);
	}

	public void onPause() {
		setExitTasksEarly(true);
	}

	public void onDestroy() {
		closeCache();
	}

	public void clearCache() {
		// new CacheExecutecTask(null).execute(new Object[] { Integer.valueOf(1)
		// });
		// --我修改的
		new CacheExecutecTask().execute(new Object[] { CacheExecutecTask.MESSAGE_CLEAR });
	}

	public void clearCache(String key) {
		// --我修改的
		// new CacheExecutecTask().execute(new Object[] { Integer.valueOf(4),
		// key });
		new CacheExecutecTask().execute(new Object[] { CacheExecutecTask.MESSAGE_CLEAR_KEY, key });

	}

	public void clearMemoryCache() {
//		MyLog.d(SplashScreen.testClear, "--clearMemoryCache----" + (this.mImageCache != null));
		if (this.mImageCache != null)
			this.mImageCache.clearMemoryCache();
	}

	public void clearMemoryCache(String key) {
		if (this.mImageCache != null)
			this.mImageCache.clearMemoryCache(key);
	}

	public void clearDiskCache() {
		// --我修改的
		// new CacheExecutecTask().execute(new Object[] { Integer.valueOf(3) });
//		MyLog.d(SplashScreen.testClear, "--clearDiskCache---disk-" + (this.mImageCache != null));
		new CacheExecutecTask().execute(new Object[] { CacheExecutecTask.MESSAGE_CLEAR_DISK });
	}

	public void clearDiskCache(String key) {
		// --我修改的
		// new CacheExecutecTask().execute(new Object[] { Integer.valueOf(5),
		// key });
		new CacheExecutecTask().execute(new Object[] { CacheExecutecTask.MESSAGE_CLEAR_KEY_IN_DISK, key });
	}

	public void closeCache() {
		// --我修改的
		// new CacheExecutecTask().execute(new Object[] { Integer.valueOf(2) });
		new CacheExecutecTask().execute(new Object[] { CacheExecutecTask.MESSAGE_CLOSE });
	}

	public void exitTasksEarly(boolean exitTasksEarly) {
		this.mExitTasksEarly = exitTasksEarly;
		if (exitTasksEarly)
			pauseWork(false);
	}

	public void pauseWork(boolean pauseWork) {
		synchronized (this.mPauseWorkLock) {
			this.mPauseWork = pauseWork;
			if (!this.mPauseWork)
				this.mPauseWorkLock.notifyAll();
		}
	}

	private static BitmapLoadAndDisplayTask getBitmapTaskFromImageView(View imageView) {
		if (imageView != null) {
			Drawable drawable = null;
			if ((imageView instanceof ImageView))
				drawable = ((ImageView) imageView).getDrawable();
			else {
				drawable = imageView.getBackground();
			}

			if ((drawable instanceof AsyncDrawable)) {
				AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
				return asyncDrawable.getBitmapWorkerTask();
			}
		}
		return null;
	}

	public static boolean checkImageTask(Object data, View imageView) {
		BitmapLoadAndDisplayTask bitmapWorkerTask = getBitmapTaskFromImageView(imageView);

		if (bitmapWorkerTask != null) {
			Object bitmapData = bitmapWorkerTask.data;
			if ((bitmapData == null) || (!bitmapData.equals(data))) {
				bitmapWorkerTask.cancel(true);
			} else {
				return false;
			}
		}
		return true;
	}

	private class AWonderBitmapConfig {
		public String cachePath;
		public Displayer displayer;
		public Downloader downloader;
		public BitmapDisplayConfig defaultDisplayConfig;
		public float memCacheSizePercent;
		public int memCacheSize;
		public int diskCacheSize;
		public int poolSize = 3;
		public boolean recycleImmediately = true;

		public AWonderBitmapConfig(Context context) {
			this.defaultDisplayConfig = new BitmapDisplayConfig();

			this.defaultDisplayConfig.setAnimation(null);
			this.defaultDisplayConfig.setAnimationType(1);

			DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
			this.defaultDisplayConfig.setScreenWidth(displayMetrics.widthPixels);
			this.defaultDisplayConfig.setScreenHeight(displayMetrics.heightPixels);

			int defaultWidth = (int) Math.floor(displayMetrics.widthPixels / 2);
			this.defaultDisplayConfig.setBitmapHeight(defaultWidth);
			this.defaultDisplayConfig.setBitmapWidth(defaultWidth);
		}
	}

	private static class AsyncDrawable extends BitmapDrawable {
		private final WeakReference<MBitmapService.BitmapLoadAndDisplayTask> bitmapWorkerTaskReference;

		public AsyncDrawable(Resources res, Bitmap bitmap, MBitmapService.BitmapLoadAndDisplayTask bitmapWorkerTask) {
			super(bitmap);
//			bitmap
//			if (bitmap != null)
			this.bitmapWorkerTaskReference = new WeakReference(bitmapWorkerTask);
		}

		public MBitmapService.BitmapLoadAndDisplayTask getBitmapWorkerTask() {
			return (MBitmapService.BitmapLoadAndDisplayTask) this.bitmapWorkerTaskReference.get();
		}
	}

	// 我修改的10-9 Object
	private class BitmapLoadAndDisplayTask extends AsyncTask<Object, Void, Bitmap> {
		private Object data;
		private boolean flagBitmap = false;
		private final WeakReference<View> imageViewReference;
		private final BitmapDisplayConfig displayConfig;

		public BitmapLoadAndDisplayTask(final  View imageView, BitmapDisplayConfig config,boolean flagBitmap) {
			this.imageViewReference = new WeakReference(imageView);
			this.displayConfig = config;

			this.flagBitmap = flagBitmap;
		}

		protected void onPostExecute(Bitmap bitmap) {
			if ((isCancelled()) || (MBitmapService.this.mExitTasksEarly)) {
				bitmap = null;
			}

			View imageView = getAttachedImageView();
			if (!flagBitmap) {
				if ((bitmap != null) && (imageView != null)) {

					MBitmapService.this.mConfig.displayer.loadCompletedisplay(imageView, bitmap, this.displayConfig);

				} else if ((bitmap == null) && (imageView != null)) {
					MBitmapService.this.mConfig.displayer.loadFailDisplay(imageView,
						this.displayConfig.getLoadfailBitmap());
			}
		} else {
			if ((imageView instanceof ImageView)){
				if (bitmap != null){
					((ImageView) imageView).setImageBitmap(bitmap);
				} else if (this.displayConfig.getLoadfailBitmap() != null){
					MBitmapService.this.mConfig.displayer.loadFailDisplay(imageView,
							this.displayConfig.getLoadfailBitmap());
				}
			}
		}

	}

	@SuppressLint({ "NewApi" })
	protected void onCancelled(Bitmap bitmap) {
		super.onCancelled(bitmap);
		synchronized (MBitmapService.this.mPauseWorkLock) {
			MBitmapService.this.mPauseWorkLock.notifyAll();
		}
	}

	private View getAttachedImageView() {
		View imageView = (View) this.imageViewReference.get();
		// BitmapLoadAndDisplayTask bitmapWorkerTask =
		// AWonderBitmap.access$11(imageView);
		// --我修改的
		BitmapLoadAndDisplayTask bitmapWorkerTask = MBitmapService.getBitmapTaskFromImageView(imageView);

		if (this == bitmapWorkerTask) {
			return imageView;
		}

		return null;
	}

	@Override
	protected Bitmap doInBackground(Object... params) {
		// TODO Auto-generated method stub

		this.data = params[0];
		String dataString = String.valueOf(this.data);
		Bitmap bitmap = null;

		// 我修改的10-8

//			synchronized (MBitmapService.this.mPauseWorkLock) {
//				// -------------------------------疑问-----------
//				do {
//					try {
//						MBitmapService.this.mPauseWorkLock.wait();
//
//					} catch (InterruptedException localInterruptedException) {
//						// sys注释
//						System.out.println("----" + localInterruptedException.getLocalizedMessage());
//					}
//					if (!MBitmapService.this.mPauseWork)
//						break;
//				} while (!isCancelled());
//				}

		synchronized (MBitmapService.this.mPauseWorkLock)

		{
			while (mPauseWork && !isCancelled()) {
				try {
					MBitmapService.this.mPauseWorkLock.wait();

				} catch (InterruptedException localInterruptedException) {
				}
			}
		}

		if ((bitmap == null) && (!isCancelled()) && (getAttachedImageView() != null) && (!MBitmapService.this.mExitTasksEarly)) {
			bitmap = MBitmapService.this.processBitmap(dataString, this.displayConfig);
			}

			if (bitmap != null) {
				MBitmapService.this.mImageCache.addToMemoryCache(dataString, bitmap);
			}

			return bitmap;

		}
	}

	private class CacheExecutecTask extends AsyncTask<Object, Void, Void> {
		public static final int MESSAGE_CLEAR = 1;
		public static final int MESSAGE_CLOSE = 2;
		public static final int MESSAGE_CLEAR_DISK = 3;
		public static final int MESSAGE_CLEAR_KEY = 4;
		public static final int MESSAGE_CLEAR_KEY_IN_DISK = 5;

		private CacheExecutecTask() {
			super();
		}

		@Override
		protected Void doInBackground(Object... params) {
			// TODO Auto-generated method stub
			switch (((Integer) params[0]).intValue()) {
			case 1:
				// 清除所有的缓存信息
//				LogUtil.e(TConfig.Echo, 1+"clearCacheInternalInBackgroud");
				MBitmapService.this.clearCacheInternalInBackgroud();
				break;
			case 2:
//				LogUtil.e(TConfig.Echo, 2+"closeCacheInternalInBackgroud");
				MBitmapService.this.closeCacheInternalInBackgroud();
				break;
			case 3:
				// 清除所有的SD卡的缓存信息
//				LogUtil.e(TConfig.Echo, 3+"clearDiskCacheInBackgroud");
				MBitmapService.this.clearDiskCacheInBackgroud();
				break;
			case 4:
				// 根据图片的Key值，清除对应Key值的缓存信息
//				LogUtil.e(TConfig.Echo, 4+"clearCacheInBackgroud");
				MBitmapService.this.clearCacheInBackgroud(String.valueOf(params[1]));
				break;
			case 5:
				// 根据图片的Key值，清除SD卡中对应Key值的缓存信息
//				LogUtil.e(TConfig.Echo, 5+"clearDiskCacheInBackgroud");
				MBitmapService.this.clearDiskCacheInBackgroud(String.valueOf(params[1]));
			}

			return null;
		}

		/*
		 * protected Void doInBackground(Object[] params) {
		 * 
		 * }
		 */
	}
}