diff --git a/gallery/build.properties b/gallery/build.properties index 9d5ea53..5563358 100644 --- a/gallery/build.properties +++ b/gallery/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Sun Apr 26 10:16:07 HKT 2026 +#Sun Apr 26 11:00:35 CST 2026 stageCount=5 libraryProject= baseVersion=15.0 publishVersion=15.0.4 -buildCount=0 +buildCount=5 baseBetaVersion=15.0.5 diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java b/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java index e0e6795..034d95d 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java @@ -33,7 +33,7 @@ public class AlbumActivity extends AppCompatActivity { private String albumPath; private String albumName; private FloatingActionButton fabScrollTop; - private RecyclerView.OnScrollListener scrollListener; + private Preferences prefs; @Override protected void onCreate(Bundle savedInstanceState) { @@ -49,6 +49,8 @@ public class AlbumActivity extends AppCompatActivity { getSupportActionBar().setTitle(albumName); + prefs = new Preferences(this); + recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); adapter = new ImageAdapter(); @@ -63,7 +65,7 @@ public class AlbumActivity extends AppCompatActivity { } }); - scrollListener = new RecyclerView.OnScrollListener() { + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); @@ -77,8 +79,7 @@ public class AlbumActivity extends AppCompatActivity { } } } - }; - recyclerView.addOnScrollListener(scrollListener); + }); adapter.setOnImageClickListener(new OnImageClickListener() { @Override @@ -122,6 +123,20 @@ public class AlbumActivity extends AppCompatActivity { } } +private String getSortOrder(int sortMode) { + switch (sortMode) { + case Preferences.SORT_TIME_ASC: + return android.provider.MediaStore.Images.Media.DATE_ADDED + " ASC"; + case Preferences.SORT_NAME_DESC: + return android.provider.MediaStore.Images.Media.DISPLAY_NAME + " DESC"; + case Preferences.SORT_NAME_ASC: + return android.provider.MediaStore.Images.Media.DISPLAY_NAME + " ASC"; + case Preferences.SORT_TIME_DESC: + default: + return android.provider.MediaStore.Images.Media.DATE_ADDED + " DESC"; + } + } + private void loadImages() { LogUtils.d(TAG, "loadImages"); ArrayList imageUrls = new ArrayList<>(); @@ -131,7 +146,8 @@ public class AlbumActivity extends AppCompatActivity { String selection = android.provider.MediaStore.Images.Media.DATA + " LIKE ?"; String[] selectionArgs = new String[]{albumPath + "%"}; - String sortOrder = android.provider.MediaStore.Images.Media.DATE_ADDED + " DESC"; + int sortMode = prefs.getAlbumSortMode(); + String sortOrder = getSortOrder(sortMode); try (Cursor cursor = contentResolver.query(collection, null, selection, selectionArgs, sortOrder)) { if (cursor != null) { @@ -155,7 +171,64 @@ public class AlbumActivity extends AppCompatActivity { adapter.setData(imageUrls, imagePaths); LogUtils.d(TAG, "Loaded " + imageUrls.size() + " images"); } - + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_album, menu); + int sortMode = prefs.getAlbumSortMode(); + int menuId = getSortMenuId(sortMode); + MenuItem item = menu.findItem(menuId); + if (item != null) { + item.setChecked(true); + } + MenuItem sortItem = menu.findItem(R.id.action_sort); + if (sortItem != null && sortItem.getSubMenu() != null) { + sortItem.getSubMenu().setGroupCheckable(0, true, true); + } + return true; + } + + private int getSortMenuId(int sortMode) { + switch (sortMode) { + case Preferences.SORT_TIME_ASC: + return R.id.sort_time_asc; + case Preferences.SORT_NAME_DESC: + return R.id.sort_name_desc; + case Preferences.SORT_NAME_ASC: + return R.id.sort_name_asc; + case Preferences.SORT_TIME_DESC: + default: + return R.id.sort_time_desc; + } + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + int itemId = item.getItemId(); + if (itemId == R.id.sort_time_desc) { + prefs.setAlbumSortMode(Preferences.SORT_TIME_DESC); + item.setChecked(true); + loadImages(); + return true; + } else if (itemId == R.id.sort_time_asc) { + prefs.setAlbumSortMode(Preferences.SORT_TIME_ASC); + item.setChecked(true); + loadImages(); + return true; + } else if (itemId == R.id.sort_name_desc) { + prefs.setAlbumSortMode(Preferences.SORT_NAME_DESC); + item.setChecked(true); + loadImages(); + return true; + } else if (itemId == R.id.sort_name_asc) { + prefs.setAlbumSortMode(Preferences.SORT_NAME_ASC); + item.setChecked(true); + loadImages(); + return true; + } + return super.onOptionsItemSelected(item); + } + @Override protected void onResume() { super.onResume(); diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java b/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java index cb1badd..16a0b89 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java @@ -58,45 +58,27 @@ public class ImageAdapter extends RecyclerView.Adapter if (pinnedDbHelper == null || imagePaths.isEmpty()) { return; } + ArrayList pinnedUrls = new ArrayList<>(); ArrayList pinnedPaths = new ArrayList<>(); + ArrayList unpinnedUrls = new ArrayList<>(); ArrayList unpinnedPaths = new ArrayList<>(); - for (String path : imagePaths) { + for (int i = 0; i < imagePaths.size(); i++) { + String path = imagePaths.get(i); if (pinnedDbHelper.isPinned(path)) { + pinnedUrls.add(imageUrls.get(i)); pinnedPaths.add(path); } else { + unpinnedUrls.add(imageUrls.get(i)); unpinnedPaths.add(path); } } - Comparator pathComparator = new Comparator() { - @Override - public int compare(String p1, String p2) { - return p1.compareToIgnoreCase(p2); - } - }; - Collections.sort(pinnedPaths, pathComparator); - Collections.sort(unpinnedPaths, pathComparator); - - ArrayList newUrls = new ArrayList<>(); - ArrayList newPaths = new ArrayList<>(); - for (String path : pinnedPaths) { - int index = imagePaths.indexOf(path); - if (index >= 0) { - newUrls.add(imageUrls.get(index)); - newPaths.add(path); - } - } - for (String path : unpinnedPaths) { - int index = imagePaths.indexOf(path); - if (index >= 0) { - newUrls.add(imageUrls.get(index)); - newPaths.add(path); - } - } imageUrls.clear(); imagePaths.clear(); - imageUrls.addAll(newUrls); - imagePaths.addAll(newPaths); + imageUrls.addAll(pinnedUrls); + imageUrls.addAll(unpinnedUrls); + imagePaths.addAll(pinnedPaths); + imagePaths.addAll(unpinnedPaths); } public void setContext(android.content.Context context) { diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java b/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java index 500cb25..d5e70e3 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java @@ -9,9 +9,16 @@ public class Preferences { private static final String PREF_NAME = "gallery_prefs"; private static final String KEY_FOLDER_PATH = "folder_path"; private static final String KEY_BG_TYPE = "bg_type"; + private static final String KEY_ALBUM_SORT_MODE = "album_sort_mode"; private static final int DEFAULT_BG_TYPE = 0; + private static final int DEFAULT_SORT_MODE = 0; private static final String DEFAULT_PATH = "/storage/emulated/0/Pictures/Gallery/owner"; + public static final int SORT_TIME_DESC = 0; + public static final int SORT_TIME_ASC = 1; + public static final int SORT_NAME_DESC = 2; + public static final int SORT_NAME_ASC = 3; + public static String getDefaultPath() { return DEFAULT_PATH; } @@ -41,4 +48,13 @@ public class Preferences { LogUtils.d(TAG, "setBgType: " + type); prefs.edit().putInt(KEY_BG_TYPE, type).apply(); } + + public int getAlbumSortMode() { + return prefs.getInt(KEY_ALBUM_SORT_MODE, DEFAULT_SORT_MODE); + } + + public void setAlbumSortMode(int mode) { + LogUtils.d(TAG, "setAlbumSortMode: " + mode); + prefs.edit().putInt(KEY_ALBUM_SORT_MODE, mode).apply(); + } } \ No newline at end of file diff --git a/gallery/src/main/res/drawable/ic_sort.xml b/gallery/src/main/res/drawable/ic_sort.xml new file mode 100644 index 0000000..7d37ecc --- /dev/null +++ b/gallery/src/main/res/drawable/ic_sort.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/gallery/src/main/res/menu/menu_album.xml b/gallery/src/main/res/menu/menu_album.xml new file mode 100644 index 0000000..9fbf668 --- /dev/null +++ b/gallery/src/main/res/menu/menu_album.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + \ No newline at end of file