diff --git a/app/build.gradle b/app/build.gradle index 214df78..d82db3f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { diff --git a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Contact.java b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Contact.java index 4751d42..784776f 100644 --- a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Contact.java +++ b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Contact.java @@ -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; @@ -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); } diff --git a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/ManageContactsActivity.java b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/ManageContactsActivity.java index 8ef03e4..9061557 100644 --- a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/ManageContactsActivity.java +++ b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/ManageContactsActivity.java @@ -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); @@ -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 @@ -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) { diff --git a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilDonut.java b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilDonut.java index 82b936b..b7e6b8f 100644 --- a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilDonut.java +++ b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilDonut.java @@ -32,7 +32,7 @@ public ArrayList get() { ); // create array of Phone contacts and fill it - final ArrayList phoneContacts = new ArrayList(); + final ArrayList phoneContacts = new ArrayList<>(); if (null != results) { while (results.moveToNext()) { final Contact phoneContact = new Contact( @@ -47,7 +47,7 @@ public ArrayList 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()); @@ -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; } diff --git a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilEclair.java b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilEclair.java index 7fbd662..72e0481 100644 --- a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilEclair.java +++ b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/PhoneUtilEclair.java @@ -41,7 +41,7 @@ public ArrayList get() { ); // create array of Phone contacts and fill it - final ArrayList phoneContacts = new ArrayList<>(results.getCount()); + final ArrayList phoneContacts = new ArrayList<>(); int indexId = results.getColumnIndex(PhoneLookup._ID); int indexName = results.getColumnIndex(PhoneLookup.DISPLAY_NAME); int indexType = results.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); @@ -62,38 +62,52 @@ public ArrayList 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 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) { diff --git a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/SimUtil.java b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/SimUtil.java index dadd853..8353d4a 100644 --- a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/SimUtil.java +++ b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/SimUtil.java @@ -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 @@ -134,9 +139,8 @@ public ArrayList 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()); @@ -144,7 +148,9 @@ public boolean create(Contact newSimContact) { // 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"); + } } /** diff --git a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Util.java b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Util.java index 795bd25..1882f2e 100644 --- a/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Util.java +++ b/app/src/main/java/com/github/yeriomin/dumbphoneassistant/Util.java @@ -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; } \ No newline at end of file