public void saveToAddressBook(Context c) { List<Address> currentAddresses = getAddressBook(c); if (!isSavedInAddressBook()) { storageIdentifier = getNextStorageIdentifier(currentAddresses); } ArrayList<Address> updatedAddresses = new ArrayList<Address>(); updatedAddresses.add(this); for (Address a : currentAddresses) { if (a.storageIdentifier != storageIdentifier) { updatedAddresses.add(a); } } persistAddressesToDisk(c, updatedAddresses); }
public void deleteFromAddressBook(Context c) { if (!isSavedInAddressBook()) { return; } List<Address> addresses = getAddressBook(c); Iterator<Address> iter = addresses.iterator(); while (iter.hasNext()) { Address o = iter.next(); if (o.storageIdentifier == storageIdentifier) { iter.remove(); break; } } persistAddressesToDisk(c, addresses); }
public static List<Address> getAddressBook(Context c) { ObjectInputStream is = null; try { is = new ObjectInputStream( new BufferedInputStream(c.openFileInput(PERSISTED_ADDRESS_BOOK_FILENAME))); ArrayList<Address> addresses = (ArrayList<Address>) is.readObject(); return addresses; } catch (FileNotFoundException ex) { // create a brand new address book ArrayList<Address> addrs = new ArrayList<Address>(); persistAddressesToDisk(c, addrs); return addrs; } catch (Exception ex) { throw new RuntimeException(ex); } finally { try { is.close(); } catch (Exception ex) { /* ignore */ } } }