Browse Source

Handle ActivityNotFoundException for OPEN_DOCUMENT_TREE. Fixes austinhuang0131/barinsta#1467

renovate/androidx.fragment-fragment-ktx-1.x
Ammar Githam 4 years ago
parent
commit
606fb986a2
  1. 12
      app/src/main/java/awais/instagrabber/activities/DirectorySelectActivity.java
  2. 38
      app/src/main/java/awais/instagrabber/fragments/settings/DownloadsPreferencesFragment.java
  3. 1
      app/src/main/res/values/strings.xml

12
app/src/main/java/awais/instagrabber/activities/DirectorySelectActivity.java

@ -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);

38
app/src/main/java/awais/instagrabber/fragments/settings/DownloadsPreferencesFragment.java

@ -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);

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

@ -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>
Loading…
Cancel
Save