Skip to content

Commit

Permalink
Contact editing link fixed. Some refactoring. Bumping version to 0.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
yeriomin committed Jun 27, 2016
1 parent ce5f6cf commit ef70596
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.github.yeriomin.dumbphoneassistant"
minSdkVersion 4
targetSdkVersion 23
versionCode 3
versionName "0.3"
versionCode 4
versionName "0.4"
}

lintOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public void setId(String id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

protected Contact(String id, String name, String number) {
this.id = id;
this.name = name;
Expand Down Expand Up @@ -54,19 +58,16 @@ private boolean compareStrings(final String one, final String two) {
@Override
public boolean equals(Object o) {
// if not Contact, can't be true
if(!(o instanceof Contact))
if(!(o instanceof Contact)) {
return false;
}
Contact c = (Contact)o;

// only if id's present, compare them
if((id != null) && (id.length()) > 0 && (c.id.length() > 0))
if((id != null) && (id.length()) > 0 && (c.id.length() > 0)) {
return c.id.compareTo(id) == 0;

// if SimNames not equal...
if(!compareStrings(name, c.name)) {
return false;
}

// finally if numbers not equal...
return compareStrings(number, c.number);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private boolean deleteFromSim(Contact contact) {
return result;
}

private boolean copyToSim(Contact contact) throws Exception {
private void copyToSim(Contact contact) throws Exception {

// convert to Contact suitable for storage on SIM
Contact newSimContact = simUtil.convertToSimContact(contact);
Expand All @@ -221,20 +221,17 @@ private boolean copyToSim(Contact contact) throws Exception {
}

// create contact on SIM card
boolean result = simUtil.create(newSimContact);

// output feedback
if (!result) {
try {
simUtil.create(newSimContact);
} catch (Exception e) {
throw new Exception(getString(R.string.error_sim_contact_not_stored));
}

simContacts.add(0, newSimContact);
return result;
}

private boolean copyToPhone(Contact contact) throws Exception {
private void copyToPhone(Contact contact) throws Exception {

boolean result;
Contact newPhoneContact = new Contact("", contact.getName(), contact.getNumber());

// check, if already present on phone
Expand All @@ -244,14 +241,12 @@ private boolean copyToPhone(Contact contact) throws Exception {

// create contact on phone
try {
result = simUtil.create(contact);
phoneUtil.create(contact);
phoneContacts.add(0, contact);
} catch (Exception e) {
// This is an exception from some util class, so it is a string id
throw new Exception(getString(Integer.parseInt(e.getMessage())));
}

return result;
}

private void initProgressDialog(int stringIdTitle, int stringIdMessage, int max) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ArrayList<Contact> get() {
);

// create array of Phone contacts and fill it
final ArrayList<Contact> phoneContacts = new ArrayList<Contact>();
final ArrayList<Contact> phoneContacts = new ArrayList<>();
if (null != results) {
while (results.moveToNext()) {
final Contact phoneContact = new Contact(
Expand All @@ -47,7 +47,7 @@ public ArrayList<Contact> get() {
return phoneContacts;
}

public boolean create(Contact newPhoneContact) throws Exception {
public void create(Contact newPhoneContact) throws Exception {
// first, we have to create the contact
ContentValues newPhoneValues = new ContentValues();
newPhoneValues.put(Contacts.People.NAME, newPhoneContact.getName());
Expand All @@ -70,8 +70,6 @@ public boolean create(Contact newPhoneContact) throws Exception {
// some unknown error has happened
throw new Exception(String.valueOf(R.string.error_phone_number_error));
}

return true;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ArrayList<Contact> get() {
);

// create array of Phone contacts and fill it
final ArrayList<Contact> phoneContacts = new ArrayList<>(results.getCount());
final ArrayList<Contact> phoneContacts = new ArrayList<>();
int indexId = results.getColumnIndex(PhoneLookup._ID);
int indexName = results.getColumnIndex(PhoneLookup.DISPLAY_NAME);
int indexType = results.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
Expand All @@ -62,38 +62,52 @@ public ArrayList<Contact> get() {
return phoneContacts;
}

public boolean create(Contact newPhoneContact) throws Exception {
public void create(Contact contact) throws Exception {
String name = contact.getName();
// Prevents previously placed phone type suffixes from being interpreted as part of the name
if (name.charAt(name.length() - 2) == ',') {
name = name.substring(0, name.length() - 2);
contact.setName(name);
}

ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build()
);
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, newPhoneContact.getName())
.withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getName())
.build()
);
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newPhoneContact.getNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MAIN)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build()
);

ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);

Uri uri = results[0].uri;
// if contacts uri returned, there was an error with adding the number
if (uri.getPath().contains("people")) {
throw new Exception(String.valueOf(R.string.error_phone_number_not_stored));
}
// if phone uri returned, everything went OK
if (!uri.getPath().contains("phones")) {
// some unknown error has happened
ContentProviderResult[] results;
try {
results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
throw new Exception(String.valueOf(R.string.error_phone_number_error));
}
newPhoneContact.setId(uri.getLastPathSegment());
return true;

if (results.length > 2) {
Uri uri = results[2].uri;
// if contacts uri returned, there was an error with adding the number
if (uri.getPath().contains("people")) {
throw new Exception(String.valueOf(R.string.error_phone_number_not_stored));
}
contact.setId(uri.getLastPathSegment());
}
}

public Uri retrieveContactUri(Contact contact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ private int detectMaxContactNameLength() {
// loop from longest to shortest contact name length until a contact is stored successfully
for (currentMax = nameString.length(); (!success && currentMax > 0); currentMax--) {
testContact = new Contact(null, nameString.substring(0, currentMax), "24448888888");
success = create(testContact);
try {
create(testContact);
success = true;
} catch (Exception e) {
// next try
}
}

// if stored successfully, remove contact
Expand Down Expand Up @@ -134,17 +139,18 @@ public ArrayList<Contact> get() {
* @param newSimContact
* The Contact object containing the name and number of the
* contact
* @return Success or failure. ContentResolver doesn't dive any other info
*/
public boolean create(Contact newSimContact) {
public void create(Contact newSimContact) throws Exception {
ContentValues newSimValues = new ContentValues();
newSimValues.put("tag", newSimContact.getName());
newSimValues.put("number", newSimContact.getNumber());
Uri newSimRow = resolver.insert(simUri, newSimValues);

// It is always "content://icc/adn/0" on success and null on failure
// TODO: Isn't there a better API for working with SIM?
return newSimRow != null;
if (newSimRow == null) {
throw new Exception("null Uri returned");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public Util(Activity activity) {
* Creates a contact
*
* @param newContact The Contact object containing the name and number of the contact
* @return Success or not
*/
public abstract boolean create(Contact newContact) throws Exception;
public abstract void create(Contact newContact) throws Exception;

}

0 comments on commit ef70596

Please # to comment.