Skip to content

Commit

Permalink
1. Entire ReferUs module completed.
Browse files Browse the repository at this point in the history
2. SelectContacts Completed
3. Fetch Contacts via Custom Content Provider Completed.
  • Loading branch information
banditVedant committed Oct 3, 2023
1 parent b6f4741 commit c7cb06e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 22 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
android:enabled="true"
android:exported="true"
android:readPermission="@string/app_name" />
<provider
android:name=".provider.MyContactsProvider"
android:authorities="com.bugbazaar.mycontacts"
android:exported="true" />

<activity
android:name=".ui.DetailedProductActivity"
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/com/BugBazaar/provider/MyContactsProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.BugBazaar.provider;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class MyContactsProvider extends ContentProvider {
// Define your authority and content URI
public static final String AUTHORITY = "com.bugbazaar.mycontacts";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/contacts");

@Override
public boolean onCreate() {
// Initialize your database or data source here
// Return true if initialization is successful
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

// Query the ContactsContract to retrieve all contacts
return getContext().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
);
}

@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// Implement update operation if needed
return 0;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement delete operation if needed
return 0;
}

@Override
public String getType(Uri uri) {
return null;
}
}
88 changes: 66 additions & 22 deletions app/src/main/java/com/BugBazaar/ui/ContactsPack/SelectContacts.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.BugBazaar.ui.ContactsPack;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.BugBazaar.R;
import com.BugBazaar.provider.MyContactsProvider;
import android.provider.ContactsContract;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,13 +27,22 @@ public class SelectContacts extends AppCompatActivity {
private ContactAdapter adapter;
private List<Contacts> 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));
Expand All @@ -44,35 +53,70 @@ 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));

// 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);
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_error_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#667D97"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
</vector>
35 changes: 35 additions & 0 deletions app/src/main/res/layout/activity_select_contacts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@
layout="@layout/nav_toolbar_sub"
android:layout_height="wrap_content"
android:layout_width="match_parent"></include>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:id="@+id/selectContactmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select any contact"
android:layout_gravity="center"
android:textStyle="bold"
android:paddingTop="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="@color/main_theme"
android:textSize="35dp"></TextView>


<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -32,5 +42,30 @@
android:layout_gravity="center"
android:layout_margin="16dp" />
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/imgNoContact"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/baseline_error_24"></ImageView>
<TextView
android:id="@+id/noContactsMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No contacts found in the device."
android:gravity="center"
android:layout_gravity="center"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textSize="25dp"
android:textStyle="bold"
android:visibility="visible"
android:textColor="#667D97"></TextView>
</LinearLayout>

</LinearLayout>

0 comments on commit c7cb06e

Please sign in to comment.