Search in sources :

Example 1 with GalleryItem

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;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) GalleryItem(com.odysee.app.model.GalleryItem) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) BitmapFactory(android.graphics.BitmapFactory) File(java.io.File)

Example 2 with GalleryItem

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));
    }
}
Also used : JSONObject(org.json.JSONObject) GalleryItem(com.odysee.app.model.GalleryItem) Claim(com.odysee.app.model.Claim)

Example 3 with GalleryItem

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);
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) LoadGalleryItemsTask(com.odysee.app.tasks.localdata.LoadGalleryItemsTask) MainActivity(com.odysee.app.MainActivity) GalleryItem(com.odysee.app.model.GalleryItem) GalleryGridAdapter(com.odysee.app.adapter.GalleryGridAdapter)

Example 4 with GalleryItem

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);
            }
        }
    });
}
Also used : GalleryItem(com.odysee.app.model.GalleryItem) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 5 with GalleryItem

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();
        }
    }
}
Also used : GalleryItem(com.odysee.app.model.GalleryItem)

Aggregations

GalleryItem (com.odysee.app.model.GalleryItem)5 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 MainActivity (com.odysee.app.MainActivity)1 GalleryGridAdapter (com.odysee.app.adapter.GalleryGridAdapter)1 Claim (com.odysee.app.model.Claim)1 LoadGalleryItemsTask (com.odysee.app.tasks.localdata.LoadGalleryItemsTask)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1