182 lines
6.8 KiB
Java
182 lines
6.8 KiB
Java
package cn.com.magnity.magnitycx;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Bitmap;
|
|
import android.os.AsyncTask;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.BaseAdapter;
|
|
import android.widget.GridView;
|
|
import android.widget.ImageView;
|
|
import cn.com.magnity.magnitycx.sdk.BitmapUtilities;
|
|
import cn.com.magnity.magnitycx.sdk.GlobalFunc;
|
|
import cn.com.magnity.magnitycx.sdk.LruCacheManager;
|
|
import java.io.File;
|
|
import java.lang.ref.WeakReference;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class MediaGridViewAdapter extends BaseAdapter {
|
|
private static final int SAMPLESIZE = 2;
|
|
private Context context_;
|
|
private ArrayList<String> fileNames_;
|
|
private WeakReference<GridView> gridView_;
|
|
private LayoutInflater layoutInflater_;
|
|
private Map mapSelStatus_;
|
|
private int mode_;
|
|
private LruCacheManager lruCacheManager_ = LruCacheManager.getInstance();
|
|
private Map mapTask_ = new HashMap();
|
|
|
|
public MediaGridViewAdapter(Context context, GridView gridView, ArrayList<String> fileNames, Map mapSelStatus) {
|
|
this.layoutInflater_ = LayoutInflater.from(context);
|
|
this.context_ = context;
|
|
this.gridView_ = new WeakReference<>(gridView);
|
|
this.fileNames_ = fileNames;
|
|
this.mapSelStatus_ = mapSelStatus;
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public int getCount() {
|
|
return this.fileNames_.size();
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public Object getItem(int position) {
|
|
return this.fileNames_.get(position);
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public long getItemId(int position) {
|
|
return this.fileNames_.get(position).hashCode();
|
|
}
|
|
|
|
public void setMode(int mode) {
|
|
this.mode_ = mode;
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
View rootView;
|
|
if (convertView == null) {
|
|
rootView = this.layoutInflater_.inflate(R.layout.item_gridview, (ViewGroup) null);
|
|
} else {
|
|
rootView = convertView;
|
|
}
|
|
String name = this.fileNames_.get(position);
|
|
ImageView imgView = (ImageView) rootView.findViewById(R.id.id_wallphoto);
|
|
imgView.setTag(name);
|
|
ImageView logoView = (ImageView) rootView.findViewById(R.id.id_photo_video);
|
|
loadCachedBitmapIfExist(imgView, logoView, name);
|
|
ImageView viewSelLogo = (ImageView) rootView.findViewById(R.id.id_photo_selected);
|
|
if (this.mode_ == 1) {
|
|
Object obj = this.mapSelStatus_.get(name);
|
|
if (obj == null || !((Boolean) obj).booleanValue()) {
|
|
viewSelLogo.setImageResource(R.mipmap.media_unselected);
|
|
imgView.setAlpha(1.0f);
|
|
} else {
|
|
viewSelLogo.setImageResource(R.mipmap.media_selected);
|
|
imgView.setAlpha(0.6f);
|
|
}
|
|
viewSelLogo.setVisibility(0);
|
|
} else {
|
|
viewSelLogo.setVisibility(4);
|
|
imgView.setAlpha(1.0f);
|
|
}
|
|
return rootView;
|
|
}
|
|
|
|
public void loadBitmap(int firstVisiblePos, int visibleCount) {
|
|
GridView gridView = this.gridView_.get();
|
|
if (gridView != null) {
|
|
Iterator iter = this.mapTask_.entrySet().iterator();
|
|
while (iter.hasNext()) {
|
|
Map.Entry entry = (Map.Entry) iter.next();
|
|
int pos = this.fileNames_.indexOf((String) entry.getKey());
|
|
if (pos < firstVisiblePos) {
|
|
iter.remove();
|
|
}
|
|
}
|
|
for (int i = firstVisiblePos; i < firstVisiblePos + visibleCount; i++) {
|
|
String name = this.fileNames_.get(i);
|
|
Bitmap bitmap = this.lruCacheManager_.get(name);
|
|
if (bitmap != null) {
|
|
ImageView imageView = (ImageView) gridView.findViewWithTag(name);
|
|
if (imageView != null && bitmap != null) {
|
|
imageView.setImageBitmap(bitmap);
|
|
}
|
|
} else if (((AsyncLoadImageTask) this.mapTask_.get(name)) == null) {
|
|
int size = gridView.getColumnWidth();
|
|
AsyncLoadImageTask task = new AsyncLoadImageTask(size, size);
|
|
this.mapTask_.put(name, task);
|
|
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void loadCachedBitmapIfExist(ImageView imgView, ImageView logoView, String name) {
|
|
Bitmap bitmap = this.lruCacheManager_.get(name);
|
|
if (bitmap != null) {
|
|
imgView.setImageBitmap(bitmap);
|
|
} else {
|
|
imgView.setImageResource(R.mipmap.empty_photo);
|
|
}
|
|
GlobalFunc globalFunc = MagApplication.globalFunc;
|
|
if (GlobalFunc.guessMediaTypeBySuffix(name) == 1) {
|
|
logoView.setVisibility(0);
|
|
} else {
|
|
logoView.setVisibility(4);
|
|
}
|
|
}
|
|
|
|
public void cancelAllTasks() {
|
|
for (Map.Entry entry : this.mapTask_.entrySet()) {
|
|
AsyncLoadImageTask task = (AsyncLoadImageTask) entry.getValue();
|
|
if (task != null) {
|
|
task.cancel(false);
|
|
}
|
|
}
|
|
this.mapTask_.clear();
|
|
}
|
|
|
|
private class AsyncLoadImageTask extends AsyncTask<String, Void, Bitmap> {
|
|
private int height_;
|
|
private String name_;
|
|
private int width_;
|
|
|
|
public AsyncLoadImageTask(int width, int height) {
|
|
this.width_ = width;
|
|
this.height_ = height;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // android.os.AsyncTask
|
|
public Bitmap doInBackground(String... params) {
|
|
this.name_ = params[0];
|
|
Bitmap bitmap = BitmapUtilities.getBitmapThumbnail(MagApplication.magParameter.mediaDir + File.separator + this.name_, this.width_, this.height_);
|
|
if (bitmap != null) {
|
|
MediaGridViewAdapter.this.lruCacheManager_.put(this.name_, bitmap);
|
|
}
|
|
return bitmap;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // android.os.AsyncTask
|
|
public void onPostExecute(Bitmap bitmap) {
|
|
ImageView imageView;
|
|
super.onPostExecute((AsyncLoadImageTask) bitmap);
|
|
if (!isCancelled()) {
|
|
GridView gridView = (GridView) MediaGridViewAdapter.this.gridView_.get();
|
|
if (gridView != null && (imageView = (ImageView) gridView.findViewWithTag(this.name_)) != null && bitmap != null) {
|
|
imageView.setImageBitmap(bitmap);
|
|
}
|
|
MediaGridViewAdapter.this.mapTask_.remove(this.name_);
|
|
}
|
|
}
|
|
}
|
|
}
|