packagexcxin.filexpert.view.adpter.dataAdapter;
importandroid.graphics.Bitmap;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.graphics.drawable.Drawable;
importandroid.support.annotation.NonNull;
importandroid.view.View;
importandroid.widget.ImageView;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.PriorityBlockingQueue;
importjava.util.concurrent.ThreadPoolExecutor;
importjava.util.concurrent.TimeUnit;
importrx.Observable;
importrx.android.schedulers.AndroidSchedulers;
importrx.functions.Action1;
importrx.functions.Func1;
importxcxin.filexpert.R;
importxcxin.filexpert.assistant.cache.ThumbCache;
importxcxin.filexpert.assistant.utils.AppThumbUtils;
importxcxin.filexpert.assistant.utils.TimeUtils;
importxcxin.filexpert.model.implement.FileInfo;
importxcxin.filexpert.orm.dao.LabelFile;
importxcxin.filexpert.orm.helper.DbUtils;
importxcxin.filexpert.orm.helper.implement.LabelFileHelper;
/**
* Created by liucheng on 2015/11/2.
*
*管理文件上所有图标
*/
public classThumbWorkManger {
private staticThumbWorkMangerinstance;
privateExecutorServicemThumbPool=null;
public staticThumbWorkMangergetInstance() {
if(instance==null) {
instance=newThumbWorkManger();
}
returninstance;
}
privateThumbWorkManger() {
this.mThumbPool=newThreadPoolExecutor(5,Integer.MAX_VALUE,
60L,TimeUnit.SECONDS,
newPriorityBlockingQueue());
}
private static classBaseThumbThreadimplementsComparable {
private longmTimeFlag;
publicBaseThumbThread() {
this.mTimeFlag= TimeUtils.getCurrentTime();
}
@Override
public intcompareTo(@NonNullBaseThumbThread another) {
return(int) another.getTimeFlag() - (int)mTimeFlag;
}
public longgetTimeFlag() {
returnmTimeFlag;
}
}
public voidgetLocalThumb(intid,BaseViewHolder holder,
FileInfo fileInfo, booleanisList) {
mThumbPool.execute(newLocalThumbThread(id,holder,fileInfo,isList));
}
private classLocalThumbThreadextendsBaseThumbThreadimplementsRunnable {
private intmId;
privateBaseViewHoldermHolder;
privateFileInfomFileInfo;
private booleanmIsList;
publicLocalThumbThread(intid,BaseViewHolder holder,
FileInfo fileInfo, booleanisList) {
super();
this.mId= id;
this.mHolder= holder;
this.mFileInfo= fileInfo;
this.mIsList= isList;
}
@Override
public voidrun() {
setLocalThumb(mFileInfo,mIsList,mHolder,mId);
}
}
private voidsetLocalThumb(FileInfo fileInfo, final booleanisList,
finalBaseViewHolder holder, intid) {
Bitmap bitmap = fileInfo.getThumb(isList);
finalString thumbKey = String.valueOf(isList) + id + fileInfo.getPath();
if(bitmap !=null) {
ThumbCache.getCache().putThumb(thumbKey,bitmap,isList);
Observable.just(bitmap)
.filter(newFunc1() {
@Override
publicBooleancall(Bitmap bitmap) {
returnthumbKey.equals(holder.getThumbView().getTag());
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newAction1() {
@Override
public voidcall(Bitmap bitmap) {
setThumbImpl(holder,isList,bitmap);
}
});
}
}
private voidsetThumbImpl(BaseViewHolder holder, booleanisList,Bitmap cacheBitmap) {
try{
if(null!= cacheBitmap) {
holder.getThumbCircleLayout().setVisibility(View.GONE);
if(isList) {
holder.getThumbView().setImageBitmap(cacheBitmap);
}else{
holder.getThumbView().setVisibility(View.GONE);
holder.getBigThumbView().setVisibility(View.VISIBLE);
holder.getBigThumbView().setImageBitmap(cacheBitmap);
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public voidgetAppIcon(String name,ImageView appIcon) {
mThumbPool.execute(newIconThread(name,appIcon));
}
private classIconThreadextendsBaseThumbThreadimplementsRunnable {
privateStringmName;
privateImageViewmAppIcon;
publicIconThread(String name,ImageView appIcon) {
super();
this.mName= name;
this.mAppIcon= appIcon;
}
@Override
public voidrun() {
Observable.just(mAppIcon)
.map(newFunc1() {
@Override
publicBitmapcall(ImageView imageView) {
if(imageView !=null) {
Drawable drawable = AppThumbUtils.getAppDrawable(mName);
if(drawable !=null) {
return((BitmapDrawable) drawable).getBitmap();
}
}
return null;
}
})
.filter(newFunc1() {
@Override
publicBooleancall(Bitmap bitmap) {
returnbitmap !=null&&
ThumbCache.getCache().putIconToMem(mName,bitmap) &&
mAppIcon.getTag().equals(mName);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newAction1() {
@Override
public voidcall(Bitmap bitmap) {
mAppIcon.setImageBitmap(bitmap);
mAppIcon.setVisibility(View.VISIBLE);
}
}, newAction1() {
@Override
public voidcall(Throwable throwable) {
}
});
}
}
public voidgetLabelIcon(String name,ListViewHolder holder) {
mThumbPool.execute(newLabelIconThread(name,holder));
}
private classLabelIconThreadextendsBaseThumbThreadimplementsRunnable {
privateStringmPath;
privateListViewHoldermHolder;
publicLabelIconThread(String path,ListViewHolder holder) {
super();
this.mPath= path;
this.mHolder= holder;
}
@Override
public voidrun() {
finalImageView labelIconView =mHolder.getLabelIcon();
if(labelIconView !=null) {
LabelFileHelper helper = DbUtils.getLabelFileHelper();
if(helper.isLabelFile(mPath)) {
LabelFile labelFile;
try{
labelFile = helper.getLabelFile(mPath);
}catch(Exception e) {
e.printStackTrace();
return;
}
if(labelFile !=null) {
longlabelId = labelFile.getLabelId();
intcolorId;
try{
colorId = DbUtils.getLabelHelper().queryColor(labelId);
}catch(Exception e) {
return;
}
Observable.just(colorId)
.filter(newFunc1() {
@Override
publicBooleancall(Integer integer) {
returnlabelIconView.getTag().equals(mPath);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newAction1() {
@Override
public voidcall(Integer colorId) {
if(colorId == -1) {
labelIconView.setImageResource(R.drawable.ic_tag_collection_small);
}else{
labelIconView.setImageResource(colorId);
}
labelIconView.setVisibility(View.VISIBLE);
}
});
}
}
}
}
}
public voidgetNetThumb(intid,BaseViewHolder holder,
FileInfo fileInfo, booleanisList) {
mThumbPool.execute(newNetThumbThread(id,holder,fileInfo,isList));
}
private classNetThumbThreadextendsBaseThumbThreadimplementsRunnable {
private intmId;
privateBaseViewHoldermHolder;
privateFileInfomFileInfo;
private booleanmIsList;
publicNetThumbThread(intid,BaseViewHolder holder,
FileInfo fileInfo, booleanisList) {
this.mId= id;
this.mHolder= holder;
this.mFileInfo= fileInfo;
this.mIsList= isList;
}
@Override
public voidrun() {
finalString thumbKey = String.valueOf(true) +mId+mFileInfo.getPath();
Bitmap bitmap = ThumbCache.getCache().getThumbFromDisk(thumbKey);
if(bitmap ==null) {
bitmap =mFileInfo.getThumb(true);
}
if(bitmap !=null) {
ThumbCache.getCache().putThumbToMemAndDisk(thumbKey,bitmap, true);
Observable.just(bitmap)
.filter(newFunc1() {
@Override
publicBooleancall(Bitmap bitmap) {
returnthumbKey.equals(mHolder.getThumbView().getTag());
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newAction1() {
@Override
public voidcall(Bitmap bitmap) {
setThumbImpl(mHolder,mIsList,bitmap);
}
});
}
}
}
}