Skip to content

Commit f9fc1cd

Browse files
Isira-SeneviratneStypox
authored andcommitted
Store/retrieve parcelable arrays as lists instead.
1 parent 76f1e58 commit f9fc1cd

6 files changed

Lines changed: 53 additions & 43 deletions

File tree

app/src/main/java/androidx/fragment/app/FragmentStatePagerAdapterMenuWorkaround.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import androidx.annotation.IntDef;
2626
import androidx.annotation.NonNull;
2727
import androidx.annotation.Nullable;
28+
import androidx.core.os.BundleCompat;
2829
import androidx.lifecycle.Lifecycle;
2930
import androidx.viewpager.widget.PagerAdapter;
3031

@@ -284,7 +285,7 @@ public Parcelable saveState() {
284285
Bundle state = null;
285286
if (!mSavedState.isEmpty()) {
286287
state = new Bundle();
287-
state.putParcelableArray("states", mSavedState.toArray(new Fragment.SavedState[0]));
288+
state.putParcelableArrayList("states", mSavedState);
288289
}
289290
for (int i = 0; i < mFragments.size(); i++) {
290291
final Fragment f = mFragments.get(i);
@@ -311,13 +312,12 @@ public void restoreState(@Nullable final Parcelable state, @Nullable final Class
311312
if (state != null) {
312313
final Bundle bundle = (Bundle) state;
313314
bundle.setClassLoader(loader);
314-
final Parcelable[] fss = bundle.getParcelableArray("states");
315+
final var states = BundleCompat.getParcelableArrayList(bundle, "states",
316+
Fragment.SavedState.class);
315317
mSavedState.clear();
316318
mFragments.clear();
317-
if (fss != null) {
318-
for (final Parcelable parcelable : fss) {
319-
mSavedState.add((Fragment.SavedState) parcelable);
320-
}
319+
if (states != null) {
320+
mSavedState.addAll(states);
321321
}
322322
final Iterable<String> keys = bundle.keySet();
323323
for (final String key : keys) {

app/src/main/java/org/schabi/newpipe/about/AboutActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class AboutActivity : AppCompatActivity() {
116116
/**
117117
* List of all software components.
118118
*/
119-
private val SOFTWARE_COMPONENTS = arrayOf(
119+
private val SOFTWARE_COMPONENTS = arrayListOf(
120120
SoftwareComponent(
121121
"ACRA", "2013", "Kevin Gaudin",
122122
"https://github.com/ACRA/acra", StandardLicenses.APACHE2

app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ import org.schabi.newpipe.BuildConfig
1818
import org.schabi.newpipe.R
1919
import org.schabi.newpipe.databinding.FragmentLicensesBinding
2020
import org.schabi.newpipe.databinding.ItemSoftwareComponentBinding
21+
import org.schabi.newpipe.ktx.parcelableArrayList
2122
import org.schabi.newpipe.util.Localization
2223
import org.schabi.newpipe.util.external_communication.ShareUtils
2324

2425
/**
2526
* Fragment containing the software licenses.
2627
*/
2728
class LicenseFragment : Fragment() {
28-
private lateinit var softwareComponents: Array<SoftwareComponent>
29+
private lateinit var softwareComponents: List<SoftwareComponent>
2930
private var activeSoftwareComponent: SoftwareComponent? = null
3031
private val compositeDisposable = CompositeDisposable()
3132

3233
override fun onCreate(savedInstanceState: Bundle?) {
3334
super.onCreate(savedInstanceState)
34-
softwareComponents = arguments?.getParcelableArray(ARG_COMPONENTS) as Array<SoftwareComponent>
35+
softwareComponents = arguments?.parcelableArrayList<SoftwareComponent>(ARG_COMPONENTS)!!
36+
.sortedBy { it.name } // Sort components by name
3537
activeSoftwareComponent = savedInstanceState?.getSerializable(SOFTWARE_COMPONENT_KEY) as? SoftwareComponent
36-
// Sort components by name
37-
softwareComponents.sortBy { it.name }
3838
}
3939

4040
override fun onDestroy() {
@@ -130,7 +130,8 @@ class LicenseFragment : Fragment() {
130130
StandardLicenses.GPL3,
131131
BuildConfig.VERSION_NAME
132132
)
133-
fun newInstance(softwareComponents: Array<SoftwareComponent>): LicenseFragment {
133+
134+
fun newInstance(softwareComponents: ArrayList<SoftwareComponent>): LicenseFragment {
134135
val fragment = LicenseFragment()
135136
fragment.arguments = bundleOf(ARG_COMPONENTS to softwareComponents)
136137
return fragment

app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
import java.io.File;
7676
import java.io.IOException;
77+
import java.util.ArrayList;
7778
import java.util.List;
7879
import java.util.Locale;
7980
import java.util.Objects;
@@ -1052,7 +1053,7 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
10521053
final char kind;
10531054
int threads = dialogBinding.threads.getProgress() + 1;
10541055
final String[] urls;
1055-
final MissionRecoveryInfo[] recoveryInfo;
1056+
final List<MissionRecoveryInfo> recoveryInfo;
10561057
String psName = null;
10571058
String[] psArgs = null;
10581059
long nearLength = 0;
@@ -1117,9 +1118,7 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
11171118
urls = new String[] {
11181119
selectedStream.getContent()
11191120
};
1120-
recoveryInfo = new MissionRecoveryInfo[] {
1121-
new MissionRecoveryInfo(selectedStream)
1122-
};
1121+
recoveryInfo = List.of(new MissionRecoveryInfo(selectedStream));
11231122
} else {
11241123
if (secondaryStream.getDeliveryMethod() != PROGRESSIVE_HTTP) {
11251124
throw new IllegalArgumentException("Unsupported stream delivery format"
@@ -1129,12 +1128,14 @@ private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
11291128
urls = new String[] {
11301129
selectedStream.getContent(), secondaryStream.getContent()
11311130
};
1132-
recoveryInfo = new MissionRecoveryInfo[] {new MissionRecoveryInfo(selectedStream),
1133-
new MissionRecoveryInfo(secondaryStream)};
1131+
recoveryInfo = List.of(
1132+
new MissionRecoveryInfo(selectedStream),
1133+
new MissionRecoveryInfo(secondaryStream)
1134+
);
11341135
}
11351136

11361137
DownloadManagerService.startMission(context, urls, storage, kind, threads,
1137-
currentInfo.getUrl(), psName, psArgs, nearLength, recoveryInfo);
1138+
currentInfo.getUrl(), psName, psArgs, nearLength, new ArrayList<>(recoveryInfo));
11381139

11391140
Toast.makeText(context, getString(R.string.download_has_started),
11401141
Toast.LENGTH_SHORT).show();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.schabi.newpipe.ktx
2+
3+
import android.os.Bundle
4+
import android.os.Parcelable
5+
import androidx.core.os.BundleCompat
6+
7+
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String?): ArrayList<T>? {
8+
return BundleCompat.getParcelableArrayList(this, key, T::class.java)
9+
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import android.os.Handler.Callback;
2424
import android.os.IBinder;
2525
import android.os.Message;
26-
import android.os.Parcelable;
2726
import android.util.Log;
2827
import android.widget.Toast;
2928

@@ -36,6 +35,7 @@
3635
import androidx.core.app.PendingIntentCompat;
3736
import androidx.core.app.ServiceCompat;
3837
import androidx.core.content.ContextCompat;
38+
import androidx.core.content.IntentCompat;
3939
import androidx.preference.PreferenceManager;
4040

4141
import org.schabi.newpipe.R;
@@ -49,6 +49,7 @@
4949
import java.io.IOException;
5050
import java.util.ArrayList;
5151
import java.util.List;
52+
import java.util.Objects;
5253

5354
import us.shandian.giga.get.DownloadMission;
5455
import us.shandian.giga.get.MissionRecoveryInfo;
@@ -359,37 +360,39 @@ public void updateForegroundState(boolean state) {
359360
*/
360361
public static void startMission(Context context, String[] urls, StoredFileHelper storage,
361362
char kind, int threads, String source, String psName,
362-
String[] psArgs, long nearLength, MissionRecoveryInfo[] recoveryInfo) {
363-
Intent intent = new Intent(context, DownloadManagerService.class);
364-
intent.setAction(Intent.ACTION_RUN);
365-
intent.putExtra(EXTRA_URLS, urls);
366-
intent.putExtra(EXTRA_KIND, kind);
367-
intent.putExtra(EXTRA_THREADS, threads);
368-
intent.putExtra(EXTRA_SOURCE, source);
369-
intent.putExtra(EXTRA_POSTPROCESSING_NAME, psName);
370-
intent.putExtra(EXTRA_POSTPROCESSING_ARGS, psArgs);
371-
intent.putExtra(EXTRA_NEAR_LENGTH, nearLength);
372-
intent.putExtra(EXTRA_RECOVERY_INFO, recoveryInfo);
373-
374-
intent.putExtra(EXTRA_PARENT_PATH, storage.getParentUri());
375-
intent.putExtra(EXTRA_PATH, storage.getUri());
376-
intent.putExtra(EXTRA_STORAGE_TAG, storage.getTag());
363+
String[] psArgs, long nearLength,
364+
ArrayList<MissionRecoveryInfo> recoveryInfo) {
365+
final Intent intent = new Intent(context, DownloadManagerService.class)
366+
.setAction(Intent.ACTION_RUN)
367+
.putExtra(EXTRA_URLS, urls)
368+
.putExtra(EXTRA_KIND, kind)
369+
.putExtra(EXTRA_THREADS, threads)
370+
.putExtra(EXTRA_SOURCE, source)
371+
.putExtra(EXTRA_POSTPROCESSING_NAME, psName)
372+
.putExtra(EXTRA_POSTPROCESSING_ARGS, psArgs)
373+
.putExtra(EXTRA_NEAR_LENGTH, nearLength)
374+
.putExtra(EXTRA_RECOVERY_INFO, recoveryInfo)
375+
.putExtra(EXTRA_PARENT_PATH, storage.getParentUri())
376+
.putExtra(EXTRA_PATH, storage.getUri())
377+
.putExtra(EXTRA_STORAGE_TAG, storage.getTag());
377378

378379
context.startService(intent);
379380
}
380381

381382
private void startMission(Intent intent) {
382383
String[] urls = intent.getStringArrayExtra(EXTRA_URLS);
383-
Uri path = intent.getParcelableExtra(EXTRA_PATH);
384-
Uri parentPath = intent.getParcelableExtra(EXTRA_PARENT_PATH);
384+
Uri path = IntentCompat.getParcelableExtra(intent, EXTRA_PATH, Uri.class);
385+
Uri parentPath = IntentCompat.getParcelableExtra(intent, EXTRA_PARENT_PATH, Uri.class);
385386
int threads = intent.getIntExtra(EXTRA_THREADS, 1);
386387
char kind = intent.getCharExtra(EXTRA_KIND, '?');
387388
String psName = intent.getStringExtra(EXTRA_POSTPROCESSING_NAME);
388389
String[] psArgs = intent.getStringArrayExtra(EXTRA_POSTPROCESSING_ARGS);
389390
String source = intent.getStringExtra(EXTRA_SOURCE);
390391
long nearLength = intent.getLongExtra(EXTRA_NEAR_LENGTH, 0);
391392
String tag = intent.getStringExtra(EXTRA_STORAGE_TAG);
392-
Parcelable[] parcelRecovery = intent.getParcelableArrayExtra(EXTRA_RECOVERY_INFO);
393+
final var recovery = IntentCompat.getParcelableArrayListExtra(intent, EXTRA_RECOVERY_INFO,
394+
MissionRecoveryInfo.class);
395+
Objects.requireNonNull(recovery);
393396

394397
StoredFileHelper storage;
395398
try {
@@ -404,15 +407,11 @@ private void startMission(Intent intent) {
404407
else
405408
ps = Postprocessing.getAlgorithm(psName, psArgs);
406409

407-
MissionRecoveryInfo[] recovery = new MissionRecoveryInfo[parcelRecovery.length];
408-
for (int i = 0; i < parcelRecovery.length; i++)
409-
recovery[i] = (MissionRecoveryInfo) parcelRecovery[i];
410-
411410
final DownloadMission mission = new DownloadMission(urls, storage, kind, ps);
412411
mission.threadCount = threads;
413412
mission.source = source;
414413
mission.nearLength = nearLength;
415-
mission.recoveryInfo = recovery;
414+
mission.recoveryInfo = recovery.toArray(MissionRecoveryInfo[]::new);
416415

417416
if (ps != null)
418417
ps.setTemporalDir(DownloadManager.pickAvailableTemporalDir(this));

0 commit comments

Comments
 (0)