Fix crash when opening contacts app when none are present.

This can also happen when the system contacts app has been disabled.

Fixes #11794
This commit is contained in:
Rashad Sookram
2021-11-30 09:43:31 -05:00
committed by Greyson Parrelli
parent 562a255478
commit 3242d97c75
2 changed files with 17 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.recipients.ui.bottomsheet;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
@@ -23,6 +24,7 @@ import androidx.lifecycle.ViewModelProviders;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.badges.BadgeImageView;
import org.thoughtcrime.securesms.badges.view.ViewBadgeBottomSheetDialogFragment;
@@ -54,6 +56,8 @@ import kotlin.Unit;
*/
public final class RecipientBottomSheetDialogFragment extends BottomSheetDialogFragment {
public static final String TAG = Log.tag(RecipientBottomSheetDialogFragment.class);
public static final int REQUEST_CODE_SYSTEM_CONTACT_SHEET = 1111;
private static final String ARGS_RECIPIENT_ID = "RECIPIENT_ID";
@@ -247,14 +251,14 @@ public final class RecipientBottomSheetDialogFragment extends BottomSheetDialogF
} else {
addContactButton.setVisibility(View.VISIBLE);
addContactButton.setOnClickListener(v -> {
startActivityForResult(RecipientExporter.export(recipient).asAddContactIntent(), REQUEST_CODE_SYSTEM_CONTACT_SHEET);
openSystemContactSheet(RecipientExporter.export(recipient).asAddContactIntent());
});
}
if (recipient.isSystemContact() && !recipient.isGroup() && !recipient.isSelf()) {
contactDetailsButton.setVisibility(View.VISIBLE);
contactDetailsButton.setOnClickListener(v -> {
startActivityForResult(new Intent(Intent.ACTION_VIEW, recipient.getContactUri()), REQUEST_CODE_SYSTEM_CONTACT_SHEET);
openSystemContactSheet(new Intent(Intent.ACTION_VIEW, recipient.getContactUri()));
});
} else {
contactDetailsButton.setVisibility(View.GONE);
@@ -315,6 +319,15 @@ public final class RecipientBottomSheetDialogFragment extends BottomSheetDialogF
});
}
private void openSystemContactSheet(@NonNull Intent intent) {
try {
startActivityForResult(intent, REQUEST_CODE_SYSTEM_CONTACT_SHEET);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "No activity existed to open the contact.");
Toast.makeText(requireContext(), R.string.RecipientBottomSheet_unable_to_open_contacts, Toast.LENGTH_LONG).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_SYSTEM_CONTACT_SHEET) {