Created
December 29, 2015 12:51
-
-
Save shivanic1986/84cbf47ea270e200ffce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.lcom53.picassotest; | |
import android.app.Activity; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.Drawable; | |
import android.media.Image; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AlphabetIndexer; | |
import android.widget.BaseAdapter; | |
import android.widget.Filter; | |
import android.widget.Filterable; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import com.nostra13.universalimageloader.core.DisplayImageOptions; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; | |
import com.nostra13.universalimageloader.core.assist.FailReason; | |
import com.nostra13.universalimageloader.core.assist.ImageScaleType; | |
import com.nostra13.universalimageloader.core.assist.ImageSize; | |
import com.nostra13.universalimageloader.core.assist.QueueProcessingType; | |
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; | |
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; | |
import com.nostra13.universalimageloader.core.process.BitmapProcessor; | |
import com.squareup.okhttp.Cache; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.Target; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.util.ArrayList; | |
public class ContactListAdapter extends BaseAdapter { | |
private final Activity context; | |
LayoutInflater inflater; | |
ArrayList<contact> data; | |
private static AlphabetIndexer mIndexer; | |
private ArrayList<contact> glossariesListForSearch; | |
String TAG = "ContactListAdapter"; | |
SendFilteredDataBack sendFilteredDataBack; | |
OkHttpClient okHttpClient = new OkHttpClient(); | |
String CACHE_DIR = "MyCache"; | |
Cache cache; | |
DisplayImageOptions options; | |
DisplayImageOptions options1; | |
File customCacheDirectory; | |
ViewHolder viewHolder; | |
public ContactListAdapter(final Activity context, ArrayList<contact> data, SendFilteredDataBack sfd) { | |
super(); | |
this.context = context; | |
this.data = data; | |
glossariesListForSearch = data; | |
inflater = LayoutInflater.from(context); | |
sendFilteredDataBack = sfd; | |
options = new DisplayImageOptions.Builder() | |
.showImageOnLoading(R.drawable.xml_src_image) | |
.showImageForEmptyUri(R.drawable.xml_src_image) | |
.showImageOnFail(R.drawable.xml_src_image) | |
.cacheOnDisk(true) | |
.build(); | |
options1 = new DisplayImageOptions.Builder() | |
.showImageOnLoading(R.drawable.xml_src_image) | |
.showImageForEmptyUri(R.drawable.xml_src_image) | |
.showImageOnFail(R.drawable.xml_src_image) | |
.cacheOnDisk(true).imageScaleType(ImageScaleType.NONE_SAFE) | |
.resetViewBeforeLoading(true) | |
.build(); | |
customCacheDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/" + CACHE_DIR); | |
if (!customCacheDirectory.exists()) { | |
boolean isCreated = customCacheDirectory.mkdirs(); | |
Log.d(TAG, "::" + customCacheDirectory.getAbsolutePath() + "isCreated:" + isCreated); | |
} else { | |
Log.d(TAG, "Already exists"); | |
} | |
} | |
public ArrayList<contact> getData() { | |
return data; | |
} | |
@Override | |
public int getCount() { | |
if (data != null) { | |
return data.size(); | |
} else { | |
return 0; | |
} | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public contact getItem(int position) { | |
return data.get(position); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View view = convertView; | |
viewHolder = new ViewHolder(); | |
try { | |
if (view == null) { | |
LayoutInflater inflator = context.getLayoutInflater(); | |
view = inflator.inflate(R.layout.row_contact_list_view, null); | |
viewHolder.tvName = (TextView) view.findViewById(R.id.tv_contact_row_name); | |
viewHolder.tvuname = (TextView) view.findViewById(R.id.tv_contact_row_user_name); | |
viewHolder.imgUserPic = (RecyclingImageView) view.findViewById(R.id.iv_contact_row_user_image); | |
viewHolder.sortKeyLayout = (LinearLayout) view.findViewById(R.id.sort_key_layout); | |
viewHolder.sortKey = (TextView) view.findViewById(R.id.sort_key); | |
viewHolder.tvBlocked = (TextView) view.findViewById(R.id.tv_blocked_status); | |
viewHolder.iv_play = (ImageView) view.findViewById(R.id.iv_play); | |
view.setTag(viewHolder); | |
} else { | |
viewHolder = (ViewHolder) view.getTag(); | |
} | |
contact glossary = data.get(position); | |
if (data.get(position).is_block == 1) { | |
viewHolder.tvBlocked.setVisibility(View.VISIBLE); | |
} else { | |
viewHolder.tvBlocked.setVisibility(View.INVISIBLE); | |
} | |
viewHolder.tvName.setText(data.get(position).completename); | |
viewHolder.tvuname.setText(data.get(position).username); | |
String picURL = data.get(position).photoUri; | |
viewHolder.imgUserPic.setTag(picURL); | |
viewHolder.imgUserPic.setTag(R.string.app_name, data.get(position).username); | |
if (picURL != null && !picURL.equalsIgnoreCase("") && !picURL.equals("null")) { | |
String fname = "image_" + picURL.substring(picURL.lastIndexOf("/") != -1 ? picURL.lastIndexOf("/") + 1 : 0) + ".jpg"; | |
File file = new File(customCacheDirectory, fname); | |
Log.d(TAG, "We pass :" + picURL); | |
if (file.exists()) { | |
Log.d(TAG, "File is exists"); | |
viewHolder.imgUserPic.setTag(fname); | |
ImageLoader.getInstance().displayImage("file://" + file.getAbsolutePath(), viewHolder.imgUserPic, options1, new ImageLoadingListener() { | |
@Override | |
public void onLoadingStarted(String s, View view) { | |
} | |
@Override | |
public void onLoadingFailed(String s, View view, FailReason failReason) { | |
} | |
@Override | |
public void onLoadingComplete(String s, View view, Bitmap bitmap) { | |
Log.d(TAG, "Bitmap is:" + bitmap.getHeight() + ":" + bitmap.getWidth() + ":" + s); | |
} | |
@Override | |
public void onLoadingCancelled(String s, View view) { | |
} | |
}); | |
} else { | |
ImageLoader.getInstance().loadImage(picURL, options | |
, new ImageLoadingListener() { | |
@Override | |
public void onLoadingStarted(String s, View view) { | |
Log.d(TAG, "Started :" + s + "::" + System.currentTimeMillis()); | |
} | |
@Override | |
public void onLoadingFailed(String s, View view, FailReason failReason) { | |
} | |
@Override | |
public void onLoadingComplete(String s, View view, Bitmap bitmap) { | |
Log.d(TAG, "Completed :" + s + "::" + System.currentTimeMillis()); | |
Log.d(TAG, "Downloaded Bitmap is:" + bitmap.getHeight() + ":" + bitmap.getWidth() + ":" + s); | |
if (s != null) { | |
String fname = "image_" + s.substring(s.lastIndexOf("/") != -1 ? s.lastIndexOf("/") + 1 : 0) + ".jpg"; | |
File file = new File(customCacheDirectory, fname); | |
try { | |
if (file.exists()) file.delete(); | |
FileOutputStream out = new FileOutputStream(file); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); | |
out.flush(); | |
out.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
Log.d(TAG, "File saved as:" + file.getAbsolutePath()); | |
ImageLoader.getInstance().displayImage("file://" + file.getAbsolutePath(), viewHolder.imgUserPic, options, new ImageLoadingListener() { | |
@Override | |
public void onLoadingStarted(String s, View view) { | |
Log.d(TAG, "Start loading downloaded file :" + s + "::" + System.currentTimeMillis()); | |
} | |
@Override | |
public void onLoadingFailed(String s, View view, FailReason failReason) { | |
Log.d(TAG, "Start loading downloaded file failed :" + s + "::" + System.currentTimeMillis() + ":" + failReason.getCause().getMessage()); | |
} | |
@Override | |
public void onLoadingComplete(String s, View view, Bitmap bitmap) { | |
Log.d(TAG, "Start loading downloaded file Complete:" + s + "::" + System.currentTimeMillis()); | |
} | |
@Override | |
public void onLoadingCancelled(String s, View view) { | |
} | |
}); | |
} | |
} | |
@Override | |
public void onLoadingCancelled(String s, View view) { | |
} | |
}); | |
} | |
} else { | |
viewHolder.imgUserPic.setImageDrawable(context.getResources().getDrawable(R.drawable.xml_src_image)); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return view; | |
} | |
static class ViewHolder { | |
TextView tvName; | |
TextView tvuname; | |
ImageView iv_play; | |
RecyclingImageView imgUserPic; | |
LinearLayout sortKeyLayout; | |
TextView sortKey; | |
TextView tvBlocked; | |
MyLoader imageTarget; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment