diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistContactsActivity.java b/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistContactsActivity.java index f5fd913..86b775f 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistContactsActivity.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistContactsActivity.java @@ -10,6 +10,8 @@ import android.os.Bundle; import android.provider.ContactsContract; import android.view.Menu; import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; @@ -18,10 +20,12 @@ import java.util.ArrayList; public class WhitelistContactsActivity extends AppCompatActivity { - ListView contactsListView; - ArrayList contacts; - ArrayAdapter listviewAdapter; - WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(this); + private ListView contactsListView; + private ArrayList contacts; + private ArrayAdapter listviewAdapter; + private WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(this); + + private static final int CONTACT_PICK_CODE = 123; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,6 +33,7 @@ public class WhitelistContactsActivity extends AppCompatActivity { setContentView(R.layout.activity_whitelist_contacts); setTitle("Whitelist"); setViews(); + setListeners(); contacts = whitelistDbHandler.getAllContacts(); listviewAdapter = new ArrayAdapter(this, @@ -41,6 +46,27 @@ public class WhitelistContactsActivity extends AppCompatActivity { contactsListView = findViewById(R.id.ContactsListView); } + private void setListeners(){ + contactsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int i, long l) { + Toast.makeText(WhitelistContactsActivity.this, "Long click to delete", Toast.LENGTH_SHORT).show(); + } + }); + + contactsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) { + String number = (String)adapterView.getItemAtPosition(i); + contacts.remove(number); + listviewAdapter.notifyDataSetChanged(); + whitelistDbHandler.deleteContact(number); + return true; + } + }); + + } + // Add "add" button on the top @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -54,9 +80,9 @@ public class WhitelistContactsActivity extends AppCompatActivity { int id = item.getItemId(); if (id == R.id.add_button) { - Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); + Intent pickContact = new Intent(Intent.ACTION_PICK); pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); - startActivityForResult(pickContact, 123); + startActivityForResult(pickContact, CONTACT_PICK_CODE); } return super.onOptionsItemSelected(item); } @@ -70,20 +96,24 @@ public class WhitelistContactsActivity extends AppCompatActivity { Cursor c = getContentResolver().query(contactData, null, null, null, null); if (c.moveToFirst()) { int phoneIndex = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); - String number = c.getString(phoneIndex).trim().replace("\\s+", ""); + int contactNameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); + String number = c.getString(phoneIndex); + String contactName = c.getString(contactNameIndex); contactSelected(number); } c.close(); } } - private void contactSelected(String number){ - if(whitelistDbHandler.isContactPresent(number)){ + private void contactSelected(String phoneNo){ + // We'll replace parenthesis, dashes and whitespaces to obtain a valid phone number + phoneNo = phoneNo.replaceAll("[-()\\s]", ""); + if(whitelistDbHandler.isContactPresent(phoneNo)){ Toast.makeText(this, "Contact already in the list", Toast.LENGTH_SHORT).show(); return; } - whitelistDbHandler.addContact(number); - contacts.add(number); + whitelistDbHandler.addContact(phoneNo); + contacts.add(phoneNo); listviewAdapter.notifyDataSetChanged(); } } \ No newline at end of file diff --git a/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java b/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java index cc7e30f..64f7a42 100644 --- a/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java +++ b/src/app/src/main/java/com/xfarrow/locatemydevice/WhitelistDbHandler.java @@ -49,7 +49,7 @@ public class WhitelistDbHandler extends SQLiteOpenHelper { // Deleting single contact public void deleteContact(String phoneNo) { SQLiteDatabase db = this.getWritableDatabase(); - db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { phoneNo }); + db.delete(TABLE_CONTACTS, KEY_PH_NO + " = ?", new String[] { phoneNo }); db.close(); }