1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

Handle ActivityNotFoundException for OPEN_DOCUMENT_TREE. Fixes austinhuang0131/barinsta#1467

This commit is contained in:
Ammar Githam 2021-07-06 07:48:12 +09:00
parent 383485abec
commit 606fb986a2
3 changed files with 38 additions and 13 deletions

View File

@ -1,5 +1,7 @@
package awais.instagrabber.activities;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
@ -66,9 +68,17 @@ public class DirectorySelectActivity extends BaseLanguageActivity {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && initialUri != null) {
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri);
}
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
try {
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "openDirectoryChooser: ", e);
showErrorDialog(getString(R.string.no_directory_picker_activity));
} catch (Exception e) {
Log.e(TAG, "openDirectoryChooser: ", e);
}
}
@SuppressLint("StringFormatInvalid")
@Override
protected void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
super.onActivityResult(requestCode, resultCode, data);

View File

@ -1,5 +1,7 @@
package awais.instagrabber.fragments.settings;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
@ -79,9 +81,17 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && initialUri != null) {
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri);
}
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
try {
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "openDirectoryChooser: ", e);
showErrorDialog(getString(R.string.no_directory_picker_activity));
} catch (Exception e) {
Log.e(TAG, "openDirectoryChooser: ", e);
}
}
@SuppressLint("StringFormatInvalid")
@Override
public void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
if (requestCode != SELECT_DIR_REQUEST_CODE) return;
@ -105,17 +115,9 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
try (final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace(pw);
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
123,
R.string.error,
"com.android.externalstorage.documents".equals(data.getData().getAuthority())
? "Please report this error to the developers:\n\n" + sw.toString()
: getString(R.string.dir_select_no_download_folder, data.getData().getAuthority()),
R.string.ok,
0,
0
);
dialogFragment.show(getChildFragmentManager(), ConfirmDialogFragment.class.getSimpleName());
showErrorDialog("com.android.externalstorage.documents".equals(data.getData().getAuthority())
? "Please report this error to the developers:\n\n" + sw.toString()
: getString(R.string.dir_select_no_download_folder, data.getData().getAuthority()));
} catch (IOException ioException) {
Log.e(TAG, "onActivityResult: ", ioException);
}
@ -123,6 +125,18 @@ public class DownloadsPreferencesFragment extends BasePreferencesFragment {
}, 500);
}
private void showErrorDialog(final String message) {
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
123,
R.string.error,
message,
R.string.ok,
0,
0
);
dialogFragment.show(getChildFragmentManager(), ConfirmDialogFragment.class.getSimpleName());
}
private Preference getPrependUsernameToFilenamePreference(@NonNull final Context context) {
final SwitchPreferenceCompat preference = new SwitchPreferenceCompat(context);
preference.setKey(PreferenceKeys.DOWNLOAD_PREPEND_USER_NAME);

View File

@ -517,4 +517,5 @@
<string name="slide_to_cancel">Slide to Cancel</string>
<string name="disable_screen_transitions">Disable screen transitions</string>
<string name="invalid_format">Invalid format</string>
<string name="no_directory_picker_activity">No activity found to select directory</string>
</resources>