11package com .xncoder .advanceprotection ;
22
3+ import android .content .Context ;
34import android .content .Intent ;
5+ import android .graphics .Bitmap ;
6+ import android .graphics .Color ;
7+ import android .graphics .drawable .ColorDrawable ;
48import android .os .Bundle ;
9+ import android .view .Gravity ;
10+ import android .view .LayoutInflater ;
11+ import android .view .View ;
12+ import android .widget .Button ;
13+ import android .widget .EditText ;
514import android .widget .ImageButton ;
15+ import android .widget .ImageView ;
16+ import android .widget .ListView ;
17+ import android .widget .PopupWindow ;
18+ import android .widget .Toast ;
619
720import androidx .activity .EdgeToEdge ;
21+ import androidx .annotation .Nullable ;
822import androidx .appcompat .app .AppCompatActivity ;
923import androidx .core .graphics .Insets ;
1024import androidx .core .view .ViewCompat ;
1125import androidx .core .view .WindowInsetsCompat ;
1226
13- import com .xncoder .advanceprotection .FaceDetection .CameraActivity ;
27+ import com .xncoder .advanceprotection .FaceDetection .FaceRecognitionActivity ;
28+ import com .xncoder .advanceprotection .FaceDetection .SaveFaces ;
29+
30+ import java .util .ArrayList ;
31+ import java .util .List ;
1432
1533public class AddFace extends AppCompatActivity {
1634
35+ private final int REQUEST_CODE = 101 ;
36+ private SaveFaces saveFaces ;
37+ private ListView facesListView ;
38+ private List <CustomItems > listFaces ;
39+ private Database database ;
40+ private int faceNo = 1 ;
41+
1742 @ Override
1843 protected void onCreate (Bundle savedInstanceState ) {
1944 super .onCreate (savedInstanceState );
@@ -25,8 +50,78 @@ protected void onCreate(Bundle savedInstanceState) {
2550 return insets ;
2651 });
2752
28- Intent cameraIntent = new Intent (this , CameraActivity .class );
53+ database = new Database (this );
54+ listFaces = new ArrayList <>();
55+ facesListView = findViewById (R .id .selected_faces );
56+ saveFaces = new SaveFaces (this );
57+ saveFaces .getAllData ().forEach ((name , vector ) -> {
58+ listFaces .add (new CustomItems ("Face " + faceNo , name , true ));
59+ faceNo += 1 ;
60+ });
61+ updateFaceList ();
62+
2963 ImageButton addFace = findViewById (R .id .add_face_btn );
30- addFace .setOnClickListener (view -> startActivity (cameraIntent ));
64+ addFace .setOnClickListener (view -> {
65+ if (faceNo != 5 )
66+ startActivityForResult (new Intent (this , FaceRecognitionActivity .class ), REQUEST_CODE );
67+ else
68+ Toast .makeText (this , "You can add maximum 5 faces" , Toast .LENGTH_SHORT ).show ();
69+ });
70+ }
71+
72+ private void showPopup (Bitmap tempBitmap , float [] tempVector ) {
73+
74+ LayoutInflater inflater = (LayoutInflater ) getSystemService (Context .LAYOUT_INFLATER_SERVICE );
75+ View popupView = inflater .inflate (R .layout .image_popup , null );
76+
77+ ImageView face = popupView .findViewById (R .id .dlg_image );
78+ EditText faceN = popupView .findViewById (R .id .face_input );
79+ Button okBtn = popupView .findViewById (R .id .button_ok );
80+ Button cancelBtn = popupView .findViewById (R .id .button_cancel );
81+
82+ face .setImageBitmap (tempBitmap );
83+
84+ PopupWindow popupWindow = new PopupWindow (popupView , (int ) (getResources ().getDisplayMetrics ().widthPixels * 0.7 ),
85+ (int ) (getResources ().getDisplayMetrics ().heightPixels * 0.5 ));
86+
87+ okBtn .setOnClickListener (viewNav -> {
88+ String input = String .valueOf (faceN .getText ());
89+ if (!input .isEmpty () && new SaveFaces (this ).getFaceVector (input ).isEmpty ()) {
90+ database .setFaceData (new SaveCredentials (this ).getAllUsers ().get (0 ).replace ("." , "_" ), input , tempVector );
91+ listFaces .add (new CustomItems ("Face " + faceNo , input , true ));
92+ faceNo += 1 ;
93+ saveFaces .addFace (input , tempVector );
94+ popupWindow .dismiss ();
95+ updateFaceList ();
96+ } else if (!new SaveFaces (this ).getFaceVector (input ).isEmpty ()) {
97+ Toast .makeText (this , "This name already exists" , Toast .LENGTH_SHORT ).show ();
98+ }
99+
100+ });
101+
102+ cancelBtn .setOnClickListener (viewNav -> popupWindow .dismiss ());
103+
104+ popupWindow .setOutsideTouchable (false );
105+ popupWindow .setFocusable (true );
106+ popupWindow .setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
107+ popupWindow .showAtLocation (findViewById (android .R .id .content ), Gravity .CENTER , 0 , 0 );
108+
109+ }
110+
111+ @ Override
112+ protected void onActivityResult (int requestCode , int resultCode , @ Nullable Intent data ) {
113+ super .onActivityResult (requestCode , resultCode , data );
114+ if (requestCode == REQUEST_CODE && resultCode == RESULT_OK ) {
115+ if (data != null ) {
116+ showPopup (data .getParcelableExtra ("Bitmap" ), data .getFloatArrayExtra ("Vector" ));
117+ }
118+ }
119+ }
120+
121+ private void updateFaceList () {
122+ if (listFaces != null ) {
123+ CustomFaceAdapter adapterFace = new CustomFaceAdapter (this , listFaces );
124+ facesListView .setAdapter (adapterFace );
125+ }
31126 }
32127}
0 commit comments