Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. package com.hazelgatesoft.restoloyale.activities;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.util.Log;
  10. import android.view.KeyEvent;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.view.Window;
  14. import android.view.WindowManager;
  15. import android.view.inputmethod.InputMethodManager;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.ImageButton;
  19. import android.widget.Toast;
  20.  
  21. import com.hazelgatesoft.restoloyale.R;
  22. import com.hazelgatesoft.restoloyale.data.Structures;
  23. import com.hazelgatesoft.restoloyale.data.User;
  24. import com.hazelgatesoft.restoloyale.utils.Constants;
  25. import com.loopj.android.http.AsyncHttpClient;
  26. import com.loopj.android.http.JsonHttpResponseHandler;
  27.  
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30.  
  31. import java.io.UnsupportedEncodingException;
  32.  
  33. import cz.msebera.android.httpclient.Header;
  34. import cz.msebera.android.httpclient.entity.StringEntity;
  35. import cz.msebera.android.httpclient.message.BasicHeader;
  36. import cz.msebera.android.httpclient.protocol.HTTP;
  37.  
  38. public class ActivityProfile extends AppCompatActivity {
  39.  
  40. EditText edit_name, edit_phone, edit_mail, edit_pass;
  41. Button save, delete, logout;
  42. ImageButton back;
  43.  
  44. public static void hideKeyboard(Activity activity) {
  45. if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
  46. InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  47. assert imm != null;
  48. imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
  49. }
  50. }
  51.  
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.activity_profile);
  56.  
  57. final ProgressDialog progress = new ProgressDialog(this);
  58. progress.setMessage("Please Wait...");
  59. progress.setIndeterminate(false);
  60. progress.setCancelable(false);
  61.  
  62.  
  63. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  64.  
  65. Window w = getWindow();
  66. w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
  67.  
  68. edit_name = findViewById(R.id.profile_edit_name);
  69. edit_phone = findViewById(R.id.profile_edit_phone);
  70. edit_mail = findViewById(R.id.profile_edit_email);
  71. edit_pass = findViewById(R.id.profile_edit_pass);
  72.  
  73. save = findViewById(R.id.button_profile_save);
  74. delete = findViewById(R.id.button_profile_del);
  75. logout = findViewById(R.id.button_profile_logout);
  76.  
  77.  
  78. // Mai jos se trec datele personale ale accounului care vor aparea ca hint-uri la casutele de EditText
  79.  
  80. User user = Structures.getInstance().currentUser;
  81.  
  82. edit_name.setText(user.getName());
  83. edit_phone.setText(user.getPhone());
  84. edit_mail.setText(user.getEmail());
  85. StringBuilder hiddenPass = new StringBuilder();
  86. for (int i = 0; i < user.getPassLength(); i++) {
  87. hiddenPass.append("●");
  88. }
  89. edit_pass.setHint(hiddenPass.toString());
  90.  
  91.  
  92. back = findViewById(R.id.profile_back);
  93.  
  94. back.setOnClickListener(new View.OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. Intent a = new Intent(ActivityProfile.this, ActivityRestaurant.class);
  98. a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  99. startActivity(a);
  100. }
  101. });
  102.  
  103. save.setOnClickListener(
  104.  
  105. new View.OnClickListener() {
  106. @Override
  107. public void onClick(View v) {
  108.  
  109. AsyncHttpClient client = new AsyncHttpClient();
  110. String submitURL = "/users/validate";
  111. JSONObject jsonParams = new JSONObject();
  112. StringEntity entity = null;
  113. try {
  114. jsonParams.put("name", edit_name.getText().toString());
  115. jsonParams.put("email", edit_mail.getText().toString());
  116. jsonParams.put("phone", edit_phone.getText().toString());
  117. } catch (JSONException e) {
  118. e.printStackTrace();
  119. }
  120. try {
  121. entity = new StringEntity(jsonParams.toString());
  122. } catch (UnsupportedEncodingException e) {
  123. e.printStackTrace();
  124. }
  125. entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  126. Log.println(Log.INFO, "HTTPRequest", "JSON_to_send: " + jsonParams.toString());
  127. progress.show();
  128. Log.println(Log.INFO, "HTTPRequest", "URL to send: " + Constants.ROOT_URL + "/users");
  129. client.setMaxRetriesAndTimeout(Constants.HTTP_RETRIES, Constants.HTTP_TIMEOUT);
  130. client.addHeader("Authorization", Structures.getInstance().currentUser.getToken());
  131. client.put(null, Constants.ROOT_URL + "/users", entity, "application/json", new JsonHttpResponseHandler(){
  132. @Override
  133. public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) {
  134. progress.dismiss();
  135.  
  136. Structures.getInstance().currentUser.setEmail(edit_mail.getText().toString());
  137. Structures.getInstance().currentUser.setName(edit_name.getText().toString());
  138. Structures.getInstance().currentUser.setPhone(edit_phone.getText().toString());
  139.  
  140. Toast.makeText(ActivityProfile.this, "Succesfully updated profile info!", Toast.LENGTH_LONG).show();
  141.  
  142. }
  143.  
  144. @Override
  145. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  146. super.onFailure(statusCode, headers, throwable, errorResponse);
  147.  
  148. try {
  149. Toast.makeText(ActivityProfile.this, errorResponse.get("status").toString(), Toast.LENGTH_LONG);
  150. } catch (JSONException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154. });
  155. }
  156. });
  157.  
  158. delete.setOnClickListener(
  159. new View.OnClickListener() {
  160. @Override
  161. public void onClick(View v) {
  162.  
  163. AsyncHttpClient client = new AsyncHttpClient();
  164. client.setMaxRetriesAndTimeout(Constants.HTTP_RETRIES, Constants.HTTP_TIMEOUT);
  165. client.addHeader("Authorization", Structures.getInstance().currentUser.getToken());
  166. client.delete(Constants.ROOT_URL + "/users", null, new JsonHttpResponseHandler(){
  167.  
  168. @Override
  169. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  170. super.onSuccess(statusCode, headers, response);
  171. Toast.makeText(ActivityProfile.this, "Your account has been deleted!", Toast.LENGTH_LONG).show();
  172. Structures.getInstance().currentUser.clearAll();
  173. Intent a = new Intent(ActivityProfile.this, ActivityRestaurant.class);
  174. a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  175. startActivity(a);
  176.  
  177. }
  178.  
  179. @Override
  180. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  181. super.onFailure(statusCode, headers, throwable, errorResponse);
  182. Toast.makeText(ActivityProfile.this, "Your account has NOT been deleted, please try again later!", Toast.LENGTH_LONG).show();
  183. }
  184. });
  185.  
  186.  
  187. }
  188. }
  189. );
  190.  
  191. logout.setOnClickListener( new View.OnClickListener() {
  192.  
  193. @Override
  194. public void onClick(View v) {
  195.  
  196. Toast.makeText(ActivityProfile.this, "Logged out successfully!", Toast.LENGTH_LONG).show();
  197. Structures.getInstance().currentUser.clearAll();
  198. Intent a = new Intent(ActivityProfile.this, ActivityRestaurant.class);
  199. a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  200. startActivity(a);
  201. }
  202. });
  203.  
  204. }
  205.  
  206. @Override
  207. public boolean dispatchTouchEvent(MotionEvent ev) {
  208. View v = getCurrentFocus();
  209.  
  210. if (v != null &&
  211. (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
  212. v instanceof EditText &&
  213. !v.getClass().getName().startsWith("android.webkit.")) {
  214. int scrcoords[] = new int[2];
  215. v.getLocationOnScreen(scrcoords);
  216. float x = ev.getRawX() + v.getLeft() - scrcoords[0];
  217. float y = ev.getRawY() + v.getTop() - scrcoords[1];
  218.  
  219. if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
  220. hideKeyboard(this);
  221. }
  222. return super.dispatchTouchEvent(ev);
  223. }
  224.  
  225. @Override
  226. public boolean onKeyDown(int keyCode, KeyEvent event) {
  227. if (keyCode == KeyEvent.KEYCODE_BACK) {
  228. Intent a = new Intent(this, ActivityRestaurant.class);
  229. a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  230. startActivity(a);
  231. return true;
  232. }
  233. return super.onKeyDown(keyCode, event);
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement