Skip to content

Commit 8db90ba

Browse files
Use SparseArrayCompat for thumbnails.
1 parent aad5e26 commit 8db90ba

3 files changed

Lines changed: 55 additions & 83 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import androidx.appcompat.app.ActionBar;
3434
import androidx.appcompat.app.AlertDialog;
3535
import androidx.appcompat.widget.TooltipCompat;
36+
import androidx.collection.SparseArrayCompat;
3637
import androidx.core.text.HtmlCompat;
3738
import androidx.preference.PreferenceManager;
3839
import androidx.recyclerview.widget.ItemTouchHelper;
@@ -70,9 +71,7 @@
7071
import java.util.ArrayList;
7172
import java.util.Arrays;
7273
import java.util.Collections;
73-
import java.util.HashMap;
7474
import java.util.List;
75-
import java.util.Map;
7675
import java.util.Queue;
7776
import java.util.concurrent.TimeUnit;
7877
import java.util.stream.Collectors;
@@ -141,7 +140,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
141140
@State
142141
boolean wasSearchFocused = false;
143142

144-
@Nullable private Map<Integer, String> menuItemToFilterName = null;
143+
private final SparseArrayCompat<String> menuItemToFilterName = new SparseArrayCompat<>();
145144
private StreamingService service;
146145
private Page nextPage;
147146
private boolean showLocalSuggestions = true;
@@ -426,8 +425,6 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
426425
supportActionBar.setDisplayHomeAsUpEnabled(true);
427426
}
428427

429-
menuItemToFilterName = new HashMap<>();
430-
431428
int itemId = 0;
432429
boolean isFirstItem = true;
433430
final Context c = getContext();
@@ -468,11 +465,8 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
468465

469466
@Override
470467
public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
471-
if (menuItemToFilterName != null) {
472-
final List<String> cf = new ArrayList<>(1);
473-
cf.add(menuItemToFilterName.get(item.getItemId()));
474-
changeContentFilter(item, cf);
475-
}
468+
final var filter = Collections.singletonList(menuItemToFilterName.get(item.getItemId()));
469+
changeContentFilter(item, filter);
476470
return true;
477471
}
478472

app/src/main/java/org/schabi/newpipe/player/seekbarpreview/SeekbarPreviewThumbnailHolder.java

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
package org.schabi.newpipe.player.seekbarpreview;
22

33
import static org.schabi.newpipe.player.seekbarpreview.SeekbarPreviewThumbnailHelper.SeekbarPreviewThumbnailType;
4+
import static org.schabi.newpipe.player.seekbarpreview.SeekbarPreviewThumbnailHelper.getSeekbarPreviewThumbnailType;
45

56
import android.content.Context;
67
import android.graphics.Bitmap;
78
import android.util.Log;
89

910
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
12+
import androidx.collection.SparseArrayCompat;
1113

1214
import com.google.common.base.Stopwatch;
1315

1416
import org.schabi.newpipe.extractor.stream.Frameset;
1517
import org.schabi.newpipe.util.PicassoHelper;
1618

1719
import java.util.Comparator;
18-
import java.util.HashMap;
1920
import java.util.List;
20-
import java.util.Map;
2121
import java.util.Optional;
2222
import java.util.UUID;
23-
import java.util.concurrent.ConcurrentHashMap;
2423
import java.util.concurrent.ExecutorService;
2524
import java.util.concurrent.Executors;
2625
import java.util.function.Supplier;
@@ -34,18 +33,15 @@ public class SeekbarPreviewThumbnailHolder {
3433

3534
// Key = Position of the picture in milliseconds
3635
// Supplier = Supplies the bitmap for that position
37-
private final Map<Integer, Supplier<Bitmap>> seekbarPreviewData = new ConcurrentHashMap<>();
36+
private final SparseArrayCompat<Supplier<Bitmap>> seekbarPreviewData =
37+
new SparseArrayCompat<>();
3838

3939
// This ensures that if the reset is still undergoing
4040
// and another reset starts, only the last reset is processed
4141
private UUID currentUpdateRequestIdentifier = UUID.randomUUID();
4242

43-
public synchronized void resetFrom(
44-
@NonNull final Context context,
45-
final List<Frameset> framesets) {
46-
47-
final int seekbarPreviewType =
48-
SeekbarPreviewThumbnailHelper.getSeekbarPreviewThumbnailType(context);
43+
public void resetFrom(@NonNull final Context context, final List<Frameset> framesets) {
44+
final int seekbarPreviewType = getSeekbarPreviewThumbnailType(context);
4945

5046
final UUID updateRequestIdentifier = UUID.randomUUID();
5147
this.currentUpdateRequestIdentifier = updateRequestIdentifier;
@@ -63,13 +59,12 @@ public synchronized void resetFrom(
6359
executorService.shutdown();
6460
}
6561

66-
private void resetFromAsync(
67-
final int seekbarPreviewType,
68-
final List<Frameset> framesets,
69-
final UUID updateRequestIdentifier) {
70-
62+
private void resetFromAsync(final int seekbarPreviewType, final List<Frameset> framesets,
63+
final UUID updateRequestIdentifier) {
7164
Log.d(TAG, "Clearing seekbarPreviewData");
72-
seekbarPreviewData.clear();
65+
synchronized (seekbarPreviewData) {
66+
seekbarPreviewData.clear();
67+
}
7368

7469
if (seekbarPreviewType == SeekbarPreviewThumbnailType.NONE) {
7570
Log.d(TAG, "Not processing seekbarPreviewData due to settings");
@@ -94,10 +89,8 @@ private void resetFromAsync(
9489
generateDataFrom(frameset, updateRequestIdentifier);
9590
}
9691

97-
private Frameset getFrameSetForType(
98-
final List<Frameset> framesets,
99-
final int seekbarPreviewType) {
100-
92+
private Frameset getFrameSetForType(final List<Frameset> framesets,
93+
final int seekbarPreviewType) {
10194
if (seekbarPreviewType == SeekbarPreviewThumbnailType.HIGH_QUALITY) {
10295
Log.d(TAG, "Strategy for seekbarPreviewData: high quality");
10396
return framesets.stream()
@@ -111,17 +104,14 @@ private Frameset getFrameSetForType(
111104
}
112105
}
113106

114-
private void generateDataFrom(
115-
final Frameset frameset,
116-
final UUID updateRequestIdentifier) {
117-
107+
private void generateDataFrom(final Frameset frameset, final UUID updateRequestIdentifier) {
118108
Log.d(TAG, "Starting generation of seekbarPreviewData");
119109
final Stopwatch sw = Log.isLoggable(TAG, Log.DEBUG) ? Stopwatch.createStarted() : null;
120110

121111
int currentPosMs = 0;
122112
int pos = 1;
123113

124-
final int frameCountPerUrl = frameset.getFramesPerPageX() * frameset.getFramesPerPageY();
114+
final int urlFrameCount = frameset.getFramesPerPageX() * frameset.getFramesPerPageY();
125115

126116
// Process each url in the frameset
127117
for (final String url : frameset.getUrls()) {
@@ -130,11 +120,11 @@ private void generateDataFrom(
130120

131121
// The data is not added directly to "seekbarPreviewData" due to
132122
// concurrency and checks for "updateRequestIdentifier"
133-
final Map<Integer, Supplier<Bitmap>> generatedDataForUrl = new HashMap<>();
123+
final var generatedDataForUrl = new SparseArrayCompat<Supplier<Bitmap>>(urlFrameCount);
134124

135125
// The bitmap consists of several images, which we process here
136126
// foreach frame in the returned bitmap
137-
for (int i = 0; i < frameCountPerUrl; i++) {
127+
for (int i = 0; i < urlFrameCount; i++) {
138128
// Frames outside the video length are skipped
139129
if (pos > frameset.getTotalCount()) {
140130
break;
@@ -161,15 +151,17 @@ private void generateDataFrom(
161151
// Check if we are still the latest request
162152
// If not abort method execution
163153
if (isRequestIdentifierCurrent(updateRequestIdentifier)) {
164-
seekbarPreviewData.putAll(generatedDataForUrl);
154+
synchronized (seekbarPreviewData) {
155+
seekbarPreviewData.putAll(generatedDataForUrl);
156+
}
165157
} else {
166158
Log.d(TAG, "Aborted of generation of seekbarPreviewData");
167159
break;
168160
}
169161
}
170162

171163
if (sw != null) {
172-
Log.d(TAG, "Generation of seekbarPreviewData took " + sw.stop().toString());
164+
Log.d(TAG, "Generation of seekbarPreviewData took " + sw.stop());
173165
}
174166
}
175167

@@ -189,17 +181,14 @@ private Bitmap getBitMapFrom(final String url) {
189181
final Bitmap bitmap = PicassoHelper.loadSeekbarThumbnailPreview(url).get();
190182

191183
if (sw != null) {
192-
Log.d(TAG,
193-
"Download of bitmap for seekbarPreview from '" + url
194-
+ "' took " + sw.stop().toString());
184+
Log.d(TAG, "Download of bitmap for seekbarPreview from '" + url + "' took "
185+
+ sw.stop());
195186
}
196187

197188
return bitmap;
198189
} catch (final Exception ex) {
199-
Log.w(TAG,
200-
"Failed to get bitmap for seekbarPreview from url='" + url
201-
+ "' in time",
202-
ex);
190+
Log.w(TAG, "Failed to get bitmap for seekbarPreview from url='" + url
191+
+ "' in time", ex);
203192
return null;
204193
}
205194
}
@@ -208,32 +197,20 @@ private boolean isRequestIdentifierCurrent(final UUID requestIdentifier) {
208197
return this.currentUpdateRequestIdentifier.equals(requestIdentifier);
209198
}
210199

211-
212200
public Optional<Bitmap> getBitmapAt(final int positionInMs) {
213-
// Check if the BitmapData is empty
214-
if (seekbarPreviewData.isEmpty()) {
215-
return Optional.empty();
216-
}
217-
218-
// Get the closest frame to the requested position
219-
final int closestIndexPosition =
220-
seekbarPreviewData.keySet().stream()
221-
.min(Comparator.comparingInt(i -> Math.abs(i - positionInMs)))
222-
.orElse(-1);
223-
224-
// this should never happen, because
225-
// it indicates that "seekbarPreviewData" is empty which was already checked
226-
if (closestIndexPosition == -1) {
227-
return Optional.empty();
201+
// Get the frame supplier closest to the requested position
202+
Supplier<Bitmap> closestFrame = () -> null;
203+
synchronized (seekbarPreviewData) {
204+
int min = Integer.MAX_VALUE;
205+
for (int i = 0; i < seekbarPreviewData.size(); i++) {
206+
final int pos = Math.abs(seekbarPreviewData.keyAt(i) - positionInMs);
207+
if (pos < min) {
208+
closestFrame = seekbarPreviewData.valueAt(i);
209+
min = pos;
210+
}
211+
}
228212
}
229213

230-
try {
231-
// Get the bitmap for the position (executes the supplier)
232-
return Optional.ofNullable(seekbarPreviewData.get(closestIndexPosition).get());
233-
} catch (final Exception ex) {
234-
// If there is an error, log it and return Optional.empty
235-
Log.w(TAG, "Unable to get seekbar preview", ex);
236-
return Optional.empty();
237-
}
214+
return Optional.ofNullable(closestFrame.get());
238215
}
239216
}

app/src/main/java/us/shandian/giga/service/DownloadManagerService.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package us.shandian.giga.service;
22

3+
import static org.schabi.newpipe.BuildConfig.APPLICATION_ID;
4+
import static org.schabi.newpipe.BuildConfig.DEBUG;
5+
36
import android.app.Notification;
47
import android.app.NotificationManager;
58
import android.app.PendingIntent;
@@ -22,12 +25,12 @@
2225
import android.os.Message;
2326
import android.os.Parcelable;
2427
import android.util.Log;
25-
import android.util.SparseArray;
2628
import android.widget.Toast;
2729

2830
import androidx.annotation.NonNull;
2931
import androidx.annotation.Nullable;
3032
import androidx.annotation.StringRes;
33+
import androidx.collection.SparseArrayCompat;
3134
import androidx.core.app.NotificationCompat;
3235
import androidx.core.app.NotificationCompat.Builder;
3336
import androidx.core.app.ServiceCompat;
@@ -37,24 +40,21 @@
3740
import org.schabi.newpipe.R;
3841
import org.schabi.newpipe.download.DownloadActivity;
3942
import org.schabi.newpipe.player.helper.LockManager;
43+
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
44+
import org.schabi.newpipe.streams.io.StoredFileHelper;
45+
import org.schabi.newpipe.util.Localization;
46+
import org.schabi.newpipe.util.PendingIntentCompat;
4047

4148
import java.io.File;
4249
import java.io.IOException;
4350
import java.util.ArrayList;
51+
import java.util.List;
4452

4553
import us.shandian.giga.get.DownloadMission;
4654
import us.shandian.giga.get.MissionRecoveryInfo;
47-
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
48-
import org.schabi.newpipe.streams.io.StoredFileHelper;
49-
import org.schabi.newpipe.util.Localization;
50-
import org.schabi.newpipe.util.PendingIntentCompat;
51-
5255
import us.shandian.giga.postprocessing.Postprocessing;
5356
import us.shandian.giga.service.DownloadManager.NetworkState;
5457

55-
import static org.schabi.newpipe.BuildConfig.APPLICATION_ID;
56-
import static org.schabi.newpipe.BuildConfig.DEBUG;
57-
5858
public class DownloadManagerService extends Service {
5959

6060
private static final String TAG = "DownloadManagerService";
@@ -95,7 +95,7 @@ public class DownloadManagerService extends Service {
9595
private Builder downloadDoneNotification = null;
9696
private StringBuilder downloadDoneList = null;
9797

98-
private final ArrayList<Callback> mEchoObservers = new ArrayList<>(1);
98+
private final List<Callback> mEchoObservers = new ArrayList<>(1);
9999

100100
private ConnectivityManager mConnectivityManager;
101101
private ConnectivityManager.NetworkCallback mNetworkStateListenerL = null;
@@ -108,7 +108,8 @@ public class DownloadManagerService extends Service {
108108

109109
private int downloadFailedNotificationID = DOWNLOADS_NOTIFICATION_ID + 1;
110110
private Builder downloadFailedNotification = null;
111-
private final SparseArray<DownloadMission> mFailedDownloads = new SparseArray<>(5);
111+
private final SparseArrayCompat<DownloadMission> mFailedDownloads =
112+
new SparseArrayCompat<>(5);
112113

113114
private Bitmap icLauncher;
114115
private Bitmap icDownloadDone;
@@ -277,7 +278,7 @@ private boolean handleMessage(@NonNull Message msg) {
277278
}
278279

279280
if (msg.what != MESSAGE_ERROR)
280-
mFailedDownloads.delete(mFailedDownloads.indexOfValue(mission));
281+
mFailedDownloads.remove(mFailedDownloads.indexOfValue(mission));
281282

282283
for (Callback observer : mEchoObservers)
283284
observer.handleMessage(msg);
@@ -461,7 +462,7 @@ public void notifyFinishedDownload(String name) {
461462
}
462463

463464
public void notifyFailedDownload(DownloadMission mission) {
464-
if (!mDownloadNotificationEnable || mFailedDownloads.indexOfValue(mission) >= 0) return;
465+
if (!mDownloadNotificationEnable || mFailedDownloads.containsValue(mission)) return;
465466

466467
int id = downloadFailedNotificationID++;
467468
mFailedDownloads.put(id, mission);

0 commit comments

Comments
 (0)