Skip to content

Commit f78983b

Browse files
committed
Show an alert/dialog when no appropriate file-manager was found
1 parent ef91214 commit f78983b

8 files changed

Lines changed: 114 additions & 20 deletions

File tree

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

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

33
import android.app.Activity;
4+
import android.content.ActivityNotFoundException;
45
import android.content.ComponentName;
56
import android.content.Context;
67
import android.content.DialogInterface;
@@ -53,6 +54,7 @@
5354
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
5455
import org.schabi.newpipe.extractor.stream.VideoStream;
5556
import org.schabi.newpipe.settings.NewPipeSettings;
57+
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
5658
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
5759
import org.schabi.newpipe.streams.io.StoredFileHelper;
5860
import org.schabi.newpipe.util.FilePickerActivityHelper;
@@ -687,7 +689,12 @@ private void showFailedDialog(@StringRes final int msg) {
687689
}
688690

689691
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
690-
launcher.launch(StoredDirectoryHelper.getPicker(context));
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+
}
691698
}
692699

693700
private void prepareSelectedDownload() {
@@ -766,8 +773,13 @@ private void prepareSelectedDownload() {
766773
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
767774
}
768775

769-
requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context,
770-
filenameTmp, mimeTmp, initialPath));
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+
}
771783

772784
return;
773785
}

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

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

33
import android.app.Activity
4+
import android.content.ActivityNotFoundException
45
import android.content.BroadcastReceiver
56
import android.content.Context
67
import android.content.DialogInterface
78
import android.content.Intent
89
import android.content.IntentFilter
910
import android.os.Bundle
1011
import android.os.Parcelable
12+
import android.util.Log
1113
import android.view.LayoutInflater
1214
import android.view.Menu
1315
import android.view.MenuInflater
@@ -55,6 +57,7 @@ import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService
5557
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_MODE
5658
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE
5759
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.PREVIOUS_EXPORT_MODE
60+
import org.schabi.newpipe.streams.io.NoFileManagerHelper
5861
import org.schabi.newpipe.streams.io.StoredFileHelper
5962
import org.schabi.newpipe.util.NavigationHelper
6063
import org.schabi.newpipe.util.OnClickGesture
@@ -179,16 +182,26 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
179182
}
180183

181184
private fun onImportPreviousSelected() {
182-
requestImportLauncher.launch(StoredFileHelper.getPicker(activity, JSON_MIME_TYPE))
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+
}
183191
}
184192

185193
private fun onExportSelected() {
186194
val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date())
187195
val exportName = "newpipe_subscriptions_$date.json"
188196

189-
requestExportLauncher.launch(
190-
StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null)
191-
)
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+
}
192205
}
193206

194207
private fun openReorderDialog() {

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

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

33
import android.app.Activity;
4+
import android.content.ActivityNotFoundException;
45
import android.content.Intent;
56
import android.os.Bundle;
67
import android.text.TextUtils;
78
import android.text.util.Linkify;
9+
import android.util.Log;
810
import android.view.LayoutInflater;
911
import android.view.View;
1012
import android.view.ViewGroup;
@@ -30,6 +32,7 @@
3032
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
3133
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
3234
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService;
35+
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
3336
import org.schabi.newpipe.streams.io.StoredFileHelper;
3437
import org.schabi.newpipe.util.Constants;
3538
import org.schabi.newpipe.util.ServiceHelper;
@@ -175,8 +178,14 @@ public void onImportUrl(final String value) {
175178
}
176179

177180
public void onImportFile() {
178-
// leave */* mime type to support all services with different mime types and file extensions
179-
requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity, "*/*"));
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+
}
180189
}
181190

182191
private void requestImportFileResult(final ActivityResult result) {

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

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

33
import android.app.Activity;
4+
import android.content.ActivityNotFoundException;
45
import android.content.Context;
56
import android.content.Intent;
67
import android.content.SharedPreferences;
@@ -25,6 +26,7 @@
2526
import org.schabi.newpipe.extractor.NewPipe;
2627
import org.schabi.newpipe.extractor.localization.ContentCountry;
2728
import org.schabi.newpipe.extractor.localization.Localization;
29+
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
2830
import org.schabi.newpipe.streams.io.StoredFileHelper;
2931
import org.schabi.newpipe.util.NavigationHelper;
3032
import org.schabi.newpipe.util.PicassoHelper;
@@ -73,19 +75,29 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
7375

7476
final Preference importDataPreference = requirePreference(R.string.import_data);
7577
importDataPreference.setOnPreferenceClickListener((Preference p) -> {
76-
requestImportPathLauncher.launch(
77-
StoredFileHelper.getPicker(requireContext(),
78-
ZIP_MIME_TYPE, getImportExportDataUri()));
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+
}
7986
return true;
8087
});
8188

8289
final Preference exportDataPreference = requirePreference(R.string.export_data);
8390
exportDataPreference.setOnPreferenceClickListener((final Preference p) -> {
8491

85-
requestExportPathLauncher.launch(
86-
StoredFileHelper.getNewPicker(requireContext(),
87-
"NewPipeData-" + exportDateFormat.format(new Date()) + ".zip",
88-
ZIP_MIME_TYPE, getImportExportDataUri()));
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+
}
89101
return true;
90102
});
91103

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

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

33
import android.app.Activity;
4+
import android.content.ActivityNotFoundException;
45
import android.content.ContentResolver;
56
import android.content.Context;
67
import android.content.Intent;
@@ -21,6 +22,7 @@
2122
import com.nononsenseapps.filepicker.Utils;
2223

2324
import org.schabi.newpipe.R;
25+
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
2426
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
2527
import org.schabi.newpipe.util.FilePickerActivityHelper;
2628

@@ -214,7 +216,12 @@ public boolean onPreferenceTreeClick(final Preference preference) {
214216
}
215217

216218
private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
217-
launcher.launch(StoredDirectoryHelper.getPicker(ctx));
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+
}
218225
}
219226

220227
private void requestDownloadVideoPathResult(final ActivityResult result) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.schabi.newpipe.streams.io;
2+
3+
import android.content.Context;
4+
5+
import androidx.appcompat.app.AlertDialog;
6+
7+
import org.schabi.newpipe.R;
8+
9+
/**
10+
* Helper for when no file-manager/activity was found.
11+
*/
12+
public final class NoFileManagerHelper {
13+
private NoFileManagerHelper() {
14+
// No impl
15+
}
16+
17+
/**
18+
* Shows an alert dialog when no file-manager is found.
19+
* @param context Context
20+
*/
21+
public static void showActivityNotFoundAlert(final Context context) {
22+
new AlertDialog.Builder(context)
23+
.setTitle(R.string.no_app_to_open_intent)
24+
.setMessage(
25+
context.getString(
26+
R.string.no_appropriate_file_manager_message,
27+
context.getString(R.string.downloads_storage_use_saf_title)))
28+
.setPositiveButton(R.string.ok, null)
29+
.show();
30+
}
31+
}

app/src/main/java/us/shandian/giga/ui/fragment/MissionsFragment.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package us.shandian.giga.ui.fragment;
22

33
import android.app.Activity;
4+
import android.content.ActivityNotFoundException;
45
import android.content.ComponentName;
56
import android.content.Context;
67
import android.content.Intent;
@@ -10,6 +11,7 @@
1011
import android.os.Bundle;
1112
import android.os.Environment;
1213
import android.os.IBinder;
14+
import android.util.Log;
1315
import android.view.LayoutInflater;
1416
import android.view.Menu;
1517
import android.view.MenuItem;
@@ -32,6 +34,7 @@
3234

3335
import org.schabi.newpipe.R;
3436
import org.schabi.newpipe.settings.NewPipeSettings;
37+
import org.schabi.newpipe.streams.io.NoFileManagerHelper;
3538
import org.schabi.newpipe.streams.io.StoredFileHelper;
3639
import org.schabi.newpipe.util.FilePickerActivityHelper;
3740

@@ -46,6 +49,7 @@
4649

4750
public class MissionsFragment extends Fragment {
4851

52+
private static final String TAG = "MissionsFragment";
4953
private static final int SPAN_SIZE = 2;
5054

5155
private SharedPreferences mPrefs;
@@ -257,9 +261,14 @@ private void recoverMission(@NonNull DownloadMission mission) {
257261
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
258262
}
259263

260-
requestDownloadSaveAsLauncher.launch(
261-
StoredFileHelper.getNewPicker(mContext, mission.storage.getName(),
262-
mission.storage.getType(), initialPath));
264+
try {
265+
requestDownloadSaveAsLauncher.launch(
266+
StoredFileHelper.getNewPicker(mContext, mission.storage.getName(),
267+
mission.storage.getType(), initialPath));
268+
} catch (final ActivityNotFoundException aex) {
269+
Log.w(TAG, "Unable to launch file-picker", aex);
270+
NoFileManagerHelper.showActivityNotFoundAlert(getContext());
271+
}
263272
}
264273

265274
@Override

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@
671671
<string name="recent">Recent</string>
672672
<string name="chapters">Chapters</string>
673673
<string name="no_app_to_open_intent">No app on your device can open this</string>
674+
<string name="no_appropriate_file_manager_message">No appropriate file-manager was found for this action.\nPlease install a file-manager or try to enable/disable \'%s\' in the download-settings.</string>
674675
<string name="georestricted_content">This content is not available in your country.</string>
675676
<string name="soundcloud_go_plus_content">This is a SoundCloud Go+ track, at least in your country, so it cannot be streamed or downloaded by NewPipe.</string>
676677
<string name="private_content">This content is private, so it cannot be streamed or downloaded by NewPipe.</string>

0 commit comments

Comments
 (0)