Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.phoenixhospman.launcher1;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AlertDialog;
- import java.util.List;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import static android.view.View.inflate;
- import static java.util.Objects.requireNonNull;
- /**
- * The main activity for the app.
- * @author Troy L. Marker
- * @version 1.0.0
- * @since 0.1.0
- */
- public class ActivityHome extends Activity implements View.OnClickListener {
- private ManagerSecurityApi managerSecurityApi;
- final private static int REQUEST_CODE_1 = 1;
- private String strCoName;
- private String strApiUrl;
- private EditText etUsername;
- private EditText etPassword;
- private ObjectUser objectUser;
- /**
- * This method will create the activity, read content to display, and show the main activity screen
- * Used when the activity is (re)created.
- *
- * @param savedInstanceState current saved instance state
- */
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- View decorView = getWindow().getDecorView();
- decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
- setContentView(R.layout.activity_home);
- etUsername = findViewById(R.id.etUsername);
- etPassword = findViewById(R.id.etPassword);
- TextView tvWarning = findViewById(R.id.tvWarning);
- Button btnLogon = findViewById(R.id.btnLogon);
- btnLogon.setOnClickListener(this);
- @SuppressLint ("Recycle") Cursor cursor = getContentResolver().query(Uri.parse("content://com.phoenixhospman.installer.ProviderSettings/settings"), null, null, null, null, null);
- assert cursor != null;
- if(cursor.moveToFirst()) {
- while(!cursor.isAfterLast()) {
- strCoName = cursor.getString(cursor.getColumnIndex("coname"));
- strApiUrl = cursor.getString(cursor.getColumnIndex("apiurl"));
- cursor.moveToNext();
- }
- managerSecurityApi = new ManagerSecurityApi(strApiUrl);
- tvWarning.setText(getString(R.string.warning, rtrim(strCoName)));
- } else {
- Error("\nRequired setting missing.\nPlease (re)run Phoenix Install App");
- }
- }
- /**
- *
- * This method will read check the entered credentials for proper access
- *
- * @param view the view calling the method.
- */
- public void onClick(final View view) {
- if (view.getId() == R.id.btnLogon) {
- managerSecurityApi.login (etUsername.getText ().toString (), etPassword.getText ().toString (), new Callback<List<ObjectUser>> () {
- @Override
- public void onResponse (@NonNull Call<List<ObjectUser>> call, @NonNull Response<List<ObjectUser>> response) {
- List<ObjectUser> JsonList = requireNonNull(response).body ();
- for (int i = 0; i < requireNonNull (JsonList).size (); i++)
- objectUser = new ObjectUser (
- JsonList.get (i).getId (),
- JsonList.get (i).getUsername (),
- JsonList.get (i).getGrade (),
- JsonList.get (i).getGradename(),
- JsonList.get (i).getDepartment (),
- JsonList.get (i).getDepartmentname()
- );
- getContentResolver ().delete (ProviderUser.CONTENT_URI, null, null);
- etUsername.setText (R.string.blank);
- etPassword.setText (R.string.blank);
- ContentValues values = new ContentValues ();
- values.put (ProviderUser.name, objectUser.getUsername ());
- values.put (ProviderUser.grade, String.valueOf (objectUser.getGrade ()));
- values.put (ProviderUser.gradename, objectUser.getGradename ());
- values.put (ProviderUser.department, String.valueOf (objectUser.getDepartment ()));
- values.put (ProviderUser.departmentname, objectUser.getDepartmentname());
- getContentResolver ().insert (ProviderUser.CONTENT_URI, values);
- showApps ();
- }
- @Override
- public void onFailure (@NonNull Call<List<ObjectUser>> call, @NonNull Throwable t) {
- Toast.makeText (getApplicationContext (), t.getMessage (), Toast.LENGTH_SHORT).show ();
- }
- });
- } else {
- throw new IllegalStateException("Unexpected value: " + view.getId());
- }
- }
- /**
- * This method will call the App Display Activity
- */
- private void showApps(){
- Intent intent = new Intent(ActivityHome.this.getApplicationContext(), ActivityApp.class);
- intent.putExtra("username", objectUser.getUsername());
- intent.putExtra("grade", String.valueOf(objectUser.getGrade()));
- intent.putExtra("department", String.valueOf(objectUser.getDepartment()));
- startActivityForResult(intent, REQUEST_CODE_1);
- }
- /**
- * Override of the onBackPress method from the parent class
- * This disable th back button on the device.
- *
- */
- @Override
- public void onBackPressed() {
- }
- /**
- * The error display method
- *
- * This method displays a dialog box with an error message and a close button.
- *
- * @param strError the error message to display
- */
- @SuppressWarnings ("SameParameterValue")
- private void Error(String strError) {
- AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
- View view = inflate(this, R.layout.dialog_error, null);
- Button btnExit = view.findViewById(R.id.buttonExitButton);
- Button btnError = view.findViewById(R.id.btnErrorMessage);
- btnError.setText(getString(R.string.error, strError ));
- mBuilder.setView(view);
- AlertDialog dialog = mBuilder.create();
- dialog.setCanceledOnTouchOutside(false);
- dialog.setCancelable(false);
- dialog.show();
- btnExit.setOnClickListener(v -> finishAffinity());
- }
- /**
- * Function to trim whitespace from the end of a string.
- *
- * @param s the string to trim
- * @return the trimmed string
- */
- public static String rtrim(String s) {
- int i = s.length()-1;
- while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
- i--;
- }
- return s.substring(0,i+1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment