forked from TeamNewPipe/NewPipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TeamNewPipe#7452 from litetex/show-alert-when-file…
…-manager-not-found Show an alert-dialog when no appropriate file-manager was found
- Loading branch information
Showing
8 changed files
with
141 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/org/schabi/newpipe/streams/io/NoFileManagerSafeGuard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters