Commit be9938dc authored by Thomas Markiewicz's avatar Thomas Markiewicz

Fixed crash when sending a message (https://source.puri.sm/Purism/LibremOne/task/issues/185)

parent 545185a3
Pipeline #5073 passed with stage
in 4 minutes and 32 seconds
......@@ -9,6 +9,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.AlertDialog;
......@@ -21,8 +22,10 @@ import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
......@@ -43,6 +46,8 @@ import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.ActivityCompat;
import com.fsck.k9.Account;
import com.fsck.k9.Account.MessageFormat;
......@@ -109,7 +114,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
CancelListener, AttachmentDownloadCancelListener, OnFocusChangeListener,
OnOpenPgpInlineChangeListener, OnOpenPgpSignOnlyChangeListener, MessageBuilder.Callback,
AttachmentPresenter.AttachmentsChangedListener, RecipientPresenter.RecipientsChangedListener,
OnOpenPgpDisableListener {
OnOpenPgpDisableListener, ActivityCompat.OnRequestPermissionsResultCallback {
private static final int DIALOG_SAVE_OR_DISCARD_DRAFT_MESSAGE = 1;
private static final int DIALOG_CONFIRM_DISCARD_ON_BACK = 2;
......@@ -155,6 +160,9 @@ public class MessageCompose extends K9Activity implements OnClickListener,
private static final int REQUEST_MASK_ATTACHMENT_PRESENTER = (1 << 10);
private static final int REQUEST_MASK_MESSAGE_BUILDER = (1 << 11);
// Request code for READ_CONTACTS. It can be any number > 0.
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
/**
* Regular expression to remove the first localized "Re:" prefix in subjects.
*
......@@ -452,6 +460,12 @@ public class MessageCompose extends K9Activity implements OnClickListener,
setProgressBarIndeterminateVisibility(true);
currentMessageBuilder.reattachCallback(this);
}
// check for READ_CONTACTS permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point we wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
}
}
@Override
......@@ -1847,4 +1861,16 @@ public class MessageCompose extends K9Activity implements OnClickListener,
return titleResource;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Until you grant the permission, we cannot display your contacts", Toast.LENGTH_LONG).show();
}
}
}
}
......@@ -16,6 +16,10 @@ import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Contacts.Data;
import android.support.annotation.Nullable;
import android.os.Build;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import com.fsck.k9.R;
import com.fsck.k9.mail.Address;
......@@ -457,13 +461,18 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
@Override
protected void onStartLoading() {
if (cachedRecipients != null) {
super.deliverResult(cachedRecipients);
return;
}
if (takeContentChanged() || cachedRecipients == null) {
forceLoad();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this.getContext(), Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
if (cachedRecipients != null) {
super.deliverResult(cachedRecipients);
return;
}
if (takeContentChanged() || cachedRecipients == null) {
forceLoad();
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment