diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2363a99..c0f1988 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -50,6 +50,10 @@ android:enabled="true" android:exported="true" android:readPermission="@string/app_name" /> + contactsList; + // Request code for the contact permission + private static final int CONTACTS_PERMISSION_REQUEST = 1; + private TextView noContactsMessage; + private TextView selectContactmsg; + private ImageView imgNoContact; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_contacts); // Initialize the contactsList here - contactsList = new ArrayList<>(); // Initialize a list to store contacts + contactsList = new ArrayList<>(); + + selectContactmsg = findViewById(R.id.selectContactmsg); + noContactsMessage=findViewById(R.id.noContactsMessage); + imgNoContact=findViewById(R.id.imgNoContact); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); @@ -44,19 +53,52 @@ protected void onCreate(Bundle savedInstanceState) { TextView toolbarTitle = findViewById(R.id.toolbarTitle); toolbarTitle.setText("Select Contacts"); - // Check if permission to access contacts is granted or not. If not, request permission. + // Check if permission to access contacts is granted or not. if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 0); + // Permission not granted, request it. + ActivityCompat.requestPermissions( + this, + new String[]{Manifest.permission.READ_CONTACTS}, + CONTACTS_PERMISSION_REQUEST + ); + } else { + // Permission granted, proceed to load data. + loadData(); } + } - // Requesting contact data + // Code to handle back button + public void onBackButtonClick(View view) { + onBackPressed(); // Navigate back to the previous activity + } + + // Handle the permission request result + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == CONTACTS_PERMISSION_REQUEST) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // Permission granted, proceed to load data. + loadData(); + } else { + // Permission denied, show a message or take appropriate action. + Log.d("Contacts", "Permission denied"); + } + } + } + + // Load data when permission is granted + private void loadData() { + // Requesting contact data using your custom content provider ContentResolver contentResolver = getContentResolver(); - Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; - Cursor cursor = contentResolver.query(uri, null, null, null, null); - StringBuilder contactText = new StringBuilder(); + Uri customUri = MyContactsProvider.CONTENT_URI; // Use your custom content provider's URI + + Cursor cursor = contentResolver.query(customUri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { + // Data available, proceed to load it. + noContactsMessage.setVisibility(View.GONE); + imgNoContact.setVisibility(View.GONE); do { String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); @@ -64,15 +106,17 @@ protected void onCreate(Bundle savedInstanceState) { // Create a Contact object and add it to the list Contacts contact = new Contacts(contactName, contactNumber); contactsList.add(contact); - contactText.append(contactName).append("=====").append(contactNumber).append("\n"); } while (cursor.moveToNext()); cursor.close(); - } - } - // Code to handle back button - public void onBackButtonClick(View view) { - onBackPressed(); // Navigate back to the previous activity + // Notify the adapter that the dataset has changed + adapter.notifyDataSetChanged(); + } else { + // No contacts found. + selectContactmsg.setVisibility(View.GONE); + noContactsMessage.setVisibility(View.VISIBLE); + imgNoContact.setVisibility(View.VISIBLE); + } } } diff --git a/app/src/main/res/drawable/baseline_error_24.xml b/app/src/main/res/drawable/baseline_error_24.xml new file mode 100644 index 0000000..cc72a67 --- /dev/null +++ b/app/src/main/res/drawable/baseline_error_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_select_contacts.xml b/app/src/main/res/layout/activity_select_contacts.xml index ba99671..9e40476 100644 --- a/app/src/main/res/layout/activity_select_contacts.xml +++ b/app/src/main/res/layout/activity_select_contacts.xml @@ -10,15 +10,25 @@ layout="@layout/nav_toolbar_sub" android:layout_height="wrap_content" android:layout_width="match_parent"> + + + + + + + + \ No newline at end of file