Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance performance after AndroidTen #721

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
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;

/**
* Load all albums (grouped by bucket_id) into a single cursor.
Expand Down Expand Up @@ -197,6 +197,8 @@ public Cursor loadInBackground() {
int totalCount = 0;
Uri allAlbumCoverUri = null;

List<Integer> firstIndexes = new ArrayList<>();
int index = 0;
// Pseudo GROUP BY
Map<Long, Long> countMap = new HashMap<>();
if (albums != null) {
Expand All @@ -206,48 +208,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<Long> 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[]{
Expand Down Expand Up @@ -290,4 +283,4 @@ public void onContentChanged() {
private static boolean beforeAndroidTen() {
return android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q;
}
}
}