From 5b2e2f1c6de21ddce8adf5f3986c3e6866dd552c Mon Sep 17 00:00:00 2001 From: Haibo Gu Date: Fri, 6 Dec 2019 10:25:33 +0800 Subject: [PATCH 1/2] Enhance performance after AndroidTen --- .../matisse/internal/loader/AlbumLoader.java | 66 +++++++++---------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java index 157a56349..44e4ff5b3 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java @@ -25,14 +25,17 @@ import android.os.Build; import android.provider.MediaStore; +import androidx.collection.ArraySet; import androidx.loader.content.CursorLoader; import com.zhihu.matisse.MimeType; import com.zhihu.matisse.internal.entity.Album; import com.zhihu.matisse.internal.entity.SelectionSpec; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -197,6 +200,8 @@ public Cursor loadInBackground() { int totalCount = 0; Uri allAlbumCoverUri = null; + List firstIndexes = new ArrayList<>(); + int index = 0; // Pseudo GROUP BY Map countMap = new HashMap<>(); if (albums != null) { @@ -206,48 +211,39 @@ public Cursor loadInBackground() { Long count = countMap.get(bucketId); if (count == null) { count = 1L; + firstIndexes.add(index); } else { count++; } countMap.put(bucketId, count); + index++; } } - MatrixCursor otherAlbums = new MatrixCursor(COLUMNS); - if (albums != null) { - if (albums.moveToFirst()) { - allAlbumCoverUri = getUri(albums); + if (albums.moveToFirst()) { + allAlbumCoverUri = getUri(albums); + } - Set done = new HashSet<>(); - - do { - long bucketId = albums.getLong(albums.getColumnIndex(COLUMN_BUCKET_ID)); - - if (done.contains(bucketId)) { - continue; - } - - long fileId = albums.getLong( - albums.getColumnIndex(MediaStore.Files.FileColumns._ID)); - String bucketDisplayName = albums.getString( - albums.getColumnIndex(COLUMN_BUCKET_DISPLAY_NAME)); - String mimeType = albums.getString( - albums.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE)); - Uri uri = getUri(albums); - long count = countMap.get(bucketId); - - otherAlbums.addRow(new String[]{ - Long.toString(fileId), - Long.toString(bucketId), - bucketDisplayName, - mimeType, - uri.toString(), - String.valueOf(count)}); - done.add(bucketId); - - totalCount += count; - } while (albums.moveToNext()); - } + MatrixCursor otherAlbums = new MatrixCursor(COLUMNS); + for (int firstIndex : firstIndexes) { + albums.moveToPosition(firstIndex); + long bucketId = albums.getLong(albums.getColumnIndex(COLUMN_BUCKET_ID)); + long fileId = albums.getLong( + albums.getColumnIndex(MediaStore.Files.FileColumns._ID)); + String bucketDisplayName = albums.getString( + albums.getColumnIndex(COLUMN_BUCKET_DISPLAY_NAME)); + String mimeType = albums.getString( + albums.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE)); + Uri uri = getUri(albums); + long count = countMap.get(bucketId); + otherAlbums.addRow(new String[]{ + Long.toString(fileId), + Long.toString(bucketId), + bucketDisplayName, + mimeType, + uri.toString(), + String.valueOf(count)}); + totalCount += count; } allAlbum.addRow(new String[]{ @@ -290,4 +286,4 @@ public void onContentChanged() { private static boolean beforeAndroidTen() { return android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q; } -} \ No newline at end of file +} From 2de0e6b5144fc2a84ea863c8687f3575aaf3ff0a Mon Sep 17 00:00:00 2001 From: Haibo Gu Date: Fri, 6 Dec 2019 10:39:11 +0800 Subject: [PATCH 2/2] Remove UnusedImports --- .../java/com/zhihu/matisse/internal/loader/AlbumLoader.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java index 44e4ff5b3..888dc4ae8 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/loader/AlbumLoader.java @@ -25,7 +25,6 @@ import android.os.Build; import android.provider.MediaStore; -import androidx.collection.ArraySet; import androidx.loader.content.CursorLoader; import com.zhihu.matisse.MimeType; @@ -34,10 +33,8 @@ import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; /** * Load all albums (grouped by bucket_id) into a single cursor.