From 75d2a4a19e3e1fb459d7ea9982e0e4073732a77e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 6 Mar 2016 19:05:37 +0100 Subject: [PATCH] allow selecting multiple items at the directories page --- .../gallery/MyRelativeLayout.java | 46 ++++++++++++++++ .../gallery/activities/MainActivity.java | 54 +++++++++++++++++-- .../gallery/activities/PhotosActivity.java | 6 +-- .../gallery/adapters/DirectoryAdapter.java | 7 ++- app/src/main/res/layout/activity_main.xml | 1 + app/src/main/res/layout/directory_item.xml | 6 +-- .../menu/{cab.xml => directories_menu.xml} | 0 app/src/main/res/menu/photos_menu.xml | 9 ++++ 8 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/simplemobiletools/gallery/MyRelativeLayout.java rename app/src/main/res/menu/{cab.xml => directories_menu.xml} (100%) create mode 100644 app/src/main/res/menu/photos_menu.xml diff --git a/app/src/main/java/com/simplemobiletools/gallery/MyRelativeLayout.java b/app/src/main/java/com/simplemobiletools/gallery/MyRelativeLayout.java new file mode 100644 index 000000000..032571b66 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/gallery/MyRelativeLayout.java @@ -0,0 +1,46 @@ +package com.simplemobiletools.gallery; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.widget.RelativeLayout; + +public class MyRelativeLayout extends RelativeLayout { + private Drawable mForegroundSelector; + + public MyRelativeLayout(Context context) { + super(context); + } + + public MyRelativeLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + { + mForegroundSelector = getResources().getDrawable(R.drawable.selector); + } + + @Override + protected void drawableStateChanged() { + super.drawableStateChanged(); + mForegroundSelector.setState(getDrawableState()); + invalidate(); + } + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + mForegroundSelector.setBounds(0, 0, w, h); + } + + @Override + protected void dispatchDraw(Canvas canvas) { + super.dispatchDraw(canvas); + mForegroundSelector.draw(canvas); + } +} diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index a74573526..e7287bfda 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -4,15 +4,19 @@ import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; +import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; +import android.view.ActionMode; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; @@ -28,9 +32,12 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -public class MainActivity extends AppCompatActivity implements OnItemClickListener { +public class MainActivity extends AppCompatActivity + implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener { private final int STORAGE_PERMISSION = 1; private List dirs; + private GridView gridView; + private int selectedItemsCnt; @Override protected void onCreate(Bundle savedInstanceState) { @@ -69,9 +76,10 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen dirs = new ArrayList<>(getDirectories().values()); final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); - final GridView gridView = (GridView) findViewById(R.id.directories_grid); + gridView = (GridView) findViewById(R.id.directories_grid); gridView.setAdapter(adapter); gridView.setOnItemClickListener(this); + gridView.setMultiChoiceModeListener(this); } private Map getDirectories() { @@ -109,4 +117,44 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen intent.putExtra(Constants.DIRECTORY, dirs.get(position).getPath()); startActivity(intent); } + + @Override + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { + if (checked) + selectedItemsCnt++; + else + selectedItemsCnt--; + + if (selectedItemsCnt > 0) + mode.setTitle(String.valueOf(selectedItemsCnt)); + + mode.invalidate(); + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + final MenuInflater inflater = mode.getMenuInflater(); + inflater.inflate(R.menu.directories_menu, menu); + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + return false; + } + + @Override + public void onDestroyActionMode(ActionMode mode) { + selectedItemsCnt = 0; + } + + @Override + public void onScanCompleted(String path, Uri uri) { + + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java index dac087d5a..c19f0193e 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java @@ -44,7 +44,6 @@ public class PhotosActivity extends AppCompatActivity private int selectedItemsCnt; private GridView gridView; private String path; - private PhotosAdapter adapter; private Snackbar snackbar; private boolean isSnackbarShown; private List toBeDeleted; @@ -95,7 +94,7 @@ public class PhotosActivity extends AppCompatActivity if (isDirEmpty()) return; - adapter = new PhotosAdapter(this, photos); + final PhotosAdapter adapter = new PhotosAdapter(this, photos); gridView = (GridView) findViewById(R.id.photos_grid); gridView.setAdapter(adapter); gridView.setOnItemClickListener(this); @@ -207,6 +206,7 @@ public class PhotosActivity extends AppCompatActivity private void updateGridView() { if (!isDirEmpty()) { + final PhotosAdapter adapter = (PhotosAdapter) gridView.getAdapter(); adapter.updateItems(photos); } } @@ -234,7 +234,7 @@ public class PhotosActivity extends AppCompatActivity @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { final MenuInflater inflater = mode.getMenuInflater(); - inflater.inflate(R.menu.cab, menu); + inflater.inflate(R.menu.photos_menu, menu); return true; } diff --git a/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java b/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java index 75c6abb57..3396062c0 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java @@ -9,12 +9,11 @@ import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; - -import java.util.List; - import com.simplemobiletools.gallery.Directory; import com.simplemobiletools.gallery.R; +import java.util.List; + public class DirectoryAdapter extends BaseAdapter { private final Context context; private final List dirs; @@ -40,7 +39,7 @@ public class DirectoryAdapter extends BaseAdapter { Directory dir = dirs.get(position); holder.dirName.setText(dir.getName()); holder.photoCnt.setText(String.valueOf(dir.getPhotoCnt())); - Glide.with(context).load(dir.getThumbnail()).centerCrop().crossFade().into(holder.dirThumbnail); + Glide.with(context).load(dir.getThumbnail()).placeholder(R.color.tmb_background).centerCrop().crossFade().into(holder.dirThumbnail); return view; } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2867ef856..5e7593f66 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,6 +5,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/black" + android:choiceMode="multipleChoiceModal" android:columnWidth="@dimen/dir_tmb_size" android:horizontalSpacing="1dp" android:numColumns="auto_fit" diff --git a/app/src/main/res/layout/directory_item.xml b/app/src/main/res/layout/directory_item.xml index 6e2d7a407..7fd09d4f0 100644 --- a/app/src/main/res/layout/directory_item.xml +++ b/app/src/main/res/layout/directory_item.xml @@ -1,9 +1,9 @@ - + android:foreground="@drawable/selector"> - + diff --git a/app/src/main/res/menu/cab.xml b/app/src/main/res/menu/directories_menu.xml similarity index 100% rename from app/src/main/res/menu/cab.xml rename to app/src/main/res/menu/directories_menu.xml diff --git a/app/src/main/res/menu/photos_menu.xml b/app/src/main/res/menu/photos_menu.xml new file mode 100644 index 000000000..3122e5205 --- /dev/null +++ b/app/src/main/res/menu/photos_menu.xml @@ -0,0 +1,9 @@ + + + +