Advertisement
Guest User

Untitled

a guest
May 31st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.14 KB | None | 0 0
  1. package com.example.avinash.myapplication;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import com.android.volley.AuthFailureError;
  20. import com.android.volley.Request;
  21. import com.android.volley.RequestQueue;
  22. import com.android.volley.Response;
  23. import com.android.volley.VolleyError;
  24. import com.android.volley.VolleyLog;
  25. import com.android.volley.toolbox.JsonObjectRequest;
  26. import com.android.volley.toolbox.StringRequest;
  27. import com.android.volley.toolbox.Volley;
  28. import com.google.android.gms.appindexing.Action;
  29. import com.google.android.gms.appindexing.AppIndex;
  30. import com.google.android.gms.common.api.GoogleApiClient;
  31.  
  32. import org.json.JSONException;
  33. import org.json.JSONObject;
  34.  
  35. import java.util.HashMap;
  36. import java.util.Map;
  37.  
  38.  
  39.  
  40. public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
  41.  
  42.  
  43. //Keys to send username, password, phone and otp
  44. public static final String KEY_SALUTATION = "salutation";
  45. public static final String KEY_FIRSTNAME = "fname";
  46. public static final String KEY_SURNAME = "surname";
  47. public static final String KEY_MOBILE_NO = "mobno";
  48. public static final String KEY_DOB = "dob";
  49. public static final String KEY_NATIONALITY = "nationality";
  50. public static final String KEY_USERNAME = "username";
  51. public static final String KEY_EMAIL = "email";
  52. public static final String KEY_PASSWORD = "password";
  53. public static final String KEY_CONFEDENTIAL = "confedential";
  54. public static final String KEY_DEVICETOKEN = "device_token";
  55. public static final String KEY_LOGINTYPE = "login_type";
  56.  
  57.  
  58. private EditText editTextConfirmOtp;
  59. private Button buttonConfirm;
  60. private RequestQueue requestQueue;
  61. private EditText editTextSalutation;
  62. private EditText editTextUserName;
  63. private EditText editTextFirstname;
  64. private EditText editTextSurname;
  65. private EditText editTextEmail;
  66. private EditText editTextMob_no;
  67. private EditText editTextDob;
  68. private EditText editTextNationality;
  69. private EditText editTextPassword;
  70.  
  71. private String salutation,fname,surname,mobno,dob,nationality,username,email,password;
  72.  
  73. private String confedential = "private";
  74. private String device_token="aswert1234";
  75. private String login_type="0";
  76.  
  77.  
  78.  
  79. private static String TAG = RegisterActivity.class.getSimpleName();
  80. private Button buttonSubmit;
  81.  
  82. private GoogleApiClient client;
  83.  
  84. @Override
  85. protected void onCreate(Bundle savedInstanceState) {
  86. super.onCreate(savedInstanceState);
  87. setContentView(R.layout.activity_register);
  88.  
  89. TextView registerScreen = (TextView) findViewById(R.id.link_to_login);
  90. // Listening to register new account link
  91. registerScreen.setOnClickListener(new View.OnClickListener() {
  92. public void onClick(View v) {
  93. // Switching to Register screen
  94. Intent i = new Intent(getApplicationContext(), HelpingActivity.class);
  95. startActivity(i);
  96. }
  97. });
  98.  
  99. // ================= Register Vadiation Code Here ============================
  100.  
  101. editTextSalutation = (EditText) findViewById(R.id.editText_fname);
  102. editTextFirstname = (EditText) findViewById(R.id.textEmail);
  103. editTextSurname = (EditText) findViewById(R.id.editText_password);
  104. editTextUserName = (EditText) findViewById(R.id.edittxt_cnfrmpassword);
  105. editTextPassword = (EditText) findViewById(R.id.edit_pswd);
  106. editTextDob = (EditText) findViewById(R.id.edit_dob);
  107. editTextNationality = (EditText) findViewById(R.id.edit_natnl);
  108. editTextMob_no = (EditText) findViewById(R.id.edit_mob_no);
  109. editTextEmail = (EditText) findViewById(R.id.edit_email_adrs);
  110.  
  111. buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
  112.  
  113. requestQueue = Volley.newRequestQueue(this);
  114.  
  115. buttonSubmit.setOnClickListener(this);
  116.  
  117. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  118. }
  119.  
  120. public void register() {
  121. Log.e(TAG, "register");
  122.  
  123. if (!validate()) {
  124. Toast.makeText(this,"Valid register here ",Toast.LENGTH_LONG).show();
  125.  
  126. return;
  127. }
  128.  
  129. else{
  130. makeJsonObjectRequest1();
  131.  
  132. }
  133. }
  134.  
  135. public boolean validate() {
  136. boolean invalid = true;
  137.  
  138. salutation = editTextSalutation.getText().toString().trim();
  139. fname = editTextFirstname.getText().toString().trim();
  140. surname = editTextSurname.getText().toString().trim();
  141. username = editTextUserName.getText().toString().trim();
  142. password = editTextPassword.getText().toString().trim();
  143. dob = editTextDob.getText().toString().trim();
  144. nationality = editTextNationality.getText().toString().trim();
  145. mobno = editTextMob_no.getText().toString().trim();
  146. email = editTextEmail.getText().toString().trim();
  147.  
  148. if (salutation.isEmpty()) {
  149. editTextSalutation.setError("enter your salutation");
  150. invalid = false;
  151. } else {
  152. editTextSalutation.setError(null);
  153. }
  154. if (fname.isEmpty()) {
  155. editTextFirstname.setError("enter your first name");
  156. invalid = false;
  157. } else {
  158. editTextFirstname.setError(null);
  159. }
  160.  
  161. if (surname.isEmpty()) {
  162. editTextSurname.setError("enter your surname");
  163. invalid = false;
  164. } else {
  165. editTextSurname.setError(null);
  166. }
  167.  
  168. if (username.isEmpty()) {
  169. editTextUserName.setError("enter your username");
  170. invalid = false;
  171. } else {
  172. editTextUserName.setError(null);
  173. } if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
  174. editTextPassword.setError("between 4 and 10 alphanumeric characters");
  175. invalid = false;
  176. } else {
  177. editTextPassword.setError(null);
  178. } if (dob.isEmpty()) {
  179. editTextDob.setError("enter your Date of Birth");
  180. invalid = false;
  181. } else {
  182. editTextDob.setError(null);
  183. } if (nationality.isEmpty()) {
  184. editTextNationality.setError("enter your Nationality");
  185. invalid = false;
  186. } else {
  187. editTextNationality.setError(null);
  188. } if (mobno.isEmpty() || mobno.length() != 10) {
  189. editTextMob_no.setError("enter a valid 10 digit mobile no");
  190. invalid = false;
  191. } else {
  192. editTextMob_no.setError(null);
  193. } if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  194. editTextEmail.setError("enter a valid email address");
  195. invalid = false;
  196. } else {
  197. editTextEmail.setError(null);
  198. }
  199.  
  200. return invalid;
  201. }
  202.  
  203.  
  204. private void confirmOtp() throws JSONException {
  205. //Creating a LayoutInflater object for the dialog box
  206. LayoutInflater li = LayoutInflater.from(this);
  207. //Creating a view to get the dialog box
  208. View confirmDialog = li.inflate(R.layout.dialog_confirm, null);
  209.  
  210. //Initizliaing confirm button fo dialog box and edittext of dialog box
  211. buttonConfirm = (Button) confirmDialog.findViewById(R.id.buttonConfirm);
  212. editTextConfirmOtp = (EditText) confirmDialog.findViewById(R.id.editTextOtp);
  213.  
  214. //Creating an alertdialog builder
  215. AlertDialog.Builder alert = new AlertDialog.Builder(this);
  216.  
  217. //Adding our dialog box to the view of alert dialog
  218. alert.setView(confirmDialog);
  219.  
  220. //Creating an alert dialog
  221. final AlertDialog alertDialog = alert.create();
  222.  
  223. //Displaying the alert dialog
  224. alertDialog.show();
  225.  
  226. //On the click of the confirm button from alert dialog
  227. buttonConfirm.setOnClickListener(new View.OnClickListener() {
  228. @Override
  229. public void onClick(View v) {
  230. //Hiding the alert dialog
  231. alertDialog.dismiss();
  232.  
  233. //Displaying a progressbar
  234. final ProgressDialog loading = ProgressDialog.show(RegisterActivity.this, "Authenticating", "Please wait while we check the entered code", false, false);
  235.  
  236. //Getting the user entered otp from edittext
  237. final String otp = editTextConfirmOtp.getText().toString().trim();
  238.  
  239. //Creating an string request
  240. StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CONFIRM_URL,
  241. new Response.Listener<String>() {
  242. @Override
  243. public void onResponse(String response) {
  244. //if the server response is success
  245. if (response.equalsIgnoreCase("success")) {
  246. //dismissing the progressbar
  247. loading.dismiss();
  248.  
  249. //Starting a new activity
  250. startActivity(new Intent(RegisterActivity.this, SuccessActivity.class));
  251. } else {
  252. //Displaying a toast if the otp entered is wrong
  253. Toast.makeText(RegisterActivity.this, "Wrong OTP Please Try Again", Toast.LENGTH_LONG).show();
  254. try {
  255. //Asking user to enter otp again
  256. confirmOtp();
  257. } catch (JSONException e) {
  258. e.printStackTrace();
  259. }
  260. }
  261. }
  262. },
  263. new Response.ErrorListener() {
  264. @Override
  265. public void onErrorResponse(VolleyError error) {
  266. alertDialog.dismiss();
  267. Toast.makeText(RegisterActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
  268. }
  269. }) {
  270. @Override
  271. protected Map<String, String> getParams() throws AuthFailureError {
  272. Map<String, String> params = new HashMap<String, String>();
  273. //Adding the parameters otp and username
  274. params.put(Config.KEY_OTP, otp);
  275. // params.put(Config.KEY_USERNAME, username);
  276. return params;
  277. }
  278. };
  279.  
  280. //Adding the request to the queue
  281. requestQueue.add(stringRequest);
  282. }
  283. });
  284. }
  285.  
  286. private void makeJsonObjectRequest1(){
  287.  
  288. isInternetOn();
  289. //Displaying a progress dialog
  290.  
  291. final ProgressDialog loading = ProgressDialog.show(this, "Registering", "Please wait...", false, false);
  292.  
  293.  
  294. JSONObject obj11 = new JSONObject();
  295.  
  296. try {
  297. obj11.put(KEY_SALUTATION, salutation);
  298. Log.e("salutation", salutation);
  299. obj11.put(KEY_FIRSTNAME, fname);
  300. Log.e("fname", fname);
  301. obj11.put(KEY_SURNAME, surname);
  302. Log.e("surname", surname);
  303. obj11.put(KEY_USERNAME, username);
  304. Log.e("username", username);
  305. obj11.put(KEY_MOBILE_NO, mobno);
  306. Log.e("mobno", mobno);
  307. obj11.put(KEY_EMAIL, email);
  308. Log.e("email", email);
  309. obj11.put(KEY_CONFEDENTIAL, confedential);
  310. Log.e("confedential", confedential);
  311. obj11.put(KEY_DEVICETOKEN, device_token);
  312. Log.e("device_token", device_token);
  313. obj11.put(KEY_PASSWORD, password);
  314. Log.e("password", password);
  315. obj11.put(KEY_DOB, dob);
  316. Log.e("dob", dob);
  317. obj11.put(KEY_NATIONALITY, nationality);
  318. Log.e("nationality", nationality);
  319. obj11.put(KEY_LOGINTYPE, login_type);
  320. Log.e("login_type", login_type);
  321. }catch(JSONException e){
  322. e.printStackTrace();
  323. }
  324.  
  325. JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
  326. Config.REGISTER_URL,obj11,
  327. new Response.Listener<JSONObject>() {
  328.  
  329. @Override
  330. public void onResponse(JSONObject response) {
  331. Log.d(TAG, response.toString());
  332.  
  333. loading.dismiss();
  334.  
  335. Toast.makeText(RegisterActivity.this, "Requesting for Otp..", Toast.LENGTH_LONG).show();
  336.  
  337.  
  338. try {
  339.  
  340. if (response.getString(Config.TAG_RESPONSE).equalsIgnoreCase("true")) {
  341. Toast.makeText(RegisterActivity.this, "Requesting for Otp..", Toast.LENGTH_LONG).show();
  342. //Asking user to confirm otp
  343. confirmOtp();
  344. } else {
  345.  
  346. Toast.makeText(RegisterActivity.this, "Username or Phone are already registered", Toast.LENGTH_LONG).show();
  347. }
  348. } catch (JSONException e) {
  349. e.printStackTrace();
  350. }
  351. }
  352. }, new Response.ErrorListener() {
  353.  
  354. @Override
  355. public void onErrorResponse(VolleyError error) {
  356.  
  357. Toast.makeText(RegisterActivity.this, "Volley Error..", Toast.LENGTH_LONG).show();
  358. VolleyLog.d(TAG, "errorRes: " + error.getMessage());
  359.  
  360. }
  361. });
  362. requestQueue.add(jsonObjReq);
  363.  
  364. }
  365.  
  366. @Override
  367. public void onClick(View v) {
  368. register();
  369.  
  370. }
  371.  
  372.  
  373. public final boolean isInternetOn() {
  374.  
  375. // get Connectivity Manager object to check connection
  376. ConnectivityManager connec =
  377. (ConnectivityManager) getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
  378.  
  379. if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
  380. connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
  381. connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
  382. connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
  383.  
  384.  
  385. } else if (
  386. connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||
  387. connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
  388.  
  389. Toast.makeText(this, " Connection Problem ", Toast.LENGTH_LONG).show();
  390. return false;
  391. }
  392. return false;
  393. }
  394.  
  395.  
  396. @Override
  397. public void onStart() {
  398. super.onStart();
  399.  
  400. client.connect();
  401. Action viewAction = Action.newAction(
  402. Action.TYPE_VIEW,
  403. "Register Page",
  404.  
  405. Uri.parse("http://host/path"),
  406.  
  407. Uri.parse("android-app://com.example.avinash.myapplication/http/host/path")
  408. );
  409. AppIndex.AppIndexApi.start(client, viewAction);
  410. }
  411.  
  412. @Override
  413. public void onStop() {
  414. super.onStop();
  415.  
  416. // ATTENTION: This was auto-generated to implement the App Indexing API.
  417. // See https://g.co/AppIndexing/AndroidStudio for more information.
  418. Action viewAction = Action.newAction(
  419. Action.TYPE_VIEW, // TODO: choose an action type.
  420. "Register Page", // TODO: Define a title for the content shown.
  421. // TODO: If you have web page content that matches this app activity's content,
  422. // make sure this auto-generated web page URL is correct.
  423. // Otherwise, set the URL to null.
  424. Uri.parse("http://host/path"),
  425. // TODO: Make sure this auto-generated app URL is correct.
  426. Uri.parse("android-app://com.example.avinash.myapplication/http/host/path")
  427. );
  428. AppIndex.AppIndexApi.end(client, viewAction);
  429. client.disconnect();
  430. }
  431.  
  432. 05-31 14:35:41.083 31467-31467/com.example.avinash.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.avinash.myapplication-1/lib/arm
  433. 05-31 14:35:48.811 31467-31467/com.example.avinash.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.avinash.myapplication-1/lib/arm
  434. 05-31 14:35:49.407 31467-31908/com.example.avinash.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
  435. 05-31 14:35:49.450 31467-31908/com.example.avinash.myapplication I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: (Ifd751822f5)
  436. OpenGL ES Shader Compiler Version: XE031.06.00.05
  437. Build Date: 01/26/16 Tue
  438. Local Branch: AU12_SBA
  439. Remote Branch:
  440. Local Patches:
  441. Reconstruct Branch:
  442. 05-31 14:35:49.451 31467-31908/com.example.avinash.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
  443. 05-31 14:36:12.926 31467-31903/com.example.avinash.myapplication D/Volley: [3407] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://allwashes.com/allwashes.com/heroes/index.php/updatePassword 0x597f401 NORMAL 1> [lifetime=3438], [size=1803], [rc=200], [retryCount=0]
  444. 05-31 14:36:12.937 31467-31467/com.example.avinash.myapplication D/Volley: [1] 7.onErrorResponse: MainActivity
  445. 05-31 14:36:12.947 31467-31467/com.example.avinash.myapplication D/Volley: [1] Request.finish: 3462 ms: [ ] http://allwashes.com/allwashes.com/heroes/index.php/updatePassword 0x597f401 NORMAL 1
  446. 05-31 14:36:13.037 31467-31908/com.example.avinash.myapplication V/RenderScript: 0xb815bf10 Launching thread(s), CPUs 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement