Browse Source

rudimentary media loader implementation (file thumbnails and any action TBD)

Andy Scherzinger 8 năm trước cách đây
mục cha
commit
36280b92d6

+ 14 - 0
src/com/owncloud/android/datamodel/MediaFolder.java

@@ -0,0 +1,14 @@
+package com.owncloud.android.datamodel;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Created by scherzia on 22.09.2016.
+ */
+
+public class MediaFolder {
+    public String folder;
+    public String path;
+    public Collection<String> filePaths = new ArrayList<>();
+}

+ 93 - 0
src/com/owncloud/android/datamodel/MediaProvider.java

@@ -0,0 +1,93 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2016 Andy Scherzinger
+ * Copyright (C) 2016 Nextcloud
+ * <p>
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ * <p>
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ * <p>
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.owncloud.android.datamodel;
+
+import android.app.Activity;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.MediaStore;
+import android.util.Log;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * media queries to gain access to media lists for the device.
+ */
+public class MediaProvider {
+    private static final Uri MEDIA_URI = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+
+    /**
+     * TODO rewrite
+     * Getting All Images Path
+     *
+     * @param activity
+     * @return List with images folders
+     */
+    public static List<MediaFolder> getAllShownImagesPath(Activity activity) {
+        Cursor cursor;
+        int column_index_data, column_index_folder_name, column_index_data_image;
+        ArrayList<String> listOfAllImages = new ArrayList<>();
+        String absolutePathOfImage = null;
+        String folderName = null;
+
+        String[] projectionTest = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
+        //String[] projection = {MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.BUCKET_ID};
+        String[] folderProjection = new String[]{"Distinct " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore
+                .MediaColumns.DATA};
+        String[] fileProjection = new String[]{MediaStore.MediaColumns.DATA};
+        String folderSelection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " IS NOT NULL) GROUP BY (" + MediaStore
+                .Images.Media
+                .BUCKET_DISPLAY_NAME;
+        String fileSelection = MediaStore.MediaColumns.DATA + " LIKE ";
+        String folderSortOrder = "MAX(" + MediaStore.Images.Media.DISPLAY_NAME + ") DESC";
+        String fileSortOrder = MediaStore.MediaColumns.DATA + " DESC LIMIT 8"; //  LIMIT 8
+
+        cursor = activity.getContentResolver().query(MEDIA_URI, folderProjection, folderSelection, null, folderSortOrder);
+
+        column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
+        column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
+
+        List<MediaFolder> mediaFolders = new ArrayList<>();
+        while (cursor.moveToNext()) {
+            MediaFolder mediaFolder = new MediaFolder();
+            absolutePathOfImage = cursor.getString(column_index_data);
+            folderName = cursor.getString(column_index_folder_name);
+            mediaFolder.path = folderName;
+            mediaFolder.folder = absolutePathOfImage.substring(0, absolutePathOfImage.lastIndexOf(folderName) + folderName.length());
+            mediaFolder.filePaths = new ArrayList<>();
+
+            Cursor cursorImages = activity.getContentResolver().query(MEDIA_URI, fileProjection, fileSelection + "'" +
+                    absolutePathOfImage.substring(0, absolutePathOfImage.lastIndexOf("/")) + "/%'", null,
+                    fileSortOrder);
+            column_index_data_image = cursorImages.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
+            Log.e("READ IMAGES", "Reading images for --> " + mediaFolder.folder);
+            while (cursorImages.moveToNext()) {
+                mediaFolder.filePaths.add(cursorImages.getString(column_index_data_image));
+            }
+
+            mediaFolders.add(mediaFolder);
+        }
+
+        return mediaFolders;
+    }
+}

+ 54 - 0
src/com/owncloud/android/ui/activity/FolderSyncActivity.java

@@ -24,15 +24,24 @@ package com.owncloud.android.ui.activity;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.Handler;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.util.Log;
 import android.view.MenuItem;
 import android.view.View;
+import android.widget.ProgressBar;
+import android.widget.TextView;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
+import com.owncloud.android.datamodel.MediaFolder;
+import com.owncloud.android.datamodel.MediaProvider;
 import com.owncloud.android.ui.adapter.FolderSyncAdapter;
 
+import java.util.List;
+import java.util.TimerTask;
+
 /**
  * Activity displaying all auto-synced folders and/or instant upload media folders.
  */
@@ -40,6 +49,8 @@ public class FolderSyncActivity extends DrawerActivity {
     private static final String TAG = FolderSyncActivity.class.getSimpleName();
     private RecyclerView mRecyclerView;
     private FolderSyncAdapter mAdapter;
+    private ProgressBar mProgress;
+    private TextView mEmpty;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -57,6 +68,7 @@ public class FolderSyncActivity extends DrawerActivity {
         setupContent();
     }
 
+
     private void setupContent() {
         // TODO setup/initialize UI
         mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
@@ -73,12 +85,54 @@ public class FolderSyncActivity extends DrawerActivity {
         mAdapter.setLayoutManager(lm);
         mRecyclerView.setLayoutManager(lm);
         mRecyclerView.setAdapter(mAdapter);
+
+        load();
     }
 
     public static void selectItem(final Activity context) {
+        // TODO implement selectItem()
         return;
     }
 
+    private void load() {
+        if (mAdapter.getItemCount() > 0) return;
+        setListShown(false);
+        final Handler mHandler = new Handler();
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                final List<MediaFolder> mediaFolders = MediaProvider.getAllShownImagesPath(FolderSyncActivity.this);
+
+                for (MediaFolder mediaFolder : mediaFolders) {
+                    Log.d(TAG, mediaFolder.path);
+                }
+
+                mHandler.post(new TimerTask() {
+                    @Override
+                    public void run() {
+                        mAdapter.setMediaFolders(mediaFolders);
+                        setListShown(true);
+                    }
+                });
+            }
+        }).start();
+    }
+
+    void setListShown(boolean shown) {
+        if (mRecyclerView != null) {
+            mRecyclerView.setVisibility(shown ?
+                    View.VISIBLE : View.GONE);
+
+            // TODO show/hide loading visuals
+            /**
+            mProgress.setVisibility(shown ?
+                    View.GONE : View.VISIBLE);
+            mEmpty.setVisibility(shown && mAdapter.getItemCount() == 0 ?
+                    View.VISIBLE : View.GONE);
+             **/
+        }
+    }
+
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         boolean retval;

+ 82 - 11
src/com/owncloud/android/ui/adapter/FolderSyncAdapter.java

@@ -23,6 +23,8 @@ package com.owncloud.android.ui.adapter;
 
 import android.content.Context;
 import android.support.v7.widget.RecyclerView;
+import android.util.Log;
+import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
@@ -32,8 +34,10 @@ import android.widget.TextView;
 
 import com.afollestad.sectionedrecyclerview.SectionedRecyclerViewAdapter;
 import com.owncloud.android.R;
+import com.owncloud.android.datamodel.MediaFolder;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Adapter to display all auto-synced folders and/or instant upload media folders.
@@ -46,20 +50,26 @@ public class FolderSyncAdapter extends SectionedRecyclerViewAdapter<FolderSyncAd
     private final Context mContext;
     private final int mGridWidth;
     private final ClickListener mListener;
-    private final ArrayList<Object> mCategories;
+    private final List<MediaFolder> mMediaFolders;
     private final RecyclerView mRecyclerView;
 
     public FolderSyncAdapter(Context context, int gridWidth, ClickListener listener, RecyclerView recyclerView) {
         mContext = context;
         mGridWidth = gridWidth * 2;
         mListener = listener;
-        mCategories = new ArrayList<>();
+        mMediaFolders = new ArrayList<>();
         mRecyclerView = recyclerView;
     }
 
+    public void setMediaFolders(List<MediaFolder> mediaFolders) {
+        mMediaFolders.clear();
+        mMediaFolders.addAll(mediaFolders);
+        notifyDataSetChanged();
+    }
+
     @Override
     public void onClick(View v) {
-
+        Log.d(TAG, v.getTag().toString());
     }
 
     @Override
@@ -69,46 +79,107 @@ public class FolderSyncAdapter extends SectionedRecyclerViewAdapter<FolderSyncAd
 
     @Override
     public int getSectionCount() {
-        return 0;
+        return mMediaFolders.size();
     }
 
     @Override
     public int getItemCount(int section) {
-        return 0;
+        return mMediaFolders.get(section).filePaths.size();
     }
 
     @Override
     public void onBindHeaderViewHolder(MainViewHolder holder, int section) {
-
+        holder.title.setText(mMediaFolders.get(section).folder);
+        holder.syncStatusButton.setVisibility(View.VISIBLE);
+        holder.syncStatusButton.setTag(section);
+        holder.syncStatusButton.setOnTouchListener(this);
+        holder.menuButton.setVisibility(View.VISIBLE);
+        holder.menuButton.setTag(section);
+        holder.menuButton.setOnTouchListener(this);
     }
 
     @Override
     public void onBindViewHolder(MainViewHolder holder, int section, int relativePosition, int absolutePosition) {
+        final Context c = holder.itemView.getContext();
+
+        /**
+        if (BitmapUtils.isImage(file)){
+            // Thumbnail in Cache?
+            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
+                    String.valueOf(file.hashCode())
+            );
+            if (thumbnail != null){
+                fileIcon.setImageBitmap(thumbnail);
+            } else {
+
+                // generate new Thumbnail
+                if (allowedToCreateNewThumbnail) {
+                    final ThumbnailsCacheManager.ThumbnailGenerationTask task =
+                            new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon);
+                    if (thumbnail == null) {
+                        if (BitmapUtils.isVideo(file)) {
+                            thumbnail = ThumbnailsCacheManager.mDefaultVideo;
+                        } else {
+                            thumbnail = ThumbnailsCacheManager.mDefaultImg;
+                        }
+                    }
+                    final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
+                            new ThumbnailsCacheManager.AsyncThumbnailDrawable(
+                                    mContext.getResources(),
+                                    thumbnail,
+                                    task
+                            );
+                    fileIcon.setImageDrawable(asyncDrawable);
+                    task.execute(file);
+                    Log_OC.v(TAG, "Executing task to generate a new thumbnail");
+
+                } // else, already being generated, don't restart it
+            }
+        } else {
+            fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(null, file.getName()));
+        }
+        */
+        holder.image.setImageResource(R.drawable.file_image);
+
+        /**
+        if (res == 0) {
+            holder.image.setBackgroundColor(Color.parseColor("#40000000"));
+        } else {
+            Glide.with(c)
+                    .fromResource()
+                    .load(res)
+                    .into(holder.image);
+        }
+         */
 
+        //holder.itemView.setTag(String.format(Locale.getDefault(), "%d:%d:%d", section, relativePos, absolutePos));
+        holder.itemView.setOnClickListener(this);
     }
 
     @Override
     public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
-        return null;
+        View v = LayoutInflater.from(parent.getContext()).inflate(
+                viewType == VIEW_TYPE_HEADER ? R.layout.folder_sync_item_header : R.layout.grid_image, parent, false);
+        return new MainViewHolder(v);
     }
 
     public interface ClickListener {
         void onClick(View view, int section, int relative, int absolute);
     }
 
-    public static class MainViewHolder extends RecyclerView.ViewHolder {
+    static class MainViewHolder extends RecyclerView.ViewHolder {
 
         public MainViewHolder(View itemView) {
             super(itemView);
-            image = (ImageView) itemView.findViewById(R.id.image);
+            image = (ImageView) itemView.findViewById(R.id.thumbnail);
             title = (TextView) itemView.findViewById(R.id.title);
             menuButton = (ImageButton) itemView.findViewById(R.id.syncStatusButton);
-            mSyncStatusButton = (ImageButton) itemView.findViewById(R.id.settingsButton);
+            syncStatusButton = (ImageButton) itemView.findViewById(R.id.settingsButton);
         }
 
         final ImageView image;
         final TextView title;
         final ImageButton menuButton;
-        final ImageButton mSyncStatusButton;
+        final ImageButton syncStatusButton;
     }
 }