allow sorting media

This commit is contained in:
tibbi 2016-07-24 19:43:12 +02:00
parent cd567c370b
commit 90e8309aa1
10 changed files with 193 additions and 26 deletions

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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();

View File

@ -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()]);

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/dialog_holder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin">
<RadioGroup
android:id="@+id/dialog_radio_sorting"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/dialog_radio_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_padding"
android:paddingTop="@dimen/medium_padding"
android:text="@string/name"/>
<RadioButton
android:id="@+id/dialog_radio_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_padding"
android:paddingTop="@dimen/medium_padding"
android:text="@string/date"/>
<RadioButton
android:id="@+id/dialog_radio_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_padding"
android:paddingTop="@dimen/medium_padding"
android:text="@string/size"/>
</RadioGroup>
<ImageView
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="@dimen/medium_padding"
android:layout_marginTop="@dimen/medium_padding"
android:background="@color/light_grey"/>
<RadioGroup
android:id="@+id/dialog_radio_order"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/dialog_radio_ascending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_padding"
android:paddingTop="@dimen/medium_padding"
android:text="@string/ascending"/>
<RadioButton
android:id="@+id/dialog_radio_descending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_padding"
android:paddingTop="@dimen/medium_padding"
android:text="@string/descending"/>
</RadioGroup>
</LinearLayout>

View File

@ -7,4 +7,5 @@
<color name="activated_item_foreground">#99ff6f00</color>
<color name="tmb_background">#ff222222</color>
<color name="actionbar_grey">#55000000</color>
<color name="light_grey">#aa888888</color>
</resources>

View File

@ -8,20 +8,20 @@
<item name="android:textSize">@dimen/normal_text_size</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
<item name="android:textSize">@dimen/normal_text_size</item>
</style>
<style name="FullScreenTheme.Base" parent="AppTheme">
<item name="android:windowContentOverlay">@null</item>
<item name="actionBarStyle">@style/MyFullScreenActionBar</item>
<item name="android:actionBarStyle">@style/MyFullScreenActionBar</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
<item name="android:textSize">@dimen/normal_text_size</item>
</style>
<style name="FullScreenTheme.Base.Dark" parent="AppTheme.Dark">
<item name="android:windowContentOverlay">@null</item>
<item name="actionBarStyle">@style/MyFullScreenActionBar</item>