package com.android.photos;
import android.app.Fragment;
+import android.app.LoaderManager.LoaderCallbacks;
import android.content.Context;
+import android.content.Loader;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
-import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import com.android.gallery3d.R;
import com.android.photos.data.AlbumSetLoader;
-import com.android.photos.drawables.DataUriThumbnailDrawable;
+import com.android.photos.drawables.DrawableFactory;
+import com.android.photos.shims.MediaSetLoader;
import java.util.Date;
-public class AlbumSetFragment extends Fragment implements OnItemClickListener {
+public class AlbumSetFragment extends Fragment implements OnItemClickListener,
+ LoaderCallbacks<Cursor> {
+
private GridView mAlbumSetView;
private View mEmptyView;
- private CursorAdapter mAdapter;
+ private AlbumSetCursorAdapter mAdapter;
+
+ private static final int LOADER_ALBUMSET = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
mAdapter = new AlbumSetCursorAdapter(getActivity());
mAlbumSetView.setAdapter(mAdapter);
mAlbumSetView.setOnItemClickListener(this);
- mAdapter.swapCursor(AlbumSetLoader.MOCK);
+ getLoaderManager().initLoader(LOADER_ALBUMSET, null, this);
+ updateEmptyStatus();
return root;
}
@Override
+ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+ // TODO: Switch to AlbumSetLoader
+ MediaSetLoader loader = new MediaSetLoader(getActivity());
+ mAdapter.setDrawableFactory(loader);
+ return loader;
+ }
+
+ @Override
+ public void onLoadFinished(Loader<Cursor> loader,
+ Cursor data) {
+ mAdapter.swapCursor(data);
+ updateEmptyStatus();
+ }
+
+ private void updateEmptyStatus() {
+ boolean empty = (mAdapter == null || mAdapter.getCount() == 0);
+ mAlbumSetView.setVisibility(empty ? View.GONE : View.VISIBLE);
+ mEmptyView.setVisibility(empty ? View.VISIBLE : View.GONE);
+ }
+
+ @Override
+ public void onLoaderReset(Loader<Cursor> loader) {
+ }
+
+ @Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
Cursor c = (Cursor) av.getItemAtPosition(pos);
int albumId = c.getInt(AlbumSetLoader.INDEX_ID);
private static class AlbumSetCursorAdapter extends CursorAdapter {
+ private DrawableFactory<Cursor> mDrawableFactory;
+
+ public void setDrawableFactory(DrawableFactory<Cursor> factory) {
+ mDrawableFactory = factory;
+ }
private Date mDate = new Date(); // Used for converting timestamps for display
public AlbumSetCursorAdapter(Context context) {
TextView dateTextView = (TextView) v.findViewById(
R.id.album_set_item_date);
- mDate.setTime(cursor.getLong(AlbumSetLoader.INDEX_TIMESTAMP));
- dateTextView.setText(DateFormat.getMediumDateFormat(context).format(mDate));
+ long timestamp = cursor.getLong(AlbumSetLoader.INDEX_TIMESTAMP);
+ if (timestamp > 0) {
+ mDate.setTime(timestamp);
+ dateTextView.setText(DateFormat.getMediumDateFormat(context).format(mDate));
+ } else {
+ dateTextView.setText(null);
+ }
ProgressBar uploadProgressBar = (ProgressBar) v.findViewById(
R.id.album_set_item_upload_progress);
uploadProgressBar.setVisibility(View.INVISIBLE);
}
- // TODO show the thumbnail
+ ImageView thumbImageView = (ImageView) v.findViewById(
+ R.id.album_set_item_image);
+ Drawable recycle = thumbImageView.getDrawable();
+ Drawable drawable = mDrawableFactory.drawableForItem(cursor, recycle);
+ if (recycle != drawable) {
+ thumbImageView.setImageDrawable(drawable);
+ }
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
- View v = LayoutInflater.from(context).inflate(
+ return LayoutInflater.from(context).inflate(
R.layout.album_set_item, parent, false);
- ImageView thumbImageView = (ImageView) v.findViewById(
- R.id.album_set_item_image);
- thumbImageView.setImageResource(android.R.color.darker_gray);
- return v;
}
}
}
import android.content.AsyncTaskLoader;
import android.content.Context;
+import android.database.Cursor;
+import android.database.MatrixCursor;
+import android.graphics.drawable.Drawable;
+import android.provider.MediaStore.Files.FileColumns;
import com.android.gallery3d.data.ContentListener;
import com.android.gallery3d.data.DataManager;
+import com.android.gallery3d.data.MediaDetails;
+import com.android.gallery3d.data.MediaItem;
import com.android.gallery3d.data.MediaSet;
+import com.android.gallery3d.data.MediaSet.ItemConsumer;
import com.android.gallery3d.data.MediaSet.SyncListener;
import com.android.gallery3d.util.Future;
+import com.android.photos.data.AlbumSetLoader;
+import com.android.photos.data.PhotoSetLoader;
+import com.android.photos.drawables.DrawableFactory;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.util.ArrayList;
/**
- * Proof of concept, don't use
+ * Returns all MediaSets in a MediaSet, wrapping them in a cursor to appear
+ * like a AlbumSetLoader.
*/
-public class MediaSetLoader extends AsyncTaskLoader<MediaSet> {
+public class MediaSetLoader extends AsyncTaskLoader<Cursor> implements DrawableFactory<Cursor>{
private static final SyncListener sNullListener = new SyncListener() {
@Override
}
};
+ private ArrayList<MediaItem> mCoverItems = new ArrayList<MediaItem>();
+
+ public MediaSetLoader(Context context) {
+ super(context);
+ DataManager dm = DataManager.from(context);
+ String path = dm.getTopSetPath(DataManager.INCLUDE_ALL);
+ mMediaSet = dm.getMediaSet(path);
+ }
+
public MediaSetLoader(Context context, String path) {
super(context);
mMediaSet = DataManager.from(getContext()).getMediaSet(path);
}
@Override
- public MediaSet loadInBackground() {
+ public Cursor loadInBackground() {
mMediaSet.loadIfDirty();
- return mMediaSet;
+ final MatrixCursor cursor = new MatrixCursor(AlbumSetLoader.PROJECTION);
+ final Object[] row = new Object[AlbumSetLoader.PROJECTION.length];
+ int count = mMediaSet.getSubMediaSetCount();
+ for (int i = 0; i < count; i++) {
+ MediaSet m = mMediaSet.getSubMediaSet(i);
+ m.loadIfDirty();
+ row[AlbumSetLoader.INDEX_ID] = i;
+ row[AlbumSetLoader.INDEX_TITLE] = m.getName();
+ row[AlbumSetLoader.INDEX_COUNT] = m.getMediaItemCount();
+ MediaItem coverItem = m.getCoverMediaItem();
+ row[AlbumSetLoader.INDEX_TIMESTAMP] = coverItem.getDateInMs();
+ mCoverItems.add(coverItem);
+ cursor.addRow(row);
+ }
+ return cursor;
}
+ @Override
+ public Drawable drawableForItem(Cursor item, Drawable recycle) {
+ BitmapJobDrawable drawable = null;
+ if (recycle == null || !(recycle instanceof BitmapJobDrawable)) {
+ drawable = new BitmapJobDrawable();
+ } else {
+ drawable = (BitmapJobDrawable) recycle;
+ }
+ int index = item.getInt(AlbumSetLoader.INDEX_ID);
+ drawable.setMediaItem(mCoverItems.get(index));
+ return drawable;
+ }
+
+ public static int getThumbnailSize() {
+ return MediaItem.getTargetSize(MediaItem.TYPE_MICROTHUMBNAIL);
+ }
}