· 8 years ago · Mar 17, 2018, 06:30 PM
1ClsUser
2-------------
3/**
4 * Created by ayalfarhat on 3/14/18.
5 */
6
7public class ClsUser {
8
9 String _id,password,role;
10
11 public ClsUser() {
12 }
13
14 public ClsUser(String _id, String password) {
15 this._id = _id;
16 this.password = password;
17 }
18}
19DonateActivity(login)
20------------
21package com.example.ayalfarhat.mytestibmapp2;
22
23import android.annotation.SuppressLint;
24import android.app.ProgressDialog;
25import android.content.Context;
26import android.content.Intent;
27import android.os.AsyncTask;
28import android.os.Bundle;
29import android.support.v7.app.AppCompatActivity;
30import android.util.Log;
31import android.view.View;
32import android.widget.Button;
33import android.widget.EditText;
34import android.widget.Toast;
35
36import com.cloudant.client.api.ClientBuilder;
37import com.cloudant.client.api.CloudantClient;
38import com.cloudant.client.api.Database;
39
40import java.util.List;
41
42
43public class DonateActivity extends AppCompatActivity {
44
45 //calling user table with correct info
46 final String TEXT_API_KEY = "illednensadstublantoompa";
47
48 final String TEXT_API_SECRET = "be61a9e14653f5deb88affac3dc255094e5a90ae";
49
50 final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
51
52 Button btn_login, btnRegDon;
53 final String DB_NAME_TEXT = "users";
54
55 EditText input_email, input_password;
56 Context context;
57
58 @Override
59 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61 setContentView(R.layout.activity_donate);
62
63 setPointer();
64 }
65
66 private void setPointer() {
67
68 this.context = this;
69
70 input_email = findViewById(R.id.input_email);
71 input_password = findViewById(R.id.input_password);
72
73 btn_login = findViewById(R.id.btn_login);
74 btn_login.setOnClickListener(new View.OnClickListener() {
75 @Override
76 public void onClick(View v) {
77 String uName = input_email.getText().toString();
78 String uPass = input_password.getText().toString();
79
80 Log.e("onClick Login", "Check user");
81 checkUser(uName, uPass);
82 }
83 });
84
85 btnRegDon = findViewById(R.id.btnRegDon);
86 btnRegDon.setOnClickListener(new View.OnClickListener() {
87 @Override
88 public void onClick(View v) {
89 openRegDonateActivity();
90 }
91 });
92
93 }
94
95 @SuppressLint("StaticFieldLeak")
96 private void checkUser(final String uName, final String uPass) {
97 final ProgressDialog pd = new ProgressDialog(context);
98 pd.setTitle("מ×מת × ×ª×•× ×™×....");
99 pd.show();
100
101 Log.e("going to async", "now");
102
103 new AsyncTask<Void, Void, Boolean>() {
104 @Override
105 protected Boolean doInBackground(Void... voids) {
106
107 CloudantClient client = ClientBuilder.account(DB_USER_NAME)
108 .username(TEXT_API_KEY)
109 .password(TEXT_API_SECRET)
110 .build();
111
112 Database db = client.database(DB_NAME_TEXT, false);
113
114 Log.e("in asynctask", "creating json string");
115
116 String myJson = "{\n" +
117 " \"selector\": {\n" +
118 " \"_id\": \"" + uName + "\",\n" +
119 " \"password\": \"" + uPass + "\"\n" +
120 " }\n" +
121 "}";
122
123 List<UserCls> test = db.findByIndex(myJson, UserCls.class);
124
125 Log.e("data sent", "now we wait");
126
127 //we check the username and userpass exist and match
128 if (test.isEmpty()) {
129 return false;
130 }
131
132 /*
133 //we start a new activity which depends on the "role" field
134 String role = test.get(0).role;
135 Log.e("not empty", "starting new activity, role is " + role);
136
137 if (role.equals("donor")) {
138 Log.e("start the role", "role is " + role);
139 Intent myIntent = new Intent(context, DonateActivity.class);
140 startActivity(myIntent);
141 } else if (role.equals("runner")) {
142 //todo: create runner activity somewhere and start it here
143 // in the meantime we just use toast
144
145 Log.e("connection", "connect runner, role is " + role);
146 } else if (role.equals("clerk")) {
147
148 Log.e("connection", "connect clerk, role is " + role);
149 }
150
151 */
152 return true;
153
154 }
155
156 @Override
157 protected void onPostExecute(Boolean aBoolean) {
158 pd.dismiss();
159 if (!aBoolean) //if the doInBackground returned false, that is if the List was empty (because there is no username with that password)
160 {
161 Log.e("test is empty", "no match");
162 Toast.makeText(context, "Username or password incorrect", Toast.LENGTH_SHORT).show();
163 }
164 Intent intent = new Intent(context,DonorActivity1.class);
165 startActivity(intent);
166 }
167
168 //return null;
169 }.execute();
170 }
171
172
173 private void openRegDonateActivity() {
174
175 Intent intentAbout = new Intent(this, RegDonate.class);
176 startActivity(intentAbout);
177 }
178}
179DonorCls
180-----------
181/**
182 * Created by ayalfarhat on 3/14/18.
183 */
184
185public class DonorCls {
186 String _id, password, role;
187 String uAddress, uMail, uMobile;
188
189 public DonorCls(String _id, String password, String uAddress, String uMail, String uMobile) {
190 this._id = _id;
191 this.password = password;
192 this.uAddress = uAddress;
193 this.uMail = uMail;
194 this.uMobile = uMobile;
195 this.role = "donor";
196 }
197}
198RegDonate (Regster)
199------------
200package com.example.ayalfarhat.mytestibmapp2;
201
202
203import android.annotation.SuppressLint;
204import android.content.Context;
205import android.content.Intent;
206import android.os.AsyncTask;
207import android.os.Bundle;
208import android.support.annotation.Nullable;
209import android.support.v7.app.AppCompatActivity;
210import android.util.Log;
211import android.view.View;
212import android.widget.Button;
213import android.widget.EditText;
214import android.widget.Toast;
215
216import com.cloudant.client.api.ClientBuilder;
217import com.cloudant.client.api.CloudantClient;
218import com.cloudant.client.api.Database;
219
220import java.util.List;
221
222
223public class RegDonate extends AppCompatActivity {
224
225 //final for getting access to users table.
226 final String TEXT_API_KEY = "illednensadstublantoompa";
227 final String TEXT_API_SECRET = "be61a9e14653f5deb88affac3dc255094e5a90ae";
228 final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
229 final String DB_NAME_TEXT = "users";
230
231 EditText userName, input_address, input_email, input_mobile, userPass1, userPass2;
232 Button btnRegister, btnLoginBack;
233 Context context;
234
235 @Override
236 protected void onCreate(Bundle savedInstanceState) {
237 super.onCreate(savedInstanceState);
238 setContentView(R.layout.activity_reg_donate);
239 setPointer();
240 }
241
242 private void setPointer() {
243 this.context = this;
244 userName = findViewById(R.id.userName);
245 input_address = findViewById(R.id.input_address);
246 input_email = findViewById(R.id.input_email);
247 input_mobile = findViewById(R.id.input_mobile);
248 userPass1 = findViewById(R.id.userPass1);
249 userPass2 = findViewById(R.id.userPass2);
250
251 btnLoginBack = findViewById(R.id.btnLoginBack);
252 btnLoginBack.setOnClickListener(new View.OnClickListener() {
253 @Override
254 public void onClick(View v) {
255 goBack();
256
257 }
258 });
259
260 btnRegister = findViewById(R.id.btnRegister);
261 btnRegister.setOnClickListener(new View.OnClickListener() {
262 @Override
263 public void onClick(View v) {
264 checkPass();
265 }
266 });
267 }
268
269 private void checkPass() {
270 //if passwords do not match
271 if (!userPass1.getText().toString().equals(userPass2.getText().toString())) {
272 Toast.makeText(context, "Passwords do not match", Toast.LENGTH_LONG).show();
273 userPass1.setText("");
274 userPass2.setText("");
275 }
276
277 //then we check if username is unique; if it is, this function will register him/her
278 Log.e("onclickRegister", "find if username exists");
279 userNameExists(userName.getText().toString());
280 }
281
282
283 private void goBack() {
284 Intent backIntent = new Intent(this, DonateActivity.class);
285 startActivity(backIntent);
286 }
287
288 //todo if does not work - change void to boolean
289 @SuppressLint("StaticFieldLeak")
290 private boolean userNameExists(final String s) { //s = userName
291
292
293 Log.e("in userNameExists", "check in database");
294
295 new AsyncTask<Void, Void, Boolean>() {
296
297 @Override
298 protected Boolean doInBackground(Void... voids) {
299 CloudantClient client = ClientBuilder.account(DB_USER_NAME)
300 .username(TEXT_API_KEY)
301 .password(TEXT_API_SECRET)
302 .build();
303
304 Database db = client.database(DB_NAME_TEXT, false);
305
306 if (db.contains(s)) { //s = userName
307 return true;
308 }
309
310 return false;
311
312 }
313
314 @Override
315 protected void onPostExecute(Boolean aVoid) { //if true = username exists, toast a message.
316 if (aVoid) {
317 Toast.makeText(context, "Username is taken, try another one", Toast.LENGTH_LONG).show();
318 } else {
319 //register user, and start a new intent
320 Toast.makeText(context, "You are now registered", Toast.LENGTH_LONG).show();
321 registerUser(s, userPass1.getText().toString(), input_address.getText().toString(),
322 input_email.getText().toString(),
323 input_mobile.getText().toString());
324 }
325 }
326 }.execute();
327 return true;
328 }
329
330 @SuppressLint("StaticFieldLeak")
331 private void registerUser(final String uName, final String uPass, final String uAddress, final String uMail, final String uMobile) {
332
333 new AsyncTask<Void, Void, Void>() {
334
335 @Override
336 protected Void doInBackground(Void... voids) {
337 CloudantClient client = ClientBuilder.account(DB_USER_NAME)
338 .username(TEXT_API_KEY)
339 .password(TEXT_API_SECRET)
340 .build();
341
342 //return database.toString();
343 Database db = client.database(DB_NAME_TEXT, false);
344
345 //a java type that can be serialized to json
346 DonorCls newDonor = new DonorCls(uName, uPass, uAddress, uMail, uMobile);
347 db.save(newDonor);
348 Log.e("newDonorRegisterUser", "doInBackGround: new donor was saved in database");
349
350 //todo:check if this is the right place to put that line, if not then in onPostExecute
351 registerDonor(uName, uPass, uAddress, uMail, uMobile);
352
353
354 return null;
355 }
356
357 @Override
358 protected void onPostExecute(Void aVoid) {
359 Toast.makeText(context, "You were registered", Toast.LENGTH_SHORT).show();
360 Intent intent = new Intent(context, DonorActivity1.class);
361 startActivity(intent);
362 }
363 }.execute();
364 }
365
366
367 //now save details in "donor" database
368 @SuppressLint("StaticFieldLeak")
369 private void registerDonor(final String uName, final String uPass, final String uAddress, final String uMail, final String uMobile) {
370 final String TEXT_API_KEY = "heyetonithorefurestureds";
371 final String TEXT_API_SECRET = "a9ae36badadeede43ad0defda75f545801013ab9";
372 final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
373 final String DB_NAME_TEXT = "donor";
374
375 new AsyncTask<Void, Void, Void>() {
376 @Override
377 protected Void doInBackground(Void... voids) {
378
379 CloudantClient client = ClientBuilder.account(DB_USER_NAME)
380 .username(TEXT_API_KEY)
381 .password(TEXT_API_SECRET)
382 .build();
383
384 //return database.toString();
385 Database db = client.database(DB_NAME_TEXT, false);
386
387 // A Java type that can be serialized to JSON
388 DonorCls newDonor = new DonorCls(uName, uPass, uAddress, uMail, uMobile);
389 db.save(newDonor);
390 Log.e("newRegisterDonor", "doInBackground: cloudant data was saves..");
391
392
393 return null;
394 }
395 }.execute();
396 }
397}
398
399
400
401
402
403 /* @Override
404 protected void onCreate(@Nullable Bundle savedInstanceState) {
405 super.onCreate(savedInstanceState);
406 setContentView(R.layout.activity_reg_donate);
407
408 }
409}
410*/
411
412/*
413 EditText userName, userPass1, userPass2,input_address,input_email,input_mobile;
414 Button btnRegister;
415 Context context;
416
417
418
419 @Override
420 protected void onCreate(@Nullable Bundle savedInstanceState) {
421 super.onCreate(savedInstanceState);
422 setContentView(R.layout.activity_reg_donate);
423 setPointer();
424 }
425
426 private void setPointer() {
427 this.context=this;
428
429 userName = findViewById(R.id.userName1);
430 userPass1 = findViewById(R.id.userPass1);
431 userPass2 = findViewById(R.id.userPass2);
432 btnRegister = findViewById(R.id.btnRegister);
433 input_address = findViewById(R.id.input_address);
434 input_email = findViewById(R.id.input_email);
435 input_mobile = findViewById(R.id.input_mobile);
436
437 btnRegister.setOnClickListener(new View.OnClickListener() {
438 @Override
439 public void onClick(View view) {
440 if (!userPass1.getText().toString().equals(userPass2.getText().toString())) {
441 //if passwords dont match
442 Toast.makeText(context, "Passwords don't match", Toast.LENGTH_SHORT).show();
443 userPass1.setText("");
444 userPass2.setText("");
445 }
446 else
447 {
448 //then we check if the username is unique and if it is this function will register him
449 Log.e("onclickregister","going to find username");
450 userNameExists(userName.getText().toString());
451 }
452
453
454
455 }
456 });
457 }
458
459 @SuppressLint("StaticFieldLeak")
460 private boolean userNameExists(final String s) {
461
462 final String TEXT_API_KEY = "heyetonithorefurestureds";
463 final String TEXT_API_SECRET = "a9ae36badadeede43ad0defda75f545801013ab9";
464 final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
465 final String DB_NAME_TEXT = "donor";
466
467 Log.e("in userNAmeexist","going to database");
468
469 new AsyncTask<Void, Void, Boolean>() {
470
471 @Override
472 protected Boolean doInBackground(Void... voids) {
473 CloudantClient client = ClientBuilder.account(DB_USER_NAME)
474 .username(TEXT_API_KEY)
475 .password(TEXT_API_SECRET)
476 .build();
477
478 Database db = client.database(DB_NAME_TEXT, false);
479
480
481 if(db.contains(s))
482 return true;
483
484 return false;
485
486
487
488 }
489
490 @Override
491 protected void onPostExecute(Boolean aVoid) {
492 if (aVoid)
493 {
494 Toast.makeText(context, "Username already taken, try something else", Toast.LENGTH_SHORT).show();
495 }
496 else
497 {
498 //register user and start new intent
499 Toast.makeText(context, "congrats!", Toast.LENGTH_SHORT).show();
500
501 registerUser(s,userPass1.getText().toString(),s,userPass2.getText().toString(),);
502 }
503
504
505 }
506 }.execute();
507
508 return true;
509
510 }
511
512 @SuppressLint("StaticFieldLeak")
513 public void registerUser(final String uName, final String uPass, final String address, final String email, final String phone)
514 {
515 final String TEXT_API_KEY = "heyetonithorefurestureds";
516 final String TEXT_API_SECRET = "a9ae36badadeede43ad0defda75f545801013ab9";
517 final String DB_USER_NAME = "4345d22e-d754-4ad3-8e8a-cf5f0a18158b-bluemix";
518 final String DB_NAME_TEXT = "donor";
519 new AsyncTask<Void, Void, Void>() {
520
521 @Override
522 protected Void doInBackground(Void... voids) {
523 CloudantClient client = ClientBuilder.account(DB_USER_NAME)
524 .username(TEXT_API_KEY)
525 .password(TEXT_API_SECRET)
526 .build();
527
528 //return databases.toString();
529 Database db = client.database(DB_NAME_TEXT, false);
530 // A Java type that can be serialized to JSON
531
532 RegDonate newDonor = new RegDonate(uName,uPass, address, email, phone);
533 db.save(newDonor);
534 Log.e("TAG", "doInBackground: cloudant data was saved.... " );
535 return null;
536 }
537
538 @Override
539 protected void onPostExecute(Void aVoid) {
540 Toast.makeText(context, "You were registered", Toast.LENGTH_SHORT).show();
541 //now starting the new activity
542 Intent myIntent = new Intent(context,DonorActivity1.class);
543 startActivity(myIntent);
544
545 }
546 }.execute();
547
548
549 }
550}
551*/