Created
April 19, 2022 00:54
-
-
Save denihida1216/b6d76e8c3e8d00080406df8464b8c9e3 to your computer and use it in GitHub Desktop.
Activity handling error get info and send report to database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mobile.activity; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Gravity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import androidx.appcompat.app.AlertDialog; | |
import androidx.appcompat.app.AppCompatActivity; | |
import com.androidadvance.topsnackbar.TSnackbar; | |
import com.google.gson.Gson; | |
import com.example.mobile.R; | |
import com.example.mobile.helper.DateHelper; | |
import com.example.mobile.helper.Dialog201; | |
import com.example.mobile.helper.LoadingHelper; | |
import com.example.mobile.helper.SessionManager; | |
import com.example.mobile.model.HandlingError; | |
import com.example.mobile.model.HandlingErrorResponse; | |
import com.example.mobile.network.ApiClient; | |
import com.example.mobile.network.ApiInterface; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
public class HandlingErrorActivity extends AppCompatActivity { | |
private static final String TAG = HandlingErrorActivity.class.getSimpleName(); | |
private ApiInterface apiService; | |
private DateHelper dateHelper; | |
private LoadingHelper loadingHelper; | |
private SessionManager sessionManager; | |
private HandlingError handlingError; | |
private TextView text_log; | |
private Button btn_send; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_handling_error); | |
text_log = findViewById(R.id.text_log); | |
btn_send = findViewById(R.id.btn_send); | |
apiService = ApiClient.getClient().create(ApiInterface.class); | |
dateHelper = new DateHelper(this); | |
loadingHelper = new LoadingHelper(this); | |
sessionManager = new SessionManager(this); | |
handlingError = new HandlingError(); | |
String logstr = getIntent().getStringExtra("logstr"); | |
String brand = getIntent().getStringExtra("brand"); | |
String device = getIntent().getStringExtra("device"); | |
String model = getIntent().getStringExtra("model"); | |
String productid = getIntent().getStringExtra("productid"); | |
String product = getIntent().getStringExtra("product"); | |
String sdk = getIntent().getStringExtra("sdk"); | |
String release = getIntent().getStringExtra("release"); | |
String incremental = getIntent().getStringExtra("incremental"); | |
handlingError.setLogstr(logstr); | |
handlingError.setBrand(brand); | |
handlingError.setDevice(device); | |
handlingError.setModel(model); | |
handlingError.setProductid(productid); | |
handlingError.setProduct(product); | |
handlingError.setSdk(sdk); | |
handlingError.setRelease(release); | |
handlingError.setIncremental(incremental); | |
text_log.setText(logstr); | |
btn_send.setOnClickListener(v -> { | |
saveData(); | |
}); | |
} | |
private void saveData() { | |
String username = sessionManager.getUser().getUser().getUsername(); | |
String token = sessionManager.getUser().getToken(); | |
loadingHelper.showLoading(); | |
Call<HandlingErrorResponse> call = apiService.handlingError( | |
username, | |
token, | |
handlingError | |
); | |
call.enqueue(new Callback<HandlingErrorResponse>() { | |
@Override | |
public void onResponse(Call<HandlingErrorResponse> call, Response<HandlingErrorResponse> response) { | |
int statusCode = response.code(); | |
loadingHelper.hideLoading(); | |
if (response.isSuccessful()) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(HandlingErrorActivity.this); | |
View layout_dialog = LayoutInflater.from(HandlingErrorActivity.this).inflate(R.layout.dialog_wbs, null); | |
builder.setView(layout_dialog); | |
Button btn_ok = layout_dialog.findViewById(R.id.btn_ok); | |
AlertDialog dialog = builder.create(); | |
dialog.show(); | |
dialog.setCancelable(false); | |
dialog.getWindow().setGravity(Gravity.CENTER); | |
btn_ok.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
dialog.dismiss(); | |
onBackPressed(); | |
} | |
}); | |
Dialog201.Show(HandlingErrorActivity.this, response.body().getMetadata()); | |
}else{ | |
TSnackbar snackbar = TSnackbar.make(findViewById(android.R.id.content), "" + statusCode, TSnackbar.LENGTH_LONG); | |
snackbar.setActionTextColor(Color.WHITE); | |
View snackbarView = snackbar.getView(); | |
snackbarView.setBackgroundColor(Color.RED); | |
TextView textView = snackbarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text); | |
textView.setTextColor(Color.WHITE); | |
snackbar.show(); | |
} | |
} | |
@Override | |
public void onFailure(Call<HandlingErrorResponse> call, Throwable t) { | |
// Log error here since request failed | |
Log.e(TAG, t.toString()); | |
loadingHelper.hideLoading(); | |
TSnackbar snackbar = TSnackbar.make(findViewById(android.R.id.content), "503", TSnackbar.LENGTH_LONG); | |
snackbar.setActionTextColor(Color.WHITE); | |
View snackbarView = snackbar.getView(); | |
snackbarView.setBackgroundColor(getResources().getColor(R.color.red)); | |
TextView textView = snackbarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text); | |
textView.setTextColor(Color.WHITE); | |
snackbar.show(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment