use of com.odysee.app.model.GalleryItem in project odysee-android by OdyseeTeam.
the class LoadGalleryItemsTask method doInBackground.
protected List<GalleryItem> doInBackground(Void... params) {
List<GalleryItem> items = new ArrayList<>();
List<GalleryItem> itemsWithThumbnails = new ArrayList<>();
Cursor cursor = null;
if (context != null) {
ContentResolver resolver = context.getContentResolver();
try {
// TODO: MediaStore.Video.Media.DURATION requires API level 29
String[] projection = { MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE, MediaStore.Video.Media.DURATION };
cursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, String.format("%s DESC LIMIT 150", MediaStore.MediaColumns.DATE_MODIFIED));
if (cursor != null) {
while (cursor.moveToNext()) {
int idColumn = cursor.getColumnIndex(MediaStore.MediaColumns._ID);
int nameColumn = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
int typeColumn = cursor.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE);
int pathColumn = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
// TODO: MediaStore.Video.Media.DURATION requires API level 29
int durationColumn = cursor.getColumnIndex(MediaStore.Video.Media.DURATION);
GalleryItem item = new GalleryItem();
item.setId(cursor.getString(idColumn));
item.setName(cursor.getString(nameColumn));
item.setType(cursor.getString(typeColumn));
item.setFilePath(cursor.getString(pathColumn));
item.setDuration(cursor.getLong(durationColumn));
items.add(item);
}
}
} catch (SQLiteException ex) {
// failed to load videos. log and pass
Log.e(TAG, ex.getMessage(), ex);
} finally {
Helper.closeCursor(cursor);
}
// load (or generate) thumbnail for each item
for (GalleryItem item : items) {
String id = item.getId();
File cacheDir = context.getExternalCacheDir();
File thumbnailsDir = new File(String.format("%s/thumbnails", cacheDir.getAbsolutePath()));
if (!thumbnailsDir.isDirectory()) {
thumbnailsDir.mkdirs();
}
String thumbnailPath = String.format("%s/%s.png", thumbnailsDir.getAbsolutePath(), id);
File file = new File(thumbnailPath);
if (!file.exists()) {
// save the thumbnail to the path
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap thumbnail = MediaStore.Video.Thumbnails.getThumbnail(resolver, Long.parseLong(id), MediaStore.Video.Thumbnails.MINI_KIND, options);
if (thumbnail != null) {
try (FileOutputStream os = new FileOutputStream(thumbnailPath)) {
thumbnail.compress(Bitmap.CompressFormat.PNG, 80, os);
} catch (IOException ex) {
// skip
}
}
}
if (file.exists() && file.length() > 0) {
item.setThumbnailPath(file.getAbsolutePath());
itemsWithThumbnails.add(item);
publishProgress(item);
}
}
}
return itemsWithThumbnails;
}
use of com.odysee.app.model.GalleryItem in project odysee-android by OdyseeTeam.
the class PublishFormFragment method checkParams.
private void checkParams() {
Map<String, Object> params = getParams();
if (params != null) {
if (params.containsKey("claim")) {
Claim claim = (Claim) params.get("claim");
if (claim != null && !claim.equals(this.currentClaim)) {
this.currentClaim = claim;
editFieldsLoaded = false;
}
} else if (params.containsKey("galleryItem")) {
currentGalleryItem = (GalleryItem) params.get("galleryItem");
} else if (params.containsKey("directFilePath")) {
currentFilePath = (String) params.get("directFilePath");
}
if (this.currentClaim == null && params.containsKey("suggestedUrl")) {
String suggestedUrl = (String) params.get("suggestedUrl");
if (!Helper.isNullOrEmpty(suggestedUrl) && Helper.isNullOrEmpty(Helper.getValue(inputAddress.getText()))) {
Helper.setViewText(inputAddress, (String) params.get("suggestedUrl"));
}
}
} else {
// shouldn't actually happen
cancelOnFatalCondition(getString(R.string.no_file_found));
}
}
use of com.odysee.app.model.GalleryItem in project odysee-android by OdyseeTeam.
the class PublishFragment method loadGalleryItems.
private void loadGalleryItems() {
Context context = getContext();
Helper.setViewVisibility(noVideosLoaded, View.GONE);
LoadGalleryItemsTask task = new LoadGalleryItemsTask(loading, context, new LoadGalleryItemsTask.LoadGalleryHandler() {
@Override
public void onItemLoaded(GalleryItem item) {
if (context != null) {
if (adapter == null) {
adapter = new GalleryGridAdapter(Arrays.asList(item), context);
adapter.setListener(new GalleryGridAdapter.GalleryItemClickListener() {
@Override
public void onGalleryItemClicked(GalleryItem item) {
Context context = getContext();
if (context instanceof MainActivity) {
Map<String, Object> params = new HashMap<>();
params.put("galleryItem", item);
params.put("suggestedUrl", getSuggestedPublishUrl());
// ((MainActivity) context).openFragment(PublishFormFragment.class, true, NavMenuItem.ID_ITEM_NEW_PUBLISH, params);
}
}
});
} else {
adapter.addItem(item);
}
if (galleryGrid.getAdapter() == null) {
galleryGrid.setAdapter(adapter);
}
Helper.setViewVisibility(loading, adapter == null || adapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
}
@Override
public void onAllItemsLoaded(List<GalleryItem> items) {
if (context != null) {
if (adapter == null) {
adapter = new GalleryGridAdapter(items, context);
} else {
adapter.addItems(items);
}
if (galleryGrid.getAdapter() == null) {
galleryGrid.setAdapter(adapter);
}
}
checkNoVideosLoaded();
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.odysee.app.model.GalleryItem in project odysee-android by OdyseeTeam.
the class GalleryGridAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(GalleryGridAdapter.ViewHolder vh, int position) {
GalleryItem item = items.get(position);
String thumbnailUrl = item.getThumbnailPath();
Glide.with(context.getApplicationContext()).load(thumbnailUrl).centerCrop().into(vh.thumbnailView);
vh.durationView.setVisibility(item.getDuration() > 0 ? View.VISIBLE : View.INVISIBLE);
vh.durationView.setText(item.getDuration() > 0 ? Helper.formatDuration(Double.valueOf(item.getDuration() / 1000.0).longValue()) : null);
vh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onGalleryItemClicked(item);
}
}
});
}
use of com.odysee.app.model.GalleryItem in project odysee-android by OdyseeTeam.
the class GalleryGridAdapter method addItems.
public void addItems(List<GalleryItem> items) {
for (GalleryItem item : items) {
if (!this.items.contains(item)) {
this.items.add(item);
notifyDataSetChanged();
}
}
}
Aggregations