Skip to content

Commit 0e6ff0d

Browse files
committed
Commit 3
1 parent 4df17fa commit 0e6ff0d

44 files changed

Lines changed: 3103 additions & 62 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99

1010
defaultConfig {
1111
applicationId = "com.xncoder.advanceprotection"
12-
minSdk = 24
12+
minSdk = 26
1313
targetSdk = 34
1414
versionCode = 1
1515
versionName = "1.0"
@@ -33,12 +33,14 @@ android {
3333
}
3434

3535
dependencies {
36-
3736
implementation(libs.appcompat)
3837
implementation(libs.material)
3938
implementation(libs.activity)
4039
implementation(libs.constraintlayout)
4140
implementation(libs.firebase.auth)
41+
implementation(libs.firebase.database)
42+
implementation("org.tensorflow:tensorflow-lite:2.15.0")
43+
implementation("com.google.mlkit:face-detection:16.1.6")
4244
testImplementation(libs.junit)
4345
androidTestImplementation(libs.ext.junit)
4446
androidTestImplementation(libs.espresso.core)

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
<uses-permission android:name="android.permission.INTERNET" />
66
<uses-permission android:name="android.permission.READ_CONTACTS" />
7+
<uses-permission android:name="android.permission.CAMERA" />
8+
9+
<uses-feature android:name="android.hardware.camera" />
10+
<uses-feature android:name="android.hardware.camera.autofocus" />
711

812
<application
913
android:allowBackup="true"
@@ -16,11 +20,12 @@
1620
android:theme="@style/AppTheme"
1721
tools:targetApi="31">
1822
<activity
19-
android:name=".ContactPopup"
23+
android:name=".AddFace"
2024
android:exported="false" />
2125
<activity
22-
android:name=".contact_popup"
23-
android:exported="false" />
26+
android:name=".FaceDetection.CameraActivity"
27+
android:exported="false"
28+
android:screenOrientation="portrait" />
2429
<activity
2530
android:name=".AddContact"
2631
android:exported="false" />

app/src/main/assets/labelmap.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x
5 MB
Binary file not shown.
Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.xncoder.advanceprotection;
22

33
import android.content.ContentResolver;
4+
import android.content.Context;
45
import android.database.Cursor;
6+
import android.graphics.Color;
7+
import android.graphics.drawable.ColorDrawable;
58
import android.os.Bundle;
69
import android.provider.ContactsContract;
7-
import android.util.Log;
10+
import android.view.Gravity;
811
import android.view.LayoutInflater;
912
import android.view.View;
10-
import android.view.ViewGroup;
11-
import android.widget.AdapterView;
12-
import android.widget.ArrayAdapter;
1313
import android.widget.Button;
1414
import android.widget.ImageButton;
1515
import android.widget.ListView;
@@ -22,11 +22,25 @@
2222
import androidx.core.view.ViewCompat;
2323
import androidx.core.view.WindowInsetsCompat;
2424

25+
import com.google.firebase.database.DatabaseReference;
26+
import com.google.firebase.database.FirebaseDatabase;
27+
2528
import java.util.ArrayList;
29+
import java.util.Collections;
30+
import java.util.Comparator;
31+
import java.util.List;
2632

2733
public 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
}

app/src/main/java/com/xncoder/advanceprotection/ContactPopup.java renamed to app/src/main/java/com/xncoder/advanceprotection/AddFace.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package com.xncoder.advanceprotection;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
5+
import android.widget.ImageButton;
46

57
import androidx.activity.EdgeToEdge;
68
import androidx.appcompat.app.AppCompatActivity;
79
import androidx.core.graphics.Insets;
810
import androidx.core.view.ViewCompat;
911
import androidx.core.view.WindowInsetsCompat;
1012

11-
public class ContactPopup extends AppCompatActivity {
13+
import com.xncoder.advanceprotection.FaceDetection.CameraActivity;
14+
15+
public class AddFace extends AppCompatActivity {
1216

1317
@Override
1418
protected void onCreate(Bundle savedInstanceState) {
1519
super.onCreate(savedInstanceState);
1620
EdgeToEdge.enable(this);
17-
setContentView(R.layout.activity_contact_popup);
21+
setContentView(R.layout.activity_add_face);
1822
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
1923
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
2024
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
2125
return insets;
2226
});
27+
28+
Intent cameraIntent = new Intent(this, CameraActivity.class);
29+
ImageButton addFace = findViewById(R.id.add_face_btn);
30+
addFace.setOnClickListener(view -> startActivity(cameraIntent));
2331
}
2432
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.xncoder.advanceprotection;
2+
3+
import android.content.Context;
4+
import android.graphics.drawable.Drawable;
5+
import android.media.Image;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.WindowManager;
10+
import android.widget.BaseAdapter;
11+
import android.widget.ImageView;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
14+
15+
import java.util.ArrayList;
16+
import java.util.Collections;
17+
import java.util.Comparator;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.function.Consumer;
21+
22+
public class CustomAdapter extends BaseAdapter {
23+
private Context context;
24+
private List<CustomItems> contacts;
25+
private HashMap<String, CustomItems> selected;
26+
27+
public CustomAdapter(Context context, List<CustomItems> contacts, List<CustomItems> selectedContacts) {
28+
this.context = context;
29+
this.contacts = contacts;
30+
this.selected = new HashMap<>();
31+
for (CustomItems selectedContact : selectedContacts) {
32+
this.selected.put(selectedContact.getName(), selectedContact);
33+
}
34+
}
35+
36+
@Override
37+
public int getCount() {
38+
return contacts.size();
39+
}
40+
41+
@Override
42+
public Object getItem(int position) {
43+
return contacts.get(position);
44+
}
45+
46+
@Override
47+
public long getItemId(int position) {
48+
return position;
49+
}
50+
51+
@Override
52+
public View getView(int position, View convertView, ViewGroup parent) {
53+
if (convertView == null) {
54+
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
55+
convertView = inflater.inflate(R.layout.custom_list_item, null);
56+
}
57+
58+
TextView nameTextView = convertView.findViewById(R.id.text_view_title);
59+
TextView numberTextView = convertView.findViewById(R.id.text_view_description);
60+
ImageView iconImage = convertView.findViewById(R.id.contact_select);
61+
62+
CustomItems contact = contacts.get(position);
63+
nameTextView.setText(contact.getName());
64+
numberTextView.setText(contact.getNumber());
65+
iconImage.setVisibility(contact.getSelected() ? View.VISIBLE : View.GONE);
66+
67+
convertView.setOnClickListener(view -> {
68+
if (selected.size() < 5 && view.findViewById(R.id.contact_select).getVisibility() == View.GONE) {
69+
contact.setSelected(true);
70+
selected.put(contact.getName(), contact);
71+
view.findViewById(R.id.contact_select).setVisibility(View.VISIBLE);
72+
} else if(view.findViewById(R.id.contact_select).getVisibility() == View.VISIBLE){
73+
contact.setSelected(false);
74+
selected.remove(contact.getName());
75+
view.findViewById(R.id.contact_select).setVisibility(View.GONE);
76+
} else {
77+
Toast.makeText(context, "You can select\nmaximum 5 contacts", Toast.LENGTH_SHORT).show();
78+
}
79+
});
80+
81+
return convertView;
82+
}
83+
84+
public HashMap<String, CustomItems> getContact() {
85+
return selected;
86+
}
87+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.xncoder.advanceprotection;
2+
3+
import android.content.Context;
4+
import android.graphics.drawable.Drawable;
5+
import android.media.Image;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.WindowManager;
10+
import android.widget.BaseAdapter;
11+
import android.widget.ImageButton;
12+
import android.widget.ImageView;
13+
import android.widget.TextView;
14+
import android.widget.Toast;
15+
16+
import java.util.ArrayList;
17+
import java.util.Collections;
18+
import java.util.Comparator;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
22+
public class CustomContactAdapter extends BaseAdapter {
23+
private Context context;
24+
private List<CustomItems> contacts;
25+
private SaveContacts saveContacts;
26+
private Database database;
27+
28+
public CustomContactAdapter(Context context, List<CustomItems> contacts) {
29+
this.context = context;
30+
this.contacts = contacts;
31+
saveContacts = new SaveContacts(context);
32+
database = new Database(context);
33+
}
34+
35+
@Override
36+
public int getCount() {
37+
return contacts.size();
38+
}
39+
40+
@Override
41+
public Object getItem(int position) {
42+
return contacts.get(position);
43+
}
44+
45+
@Override
46+
public long getItemId(int position) {
47+
return position;
48+
}
49+
50+
@Override
51+
public View getView(int position, View convertView, ViewGroup parent) {
52+
if (convertView == null) {
53+
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
54+
convertView = inflater.inflate(R.layout.contact_list_item, null);
55+
}
56+
57+
TextView nameTextView = convertView.findViewById(R.id.list_name);
58+
TextView numberTextView = convertView.findViewById(R.id.list_number);
59+
ImageButton removeItem = convertView.findViewById(R.id.contact_select);
60+
61+
CustomItems contact = contacts.get(position);
62+
nameTextView.setText(contact.getName());
63+
numberTextView.setText(contact.getNumber());
64+
removeItem.setOnClickListener(view -> {
65+
if (saveContacts.deleteData(contacts.get(position).getNumber())) {
66+
database.deleteUserData(new SaveCredentials(context).getAllUsers().get(0).replace(".", "_"), contacts.get(position).getNumber());
67+
Toast.makeText(context, contacts.get(position).getNumber() + "\nremoved", Toast.LENGTH_SHORT).show();
68+
contacts.remove(position);
69+
}
70+
else
71+
Toast.makeText(context, contacts.get(position).getNumber()+"\nfailed", Toast.LENGTH_SHORT).show();
72+
notifyDataSetChanged();
73+
});
74+
75+
return convertView;
76+
}
77+
}

0 commit comments

Comments
 (0)