Created
March 10, 2014 17:30
-
-
Save nseidm1/9469800 to your computer and use it in GitHub Desktop.
DatabaseManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.project.vegassms.managers; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import android.app.Application; | |
import android.content.ContentUris; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Handler; | |
import android.os.Looper; | |
import android.provider.ContactsContract; | |
import android.provider.ContactsContract.Contacts; | |
import android.provider.Telephony.Mms; | |
import android.provider.Telephony.MmsSms.PendingMessages; | |
import com.concentriclivers.mms.com.google.android.mms.pdu.NotificationInd; | |
import com.concentriclivers.mms.com.google.android.mms.pdu.PduParser; | |
import com.concentriclivers.mms.com.google.android.mms.pdu.PduPersister; | |
import com.google.common.io.ByteStreams; | |
import com.project.vegassms.Call; | |
import com.project.vegassms.CallDao; | |
import com.project.vegassms.Contact; | |
import com.project.vegassms.ContactDao; | |
import com.project.vegassms.DaoMaster; | |
import com.project.vegassms.DaoSession; | |
import com.project.vegassms.Email; | |
import com.project.vegassms.EmailDao; | |
import com.project.vegassms.Message; | |
import com.project.vegassms.MessageDao; | |
import com.project.vegassms.Phone; | |
import com.project.vegassms.PhoneDao; | |
import com.project.vegassms.Push; | |
import com.project.vegassms.PushDao; | |
import com.project.vegassms.R; | |
import com.project.vegassms.helpers.MessageBus; | |
import com.project.vegassms.helpers.MessageBus.Command; | |
import ezvcard.VCard; | |
import ezvcard.property.Address; | |
import ezvcard.property.Birthday; | |
import ezvcard.property.Note; | |
import ezvcard.property.Telephone; | |
public class DatabaseManager { | |
public static DatabaseManager sInstance = new DatabaseManager(); | |
private static Handler mHandler = new Handler(Looper.getMainLooper()); | |
private Context mContext; | |
private ContactDao mContactDao; | |
private PhoneDao mPhoneDao; | |
private EmailDao mEmailDao; | |
private MessageDao mMessageDao; | |
private CallDao mCallDao; | |
private PushDao mPushDao; | |
private DatabaseManager() { | |
} | |
public void init(Application context) { | |
mContext = context; | |
DaoSession daoSession = new DaoMaster(new DaoMaster.DevOpenHelper(context, "vegas-db", null).getWritableDatabase()).newSession(); | |
mContactDao = daoSession.getContactDao(); | |
mPhoneDao = daoSession.getPhoneDao(); | |
mEmailDao = daoSession.getEmailDao(); | |
mMessageDao = daoSession.getMessageDao(); | |
mCallDao = daoSession.getCallDao(); | |
mPushDao = daoSession.getPushDao(); | |
} | |
public long addContact(Contact contact) { | |
if (mContactDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mContactDao.insert(contact); | |
} | |
public Contact newEmptyContact() { | |
Contact contact = new Contact(); | |
contact.setVibration_setting(1); | |
contact.setSound_setting(1); | |
contact.setSent_reports(1); | |
contact.setSnippet(""); | |
contact.setBirthday(""); | |
contact.setSignature_text(""); | |
contact.setPrefix_text(""); | |
contact.setNote(""); | |
contact.setDraft_message(""); | |
contact.setVibration_setting(1); | |
contact.setSound_setting(1); | |
contact.setNew_calls(0); | |
contact.setNew_messages(0); | |
contact.setBackground(0); | |
contact.setIncoming_background("ffffff"); | |
contact.setOutgoing_background("ffffff"); | |
contact.setIncoming_text_color("ffffff"); | |
contact.setOutgoing_text_color("ffffff"); | |
contact.setFont(0); | |
contact.setFont_size(0); | |
contact.setDelivery_reports(0); | |
contact.setSent_reports(Build.MANUFACTURER.toLowerCase().contains("htc") ? 0 : 1); | |
contact.setSend_confirm(0); | |
contact.setKeyboard_style(0); | |
contact.setAuto_closing_keyboard(0); | |
contact.setNotification_icon(45); | |
contact.setVibration_pattern("100, 500, 100, 500, 100, 500"); | |
contact.setNotification_title(mContext.getString(R.string.spam_filter)); | |
contact.setNotification_body(mContext.getString(R.string.spam_filter_message)); | |
contact.setBlink_rate_on(100); | |
contact.setBlink_rate_off(500); | |
contact.setNotification_sound(""); | |
contact.setDisplay_name(""); | |
contact.setRoot_send_to_voicemail(0); | |
contact.setRoot_call_terminator(0); | |
contact.setCall_terminator(0); | |
contact.setCall_silence(0); | |
return contact; | |
} | |
public Message newEmptyMessage() { | |
Message message = new Message(); | |
message.setCellphone(""); | |
message.setContact_id(0L); | |
message.setDelivery_report_enabled(0); | |
message.setDirection(0); | |
message.setFailure_count(0); | |
message.setMessage(""); | |
message.setPath_to_attachment(""); | |
message.setReceived(0); | |
message.setRegular_sms_uri(""); | |
message.setSent(0); | |
message.setSent_report_enabled(0); | |
message.setTime(0L); | |
message.setType(0); | |
message.setLocked(0); | |
return message; | |
} | |
public Call newEmptyCall() { | |
Call call = new Call(); | |
call.setCellphone(""); | |
call.setContact_id(0L); | |
call.setDirection(0); | |
call.setDuration(0L); | |
call.setTime(0L); | |
return call; | |
} | |
public Phone newEmptyPhone() { | |
Phone phone = new Phone(); | |
phone.setContact_id(0L); | |
phone.setNew_calls(0); | |
phone.setNew_messages(0); | |
phone.setNumber(""); | |
phone.setType(0); | |
return phone; | |
} | |
public Email newEmptyEmail() { | |
Email email = new Email(); | |
email.setContact_id(0L); | |
email.setEmail(""); | |
email.setNew_messages(0); | |
return email; | |
} | |
public Push newEmptyPush() { | |
Push push = new Push(); | |
push.setContact(new byte[0]); | |
push.setPhone(new byte[0]); | |
push.setEmail(new byte[0]); | |
push.setPdu(new byte[0]); | |
push.setFilter_message(false); | |
return push; | |
} | |
public Contact addContact(long contactId) { | |
if (mContactDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
Contact contact = newEmptyContact(); | |
getAddress(contact, contactId); | |
getDisplayName(contact, contactId); | |
long id = mContactDao.insert(contact); | |
contact.setId(id); | |
getEmails(contact, contactId); | |
getPhones(contact, contactId); | |
importContactThumb(contact, contactId); | |
return contact; | |
} | |
public void addnewContactFromVcard(final VCard vcard) { | |
ExecutorManager.sInstance.mExecutor.execute(new Runnable() { | |
@Override | |
public void run() { | |
Contact contactToAdd = null; | |
List<Contact> contacts = getContacts(); | |
for (Contact contact : contacts) { | |
if (contact.getDisplay_name().equals(vcard.getFormattedName().getValue())) | |
contactToAdd = contact; | |
} | |
if (contactToAdd != null) { | |
//What to do if the contact already exists? | |
} else { | |
Contact contact = newEmptyContact(); | |
contact.setDisplay_name(vcard.getFormattedName().getValue()); | |
List<Address> addresses = vcard.getAddresses(); | |
for (Address address : addresses) { | |
contact.setAddress(address.getStreetAddress()); | |
contact.setZip(address.getPostalCode()); | |
} | |
Birthday birthday = vcard.getBirthday(); | |
if (birthday != null) | |
contact.setBirthday(birthday.getText()); | |
List<Note> notes = vcard.getNotes(); | |
for (Note note : notes) { | |
contact.setNote(note.getValue()); | |
} | |
long id = mContactDao.insert(contact); | |
List<Telephone> phones = vcard.getTelephoneNumbers(); | |
for (Telephone telephone : phones) { | |
Phone phone = newEmptyPhone(); | |
phone.setContact_id(id); | |
phone.setNumber(telephone.getText()); | |
phone.setType(7); | |
addPhone(phone); | |
} | |
List<ezvcard.property.Email> emails = vcard.getEmails(); | |
for (ezvcard.property.Email email : emails) { | |
Email newEmail = newEmptyEmail(); | |
newEmail.setContact_id(id); | |
newEmail.setEmail(email.getValue()); | |
addEmail(newEmail); | |
} | |
} | |
} | |
}); | |
} | |
public void addPush(Push push) { | |
if (mPushDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mPushDao.insert(push); | |
} | |
public boolean pushExists(Push test) { | |
if (mPushDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
List<Push> pushes = mPushDao.queryBuilder().list(); | |
NotificationInd nTest = (NotificationInd) new PduParser(test.getPdu()).parse(); | |
for (Push push : pushes) { | |
if (Arrays.equals(nTest.getContentLocation(), ((NotificationInd) new PduParser(push.getPdu()).parse()).getContentLocation())) | |
return true; | |
} | |
return false; | |
} | |
public List<Push> getPushes() { | |
if (mPushDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mPushDao.queryBuilder().list(); | |
} | |
public void removePush(Push push) { | |
if (mPushDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mPushDao.delete(push); | |
} | |
public void removeAllPushes() { | |
if (mPushDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mPushDao.deleteAll(); | |
} | |
public void deletePendingPdus() { | |
Cursor cursor = PduPersister.getPduPersister(mContext).getPendingMessages(System.currentTimeMillis()); | |
int columnIndexOfMsgId = cursor.getColumnIndexOrThrow(PendingMessages.MSG_ID); | |
while(cursor.moveToNext()) { | |
Uri uri = ContentUris.withAppendedId(Mms.CONTENT_URI, cursor.getLong(columnIndexOfMsgId)); | |
mContext.getContentResolver().delete(uri, null, null); | |
} | |
cursor.close(); | |
} | |
private void importContactThumb(Contact contact, long contactId) { | |
try { | |
FileOutputStream fOut = mContext.openFileOutput("thumbnail" + contact.getId(), Context.MODE_PRIVATE); | |
ByteStreams.copy(openDisplayPhoto(contactId), fOut); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
} | |
public InputStream openDisplayPhoto(long contactId) { | |
return Contacts.openContactPhotoInputStream(mContext.getContentResolver(), ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId), true); | |
} | |
private void getPhones(Contact contact, long contactId) { | |
Cursor phoneCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null); | |
while (phoneCursor.moveToNext()) { | |
Phone phone = newEmptyPhone(); | |
phone.setContact_id(contact.getId()); | |
phone.setNumber(phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); | |
phone.setType(phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))); | |
addPhone(phone); | |
} | |
close(phoneCursor); | |
} | |
private void getEmails(Contact contact, long contactId) { | |
Cursor emailCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =?", new String[] { String.valueOf(contactId) }, null); | |
while (emailCursor.moveToNext()) { | |
Email email = newEmptyEmail(); | |
email.setContact_id(contact.getId()); | |
email.setEmail(emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1))); | |
addEmail(email); | |
} | |
close(emailCursor); | |
} | |
private void getAddress(Contact contact, long contactId) { | |
Cursor addressCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + "= ?", new String[] { String.valueOf(contactId) }, null); | |
if (addressCursor.moveToFirst()) { | |
contact.setAddress(addressCursor.getString(addressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET))); | |
contact.setCity(addressCursor.getString(addressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY))); | |
contact.setState(addressCursor.getString(addressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION))); | |
contact.setZip(addressCursor.getString(addressCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE))); | |
} | |
close(addressCursor); | |
} | |
private void getDisplayName(Contact contact, long contactId) { | |
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); | |
Cursor displayNameCursor = mContext.getContentResolver().query(person, null, null, null, null); | |
if (displayNameCursor.moveToFirst()) | |
contact.setDisplay_name(displayNameCursor.getString(displayNameCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); | |
close(displayNameCursor); | |
} | |
private void close(Cursor cursor) { | |
if (cursor != null && !cursor.isClosed()) | |
cursor.close(); | |
} | |
public long addPhone(Phone phone) { | |
if (mPhoneDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mPhoneDao.insert(phone); | |
} | |
public long addEmail(Email email) { | |
if (mEmailDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mEmailDao.insert(email); | |
} | |
public long addMessage(Message message) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
long id = mMessageDao.insert(message); | |
MessageBus.sInstance.sendMessage(new Command(Command.NEW_MESSAGE, message)); | |
return id; | |
} | |
public long addCall(Call call) { | |
if (mCallDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mCallDao.insert(call); | |
} | |
public List<Phone> getPhones() { | |
if (mPhoneDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mPhoneDao.queryBuilder().list(); | |
} | |
public List<Email> getEmails() { | |
if (mEmailDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mEmailDao.queryBuilder().list(); | |
} | |
public List<Phone> getPhones(Contact contact) { | |
if (mPhoneDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mPhoneDao.queryBuilder().where(PhoneDao.Properties.Contact_id.eq(contact.getId())).list(); | |
} | |
public Phone getPhoneForNumber(String number) { | |
List<Phone> phones = getPhones(); | |
for (Phone phone : phones) { | |
if (phone.getNumber().equals(number)) | |
return phone; | |
} | |
return null; | |
} | |
public Email getEmailForAddress(String address) { | |
List<Email> emails = getEmails(); | |
for(Email email : emails) | |
if (email.getEmail().equals(address)) | |
return email; | |
return null; | |
} | |
public Phone getPhone(Contact contact, Phone getPhone) { | |
if (getPhone == null) | |
return null; | |
List<Phone> phones = getPhones(contact); | |
for (Phone phone : phones) | |
if (phone.getId() == getPhone.getId()) | |
return phone; | |
return null; | |
} | |
public Email getEmail(Contact contact, Email getEmail) { | |
if (getEmail == null) | |
return null; | |
List<Email> emails = getEmails(contact); | |
for (Email email : emails) | |
if (email.getId() == getEmail.getId()) | |
return email; | |
return null; | |
} | |
public List<String> getPhoneNumbers(Contact contact) { | |
List<Phone> phones = getPhones(contact); | |
List<String> phoneNumbers = new ArrayList<String>(); | |
for (Phone phone : phones) | |
phoneNumbers.add(phone.getNumber()); | |
return phoneNumbers; | |
} | |
public List<Email> getEmails(Contact contact) { | |
if (mPhoneDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mEmailDao.queryBuilder().where(EmailDao.Properties.Contact_id.eq(contact.getId())).list(); | |
} | |
public List<Contact> getContacts() { | |
if (mContactDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mContactDao.queryBuilder().orderAsc(ContactDao.Properties.Display_name).list(); | |
} | |
public List<Contact> getContactsSorted() { | |
if (mContactDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
updateNewMessages(); | |
return mContactDao.queryBuilder().orderDesc(ContactDao.Properties.New_messages).orderDesc(ContactDao.Properties.New_calls).orderAsc(ContactDao.Properties.Display_name).list(); | |
} | |
private void updateNewMessages() { | |
final List<Contact> contacts = getContacts(); | |
for (Contact contact : contacts) { | |
boolean newMessages = false; | |
boolean newCalls = false; | |
List<Phone> phones = DatabaseManager.sInstance.getPhones(contact); | |
for (Phone phone : phones) { | |
if (phone.getNew_messages() == 1) { | |
newMessages = true; | |
break; | |
} | |
} | |
//If already true no need to check if new messages exist in the email | |
if (!newMessages) { | |
List<Email> emails = DatabaseManager.sInstance.getEmails(contact); | |
for (Email email : emails) { | |
if (email.getNew_messages() == 1) { | |
newMessages = true; | |
break; | |
} | |
} | |
} | |
for (Phone phone : phones) { | |
if(phone.getNew_calls() == 1) { | |
newCalls = true; | |
break; | |
} | |
} | |
//Only hit the database of changes are needed | |
boolean currentNewMessages = contact.getNew_messages() == 1 ? true : false; | |
boolean currentNewCalls = contact.getNew_calls() == 1 ? true : false; | |
if (newMessages != currentNewMessages || newCalls != currentNewCalls) { | |
contact.setNew_messages(newMessages ? 1 : 0); | |
contact.setNew_calls(newCalls ? 1 : 0); | |
updateContact(contact); | |
} | |
} | |
} | |
public List<Message> getMessages(Contact contact) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
List<Message> messages = mMessageDao.queryBuilder().where(MessageDao.Properties.Contact_id.eq(contact.getId())).list(); | |
List<Phone> phones = getPhones(contact); | |
for (Phone phone : phones) | |
messages.addAll(getMessages(phone)); | |
List<Email> emails = getEmails(contact); | |
for (Email email : emails) | |
messages.addAll(getMessages(email)); | |
return messages; | |
} | |
public List<Message> getMessages(Phone phone) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mMessageDao.queryBuilder().where(MessageDao.Properties.Cellphone.eq(phone.getNumber())).list(); | |
} | |
public List<Message> getMessages(Email email) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mMessageDao.queryBuilder().where(MessageDao.Properties.Cellphone.eq(email.getEmail())).list(); | |
} | |
public Message getMessage(Contact contact, String snippet) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
List<Message> messages = mMessageDao.queryBuilder().where(MessageDao.Properties.Message.like(snippet)).where(MessageDao.Properties.Contact_id.eq(contact.getId())).list(); | |
if (messages.size() > 0) | |
return messages.get(0); | |
return null; | |
} | |
public Message getMessage(long messageId) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
List<Message> messages = mMessageDao.queryBuilder().where(MessageDao.Properties.Id.eq(messageId)).list(); | |
if (messages.size() > 0) | |
return messages.get(0); | |
return null; | |
} | |
public List<Call> getCalls(Contact contact) { | |
if (mCallDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
return mCallDao.queryBuilder().where(CallDao.Properties.Contact_id.eq(contact.getId())).orderDesc(CallDao.Properties.Time).list(); | |
} | |
public void updatePhone(Phone phone) { | |
if (mPhoneDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mPhoneDao.update(phone); | |
ContactListDataManager.sInstance.update(); | |
} | |
public void updateContact(Contact contact) { | |
if (contact == null) | |
return; | |
if (mContactDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mContactDao.update(contact); | |
ContactListDataManager.sInstance.update(); | |
} | |
public void updateEmail(Email email) { | |
if (mEmailDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mEmailDao.update(email); | |
ContactListDataManager.sInstance.update(); | |
} | |
public void updateMessage(Message message) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mMessageDao.update(message); | |
MessageBus.sInstance.sendMessage(new Command(Command.UPDATED_MESSAGE, message)); | |
} | |
public void updateCall(Call call) { | |
if (mCallDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mCallDao.update(call); | |
} | |
public void deletePhone(Phone phone) { | |
if (mPhoneDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mPhoneDao.delete(phone); | |
} | |
public void deleteContact(Contact contact) { | |
if (mContactDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
//Delete the messager background if it exists | |
File messagerBackground = new File(mContext.getFilesDir(), "thread_background" + contact.getId() + ".jpg"); | |
if (messagerBackground != null && messagerBackground.exists()) | |
messagerBackground.delete(); | |
mContactDao.delete(contact); | |
List<Message> messages = getMessages(contact); | |
for (Message message : messages) | |
deleteMessage(message); | |
List<Phone> phones = getPhones(contact); | |
for (Phone phone : phones) | |
deletePhone(phone); | |
List<Email> emails = getEmails(contact); | |
for (Email email : emails) | |
deleteEmail(email); | |
List<Call> calls = getCalls(contact); | |
for (Call call : calls) | |
deleteCall(call); | |
} | |
public void deleteEmail(Email email) { | |
if (mEmailDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mEmailDao.delete(email); | |
} | |
public void deleteMessage(Message message) { | |
if (mMessageDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
//Delete the mms files if they exist | |
if (message.getPath_to_attachment() != null) { | |
File file = new File(mContext.getFilesDir(), message.getPath_to_attachment()); | |
if (file != null && file.exists()) | |
file.delete(); | |
} | |
mMessageDao.delete(message); | |
MessageBus.sInstance.sendMessage(new Command(Command.DELETE_MESSAGE, message)); | |
} | |
public static interface DeleteAllMessagesListener { | |
public void onMessagesDeleted(); | |
} | |
public void deleteAllMessages(final DeleteAllMessagesListener listener) { | |
ExecutorManager.sInstance.mExecutor.execute(new Runnable() { | |
@Override | |
public void run() { | |
List<Contact> contacts = getContacts(); | |
for (Contact contact : contacts) { | |
List<Message> messages = getMessages(contact); | |
for (Message message : messages) | |
deleteMessage(message); | |
} | |
if (listener != null) { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
listener.onMessagesDeleted(); | |
} | |
}); | |
} | |
} | |
}); | |
} | |
public void deleteAllMessagesForPhone(Phone phone) { | |
if (phone == null) | |
return; | |
List<Message> messages = DatabaseManager.sInstance.getMessages(phone); | |
for (Message message : messages) { | |
if (message.getLocked() == 0) | |
deleteMessage(message); | |
} | |
} | |
public void deleteAllMessagesForEmail(Email email) { | |
if (email == null) | |
return; | |
List<Message> messages = DatabaseManager.sInstance.getMessages(email); | |
for (Message message : messages) { | |
if (message.getLocked() == 0) | |
deleteMessage(message); | |
} | |
} | |
public void deleteCall(Call call) { | |
if (mCallDao == null) | |
throw new IllegalStateException("Not initialized in the Application"); | |
mCallDao.delete(call); | |
} | |
public static interface DeleteAllCallsListener { | |
public void onAllCallDeleted(); | |
} | |
public void deleteAllCalls(final DeleteAllCallsListener listener) { | |
ExecutorManager.sInstance.mExecutor.execute(new Runnable() { | |
@Override | |
public void run() { | |
List<Contact> contacts = getContacts(); | |
for (Contact contact : contacts) { | |
List<Call> calls = getCalls(contact); | |
for (Call call : calls) | |
deleteCall(call); | |
} | |
if (listener != null) { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
listener.onAllCallDeleted(); | |
} | |
}); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment