Skip to content

Commit

Permalink
Changed: Show system chooser if failed to find activity to handle url
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed Sep 5, 2021
1 parent da07826 commit dd952a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.termux.shared.R;
import com.termux.shared.data.DataUtils;
import com.termux.shared.data.IntentUtils;
import com.termux.shared.file.FileUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.models.errors.Error;
Expand All @@ -39,7 +40,11 @@ private static void openSystemAppChooser(final Context context, final Intent int
chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, title);
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooserIntent);
try {
context.startActivity(chooserIntent);
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to open system chooser for:\n" + IntentUtils.getIntentString(chooserIntent), e);
}
}

/**
Expand Down Expand Up @@ -88,12 +93,13 @@ public static void copyTextToClipboard(final Context context, final String text,
*/
public static void openURL(final Context context, final String url) {
if (context == null || url == null || url.isEmpty()) return;
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to open the url \"" + url + "\"", e);
// If no activity found to handle intent, show system chooser
openSystemAppChooser(context, intent, context.getString(R.string.title_open_url_with));
}
}

Expand Down
1 change: 1 addition & 0 deletions termux-shared/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

<!-- ShareUtils -->
<string name="title_share_with">Share With</string>
<string name="title_open_url_with">Open URL With</string>
<string name="msg_storage_permission_not_granted">The storage permission not granted."</string>
<string name="msg_file_saved_successfully">The %1$s file saved successfully at \"%2$s\""</string>

Expand Down

0 comments on commit dd952a9

Please sign in to comment.