Skip to content

Commit

Permalink
Merge pull request TeamNewPipe#7452 from litetex/show-alert-when-file…
Browse files Browse the repository at this point in the history
…-manager-not-found

Show an alert-dialog when no appropriate file-manager was found
  • Loading branch information
litetex authored Dec 14, 2021
2 parents 4058277 + e806f8c commit b21d231
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 16 deletions.
16 changes: 13 additions & 3 deletions app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.settings.NewPipeSettings;
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper;
Expand Down Expand Up @@ -687,7 +688,12 @@ private void showFailedDialog(@StringRes final int msg) {
}

private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
launcher.launch(StoredDirectoryHelper.getPicker(context));
NoFileManagerSafeGuard.launchSafe(
launcher,
StoredDirectoryHelper.getPicker(context),
TAG,
context
);
}

private void prepareSelectedDownload() {
Expand Down Expand Up @@ -766,8 +772,12 @@ private void prepareSelectedDownload() {
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
}

requestDownloadSaveAsLauncher.launch(StoredFileHelper.getNewPicker(context,
filenameTmp, mimeTmp, initialPath));
NoFileManagerSafeGuard.launchSafe(
requestDownloadSaveAsLauncher,
StoredFileHelper.getNewPicker(context, filenameTmp, mimeTmp, initialPath),
TAG,
context
);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_MODE
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.KEY_VALUE
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.PREVIOUS_EXPORT_MODE
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard
import org.schabi.newpipe.streams.io.StoredFileHelper
import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.OnClickGesture
Expand Down Expand Up @@ -179,15 +180,23 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
}

private fun onImportPreviousSelected() {
requestImportLauncher.launch(StoredFileHelper.getPicker(activity, JSON_MIME_TYPE))
NoFileManagerSafeGuard.launchSafe(
requestImportLauncher,
StoredFileHelper.getPicker(activity, JSON_MIME_TYPE),
TAG,
requireContext()
)
}

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

requestExportLauncher.launch(
StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null)
NoFileManagerSafeGuard.launchSafe(
requestExportLauncher,
StoredFileHelper.getNewPicker(activity, exportName, JSON_MIME_TYPE, null),
TAG,
requireContext()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService;
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ServiceHelper;
Expand Down Expand Up @@ -174,8 +175,14 @@ public void onImportUrl(final String value) {
}

public void onImportFile() {
// leave */* mime type to support all services with different mime types and file extensions
requestImportFileLauncher.launch(StoredFileHelper.getPicker(activity, "*/*"));
NoFileManagerSafeGuard.launchSafe(
requestImportFileLauncher,
// leave */* mime type to support all services
// with different mime types and file extensions
StoredFileHelper.getPicker(activity, "*/*"),
TAG,
getContext()
);
}

private void requestImportFileResult(final ActivityResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PicassoHelper;
Expand Down Expand Up @@ -73,19 +74,28 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro

final Preference importDataPreference = requirePreference(R.string.import_data);
importDataPreference.setOnPreferenceClickListener((Preference p) -> {
requestImportPathLauncher.launch(
NoFileManagerSafeGuard.launchSafe(
requestImportPathLauncher,
StoredFileHelper.getPicker(requireContext(),
ZIP_MIME_TYPE, getImportExportDataUri()));
ZIP_MIME_TYPE, getImportExportDataUri()),
TAG,
getContext()
);

return true;
});

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

requestExportPathLauncher.launch(
NoFileManagerSafeGuard.launchSafe(
requestExportPathLauncher,
StoredFileHelper.getNewPicker(requireContext(),
"NewPipeData-" + exportDateFormat.format(new Date()) + ".zip",
ZIP_MIME_TYPE, getImportExportDataUri()));
ZIP_MIME_TYPE, getImportExportDataUri()),
TAG,
getContext()
);

return true;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.nononsenseapps.filepicker.Utils;

import org.schabi.newpipe.R;
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper;

Expand Down Expand Up @@ -214,7 +215,12 @@ public boolean onPreferenceTreeClick(final Preference preference) {
}

private void launchDirectoryPicker(final ActivityResultLauncher<Intent> launcher) {
launcher.launch(StoredDirectoryHelper.getPicker(ctx));
NoFileManagerSafeGuard.launchSafe(
launcher,
StoredDirectoryHelper.getPicker(ctx),
TAG,
ctx
);
}

private void requestDownloadVideoPathResult(final ActivityResult result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.schabi.newpipe.streams.io;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AlertDialog;

import org.schabi.newpipe.R;

/**
* Helper for when no file-manager/activity was found.
*/
public final class NoFileManagerSafeGuard {
private NoFileManagerSafeGuard() {
// No impl
}

/**
* Shows an alert dialog when no file-manager is found.
* @param context Context
*/
private static void showActivityNotFoundAlert(final Context context) {
if (context == null) {
throw new IllegalArgumentException(
"Unable to open no file manager alert dialog: Context is null");
}

final String message;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Android 10+ only allows SAF
message = context.getString(R.string.no_appropriate_file_manager_message_android_10);
} else {
message = context.getString(
R.string.no_appropriate_file_manager_message,
context.getString(R.string.downloads_storage_use_saf_title));
}


new AlertDialog.Builder(context)
.setTitle(R.string.no_app_to_open_intent)
.setMessage(message)
.setPositiveButton(R.string.ok, null)
.show();
}

/**
* Launches the file manager safely.
*
* If no file manager is found (which is normally only the case when the user uninstalled
* the default file manager or the OS lacks one) an alert dialog shows up, asking the user
* to fix the situation.
*
* @param activityResultLauncher see {@link ActivityResultLauncher#launch(Object)}
* @param input see {@link ActivityResultLauncher#launch(Object)}
* @param tag Tag used for logging
* @param context Context
* @param <I> see {@link ActivityResultLauncher#launch(Object)}
*/
public static <I> void launchSafe(
final ActivityResultLauncher<I> activityResultLauncher,
final I input,
final String tag,
final Context context
) {
try {
activityResultLauncher.launch(input);
} catch (final ActivityNotFoundException aex) {
Log.w(tag, "Unable to launch file/directory picker", aex);
NoFileManagerSafeGuard.showActivityNotFoundAlert(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import org.schabi.newpipe.R;
import org.schabi.newpipe.settings.NewPipeSettings;
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.FilePickerActivityHelper;

Expand All @@ -46,6 +47,7 @@

public class MissionsFragment extends Fragment {

private static final String TAG = "MissionsFragment";
private static final int SPAN_SIZE = 2;

private SharedPreferences mPrefs;
Expand Down Expand Up @@ -257,9 +259,13 @@ private void recoverMission(@NonNull DownloadMission mission) {
initialPath = Uri.parse(initialSavePath.getAbsolutePath());
}

requestDownloadSaveAsLauncher.launch(
NoFileManagerSafeGuard.launchSafe(
requestDownloadSaveAsLauncher,
StoredFileHelper.getNewPicker(mContext, mission.storage.getName(),
mission.storage.getType(), initialPath));
mission.storage.getType(), initialPath),
TAG,
mContext
);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@
<string name="recent">Recent</string>
<string name="chapters">Chapters</string>
<string name="no_app_to_open_intent">No app on your device can open this</string>
<string name="no_appropriate_file_manager_message">No appropriate file manager was found for this action.\nPlease install a file manager or try to disable \'%s\' in the download settings.</string>
<string name="no_appropriate_file_manager_message_android_10">No appropriate file manager was found for this action.\nPlease install a Storage Access Framework compatible file manager.</string>
<string name="georestricted_content">This content is not available in your country.</string>
<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>
<string name="private_content">This content is private, so it cannot be streamed or downloaded by NewPipe.</string>
Expand Down

0 comments on commit b21d231

Please sign in to comment.