From 90e8309aa16b954bfb913498e1a8ffe6773f2207 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 24 Jul 2016 19:43:12 +0200 Subject: [PATCH] allow sorting media --- .../com/simplemobiletools/gallery/Config.java | 8 +++ .../simplemobiletools/gallery/Constants.java | 8 +++ .../gallery/activities/MainActivity.java | 4 +- .../gallery/activities/MediaActivity.java | 58 +++++++++++++++- .../activities/PhotoVideoActivity.java | 5 +- .../gallery/activities/ViewPagerActivity.java | 22 +++--- .../gallery/models/Medium.java | 28 ++++++-- app/src/main/res/layout/change_sorting.xml | 69 +++++++++++++++++++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/styles.xml | 16 ++--- 10 files changed, 193 insertions(+), 26 deletions(-) create mode 100644 app/src/main/res/layout/change_sorting.xml diff --git a/app/src/main/java/com/simplemobiletools/gallery/Config.java b/app/src/main/java/com/simplemobiletools/gallery/Config.java index 89530e046..9d7e98bda 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Config.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Config.java @@ -29,4 +29,12 @@ public class Config { public void setIsDarkTheme(boolean isDarkTheme) { mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply(); } + + public int getSorting() { + return mPrefs.getInt(Constants.SORT_ORDER, Constants.SORT_BY_DATE | Constants.SORT_DESCENDING); + } + + public void setSorting(int order) { + mPrefs.edit().putInt(Constants.SORT_ORDER, order).apply(); + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/Constants.java b/app/src/main/java/com/simplemobiletools/gallery/Constants.java index 9f99435bf..0b572fcc4 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Constants.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Constants.java @@ -11,4 +11,12 @@ public class Constants { public static final String PREFS_KEY = "Gallery"; public static final String IS_FIRST_RUN = "is_first_run"; public static final String IS_DARK_THEME = "is_dark_theme"; + public static final String SORT_ORDER = "sort_order"; + + // sorting + public static final int SORT_BY_NAME = 1; + public static final int SORT_BY_DATE = 2; + public static final int SORT_BY_SIZE = 4; + + public static final int SORT_DESCENDING = 1024; } 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 8d4bd8031..b7f6f0954 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -334,8 +334,8 @@ public class MainActivity extends SimpleActivity final TextView dirPath = (TextView) renameDirView.findViewById(R.id.directory_path); dirPath.setText(dir.getParent() + "/"); - builder.setPositiveButton("OK", null); - builder.setNegativeButton("Cancel", null); + builder.setPositiveButton(android.R.string.ok, null); + builder.setNegativeButton(android.R.string.cancel, null); final AlertDialog alertDialog = builder.create(); alertDialog.show(); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index 19215de76..314453cec 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -1,6 +1,7 @@ package com.simplemobiletools.gallery.activities; import android.app.WallpaperManager; +import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.database.Cursor; @@ -14,6 +15,7 @@ import android.provider.MediaStore; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v7.app.AlertDialog; import android.util.Log; import android.util.SparseBooleanArray; import android.view.ActionMode; @@ -24,6 +26,8 @@ import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; +import android.widget.RadioButton; +import android.widget.RadioGroup; import com.bumptech.glide.Glide; import com.bumptech.glide.request.animation.GlideAnimation; @@ -134,12 +138,63 @@ public class MediaActivity extends SimpleActivity public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.sort: + showSortingDialog(); return true; default: return super.onOptionsItemSelected(item); } } + private void showSortingDialog() { + final View sortingView = getLayoutInflater().inflate(R.layout.change_sorting, null); + + final AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getResources().getString(R.string.sort_by)); + builder.setView(sortingView); + + final int currSorting = mConfig.getSorting(); + final RadioGroup sortingRadio = (RadioGroup) sortingView.findViewById(R.id.dialog_radio_sorting); + RadioButton sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_name); + if ((currSorting & Constants.SORT_BY_DATE) != 0) { + sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_date); + } else if ((currSorting & Constants.SORT_BY_SIZE) != 0) { + sortBtn = (RadioButton) sortingRadio.findViewById(R.id.dialog_radio_size); + } + sortBtn.setChecked(true); + + final RadioGroup orderRadio = (RadioGroup) sortingView.findViewById(R.id.dialog_radio_order); + RadioButton orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_ascending); + if ((currSorting & Constants.SORT_DESCENDING) != 0) { + orderBtn = (RadioButton) orderRadio.findViewById(R.id.dialog_radio_descending); + } + orderBtn.setChecked(true); + + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int sorting = Constants.SORT_BY_NAME; + switch (sortingRadio.getCheckedRadioButtonId()) { + case R.id.dialog_radio_date: + sorting = Constants.SORT_BY_DATE; + break; + case R.id.dialog_radio_size: + sorting = Constants.SORT_BY_SIZE; + break; + default: + break; + } + + if (orderRadio.getCheckedRadioButtonId() == R.id.dialog_radio_descending) { + sorting |= Constants.SORT_DESCENDING; + } + mConfig.setSorting(sorting); + initializeGallery(); + } + }); + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + } + private void deleteDirectoryIfEmpty() { final File file = new File(mPath); if (file.isDirectory() && file.listFiles().length == 0) { @@ -176,7 +231,7 @@ public class MediaActivity extends SimpleActivity if (file.exists()) { final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN); final long timestamp = cursor.getLong(dateIndex); - media.add(new Medium(curPath, (i == 1), timestamp)); + media.add(new Medium(curPath, (i == 1), timestamp, file.length())); } else { invalidFiles.add(file.getAbsolutePath()); } @@ -186,6 +241,7 @@ public class MediaActivity extends SimpleActivity } } + Medium.mOrder = mConfig.getSorting(); Collections.sort(media); final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java index 17a5e635e..eb6860196 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java @@ -15,6 +15,8 @@ import com.simplemobiletools.gallery.fragments.VideoFragment; import com.simplemobiletools.gallery.fragments.ViewPagerFragment; import com.simplemobiletools.gallery.models.Medium; +import java.io.File; + public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragment.FragmentClickListener { private static ActionBar mActionbar; private static Uri mUri; @@ -37,7 +39,8 @@ public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragm hideSystemUI(); final Bundle bundle = new Bundle(); - final Medium medium = new Medium(mUri.toString(), mIsVideo, 0); + final File file = new File(mUri.toString()); + final Medium medium = new Medium(mUri.toString(), mIsVideo, 0, file.length()); bundle.putSerializable(Constants.MEDIUM, medium); if (savedInstanceState == null) { diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 094115d16..b103ea266 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -70,9 +70,11 @@ public class ViewPagerActivity extends SimpleActivity try { final String[] proj = {MediaStore.Images.Media.DATA}; cursor = getContentResolver().query(uri, proj, null, null, null); - final int dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); - cursor.moveToFirst(); - mPath = cursor.getString(dataIndex); + if (cursor != null) { + final int dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + cursor.moveToFirst(); + mPath = cursor.getString(dataIndex); + } } finally { if (cursor != null) { cursor.close(); @@ -226,8 +228,8 @@ public class ViewPagerActivity extends SimpleActivity builder.setTitle(getResources().getString(R.string.rename_file)); builder.setView(renameFileView); - builder.setPositiveButton("OK", null); - builder.setNegativeButton("Cancel", null); + builder.setPositiveButton(android.R.string.ok, null); + builder.setNegativeButton(android.R.string.cancel, null); final AlertDialog alertDialog = builder.create(); alertDialog.show(); @@ -246,7 +248,7 @@ public class ViewPagerActivity extends SimpleActivity if (file.renameTo(newFile)) { final int currItem = mPager.getCurrentItem(); - mMedia.set(currItem, new Medium(newFile.getAbsolutePath(), mMedia.get(currItem).getIsVideo(), 0)); + mMedia.set(currItem, new Medium(newFile.getAbsolutePath(), mMedia.get(currItem).getIsVideo(), 0, file.length())); final String[] changedFiles = {file.getAbsolutePath(), newFile.getAbsolutePath()}; MediaScannerConnection.scanFile(getApplicationContext(), changedFiles, null, null); @@ -294,7 +296,7 @@ public class ViewPagerActivity extends SimpleActivity } final String where = MediaStore.Images.Media.DATA + " like ? "; final String[] args = new String[]{mDirectory + "%"}; - final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN}; + final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE}; final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); final String pattern = Pattern.quote(mDirectory) + "/[^/]*"; @@ -305,13 +307,17 @@ public class ViewPagerActivity extends SimpleActivity if (curPath.matches(pattern) && !curPath.equals(mToBeDeleted) && !curPath.equals(mBeingDeleted)) { final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN); final long timestamp = cursor.getLong(dateIndex); - media.add(new Medium(curPath, i == 1, timestamp)); + + final int sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE); + final long size = cursor.getLong(sizeIndex); + media.add(new Medium(curPath, i == 1, timestamp, size)); } } while (cursor.moveToNext()); cursor.close(); } } + Medium.mOrder = mConfig.getSorting(); Collections.sort(media); int j = 0; for (Medium medium : media) { diff --git a/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java b/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java index c443536ff..adc128057 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java +++ b/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java @@ -1,5 +1,7 @@ package com.simplemobiletools.gallery.models; +import com.simplemobiletools.gallery.Constants; + import java.io.Serializable; public class Medium implements Serializable, Comparable { @@ -7,11 +9,14 @@ public class Medium implements Serializable, Comparable { private final String mPath; private final boolean mIsVideo; private final long mTimestamp; + private final long mSize; + public static int mOrder; - public Medium(String path, boolean isVideo, long timestamp) { + public Medium(String path, boolean isVideo, long timestamp, long size) { mPath = path; mIsVideo = isVideo; mTimestamp = timestamp; + mSize = size; } public String getPath() { @@ -26,6 +31,10 @@ public class Medium implements Serializable, Comparable { return mTimestamp; } + public long getSize() { + return mSize; + } + public boolean isGif() { return getPath().endsWith(".gif"); } @@ -33,12 +42,19 @@ public class Medium implements Serializable, Comparable { @Override public int compareTo(Object object) { final Medium medium = (Medium) object; - if (mTimestamp < medium.getTimestamp()) { - return 1; - } else if (mTimestamp > medium.getTimestamp()) { - return -1; + int res; + if ((mOrder & Constants.SORT_BY_NAME) == Constants.SORT_BY_NAME) { + res = mPath.compareTo(medium.getPath()); + } else if ((mOrder & Constants.SORT_BY_DATE) == Constants.SORT_BY_DATE) { + res = (mTimestamp > medium.getTimestamp()) ? 1 : -1; + } else { + res = (mSize > medium.getSize()) ? 1 : -1; } - return 0; + + if ((mOrder & Constants.SORT_DESCENDING) == Constants.SORT_DESCENDING) { + res *= -1; + } + return res; } @Override diff --git a/app/src/main/res/layout/change_sorting.xml b/app/src/main/res/layout/change_sorting.xml new file mode 100644 index 000000000..65a131705 --- /dev/null +++ b/app/src/main/res/layout/change_sorting.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index a00e2ebf5..3fbae7ad2 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -7,4 +7,5 @@ #99ff6f00 #ff222222 #55000000 + #aa888888 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 10e019eea..ce273a062 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -8,20 +8,20 @@ @dimen/normal_text_size + + - -