Whitelist update

This commit is contained in:
Alessandro Ferro 2022-10-25 10:12:50 +02:00
parent 945c145133
commit 9706c31afb
2 changed files with 42 additions and 12 deletions

View File

@ -10,6 +10,8 @@ import android.os.Bundle;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
@ -18,10 +20,12 @@ import java.util.ArrayList;
public class WhitelistContactsActivity extends AppCompatActivity { public class WhitelistContactsActivity extends AppCompatActivity {
ListView contactsListView; private ListView contactsListView;
ArrayList<String> contacts; private ArrayList<String> contacts;
ArrayAdapter<String> listviewAdapter; private ArrayAdapter<String> listviewAdapter;
WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(this); private WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(this);
private static final int CONTACT_PICK_CODE = 123;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -29,6 +33,7 @@ public class WhitelistContactsActivity extends AppCompatActivity {
setContentView(R.layout.activity_whitelist_contacts); setContentView(R.layout.activity_whitelist_contacts);
setTitle("Whitelist"); setTitle("Whitelist");
setViews(); setViews();
setListeners();
contacts = whitelistDbHandler.getAllContacts(); contacts = whitelistDbHandler.getAllContacts();
listviewAdapter = new ArrayAdapter<String>(this, listviewAdapter = new ArrayAdapter<String>(this,
@ -41,6 +46,27 @@ public class WhitelistContactsActivity extends AppCompatActivity {
contactsListView = findViewById(R.id.ContactsListView); 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 // Add "add" button on the top
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
@ -54,9 +80,9 @@ public class WhitelistContactsActivity extends AppCompatActivity {
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.add_button) { 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); pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContact, 123); startActivityForResult(pickContact, CONTACT_PICK_CODE);
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@ -70,20 +96,24 @@ public class WhitelistContactsActivity extends AppCompatActivity {
Cursor c = getContentResolver().query(contactData, null, null, null, null); Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) { if (c.moveToFirst()) {
int phoneIndex = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 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); contactSelected(number);
} }
c.close(); c.close();
} }
} }
private void contactSelected(String number){ private void contactSelected(String phoneNo){
if(whitelistDbHandler.isContactPresent(number)){ // 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(); Toast.makeText(this, "Contact already in the list", Toast.LENGTH_SHORT).show();
return; return;
} }
whitelistDbHandler.addContact(number); whitelistDbHandler.addContact(phoneNo);
contacts.add(number); contacts.add(phoneNo);
listviewAdapter.notifyDataSetChanged(); listviewAdapter.notifyDataSetChanged();
} }
} }

View File

@ -49,7 +49,7 @@ public class WhitelistDbHandler extends SQLiteOpenHelper {
// Deleting single contact // Deleting single contact
public void deleteContact(String phoneNo) { public void deleteContact(String phoneNo) {
SQLiteDatabase db = this.getWritableDatabase(); 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(); db.close();
} }