Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hazelgatesoft.restoloyale.activities;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.Toast;
- import com.hazelgatesoft.restoloyale.R;
- import com.hazelgatesoft.restoloyale.data.Structures;
- import com.hazelgatesoft.restoloyale.data.User;
- import com.hazelgatesoft.restoloyale.utils.Constants;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.JsonHttpResponseHandler;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.UnsupportedEncodingException;
- import cz.msebera.android.httpclient.Header;
- import cz.msebera.android.httpclient.entity.StringEntity;
- import cz.msebera.android.httpclient.message.BasicHeader;
- import cz.msebera.android.httpclient.protocol.HTTP;
- public class ActivityProfile extends AppCompatActivity {
- EditText edit_name, edit_phone, edit_mail, edit_pass;
- Button save, delete, logout;
- ImageButton back;
- public static void hideKeyboard(Activity activity) {
- if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
- InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
- assert imm != null;
- imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_profile);
- final ProgressDialog progress = new ProgressDialog(this);
- progress.setMessage("Please Wait...");
- progress.setIndeterminate(false);
- progress.setCancelable(false);
- getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
- Window w = getWindow();
- w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
- edit_name = findViewById(R.id.profile_edit_name);
- edit_phone = findViewById(R.id.profile_edit_phone);
- edit_mail = findViewById(R.id.profile_edit_email);
- edit_pass = findViewById(R.id.profile_edit_pass);
- save = findViewById(R.id.button_profile_save);
- delete = findViewById(R.id.button_profile_del);
- logout = findViewById(R.id.button_profile_logout);
- // Mai jos se trec datele personale ale accounului care vor aparea ca hint-uri la casutele de EditText
- User user = Structures.getInstance().currentUser;
- edit_name.setText(user.getName());
- edit_phone.setText(user.getPhone());
- edit_mail.setText(user.getEmail());
- StringBuilder hiddenPass = new StringBuilder();
- for (int i = 0; i < user.getPassLength(); i++) {
- hiddenPass.append("●");
- }
- edit_pass.setHint(hiddenPass.toString());
- back = findViewById(R.id.profile_back);
- back.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent a = new Intent(ActivityProfile.this, ActivityRestaurant.class);
- a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
- startActivity(a);
- }
- });
- save.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AsyncHttpClient client = new AsyncHttpClient();
- String submitURL = "/users/validate";
- JSONObject jsonParams = new JSONObject();
- StringEntity entity = null;
- try {
- jsonParams.put("name", edit_name.getText().toString());
- jsonParams.put("email", edit_mail.getText().toString());
- jsonParams.put("phone", edit_phone.getText().toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- try {
- entity = new StringEntity(jsonParams.toString());
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
- Log.println(Log.INFO, "HTTPRequest", "JSON_to_send: " + jsonParams.toString());
- progress.show();
- Log.println(Log.INFO, "HTTPRequest", "URL to send: " + Constants.ROOT_URL + "/users");
- client.setMaxRetriesAndTimeout(Constants.HTTP_RETRIES, Constants.HTTP_TIMEOUT);
- client.addHeader("Authorization", Structures.getInstance().currentUser.getToken());
- client.put(null, Constants.ROOT_URL + "/users", entity, "application/json", new JsonHttpResponseHandler(){
- @Override
- public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) {
- progress.dismiss();
- Structures.getInstance().currentUser.setEmail(edit_mail.getText().toString());
- Structures.getInstance().currentUser.setName(edit_name.getText().toString());
- Structures.getInstance().currentUser.setPhone(edit_phone.getText().toString());
- Toast.makeText(ActivityProfile.this, "Succesfully updated profile info!", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
- super.onFailure(statusCode, headers, throwable, errorResponse);
- try {
- Toast.makeText(ActivityProfile.this, errorResponse.get("status").toString(), Toast.LENGTH_LONG);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
- }
- });
- delete.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AsyncHttpClient client = new AsyncHttpClient();
- client.setMaxRetriesAndTimeout(Constants.HTTP_RETRIES, Constants.HTTP_TIMEOUT);
- client.addHeader("Authorization", Structures.getInstance().currentUser.getToken());
- client.delete(Constants.ROOT_URL + "/users", null, new JsonHttpResponseHandler(){
- @Override
- public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
- super.onSuccess(statusCode, headers, response);
- Toast.makeText(ActivityProfile.this, "Your account has been deleted!", Toast.LENGTH_LONG).show();
- Structures.getInstance().currentUser.clearAll();
- Intent a = new Intent(ActivityProfile.this, ActivityRestaurant.class);
- a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
- startActivity(a);
- }
- @Override
- public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
- super.onFailure(statusCode, headers, throwable, errorResponse);
- Toast.makeText(ActivityProfile.this, "Your account has NOT been deleted, please try again later!", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- );
- logout.setOnClickListener( new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(ActivityProfile.this, "Logged out successfully!", Toast.LENGTH_LONG).show();
- Structures.getInstance().currentUser.clearAll();
- Intent a = new Intent(ActivityProfile.this, ActivityRestaurant.class);
- a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
- startActivity(a);
- }
- });
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- View v = getCurrentFocus();
- if (v != null &&
- (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
- v instanceof EditText &&
- !v.getClass().getName().startsWith("android.webkit.")) {
- int scrcoords[] = new int[2];
- v.getLocationOnScreen(scrcoords);
- float x = ev.getRawX() + v.getLeft() - scrcoords[0];
- float y = ev.getRawY() + v.getTop() - scrcoords[1];
- if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
- hideKeyboard(this);
- }
- return super.dispatchTouchEvent(ev);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- Intent a = new Intent(this, ActivityRestaurant.class);
- a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
- startActivity(a);
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement