Skip to content

Commit b232385

Browse files
committed
Refactoring + deduplicated code
1 parent 68e7fcf commit b232385

8 files changed

Lines changed: 134 additions & 112 deletions

File tree

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.download;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.ComponentName;
65
import android.content.Context;
76
import android.content.DialogInterface;
@@ -54,7 +53,7 @@
5453
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
5554
import org.schabi.newpipe.extractor.stream.VideoStream;
5655
import org.schabi.newpipe.settings.NewPipeSettings;
57-
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
56+
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
5857
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
5958
import org.schabi.newpipe.streams.io.StoredFileHelper;
6059
import org.schabi.newpipe.util.FilePickerActivityHelper;
@@ -689,12 +688,12 @@ private void showFailedDialog(@StringRes final int msg) {
689688
}
690689

691690
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
692-
try {
693-
launcher.launch(StoredDirectoryHelper.getPicker(context));
694-
} catch (final ActivityNotFoundException aex) {
695-
Log.w(TAG, "Unable to launch directory picker", aex);
696-
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
697-
}
691+
NoFileManagerSafeGuard.launchSafe(
692+
launcher,
693+
StoredDirectoryHelper.getPicker(context),
694+
TAG,
695+
context
696+
);
698697
}
699698

700699
private void prepareSelectedDownload() {
@@ -773,13 +772,12 @@ private void prepareSelectedDownload() {
773772
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
774773
}
775774

776-
try {
777-
requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context,
778-
filenameTmp, mimeTmp, initialPath));
779-
} catch (final ActivityNotFoundException aex) {
780-
Log.w(TAG, "Unable to launch file picker", aex);
781-
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
782-
}
775+
NoFileManagerSafeGuard.launchSafe(
776+
requestDownloadSaveAsLauncher,
777+
StoredFileHelper.getNewPicker(context, filenameTmp, mimeTmp, initialPath),
778+
TAG,
779+
context
780+
);
783781

784782
return;
785783
}

app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package org.schabi.newpipe.local.subscription
22

33
import android.app.Activity
4-
import android.content.ActivityNotFoundException
54
import android.content.BroadcastReceiver
65
import android.content.Context
76
import android.content.DialogInterface
87
import android.content.Intent
98
import android.content.IntentFilter
109
import android.os.Bundle
1110
import android.os.Parcelable
12-
import android.util.Log
1311
import android.view.LayoutInflater
1412
import android.view.Menu
1513
import android.view.MenuInflater
@@ -57,7 +55,7 @@ import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService
5755
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_MODE
5856
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE
5957
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.PREVIOUS_EXPORT_MODE
60-
import org.schabi.newpipe.streams.io.NoFileManagerHelper
58+
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard
6159
import org.schabi.newpipe.streams.io.StoredFileHelper
6260
import org.schabi.newpipe.util.NavigationHelper
6361
import org.schabi.newpipe.util.OnClickGesture
@@ -182,26 +180,24 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
182180
}
183181

184182
private fun onImportPreviousSelected() {
185-
try {
186-
requestImportLauncher.launch(StoredFileHelper.getPicker(activity, JSON_MIME_TYPE))
187-
} catch (aex: ActivityNotFoundException) {
188-
Log.w(TAG, "Unable to launch file picker", aex)
189-
NoFileManagerHelper.showActivityNotFoundAlert(context)
190-
}
183+
NoFileManagerSafeGuard.launchSafe(
184+
requestImportLauncher,
185+
StoredFileHelper.getPicker(activity, JSON_MIME_TYPE),
186+
TAG,
187+
requireContext()
188+
)
191189
}
192190

193191
private fun onExportSelected() {
194192
val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date())
195193
val exportName = "newpipe_subscriptions_$date.json"
196194

197-
try {
198-
requestExportLauncher.launch(
199-
StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null)
200-
)
201-
} catch (aex: ActivityNotFoundException) {
202-
Log.w(TAG, "Unable to launch file picker", aex)
203-
NoFileManagerHelper.showActivityNotFoundAlert(context)
204-
}
195+
NoFileManagerSafeGuard.launchSafe(
196+
requestExportLauncher,
197+
StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null),
198+
TAG,
199+
requireContext()
200+
)
205201
}
206202

207203
private fun openReorderDialog() {

app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionsImportFragment.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.schabi.newpipe.local.subscription;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.Intent;
65
import android.os.Bundle;
76
import android.text.TextUtils;
87
import android.text.util.Linkify;
9-
import android.util.Log;
108
import android.view.LayoutInflater;
119
import android.view.View;
1210
import android.view.ViewGroup;
@@ -32,7 +30,7 @@
3230
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
3331
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
3432
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService;
35-
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
33+
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
3634
import org.schabi.newpipe.streams.io.StoredFileHelper;
3735
import org.schabi.newpipe.util.Constants;
3836
import org.schabi.newpipe.util.ServiceHelper;
@@ -178,14 +176,14 @@ public void onImportUrl(final String value) {
178176
}
179177

180178
public void onImportFile() {
181-
try {
182-
// leave */* mime type to support all services
183-
// with different mime types and file extensions
184-
requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity, "*/*"));
185-
} catch (final ActivityNotFoundException aex) {
186-
Log.w(TAG, "Unable to launch file picker", aex);
187-
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
188-
}
179+
NoFileManagerSafeGuard.launchSafe(
180+
requestImportFileLauncher,
181+
// leave */* mime type to support all services
182+
// with different mime types and file extensions
183+
StoredFileHelper.getPicker(activity, "*/*"),
184+
TAG,
185+
getContext()
186+
);
189187
}
190188

191189
private void requestImportFileResult(final ActivityResult result) {

app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.settings;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.Context;
65
import android.content.Intent;
76
import android.content.SharedPreferences;
@@ -26,7 +25,7 @@
2625
import org.schabi.newpipe.extractor.NewPipe;
2726
import org.schabi.newpipe.extractor.localization.ContentCountry;
2827
import org.schabi.newpipe.extractor.localization.Localization;
29-
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
28+
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
3029
import org.schabi.newpipe.streams.io.StoredFileHelper;
3130
import org.schabi.newpipe.util.NavigationHelper;
3231
import org.schabi.newpipe.util.PicassoHelper;
@@ -75,29 +74,28 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
7574

7675
final Preference importDataPreference = requirePreference(R.string.import_data);
7776
importDataPreference.setOnPreferenceClickListener((Preference p) -> {
78-
try {
79-
requestImportPathLauncher.launch(
80-
StoredFileHelper.getPicker(requireContext(),
81-
ZIP_MIME_TYPE, getImportExportDataUri()));
82-
} catch (final ActivityNotFoundException aex) {
83-
Log.w(TAG, "Unable to launch file picker", aex);
84-
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
85-
}
77+
NoFileManagerSafeGuard.launchSafe(
78+
requestImportPathLauncher,
79+
StoredFileHelper.getPicker(requireContext(),
80+
ZIP_MIME_TYPE, getImportExportDataUri()),
81+
TAG,
82+
getContext()
83+
);
84+
8685
return true;
8786
});
8887

8988
final Preference exportDataPreference = requirePreference(R.string.export_data);
9089
exportDataPreference.setOnPreferenceClickListener((final Preference p) -> {
90+
NoFileManagerSafeGuard.launchSafe(
91+
requestExportPathLauncher,
92+
StoredFileHelper.getNewPicker(requireContext(),
93+
"NewPipeData-" + exportDateFormat.format(new Date()) + ".zip",
94+
ZIP_MIME_TYPE, getImportExportDataUri()),
95+
TAG,
96+
getContext()
97+
);
9198

92-
try {
93-
requestExportPathLauncher.launch(
94-
StoredFileHelper.getNewPicker(requireContext(),
95-
"NewPipeData-" + exportDateFormat.format(new Date()) + ".zip",
96-
ZIP_MIME_TYPE, getImportExportDataUri()));
97-
} catch (final ActivityNotFoundException aex) {
98-
Log.w(TAG, "Unable to launch file picker", aex);
99-
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
100-
}
10199
return true;
102100
});
103101

app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.settings;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.ContentResolver;
65
import android.content.Context;
76
import android.content.Intent;
@@ -22,7 +21,7 @@
2221
import com.nononsenseapps.filepicker.Utils;
2322

2423
import org.schabi.newpipe.R;
25-
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
24+
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
2625
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
2726
import org.schabi.newpipe.util.FilePickerActivityHelper;
2827

@@ -216,12 +215,12 @@ public boolean onPreferenceTreeClick(final Preference preference) {
216215
}
217216

218217
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
219-
try {
220-
launcher.launch(StoredDirectoryHelper.getPicker(ctx));
221-
} catch (final ActivityNotFoundException aex) {
222-
Log.w(TAG, "Unable to launch directory picker", aex);
223-
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
224-
}
218+
NoFileManagerSafeGuard.launchSafe(
219+
launcher,
220+
StoredDirectoryHelper.getPicker(ctx),
221+
TAG,
222+
ctx
223+
);
225224
}
226225

227226
private void requestDownloadVideoPathResult(final ActivityResult result) {

app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerHelper.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.schabi.newpipe.streams.io;
2+
3+
import android.content.ActivityNotFoundException;
4+
import android.content.Context;
5+
import android.util.Log;
6+
7+
import androidx.activity.result.ActivityResultLauncher;
8+
import androidx.annotation.NonNull;
9+
import androidx.appcompat.app.AlertDialog;
10+
11+
import org.schabi.newpipe.R;
12+
13+
/**
14+
* Helper for when no file-manager/activity was found.
15+
*/
16+
public final class NoFileManagerSafeGuard {
17+
private NoFileManagerSafeGuard() {
18+
// No impl
19+
}
20+
21+
/**
22+
* Shows an alert dialog when no file-manager is found.
23+
* @param context Context
24+
*/
25+
private static void showActivityNotFoundAlert(@NonNull final Context context) {
26+
if (context == null) {
27+
throw new IllegalArgumentException(
28+
"Unable to open no file manager alert dialog: Context is null");
29+
}
30+
31+
new AlertDialog.Builder(context)
32+
.setTitle(R.string.no_app_to_open_intent)
33+
.setMessage(
34+
context.getString(
35+
R.string.no_appropriate_file_manager_message,
36+
context.getString(R.string.downloads_storage_use_saf_title)))
37+
.setPositiveButton(R.string.ok, null)
38+
.show();
39+
}
40+
41+
/**
42+
* Launches the file manager safely.
43+
*
44+
* If no file manager is found (which is normally only the case when the user uninstalled
45+
* the default file manager or the OS lacks one) an alert dialog shows up, asking the user
46+
* to fix the situation.
47+
*
48+
* @param activityResultLauncher see {@link ActivityResultLauncher#launch(Object)}
49+
* @param input see {@link ActivityResultLauncher#launch(Object)}
50+
* @param tag Tag used for logging
51+
* @param context Context
52+
* @param <I> see {@link ActivityResultLauncher#launch(Object)}
53+
*/
54+
public static <I> void launchSafe(
55+
final ActivityResultLauncher<I> activityResultLauncher,
56+
final I input,
57+
@NonNull final String tag,
58+
@NonNull final Context context
59+
) {
60+
try {
61+
activityResultLauncher.launch(input);
62+
} catch (final ActivityNotFoundException aex) {
63+
Log.w(tag, "Unable to launch file/directory picker", aex);
64+
NoFileManagerSafeGuard.showActivityNotFoundAlert(context);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)