11package com .xncoder .advanceprotection ;
22
33import android .content .ContentResolver ;
4+ import android .content .Context ;
45import android .database .Cursor ;
6+ import android .graphics .Color ;
7+ import android .graphics .drawable .ColorDrawable ;
58import android .os .Bundle ;
69import android .provider .ContactsContract ;
7- import android .util . Log ;
10+ import android .view . Gravity ;
811import android .view .LayoutInflater ;
912import android .view .View ;
10- import android .view .ViewGroup ;
11- import android .widget .AdapterView ;
12- import android .widget .ArrayAdapter ;
1313import android .widget .Button ;
1414import android .widget .ImageButton ;
1515import android .widget .ListView ;
2222import androidx .core .view .ViewCompat ;
2323import androidx .core .view .WindowInsetsCompat ;
2424
25+ import com .google .firebase .database .DatabaseReference ;
26+ import com .google .firebase .database .FirebaseDatabase ;
27+
2528import java .util .ArrayList ;
29+ import java .util .Collections ;
30+ import java .util .Comparator ;
31+ import java .util .List ;
2632
2733public class AddContact extends AppCompatActivity {
2834
29- private ArrayList <String > contactsList ;
35+ private List <CustomItems > contactsList ;
36+ private ImageButton selectContact ;
37+ private PopupWindow popupWindow ;
38+ private List <CustomItems > selectedContacts ;
39+ private ArrayList <String > selected ;
40+ private ListView contactListView ;
41+ private SaveContacts saveContacts ;
42+ private Database database ;
43+
3044 @ Override
3145 protected void onCreate (Bundle savedInstanceState ) {
3246 super .onCreate (savedInstanceState );
@@ -38,27 +52,87 @@ protected void onCreate(Bundle savedInstanceState) {
3852 return insets ;
3953 });
4054
41- contactsList = getContacts ();
55+ selectedContacts = new ArrayList <>();
56+ database = new Database (this );
57+ selected = new ArrayList <>();
58+ contactListView = findViewById (R .id .selected_contacts );
59+ saveContacts = new SaveContacts (this );
60+ Cursor cursor = saveContacts .getAllData ();
61+ if (cursor .getCount () != 0 ) {
62+ while (cursor .moveToNext ()) {
63+ selectedContacts .add (new CustomItems (cursor .getString (1 ), cursor .getString (2 ), true ));
64+ selected .add (cursor .getString (2 ));
65+ }
66+ updateContactList ();
67+ }
68+ cursor .close ();
4269
43- ImageButton selectContact = findViewById (R .id .select_contact );
44- selectContact .setOnClickListener (view -> {});
70+ contactsList = getContacts ();
71+ Collections .sort (contactsList , Comparator .comparing (CustomItems ::getName ));
72+ selectContact = findViewById (R .id .select_contact );
73+ selectContact .setOnClickListener (view -> showPopup ());
4574 }
4675
47- private ArrayList < String > getContacts () {
48- ArrayList < String > contactsList = new ArrayList <>();
76+ private List < CustomItems > getContacts () {
77+ List < CustomItems > contactsList = new ArrayList <>();
4978 ContentResolver contentResolver = getContentResolver ();
5079 Cursor cursor = contentResolver .query (ContactsContract .CommonDataKinds .Phone .CONTENT_URI ,
5180 new String []{ContactsContract .CommonDataKinds .Phone .DISPLAY_NAME ,
5281 ContactsContract .CommonDataKinds .Phone .NUMBER },
5382 null , null , null );
5483 if (cursor != null && cursor .moveToFirst ()) {
5584 do {
85+ boolean slct = false ;
5686 String contactName = cursor .getString (cursor .getColumnIndex (ContactsContract .CommonDataKinds .Phone .DISPLAY_NAME ));
5787 String phoneNumber = cursor .getString (cursor .getColumnIndex (ContactsContract .CommonDataKinds .Phone .NUMBER ));
58- contactsList .add (contactName + "\n " + phoneNumber );
88+ if (selected .contains (phoneNumber ))
89+ slct = true ;
90+ contactsList .add (new CustomItems (contactName , phoneNumber , slct ));
5991 } while (cursor .moveToNext ());
6092 cursor .close ();
6193 }
6294 return contactsList ;
6395 }
96+
97+ private void showPopup () {
98+ LayoutInflater inflater = (LayoutInflater ) getSystemService (Context .LAYOUT_INFLATER_SERVICE );
99+ View popupView = inflater .inflate (R .layout .contact_popup , null );
100+
101+ ListView contactList = popupView .findViewById (R .id .list_view );
102+ Button okButton = popupView .findViewById (R .id .button_ok );
103+ Button cancelButton = popupView .findViewById (R .id .button_cancel );
104+
105+ CustomAdapter adapter = new CustomAdapter (this , contactsList , selectedContacts );
106+ contactList .setAdapter (adapter );
107+
108+ popupWindow = new PopupWindow (popupView , (int ) (getResources ().getDisplayMetrics ().widthPixels * 0.9 ),
109+ (int ) (getResources ().getDisplayMetrics ().heightPixels * 0.7 ));
110+
111+ okButton .setOnClickListener (v -> {
112+ selectedContacts .clear ();
113+ saveContacts .deleteAllData ();
114+ database .deleteUserAllData (new SaveCredentials (this ).getAllUsers ().get (0 ).replace ("." , "_" ));
115+ adapter .getContact ().forEach ((s , customItems ) -> {
116+ selectedContacts .add (customItems );
117+ saveContacts .insertData (customItems .getName (), customItems .getNumber ());
118+ database .setData (new SaveCredentials (this ).getAllUsers ().get (0 ).replace ("." , "_" ), customItems .getNumber (), customItems .getName ());
119+ });
120+ updateContactList ();
121+ popupWindow .dismiss ();
122+ });
123+
124+ cancelButton .setOnClickListener (v -> {
125+ popupWindow .dismiss ();
126+ });
127+ popupWindow .setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
128+ popupWindow .showAtLocation (findViewById (android .R .id .content ), Gravity .CENTER , 0 , 0 );
129+ }
130+
131+ private void updateContactList () {
132+ if (selectedContacts != null ) {
133+ CustomContactAdapter adapterContact = new CustomContactAdapter (this , selectedContacts );
134+ contactListView .setAdapter (adapterContact );
135+ }
136+ }
137+
64138}
0 commit comments