Skip to content
Open
Show file tree
Hide file tree
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code you added in this class should be deduplicated with respect to the code in SubscriptionFragment.kt. You could create a few utils functions in a new file SubscriptionsImportExportHelper.kt under app/src/main/java/org/schabi/newpipe/local/subscription/services, and call them both from here and from SubscriptionFragment.kt. Obviously the ActivityResultLauncher cannot be deduplicated, but the rest of the code should be.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.ZipHelper;
import org.schabi.newpipe.local.subscription.services.SubscriptionsExportService;
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService;

import java.io.File;
import java.io.IOException;
Expand All @@ -56,6 +58,13 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
private final ActivityResultLauncher<Intent> requestExportPathLauncher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
this::requestExportPathResult);
private final ActivityResultLauncher<Intent> requestExportSubsLauncher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
this::requestExportSubsResult);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

private final ActivityResultLauncher<Intent> requestImportSubsLauncher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
this::requestImportSubsResult);


@Override
Expand Down Expand Up @@ -123,6 +132,42 @@ ZIP_MIME_TYPE, getImportExportDataUri()),
alertDialog.show();
return true;
});

final Preference exportSubsPreference = findPreference("export_subscriptions");
if (exportSubsPreference != null) {
exportSubsPreference.setOnPreferenceClickListener(preference -> {
NoFileManagerSafeGuard.launchSafe(
requestExportSubsLauncher,
StoredFileHelper.getNewPicker(
requireContext(),
"newpipe_subscriptions_" + exportDateFormat.format(new Date())
+ ".json",
"application/json",
null
),
TAG,
getContext()
);
return true;
});
}

final Preference importSubsPreference = findPreference("import_subscriptions");
if (importSubsPreference != null) {
importSubsPreference.setOnPreferenceClickListener(preference -> {
NoFileManagerSafeGuard.launchSafe(
requestImportSubsLauncher,
StoredFileHelper.getPicker(
requireContext(),
"application/json"
),
TAG,
getContext()
);
return true;
});
}

}

private void requestExportPathResult(final ActivityResult result) {
Expand Down Expand Up @@ -157,6 +202,36 @@ private void requestImportPathResult(final ActivityResult result) {
}
}

private void requestExportSubsResult(final ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
final Uri fileUri = result.getData().getData();
if (fileUri != null) {
final Intent intent = new Intent(
requireContext(), SubscriptionsExportService.class);
intent.putExtra(SubscriptionsExportService.KEY_FILE_PATH, fileUri);
requireContext().startService(intent);
Toast.makeText(requireContext(), R.string.exporting, Toast.LENGTH_SHORT).show();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of showing a Toast I would do the same as in SubscriptionFragment and show an ImportConfirmationDialog. This will come naturally after deduplicating the code.

}
}
}

private void requestImportSubsResult(final ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
final Uri fileUri = result.getData().getData();
if (fileUri != null) {
final Intent intent = new Intent(
requireContext(), SubscriptionsImportService.class);
intent.putExtra(
SubscriptionsImportService.KEY_MODE,
SubscriptionsImportService.PREVIOUS_EXPORT_MODE
);
intent.putExtra(SubscriptionsImportService.KEY_VALUE, fileUri);
requireContext().startService(intent);
Toast.makeText(requireContext(), R.string.importing, Toast.LENGTH_SHORT).show();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Toast is redundant with showToast(R.string.export_ongoing); in SubscriptionsExportService line 112, and should be removed

}
}
}

private void exportDatabase(final StoredFileHelper file, final Uri exportDataUri) {
try {
//checkpoint before export
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,7 @@
<string name="show_more">Show more</string>
<string name="show_less">Show less</string>
<string name="import_settings_vulnerable_format">The settings in the export being imported use a vulnerable format that was deprecated since NewPipe 0.27.0. Make sure the export being imported is from a trusted source, and prefer using only exports obtained from NewPipe 0.27.0 or newer in the future. Support for importing settings in this vulnerable format will soon be removed completely, and then old versions of NewPipe will not be able to import settings of exports from new versions anymore.</string>
<string name="exporting">Exporting subscriptions…</string>
<string name="importing">Importing subscriptions…</string>

</resources>
14 changes: 14 additions & 0 deletions app/src/main/res/xml/backup_restore_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@
android:summary="@string/reset_settings_summary"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />

<Preference
android:key="export_subscriptions"
android:title="Export subscriptions"
android:summary="Export your subscriptions to a .json file"
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new strings under strings.xml for these, otherwise they can't be translated

app:singleLineTitle="false"
app:iconSpaceReserved="false" />

<Preference
android:key="import_subscriptions"
android:title="Import subscriptions"
android:summary="Import subscriptions from a previous .json export"
Comment on lines +35 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

app:singleLineTitle="false"
app:iconSpaceReserved="false" />
</PreferenceScreen>
Loading