securesmartcam / app / src / main / java / org / witness / obscuracam / ui / adapters / AlbumAdapter.java @ 41590feb
History | View | Annotate | Download (7.27 KB)
1 |
package org.witness.obscuracam.ui.adapters; |
---|---|
2 |
|
3 |
import android.content.Context; |
4 |
import android.database.Cursor; |
5 |
import android.net.Uri; |
6 |
import android.os.AsyncTask; |
7 |
import android.provider.MediaStore; |
8 |
import android.support.v7.widget.RecyclerView; |
9 |
import android.text.TextUtils; |
10 |
import android.util.Log; |
11 |
import android.view.LayoutInflater; |
12 |
import android.view.View; |
13 |
import android.view.ViewGroup; |
14 |
|
15 |
import com.squareup.picasso.Picasso; |
16 |
|
17 |
import org.witness.sscphase1.R; |
18 |
|
19 |
import java.io.File; |
20 |
import java.util.ArrayList; |
21 |
import java.util.Locale; |
22 |
|
23 |
|
24 |
public class AlbumAdapter extends RecyclerView.Adapter<AlbumViewHolder> { |
25 |
|
26 |
private static final boolean LOGGING = false; |
27 |
private static final String LOGTAG = "AlbumAdapter"; |
28 |
|
29 |
public interface AlbumAdapterListener { |
30 |
void onAlbumSelected(String id, String albumName); |
31 |
void onPickExternalSelected();
|
32 |
} |
33 |
|
34 |
private final Context mContext; |
35 |
private final ArrayList<AlbumInfo> mAlbums; |
36 |
private final boolean mShowPickExternal; |
37 |
private AlbumAdapterListener mListener;
|
38 |
|
39 |
public AlbumAdapter(Context context, boolean showPickExternal) { |
40 |
super();
|
41 |
mContext = context; |
42 |
mAlbums = new ArrayList<>(); |
43 |
mShowPickExternal = showPickExternal; |
44 |
getAlbums(); |
45 |
} |
46 |
|
47 |
public void setListener(AlbumAdapterListener listener) { |
48 |
mListener = listener; |
49 |
} |
50 |
|
51 |
private void getThumbnailAndCountForAlbum(AlbumInfo album) { |
52 |
album.thumbnail = null;
|
53 |
album.count = 0;
|
54 |
try {
|
55 |
final String orderBy = MediaStore.Images.Media.DATE_TAKEN; |
56 |
String searchParams = MediaStore.Images.Media.BUCKET_ID + " = \"" + album.id + "\""; |
57 |
|
58 |
Cursor photoCursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
59 |
null, searchParams, null, orderBy + " DESC"); |
60 |
if (photoCursor.getCount() > 0) { |
61 |
album.count = photoCursor.getCount(); |
62 |
photoCursor.moveToNext(); |
63 |
int colIndexUri = photoCursor.getColumnIndex(MediaStore.MediaColumns.DATA);
|
64 |
album.thumbnail = photoCursor.getString(colIndexUri); |
65 |
} |
66 |
photoCursor.close(); |
67 |
} catch (Exception e) { |
68 |
e.printStackTrace(); |
69 |
if (LOGGING)
|
70 |
Log.e(LOGTAG, "Failed to get album info: " + e.toString());
|
71 |
} |
72 |
} |
73 |
|
74 |
private void getAlbums() { |
75 |
|
76 |
mAlbums.clear(); |
77 |
try {
|
78 |
String[] PROJECTION_BUCKET = {MediaStore.Images.ImageColumns.BUCKET_ID, |
79 |
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.DATE_TAKEN, |
80 |
MediaStore.Images.ImageColumns.DATA}; |
81 |
|
82 |
String BUCKET_GROUP_BY = "1) GROUP BY 1,(2"; |
83 |
String BUCKET_ORDER_BY = "MAX(datetaken) DESC"; |
84 |
|
85 |
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
86 |
|
87 |
Cursor cur = mContext.getContentResolver().query(images, PROJECTION_BUCKET,
|
88 |
BUCKET_GROUP_BY, null, BUCKET_ORDER_BY);
|
89 |
|
90 |
if (cur.moveToFirst()) {
|
91 |
String bucket;
|
92 |
String bucketId;
|
93 |
int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
|
94 |
int bucketIdColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
|
95 |
do {
|
96 |
// Get the field values
|
97 |
bucket = cur.getString(bucketColumn); |
98 |
bucketId = cur.getString(bucketIdColumn); |
99 |
if (!TextUtils.isEmpty(bucketId)) {
|
100 |
AlbumInfo album = new AlbumInfo();
|
101 |
album.id = bucketId; |
102 |
album.albumName = bucket; |
103 |
mAlbums.add(album); |
104 |
} |
105 |
} while (cur.moveToNext());
|
106 |
} |
107 |
cur.close(); |
108 |
} catch (Exception e) { |
109 |
if (LOGGING)
|
110 |
Log.e(LOGTAG, "Failed to get albums: " + e.toString());
|
111 |
} |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public int getItemViewType(int position) { |
116 |
if (mShowPickExternal) {
|
117 |
if (position == 0) |
118 |
return 1; |
119 |
} |
120 |
return 0; |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
125 |
View view = LayoutInflater.from(mContext).inflate(viewType == 0 ? R.layout.album_item : R.layout.album_external_item, parent, false); |
126 |
return new AlbumViewHolder(view); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void onBindViewHolder(AlbumViewHolder holder, int position) { |
131 |
if (mShowPickExternal) {
|
132 |
if (position == 0) { |
133 |
holder.mRootView.setOnClickListener(new View.OnClickListener() { |
134 |
@Override
|
135 |
public void onClick(View view) { |
136 |
if (mListener != null) { |
137 |
mListener.onPickExternalSelected(); |
138 |
} |
139 |
} |
140 |
}); |
141 |
return;
|
142 |
} else {
|
143 |
position--; //Offset by this item
|
144 |
} |
145 |
} |
146 |
holder.mRootView.setOnClickListener(new ItemClickListener(position));
|
147 |
AlbumInfo album = mAlbums.get(position); |
148 |
holder.mAlbumName.setText(album.albumName); |
149 |
holder.mAlbumCount.setText(String.format(Locale.getDefault(), "(%d)", album.count)); |
150 |
try {
|
151 |
holder.mAlbumThumbnail.setBackgroundResource(0);
|
152 |
try {
|
153 |
Picasso.with(mContext) |
154 |
.load(new File(album.thumbnail)) |
155 |
.fit() |
156 |
.centerCrop() |
157 |
.into(holder.mAlbumThumbnail); |
158 |
} catch (Exception ignored) {} |
159 |
} catch (Exception e) { |
160 |
holder.mAlbumThumbnail.setBackgroundResource(R.drawable.camera_frame); |
161 |
holder.mAlbumThumbnail.setImageDrawable(null);
|
162 |
} |
163 |
|
164 |
if (album.thumbnail == null && album.count == 0) { |
165 |
// Fetch info
|
166 |
AsyncTask<AlbumInfo,Void,AlbumInfo> task = new AsyncTask<AlbumInfo, Void, AlbumInfo>() { |
167 |
@Override
|
168 |
protected AlbumInfo doInBackground(AlbumInfo... albumInfos) {
|
169 |
getThumbnailAndCountForAlbum(albumInfos[0]);
|
170 |
return albumInfos[0]; |
171 |
} |
172 |
|
173 |
@Override
|
174 |
protected void onPostExecute(AlbumInfo albumInfo) { |
175 |
super.onPostExecute(albumInfo);
|
176 |
AlbumAdapter.this.notifyItemChanged((mShowPickExternal ? 1 : 0) + mAlbums.indexOf(albumInfo)); |
177 |
} |
178 |
}.execute(album); |
179 |
} |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public int getItemCount() { |
184 |
return (mShowPickExternal ? 1 : 0) + mAlbums.size(); |
185 |
} |
186 |
|
187 |
private class AlbumInfo { |
188 |
public AlbumInfo() {
|
189 |
} |
190 |
public String id; |
191 |
public String albumName; |
192 |
public String thumbnail; |
193 |
public int count; |
194 |
} |
195 |
|
196 |
private class ItemClickListener implements View.OnClickListener { |
197 |
private final int mPosition; |
198 |
|
199 |
public ItemClickListener(int position) { |
200 |
mPosition = position; |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public void onClick(View view) { |
205 |
AlbumInfo album = mAlbums.get(mPosition); |
206 |
if (mListener != null) { |
207 |
mListener.onAlbumSelected(album.id, album.albumName); |
208 |
} |
209 |
} |
210 |
} |
211 |
} |
212 |
|