Addressed Review feedback

This commit is contained in:
hstrobel 2018-11-27 23:11:58 +01:00
parent 455228f182
commit 72c3ac5ad0
4 changed files with 65 additions and 65 deletions

View file

@ -267,12 +267,12 @@ public class Contacts {
}
}
private Boolean hasPerm() {
return
((ContextCompat.checkSelfPermission(mContext,
Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)
&& ((ContextCompat.checkSelfPermission(mContext,
Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED)));
private boolean hasContactPermission() {
boolean canRead = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
boolean canWrite = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED;
return canRead && canWrite;
}
/**
@ -286,61 +286,16 @@ public class Contacts {
private Cursor getContactByAddress(final String address) {
final Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(address));
if (!hasPerm()) {
// return blank cursor
return new AbstractCursor() {
@Override
public int getCount() {
return 0;
}
@Override
public String[] getColumnNames() {
return new String[0];
}
@Override
public String getString(int column) {
return null;
}
@Override
public short getShort(int column) {
return 0;
}
@Override
public int getInt(int column) {
return 0;
}
@Override
public long getLong(int column) {
return 0;
}
@Override
public float getFloat(int column) {
return 0;
}
@Override
public double getDouble(int column) {
return 0;
}
@Override
public boolean isNull(int column) {
return false;
}
};
} else {
if (hasContactPermission()) {
return mContentResolver.query(
uri,
PROJECTION,
null,
null,
SORT_ORDER);
} else {
// return blank cursor
return new EmptyCursor();
}
}
@ -352,4 +307,54 @@ public class Contacts {
nameCache.clear();
}
/**
* A dummy class that provides a empty cursor
*/
private class EmptyCursor extends AbstractCursor {
@Override
public int getCount() {
return 0;
}
@Override
public String[] getColumnNames() {
return new String[0];
}
@Override
public String getString(int column) {
return null;
}
@Override
public short getShort(int column) {
return 0;
}
@Override
public int getInt(int column) {
return 0;
}
@Override
public long getLong(int column) {
return 0;
}
@Override
public float getFloat(int column) {
return 0;
}
@Override
public double getDouble(int column) {
return 0;
}
@Override
public boolean isNull(int column) {
return false;
}
}
}

View file

@ -40,7 +40,6 @@ dependencies {
implementation "net.jcip:jcip-annotations:1.0"
implementation "com.jakewharton.timber:timber:${versions.timber}"
implementation "org.apache.james:apache-mime4j-core:${versions.mime4j}"
implementation "org.apache.james:apache-mime4j-dom:${versions.mime4j}"
implementation "org.jetbrains.anko:anko-coroutines:0.10.4"
testImplementation project(':mail:testing')
@ -52,6 +51,7 @@ dependencies {
testImplementation "com.nhaarman:mockito-kotlin:${versions.mockitoKotlin}"
testImplementation "org.jdom:jdom2:2.0.6"
testImplementation "org.koin:koin-test:${versions.koin}"
testImplementation "org.apache.james:apache-mime4j-dom:${versions.mime4j}"
}
android {

View file

@ -42,13 +42,9 @@ public abstract class K9Activity extends AppCompatActivity implements K9Activity
case PERMISSIONS_REQUEST_READ_CONTACTS:
case PERMISSIONS_REQUEST_WRITE_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.contact_permission_thanks,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.contact_permission_request,
boolean permissionWasGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (!permissionWasGranted) {
Toast.makeText(this, R.string.contact_permission_request_denied,
Toast.LENGTH_LONG).show();
}
}

View file

@ -1353,6 +1353,5 @@ You can keep this message and use it as a backup for your secret key. If you wan
<!-- permissions -->
<string name="contact_permission_request">K-9 works best when this permission is granted.</string>
<string name="contact_permission_thanks">Your contact data is now available.</string>
<string name="contact_permission_request_denied">K-9 can\'t show you contact suggestions without this permission.</string>
</resources>