Skip to content

Instantly share code, notes, and snippets.

@dan085
Created April 16, 2020 16:08
Show Gist options
  • Save dan085/cf97cc6ec69d2cd27a9ce75e37348603 to your computer and use it in GitHub Desktop.
Save dan085/cf97cc6ec69d2cd27a9ce75e37348603 to your computer and use it in GitHub Desktop.
private class AccesoRemoto extends AsyncTask<Void, Void, String> {
private MaterialDialog a;
private Context context;
private static final String SINGULARITY_HOST = "https://cloud.careyou.cl/api/";
private RequestFuture<JSONObject> futureRequest = RequestFuture.newFuture();
public AccesoRemoto(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
a = new MaterialDialog(context);
a.setCancelable(false);
a.title(R.string.loading, context.getResources().getString(R.string.loading));
a.message(R.string.loading, " " + context.getResources().getString(R.string.loading) + " ", false, 1.0f);
int llPadding = 30;
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(llPadding, llPadding, llPadding, llPadding);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
ll.setLayoutParams(llParam);
ProgressBar progressBar = new ProgressBar(context);
progressBar.setIndeterminate(true);
progressBar.setPadding(0, 0, llPadding, 0);
progressBar.setLayoutParams( llParam);
LinearLayout.LayoutParams llParam_ = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llParam_.gravity = Gravity.CENTER;
TextView tvText = new TextView(context);
tvText.setText(context.getResources().getString(R.string.loading) + " ...");
tvText.setTextColor(Color.parseColor("#000000"));
tvText.setTextSize(18f);
tvText.setLayoutParams(llParam_);
ll.addView(progressBar);
ll.addView(tvText);
a.setContentView(ll);
a.show();
}
protected String doInBackground(Void... argumentos) {
String rut_final = numbers + dig_verification;
try {
if (numbers.length() == 0) {
Log.d("rut", "cero");
}
}catch ( java.lang.NullPointerException e){
return "";
}
Log.d("rut",rut_final.replace(".", "").replace("-", ""));
SharedPreference sharedPreference = SharedPreference.Companion.newInstance(context);
LoadInfo loadInfo = LoadInfo.Companion.newInstance(context);
String token = loadInfo.get_login_or_register_token();//StorageHandler.getAuthToken(context);
futureRequest = RequestFuture.newFuture();
RequestQueue mQueue_ = CustomVolleyRequestQueue.Companion.getInstance(context.getApplicationContext()).getRequestQueue();
JSONObject jsonBody_ = new JSONObject();
try {
String first_name = nameTxt.getText().toString();
String last_name = surnameTxt.getText().toString();
int age = Integer.valueOf(ageTxt.getText().toString());
String rut = rutTxt.getText().toString();
jsonBody_.put("rut", rut_final.replace(".", "").replace("-", ""));
jsonBody_.put("requestType", "REQUEST_EXISTING_USER");
jsonBody_.put("region_name", region_name);
jsonBody_.put("comuna_name", comuna_name);
jsonBody_.put("serial", n_serie);
jsonBody_.put("first_name", first_name);
jsonBody_.put("last_name", last_name);
jsonBody_.put("age", age);
jsonBody_.put("rut", rut.replace(".", "").replace("-", ""));
jsonBody_.put("serial",n_serie);
jsonBody_.put("region_name", region_name);
jsonBody_.put("comuna_name", comuna_name);
Spinner genderSpn = findViewById(R.id.gender);
jsonBody_.put("gender", genderSpn.getSelectedItem().toString());
} catch (JSONException enn) {
enn.printStackTrace();
}catch(java.lang.NumberFormatException ee){
ee.printStackTrace();
return "ERROR_INTERNET";
}
Log.d("CHECK_EXIST",jsonBody_.toString());
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, SINGULARITY_HOST + "rule-engine/", jsonBody_, futureRequest, futureRequest) {
@Override
public HashMap<String, String> getHeaders() {
//String token = chatService.get_token();
HashMap<String, String> params = new HashMap<>();
if (token.length() > 0) {
params.put("X-Authorization", "Bearer " + token);
}
params.put("Content-Type", "application/json;charset=UTF-8");
params.put("Charset", "UTF-8");
params.put("Accept", "application/json");
return params;
}
};
jsonRequest.setTag("REQUEST_EXISTING_USER");
JSONObject response = ServerRestHandler.httpRequest_sync("POST", SINGULARITY_HOST + "rule-engine/", jsonBody_.toString(), token, false);//futureRequest.get(15, TimeUnit.SECONDS);
Log.d("response",response.toString());
try {
String status = response.getString("status");
if (status.equals("ERROR")) {
String reason = response.getString("reason");
if (reason.equals("Incorrect RUT or Serial N°")) {
} else if (reason.equals("User device not found")) {
futureRequest = RequestFuture.newFuture();
RequestQueue mQueue__ = CustomVolleyRequestQueue.Companion.getInstance(context.getApplicationContext()).getRequestQueue();
JSONObject jsonBody__ = new JSONObject();
try {
jsonBody__.put("rut", rut_final.replace(".", "").replace("-", ""));
jsonBody__.put("requestType", "CREATE_C_USER");
jsonBody__.put("serial",n_serie);
jsonBody__.put("region_name", region_name);
jsonBody__.put("comuna_name", comuna_name);
} catch (JSONException enn) {
enn.printStackTrace();
}
Log.d("REGISTER_USER", jsonBody__.toString());
JsonObjectRequest jsonRequest_ = new JsonObjectRequest(Request.Method.POST, SINGULARITY_HOST + "rule-engine/", jsonBody__, futureRequest, futureRequest) {
@Override
public HashMap<String, String> getHeaders() {
//String token = chatService.get_token();
HashMap<String, String> params = new HashMap<>();
if (token.length() > 0) {
params.put("X-Authorization", "Bearer " + token);
}
params.put("Content-Type", "application/json;charset=UTF-8");
params.put("Charset", "UTF-8");
params.put("Accept", "application/json");
return params;
}
};
jsonRequest_.setTag("REQUEST_EXISTING_USER");
// jsonRequest.retryPolicy = DefaultRetryPolicy(15000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
mQueue__.add(jsonRequest_);
try {
JSONObject response_exist_user = futureRequest.get(15, TimeUnit.SECONDS);
Log.d("response_create_user", response_exist_user.toString());
JSONObject customerId = response_exist_user.getJSONObject("id");
String id = customerId.getString("id");
StorageHandler.storeUserId(context, id);
sharedPreference.set_informatin_user(customerId.getString("id"), TAG_INFO.KEY_ID_USER);
ArrayList<UserProfile> a_new_user = new ArrayList<UserProfile>();
StorageHandler.storeUserId(context, id);
String access_token = loadInfo.getUserCredentials();
if(access_token.length()>0) {
Log.d("access_token", access_token);
UserProfile new_user = new UserProfile(id, rut_final.replace(".", "").replace("-", ""), nameTxt.getText().toString(), comuna_name, true,access_token, new JSONObject());
a_new_user.add(new_user);
sharedPreference.set_list_users(TAG.INFO_USERS, a_new_user);
loadInfo.set_list_user(a_new_user);
loadInfo.account_selected(new_user);
sharedPreference.set_token_web("credentialsId", access_token);
loadInfo.account_selected(new_user);
sharedPreference.set_user_selected(TAG.INFO_USERS_SELECTED,new_user);
loadInfo.set_access_token_instance(access_token);
JSONObject o = new JSONObject();
try {
String first_name = nameTxt.getText().toString();
String last_name = surnameTxt.getText().toString();
int age = Integer.valueOf(ageTxt.getText().toString());
String rut = rutTxt.getText().toString();
o.put("first_name", first_name);
o.put("last_name", last_name);
o.put("age", age);
o.put("rut", rut.replace(".", "").replace("-", ""));
o.put("serial", n_serie);
o.put("region_name", region_name);
o.put("comuna_name", comuna_name);
o.put("address", address);
Spinner genderSpn = findViewById(R.id.gender);
o.put("gender", genderSpn.getSelectedItem().toString());
} catch (Exception e) {
e.printStackTrace();
}
Log.d("envio attributos", o.toString());
JSONObject result_create_user_Atribute = ServerRestHandler.pushAttributes(o.toString(), null, "DEVICE");
if (result_create_user_Atribute.getString("responseCode").equals("200")) {
String token_ = loadInfo.getUserCredentials();
// String token__ = ServerRestHandler.getUserCredentials();;
Log.d("token_", token_);
// loadInfo.set_token_instance(token_);
sharedPreference.set_token_web(TAG.TOKEN_USER, token_);
sharedPreference.set_user_id(TAG.USER_ID, customerId.getString("id"));
sharedPreference.set_isUserLoggedIn(true);
return customerId.getString("id");
} else {
loadInfo.fifo_sendPatiendSample(new sendPatiendSample(o,TAG._SEND_ATRIBUTTES));
}
}else{
return "";
}
} catch (InterruptedException ec) {
ec.printStackTrace();
return "ERROR_INTERNET";
} catch (TimeoutException ec) {
ec.printStackTrace();
return "ERROR_INTERNET";
} catch (ExecutionException be) {
be.printStackTrace();
return "ERROR_INTERNET";
} catch (JSONException bbe) {
bbe.printStackTrace();
return "ERROR_INTERNET";
}
}
}
}catch ( JSONException bbe) {
bbe.printStackTrace();
try {
Log.d("aqui error","probando");
JSONObject customerId = response.getJSONObject("id");
String first_name = nameTxt.getText().toString();
String last_name = surnameTxt.getText().toString();
int age = Integer.valueOf(ageTxt.getText().toString());
String rut = rutTxt.getText().toString();
sharedPreference.set_isUserLoggedIn(true);
sharedPreference.set_informatin_user(customerId.getString("id"), TAG_INFO.KEY_ID_USER);
StorageHandler.storeUserId(context, customerId.getString("id"));
String token_ = loadInfo.getUserCredentials();
UserProfile new_user = new UserProfile(customerId.getString("id"), rut_final.replace(".", "").replace("-", ""), nameTxt.getText().toString(), comuna_name, true,token_, new JSONObject());
ArrayList<UserProfile> a_new_user = new ArrayList<UserProfile>();
a_new_user.add(new_user);
sharedPreference.set_list_users(TAG.INFO_USERS, a_new_user);
loadInfo.account_selected(new_user);
sharedPreference.set_user_selected(TAG.INFO_USERS_SELECTED,new_user);
loadInfo.set_list_user(a_new_user);
JSONObject o = new JSONObject();
try {
o.put("first_name", first_name);
o.put("last_name", last_name);
o.put("age", age);
o.put("rut", rut.replace(".", "").replace("-", ""));
o.put("serial",n_serie);
o.put("region_name", region_name);
o.put("comuna_name", comuna_name);
o.put("address",address);
Spinner genderSpn = findViewById(R.id.gender);
o.put("gender", genderSpn.getSelectedItem().toString());
} catch (Exception e) {
e.printStackTrace();
}
Log.d("register",o.toString());
ServerRestHandler.pushAttributes(o.toString(), token_, "DEVICE");
// loadInfo.fifo_sendPatiendSample(new sendPatiendSample(o, 1, TAG._SEND_ATRIBUTTES));
return customerId.getString("id");
}catch ( JSONException rr) {
rr.printStackTrace();
}
}
return "";
}
protected void onPostExecute(String mensaje) {
a.cancel();
if(mensaje.equals("TRUE")){
StorageHandler.storeUserId(context, "");
StorageHandler.storeUserToken(context, "");
UToast.show(context, "Registro falló, usuario ya existe", Toast.LENGTH_LONG);
}else if(mensaje.equals("ERROR_INTERNET")){
UToast.show(context, "Registro falló - Ingrese Edad", Toast.LENGTH_LONG);
}else if(mensaje.length()==0){
UToast.show(context, "Registro falló, intentarlo denuevo", Toast.LENGTH_LONG);
}else{
try {
StorageHandler.storeUserId(context, mensaje);
ActivityHelper.launcher(context, BasicInfoActivity.class);
}catch (NumberFormatException ee){
UToast.show(context, "Registro falló - Ingrese Edad", Toast.LENGTH_LONG);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment